View previous topic :: View next topic |
Author |
Message |
Nisar
Joined: 16 Nov 2007 Posts: 20
|
Help 12hour Format |
Posted: Sun Apr 12, 2009 1:00 pm |
|
|
Hello every one
I have problem to change 24hr format to 12hr format in DS1302 driver.
Code: |
#ifndef RTC_SCLK
// DS1302 PIC16F877
#define RTC_SCLK PIN_C1 // Pin7 Pin16
#define RTC_IO PIN_C0 // Pin6 Pin15
#define RTC_RST PIN_C2 // Pin5 Pin17
#endif
void write_ds1302_byte(BYTE cmd) {
BYTE i;
for(i=0;i<=7;++i) {
output_bit(RTC_IO, shift_right(&cmd,1,0) );
output_high(RTC_SCLK);
output_low(RTC_SCLK);
}
}
void write_ds1302(BYTE cmd, BYTE data) {
output_high(RTC_RST);
write_ds1302_byte(cmd);
write_ds1302_byte(data);
output_low(RTC_RST);
}
BYTE read_ds1302(BYTE cmd) {
BYTE i,data;
output_high(RTC_RST);
write_ds1302_byte(cmd);
for(i=0;i<=7;++i) {
shift_right(&data,1,input(RTC_IO));
output_high(RTC_SCLK);
delay_us(2);
output_low(RTC_SCLK);
delay_us(2);
}
output_low(RTC_RST);
return(data);
}
void rtc_init() {
BYTE x;
output_low(RTC_RST);
delay_us(2);
output_low(RTC_SCLK);
write_ds1302(0x8e,0);
write_ds1302(0x90,0xa4);
x=read_ds1302(0x81);
if((x & 0x80)!=0)
write_ds1302(0x80,0);
}
int get_bcd(BYTE data)
{
int nibh;
int nibl;
nibh=data/10;
nibl=data-(nibh*10);
return((nibh<<4)|nibl);
}
int rm_bcd(BYTE data)
{
int i;
i=data;
data=(i>>4)*10;
data=data+(i<<4>>4);
return data;
}
void rtc_set_datetime(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min) {
write_ds1302(0x86,get_bcd(day));
write_ds1302(0x88,get_bcd(mth));
write_ds1302(0x8c,get_bcd(year));
write_ds1302(0x8a,get_bcd(dow));
write_ds1302(0x84,get_bcd(hr));
write_ds1302(0x82,get_bcd(min));
write_ds1302(0x80,get_bcd(0));
}
void rtc_get_date(BYTE& day, BYTE& mth, BYTE& year, BYTE& dow) {
day = rm_bcd(read_ds1302(0x87));
mth = rm_bcd(read_ds1302(0x89));
year = rm_bcd(read_ds1302(0x8d));
dow = rm_bcd(read_ds1302(0x8b));
}
void rtc_get_time(BYTE& hr, BYTE& min, BYTE& sec) {
hr = rm_bcd(read_ds1302(0x85));
min = rm_bcd(read_ds1302(0x83));
sec = rm_bcd(read_ds1302(0x81));
}
void rtc_write_nvr(BYTE address, BYTE data) {
write_ds1302(address|0xc0,data);
}
BYTE rtc_read_nvr(BYTE address) {
return(read_ds1302(address|0xc1));
} |
Thanks in advance
Nisar |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Apr 13, 2009 5:01 pm |
|
|
Code: | data=data+(i<<4>>4); | When posting code ensure the 'Disable BBCode in this post' is not selected. Now part of your code is interpreted as HTML code and unreadable.
Better, disable this option by default in your profile.
What is your exact problem? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 13, 2009 5:10 pm |
|
|
Actually that's real code. That's how CCS clears the upper 4 bits.
First they shift it left to wipe out the upper 4 bits, then they shift it
right and it fills in the upper 4 bits with zeros.
I've never liked that kind of stuff. CCS doesn't understand that they're
teaching newbies. They shouldn't do code tricks. But I don't email
email them about it because they might be offended. |
|
|
Nisar
Joined: 16 Nov 2007 Posts: 20
|
|
Posted: Wed Apr 15, 2009 3:24 am |
|
|
Hello PCM
Sorry for my mistakes,i'm new for CCS C .plz guide me right way.
Nisar |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Apr 15, 2009 5:01 am |
|
|
PCM programmer wrote: | Actually that's real code. That's how CCS clears the upper 4 bits.
First they shift it left to wipe out the upper 4 bits, then they shift it
right and it fills in the upper 4 bits with zeros. | Ouch... very, very ugly.
The C Standard does not define whether a 0 or 1 is shifted in on the shift right (shift left is defined). We know the CCS compiler always shifts in a zero, but this does make the code less portable.
Nisar wrote: | Sorry for my mistakes,i'm new for CCS C .plz guide me right way. | A better way to write the specific line is: Code: | data = data + (i & 0x0F); |
About your original question: Why do you want to change the driver to 12hr format instead of 24hr format? You will have to make quiet a lot of changes, including adding a parameter for AM/PM selection. Easier to do would be to make the conversion in your program. Or add a 'wrapper' function which does the AM/PM conversion. |
|
|
Nisar
Joined: 16 Nov 2007 Posts: 20
|
|
Posted: Wed Apr 15, 2009 6:38 am |
|
|
Actually my project requirement is 12 hour format.In DS1302 data sheet register address writes bit7 high for 12 hour format.I take some help from
[quote]http://www.ccsinfo.com/forum/viewtopic.php?p=74171[/quote]
but no positive progress. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Apr 15, 2009 7:06 am |
|
|
Sounds like a school project. I'm willing to help you but I'm not going to do your work.
For a 12hr format you somehow have to specify the AM/PM parameter. Give an example of how you want the new parameter list for the rtc_get_time and rtc_set_datetime functions to look like. |
|
|
|