|
|
View previous topic :: View next topic |
Author |
Message |
akokyaw
Joined: 11 Feb 2005 Posts: 24
|
DS1302 with 12 hr format |
Posted: Tue Jan 02, 2007 9:54 am |
|
|
Hi,
I try to wirte RTC program with DS1302. The DS1302 can be run in either 12-hour or 24-hour mode. In example of CCS, it used 24hr format. I would like to changed to 12hr format. According to datasheet, I need to change BCD format to write the registers.
For example,
11:50 AM --> " write_ds1302(0x84,0x91);" pls refer to DS1302.C
Now, I have a problem with 11:50 PM. Anyone know how to convert it?
Thank in advance,
AKO |
|
|
Ttelmah Guest
|
|
Posted: Tue Jan 02, 2007 10:21 am |
|
|
The 'get_bcd' function converts a number into bcd, so you can just use:
write_ds1302(0x84,get_bcd(11) | 0x80);
To put the BCD value '11' into the chip's 'hour' register, with the 12hour mode set.
The rm_bcd function converts a number the other way.
For working in 12hr mode, you just need to add the 'PM' flag to bit 5 of the hour value, together with the 12hour mode enable, and when you read this, mask these flag off, and convert the rest of the number back.
Best Wishes |
|
|
akokyaw
Joined: 11 Feb 2005 Posts: 24
|
|
Posted: Tue Jan 02, 2007 11:12 pm |
|
|
Hi Ttelmah,
Thank you for replying. In 12hr mode (AM), your suggested code is the same as my code i.e, get_bcd(11) | 0x80 = 0x91
Code: |
write_ds1302(0x84,get_bcd(11) | 0x80);
write_ds1302(0x84,0x91);
|
Quote: | For working in 12hr mode, you just need to add the 'PM' flag to bit 5 of the hour value, together with the 12hour mode enable, and when you read this, mask these flag off, and convert the rest of the number back.
|
For 12 hr mode (PM), I tried to write code according to your code:
Code: |
write_ds1302(0x84,get_bcd(11) | 0xa0); // for 11:50 PM
|
After running the program, I have 15:50 PM, (where PM is given by 'PM' flag), I doubt I have to change BCD format but how to change 0xa0 to BCD format? Anyone has idea?
I appreciate your help and welcome all comments.
Thanks,
AKO |
|
|
Ttelmah Guest
|
|
Posted: Wed Jan 03, 2007 3:43 am |
|
|
Code: |
#define PMBIT 0x20
//sending the hours _to_ the chip
//-----------------
int8 regval;
regval=get_bcd(hours) | 0x80;
if (PM) regval=hours | PMBIT;
write_ds1302(0x84,regval);
//-----------------
//getting the value from the chip
regval=read_ds1302(0x84);
if (regval & PMBIT) PM=true;
else PM=false;
hours=regval & 0x1F;
|
The point is there is nothing 'BCD' about the registers, when dealing with the 12/24 hour enable, and the PM flag. These are stored as binary bits in the register, or'ed wih the BCD 'hours' value. BCD, implies dealing with decimal numbers (hence 'binary coded decimal'), but the flags are already simple binary values. You just or the binary value with the BCD converted 'hours' when writing to the chip, and going the other way, use 'and' as a mask to remove these bits to access the BCD number.
Best Wishes |
|
|
akokyaw
Joined: 11 Feb 2005 Posts: 24
|
|
Posted: Wed Jan 03, 2007 8:21 am |
|
|
Hi Ttelmah,
Thank for your code. And can I ask some questions, pls? How do you define PM?
Code: |
#define PMBIT 0x20
//sending the hours _to_ the chip
//-----------------
int8 regval;
regval=get_bcd(hours) | 0x80;
if (PM) regval=hours | PMBIT; // <-- PM ?
write_ds1302(0x84,regval);
|
And read reg will be 0x85, and PM again.
Code: |
//-----------------
//getting the value from the chip
regval=read_ds1302(0x84); // <-- 0x085 ?
if (regval & PMBIT) PM=true; // <-- PM?
else PM=false;
hours=regval & 0x1F;
|
Thanks a lot,
AKO |
|
|
Ttelmah Guest
|
|
Posted: Wed Jan 03, 2007 9:33 am |
|
|
PM, is just the local flag you are using to say that this is a PM value. I'm assuming you set it true/false to refect a time being 'PM'.
So your time would be stored as 'hours', and 'PM', with the latter being set true after midday.
I assumed that the 'read' command, knows to set the bottom bit of the address given (my versions all do). The standard version does not do this, so, yes you would use 0x85.
Best Wishes |
|
|
AKO Guest
|
|
Posted: Wed Jan 03, 2007 11:09 pm |
|
|
Hi Ttelmah,
If I define PM as variable and PM=1; // for 12hr PM mode
Then "write_ds1302(0x84,regval);" will process.
Can it be done for variable sending to RTC chip without converting bcd format?
Code: |
#define PMBIT 0x20
//sending the hours _to_ the chip
//-----------------
int8 regval;
regval=get_bcd(hours) | 0x80;
if (PM) regval=hours | PMBIT;
write_ds1302(0x84,regval); // <-- without converting bcd format
|
Unfortunately, I cannot get what I expected, Anyone has idea?
Thank anyway,
AKO |
|
|
Ttelmah Guest
|
|
Posted: Thu Jan 04, 2007 5:39 am |
|
|
regval, already is in BCD fomat. That is what the 'get_bcd' line does.
If 'hours', is '0xA' (10 o'clock), then the get_bcd call, turns this into 0x10, the BCD equivalent of this value. This is then or'ed with 0x80, to give '0x90', and (assuming it is morning), this is the value sent to the chip.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|