Help on converting decimal to hex

For E-blocks user to discuss using E-blocks and programming for them.

Moderators: Benj, Mods

Post Reply
User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 91 times
Been thanked: 8 times

Help on converting decimal to hex

Post by achillis1 »

Hello,

I am interfacing an RTC ds1307. I can read the registers and print them on screen. To read them, first I get the hex value from a register and then split it in High Nibble and Low Nibble. Then I assign the value of the high nibble to a string1 and the low nibble to a string2; then I add the two string into the string3. Then if I want to print the value I select at the LCD macro to print the string3.(e.x string1=4 , string2=7 , string3 then is 47)
Now in order to write to a specific register I must sent to that address a BYTE value by the "transmit byte" command at the I2C macro.
The value can be in the form of 01..02..... or in the form of 0x01...0x02...e.t.c.

1.Now the problem is that I need to change in real time the registers value. For this I need to convert the string3 value(47) to a byte value in order to send it back to the RTC.
Can the conversion happen?

By the way I can split the hex value from the RTC and merge it back again with the following way:
merged = high
merged =merged << 4
merged = merged + low

This is the opposite procedure of splitting the hex value to a high and a low nibble-I think- can you confirm that?

2. I try to change the RTC registers in that way: e.x the hex value from RTC is 0x1f. Then I split and know that the decimal is 31. 31 I can print it on LCD. But if I want to add one; to be 32, then I add one to the low nibble and then merge them back with high(high=1, low=15+1-->16, merged =32=0x20). If you check from the variables window then the procedure is OK.
But when I send the "merged" byte to RTC then some of the values are correct but other are false! E.x 17..18..19..111..112..113..114..25..26..27..28..29..210..211..32.
If you take a look at the sequense then you will see that there is a reason to that bizzare sequense. After 19 it should be 20 but instead the "2" digit is being represented as 11(1+1=2)!
same as 29 when it is going to be 30 then the "3" digit is being shown as 21(2+1=3) and another 1 after, which means 31!

I also try to modify directly the return value from I2C macro taken from the RTC register. I mean that I do: "retValue=retValue+1" instead of splitting the retValue, add one to the low nibble and again merge it back. But this also gives the same results!
Can it be some crazy act of the RTC?

I do not know if I am clear or not, I know that I wrote a lot!!! I do not know if you have the ds1307 RTC to test any of this but if you have a wild guess of what might be wrong please let me know!

Basicaly I would like to convert the numeric value of a string to a byte and to be able to be printed as that value, because I do in the "calculation icon" TestByte=String, it then the TestByte takes the value of the string but when I print on LCD it prints other character!

And to find a way to increment or decrement a hex value for the purpose of changing the registers of the RTC clock.

Also a huge help for this program is that I could assign a value to a variable from the numeric keypad. More specificaly to be able to push for example the key 5 two times and assign the value of 55. That would bypass all the above.

Thank you in advance,

Best Regards,
Achillis

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 91 times
Been thanked: 8 times

Re: Help on converting decimal to hex

Post by achillis1 »

Hello,

A quick correction at the above mentioned.
The registers of the RTC are written in BCD format.
If the value I get from the RTC, I split it and print it is OK.

If I write to the RTC the value 0x59, 0x31, 0x5 e.t.c and after I read that specific register the value will be correct, 0x59 will display 59 and so on.

But if I write the values without the prefix 0x then this is how the value will be displayed if I read the register after:
59-->311, 31-->115, 5-->05, 12-->012, 23-->17, 38-->26.

I am attaching the datasheets of the RTC.

Thank you.
Attachments
main.c
(859 Bytes) Downloaded 377 times
AN504.pdf
(193.62 KiB) Downloaded 388 times
AN95.pdf
(105.98 KiB) Downloaded 400 times

KeithSloan
Posts: 114
Joined: Fri Jul 27, 2007 10:50 am
Been thanked: 1 time

Re: Help on converting decimal to hex

Post by KeithSloan »

"59-->311, 31-->115, 5-->05, 12-->012, 23-->17, 38-->26."

Don't know if it helps but 59 = (3 * 16) + 11
31 = (1 * 16) + 15
5 = 5
12 = 12
23 = ( 1 * 16) + 7
38 = ( 2 * 16 ) + 6

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 91 times
Been thanked: 8 times

Re: Help on converting decimal to hex

Post by achillis1 »

Hello,

Thank you Keith for your time and trouble.
Nice observation by the way!
You have calculated correct, because if I take for example the number 31-->115 which by you way is: 31=(1*16)+15 then the following logic comes in place:
1. The parenthesis states the high nibble of a hex value and
2.the + n states the low nibble of the hex value.
3.Otherwise= 31=0x1F.

I can say that these calculations helped me understand how RTC Clock formats the value stored in it's registers.
Finally I am well manipulating the RTC clock.

Thank you Keith.

Best Regards,
Achillis

User avatar
Dan81
Valued Contributor
Valued Contributor
Posts: 268
Joined: Sun Jan 15, 2006 4:07 pm
Location: Albi France
Been thanked: 60 times
Contact:

Re: Help on converting decimal to hex

Post by Dan81 »

Hello Achillis

I'm not sure that strings are useful. Conversion can be made with a calculation.

Daniel
Attachments
RTC-DS1307.fcf
(10.5 KiB) Downloaded 551 times

Post Reply