Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Decimal to Hex Conversion C Code needed

Status
Not open for further replies.
@ bigdogguru

Actually I want to know what value I have to write to ds1307's year register so that when it reads ds1307 and bcd2dec is done it displays 99 year on lcd.

What is the decimal or hex or bcd value I have to write to year register?
 

So you are saying that I have to conver my decimal 23 to bcd 35 and write that 0x35 to hours register to set it to 23 hours. So after it is set and again read and transformed using bcd2dec I get my 23 back. Am I right?

No.
A decimal value of 23 when converted to BCD equals DECIMAL 35, NOT HEXADECIMAL 0x35

When represented as a hexadecimal number then a decimal value of 23 when converted to BCD equal 0x23
 

@ FvM

Does that mean I have to write 0x63 to rtc year register so that it is set to year 99? Then where is the BCD number used?

99 decimal = 1001 1001 BCD = 0x99. which value should I write to year register. Is ir 99 or 0x99 or 0x63?
 

0x63 is 6 for the high nibble and 3 for the low nibble , this will result is year 63
 

Add the following routines:

DS1307 : Setting Time
Looks like the author had some problems to perform proper dec2bcd(), too. Or why he's using fixed numbers?

The example numbers are nevertheless correct.

To set year 99 you write dec2bcd(99) to the register, which is 0x99.
 

@ FvM what is the reault of dec2bcd(99)? Is it 0x63 or 0x99?

- - - Updated - - -

@ bigdogguru

I have completed my graduate course. I don't have teachers anymore.
 

@ alexan

I think it is 0x99

Can I give decimal 153 to year register instead of 0x99 for year 99. That is what I was doing.
 

It is, is it that complicated?

it doesn't matter if you write it as HEX 0x99 or DEC 153 or BIN 0b10011001
 

But that is what I am doing. I used to give decimal 153 for year 99, but I am getting the right value on lcd but not in the proteus clock window. You can see my problem link posted above.

you can see the image in post 5 here https://www.edaboard.com/threads/272381/

I have circled the proteus clock window in the image. Those undesired values are causing the problem, because of those values my alarm is not working.
 

So you say, the RTC value is displayed correctly on LCD and wrong in a Proteus window? Perhaps you should start a new thread about Proteus bugs.

Even if you are sure what the problem is, did you notice that the strange numbers shown in the Proteus DS1307 don't fit into a BCD number?
 

@ FvM

This is what I am writin to the day register. These are decimal values.

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,22,23,24,25,26,27,28,29,30,31,38,39,40,41,42,43

they represent 0 to 31 days. But it is giving errors in proteus clock window.

I am not able to use alarm because of such problem with day, year, hour, and minute variables.
 

This is what I am writin to the day register. These are decimal values.

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,22,23,24,25, 26,27,28,29,30,31,38,39,40,41,42,43

they represent 0 to 31 days. But it is giving errors in proteus clock window.
I just browsed the rtc code in the said thread and found that the set-routine is using incorrect values as well. I remember that illegal values are not filtered by DS1307 and are given back as illegal values or even cause unexpected bevaviour. The correct decimal number sequence for 0..31 day values should be 0..9,16..25, 32..41,48,49

You'll get it by writing the two decimal digits as hex value and converting to decimal again. e.g. today is day 26, write as 0x26 = 38. Or use suggested dec2bcd() code.

P.S.: For the intended INC/DEC set routine, I would work with the decimal value and convert with dec2bcd() when sending to register.
 
Last edited:
If these are the decimal values that you write to the chip then they are not valid

for example
decimal 10 equals HEX 0x0A
decimal 11 equals HEX 0x0B
decimal 12 equals HEX 0x0C
decimal 13 equals HEX 0x0D
decimal 14 equals HEX 0x0E
decimal 15 equals HEX 0x0F
....

The allowed values are

0x00
0x01
0x02
0x03
0x04
0x05
0x06
0x07
0x08
0x09
0x10 // and not 0x0A because these values represent BCD
0x11
...
0x99


If you want the correct decimal values then just convert these hex values to decimal
 

Woo Hoo. the alarm is working. Set date and time to alarm and see. It works.

- - - Updated - - -

@ FvM Should I write these decimal values to days register?

0-9
22-37
50-65
72
73

for 0-31 days

- - - Updated - - -

@ alexan

No, I am not writing A, B, C, D,.....
 

Attachments

  • PIC16F877 DS1307 RTC.rar
    104.4 KB · Views: 73

@ alexan

No, I am not writing A, B, C, D,.....

Well, you are the one who said
This is what I am writing to the day register. These are decimal values.

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,22,23,24,25, 26,27,28,29,30,31,38,39,40,41,42,43

when you write a decimal value of 10 to the day register it is like writing 0x0A.
 

OK. I got it. But how the alarm is working. I have posted the files in my last post. Here the date and time is 27/11/2012 and 03:43 AM

at 3:20 AM I set the alarm for 3:22 AM and it worked.

- - - Updated - - -

OK. I got it. But how the alarm is working. I have posted the files in my last post. Here the date and time is 27/11/2012 and 03:43 AM

at 3:20 AM I set the alarm for 3:22 AM and it worked.

OK . I got it. I have to write bcd 99 i.e., 1001 1001 or decimal 153 for year 99. right?

- - - Updated - - -

@ FvM

49 decimal is 0x31, but you said I have to use bcd? bcd of decimal 49 is 49. Should write 0x49 or just 49? Just 49 means decimal 49, 0x49 means bcd 49.
 

I clearly said decimal 49, referring to your decimal number sequence. Reviewing the day 26 example should clarify the suggestion.
 

@ FvM

Is these values correct

day 0-31 = 0..9,16..25, 32..41,48,49

year 0-99 = 0..9,16..25, 32..41, 48..57, 64..73, 80..89, 96..105, 112..121, 128..137, 144..153

month 1-12 = 1..9, 16..18

hour 1-23 = 1..9, 16..25, 32..35

minutes 0-59 = 0..9, 16..25, 32..41, 48..57, 64..73, 80..89
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top