CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

RTC bcd->binary in 24 hour modes

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Oli
Guest







RTC bcd->binary in 24 hour modes
PostPosted: Tue May 04, 2010 10:18 am     Reply with quote

Hi All

I'm using the DS1337 - appears to work fine. I based my code loosely on the driver here:

http://www.ccsinfo.com/forum/viewtopic.php?t=28366&highlight=ds1337

my question relates to the HOUR registers. In the datasheet bits 0-3 set the hour. Bits 4 and 5 each indicate an additional 10 hours. Bit 6 is the 12/ !24 hour setting.

All of the drivers I have seen on the web read the hours value using a straight BCD to binary conversion, meaning that if the time is (in 24hr format) 10pm == 22:00

then the register reads 00110010 == 0x32 == 50 decimal

then that converts from BCD to binary as an hour of 32 o'clock, which is clearly wrong. In the linked driver, this line reads the register:

Code:
dt->hours   = bcd2bin(bcd_hrs  & 0x3F);


and converts using:


Code:
char bcd2bin(char bcd_value) {

   char temp;
   char temp1;
   
   temp = bcd_value;

   // Shifting the upper digit right by 1 is
   // the same as multiplying it by 8.
   temp >>= 1;

   // Isolate the bits for the upper digit.
   temp &= 0x78;

   
   // Now return: (Tens * 8) + (Tens * 2) + Ones
   (temp + (temp >> 2) + (bcd_value & 0x0f));
   
   return temp;
}



What am I missing? Are all of the drivers wrong?

Thanks in advance
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue May 04, 2010 11:55 am     Reply with quote

You misunderstood the coding of hour tens. 22h is actually coded as 0x22, not 0x32. The tens field has
regular BCD encoding as all others, too.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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