conversion Hex to Dec for clock chip DS1307

JanJor

Member
Dear forum-members,
In order to read the data from my clock chip DS1307, I use the program below, including a 5-line conversion calculation (the sub called "reken") to change the clock data from hex to dec.
The program runs OK, so that's not the problem.
My question now is: is there a conversion command in the Pixcaxe programming language (that I would have overlooked) that avoids the use of my quite lengthy conversion calculation?

Thanks very much in advance for your help,

Ronald.

Here's the program:

(Picaxe 40X2)
Code:
#no_table
#no_data


pause 1000
serout a.0, t2400,(254,128,1)
pause 200
serout a.0, t2400,(254,192,1)
pause 200
serout a.0, t2400,(254,128,"                ")
pause 200
serout a.0, t2400,(254,192,"                ")
pause 200

i2cslave %11010000,i2cslow, i2cbyte

loopje:
let b7=b0
readen:
readi2c 0, (b0)
let b8=b0
gosub reken
let b0=b11
if b0=b7 then goto readen
readi2c 0, (b0,b1,b2,b3,b4,b5,b6)

let b8=b0
gosub reken
let b0=b11
let b8=b1
gosub reken
let b1=b11
let b8=b2
gosub reken
let b2=b11

serout a.0, t2400,(254,128,#b2,"h, ",#b1,"m        ") 
pause 30
serout a.0, t2400,(254,192,", ",#b0,"s         ")
pause 30
goto loopje


reken: 'converts hex to dec
b9=b8/16
b10=b9*16
b10=b8-b10
b11=b9*10
b11=b11+b10
return

end
 
Last edited by a moderator:

Goeytex

Senior Member
There should be no need to convert hex to decimal. Hex and decimal are just different ways to display binary data.

For example:
"Let B0 = 15" is exactly the same thing as "Let B0 = $0F" and Let B0 = %00001111. In all three cases, the memory register for B0 will hold the binary value of 00001111 in the respective bits.

Therefore .... readi2c 0, (b0,b1,b2,b3,b4,b5,b6) does not receive data as hex or decimal since the DS1307 actually sends the data as binary. It receives the data as binary data in to B0. The So for example it if receives "10001111". That represents both HEX 8F and Decimal 143.
 
Last edited:

bpowell

Senior Member
That formula is not converting HEX to Decimal, it's converting Binary-coded-decimal to decimal. Just FYI.

The DS1307 sends data out in BCD...So, 5:00 PM in 24-hour time would be 17-hours...in regular binary, that would be 0b00010001 However, in BCD, it's 00010111 (both nibbles create the numbers 1 and 7) That's what you're converting to Binary.
 

JanJor

Member
Dear Goeytex, dear bpowell, thanks for your reply. However I'm lost now completely. I tried, following Goeytex comment ""Let B0 = 15" is exactly the same thing as "Let B0 = $0F" and Let B0 = %00001111.", to leave the conversion step away, but then I get, for the seconds, the value 1, 2, 3, 4, 5, 6, 7, 8, 9 and then a jump to 16-25 and then a jumt from 32-41.
Thus, I need to make the conversion in the sub-program "reken". But the point is: is there a way to avoid the five conversion steps in "reken" for the second, the minutes and the hours by replacing them through a simple Picaxe command and still get a sensible reading on my LCD-screen?
Thanks again for your help.
Ronald.
 

jims

Senior Member
I use something like this to get data into format for my serial OLED display. Jim S
Code:
convert:	'* Convert BCD to AScii
	 bcdtoascii secs, b10,b11
	 bcdtoascii mins, b12,b13
	 bcdtoascii hour, b14,b15
	 bcdtoascii dow, b16,b17
	 bcdtoascii day, b18,b19
	 bcdtoascii month, b20,b21
	 bcdtoascii year, b22,b23
 

bpowell

Senior Member
JanJor,

If you just want to display the result...then "BCDTOASCII" is an option

Code:
bcdtoascii b1,b2,b3
Where B1 is your BCD encoded number, and B2 is the "Tens" and B3 is the "Ones".

(Picaxe Manual #2, Page 36).

However, if you want to do any calculations, comparisons, etc on the time...then your subroutine is the way to go...it converts the BCD number to a regular Binary number that you can then compare, test, etc.

You're subroutine looks great; why get rid of it?
 

AllyCat

Senior Member
Hi JanJor,

Your subroutine reken: actually converts BCD to binary (which is then converted to ASCII by the # operator). It can actually be done in one line as hippy showed here, but it's quite an "advanced" method.

It's important to decide how you want the characters to be formatted on the LCD. Suppose the BCD value is "00" (binary %0000 0000). If you "convert" that to binary or decimal (actually, that particular value doesn't change) and then use PICaxe's # operator (to convert to ASCII), it will print as "0". But BCDTOASCII will produce two "digits" (e.g. b2 and b3) and you will print both to produce "00". Note that if you print "0" after "59" then the display may show "09" because only the 5 is overwritten.

Here is another thread which discusses methods, but the final link now seems broken.

Cheers, Alan.
 

JanJor

Member
Dear all,
Thanks for all your comments. It helped me to better understand the BCD - binary - ASCII relation. Very usefull and helpful.
Thanks again,
Ronald
 
Top