Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
07/11/09 06:12
Read: times


 
Msg Score: -1
 -1 Didn't Search First
#167058 - Convert BCD, hex and decimal in C (DS1307 related)
Hi,

I am setting the time for DS1307 (RTC) that connects to an 8051 controller.

If I set the time to the RTC when loading the hex file to the controller. I have to write the data in the C code like this:

e.g. to set date: 16, July, 2020 and time: 15:10:00

RTC_ARR[1] = 0x10; // minute = 10
RTC_ARR[2] = 0x15; // hour = 15 ,24-hour mode(bit 6=0)
RTC_ARR[3] = 0x07; // Day = 7 or Sat
RTC_ARR[4] = 0x16; // Date = 16
RTC_ARR[5] = 0x07; // month = July
RTC_ARR[6] = 0x20; // year = 20 or 2020

With this, after I load the code to 8051, it will set the RTC correctly:

Day : Sat
Time : 15:10:00
Data : 16-Jul-2020

However, after the controller is up and running, if I want to re-set the RTC time, I could only do it via the controller keyboards which is decimal value. Therefore, I have tested by:

//decimal value similar to one later inputs via the 8051 keyboards, declare as constant value now just for testing

data unsigned char minute=10;
data unsigned char hour=15;
data unsigned char weekday=7;
data unsigned char day=16;
data unsigned char month=7;
data unsigned char year=20;

RTC_ARR[1] = minute;
RTC_ARR[2] = hour;
RTC_ARR[3] = weekday;
RTC_ARR[4] = day;
RTC_ARR[5] = month;
RTC_ARR[6] = year;

I get the wrong output:

Day : Sat
Time : 0F:0A:00
Data : 10-Jul-2014

;----------------------------------------------------------
The wrong setting I get because I have entered:

10(or 0x0A) instead of 16 (or 0x10)
15(or 0x0F) instead of 21 (or 0x15)
16(or 0x10) instead of 22 (or 0x16)
20(or 0x14) instead of 32 (or 0x20)

So my question is "how can I convert from those decimal value to hex format (e.g. 10 to becomes 0x10)?"
or
how can I enter the hex format directly from the keyboards?

It's easier for user to enter the setting value in decimal instead of hex.

Any suggestion will be much appreciated.


List of 14 messages in thread
TopicAuthorDate
Convert BCD, hex and decimal in C (DS1307 related)            01/01/70 00:00      
   Keyboard?            01/01/70 00:00      
   Another thought about that keyboard...            01/01/70 00:00      
      Re:Another thought about that keyboard...            01/01/70 00:00      
         Wrong approach?            01/01/70 00:00      
            Re: Wrong approach?            01/01/70 00:00      
               Division            01/01/70 00:00      
               Not a shortcut?            01/01/70 00:00      
                  Re: Not a shortcut?            01/01/70 00:00      
                     Basics (edited)            01/01/70 00:00      
                        addition to the above            01/01/70 00:00      
                           Look at the DS1307 datasheet            01/01/70 00:00      
   Check this            01/01/70 00:00      
      Incorrect            01/01/70 00:00      

Back to Subject List