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
Converting data types
Moderator: Benj
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Converting data types
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])
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])
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: Converting data types
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
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
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Converting data types
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.
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.
Martin