Page 1 of 1

Converting data types

Posted: Thu May 27, 2010 7:54 am
by JAW
I'm having trouble being able to convert to decimal values into a hex value to send to a dallas time chip. I'm trying to write code for a clock with a menu enabelling you to change the time. The display shows the decimal value but I can't get my head round converting to hex and then sending down the I2C. Any help would be greatly appreciated.

Regards

James

Re: Converting data types

Posted: Thu May 27, 2010 9:00 am
by Benj
Hello James,

First use the string manipulation function toString to convert the number in to an ascii string. Once you have done this you can send the string by referencing the string as a byte array.

eg

stringvar = toString(123)

i2csend (stringvar[0])
i2csend (stringvar[1])
i2csend (stringvar[2])

Re: Converting data types

Posted: Thu May 27, 2010 9:48 am
by JAW
Hi Benj,

Thank you for your reply. I'm trying to go down the route of converting the decimal value to a bcd code. So far I've found a previous post but can't get it to workk correctly :-

dec_t = dec/10
dec_u = dec MOD 10
bcd = (dec_u << 4)
bcd = bcd + dec_u

dec is the original decimal value
dec_t and dec_u are temporary values
bcd is the final final value

Regards

James

Re: Converting data types

Posted: Thu May 27, 2010 12:03 pm
by medelec35
Hello JAW.
Try chaging bcd = (dec_u << 4)
to
bcd = (dec_t << 4)

So you have:
dec_t = dec/10
dec_u = dec MOD 10
bcd = (dec_t << 4)
bcd = bcd + dec_u

Hope this helps.

Re: Converting data types

Posted: Thu May 27, 2010 12:41 pm
by JAW
Thanks Medelec that's working perfectly now.

Many Thanks

James