2 Decimal Varibles to 1 Hexadecimal Varible

NOS9

New Member
Greetings Everyone, Im new to this forum, though ive been watching from the seems for a few months.

Im completing a PICAXE project that is based around a 28x2 AXE001 starter kit, connected with a AXE033 DS1307 & LCD starter kit.

Ive been working with the design for a while, and have already implemented my reading of the date varibles from the DS1307 clock, however there is a couple issuse i am having, that i am suppised/irritated that are not covered.

When the DS1307 is programmed with the Time & Date data, the data is in a Hex format, well that great, but how the hell do you convert values to hex, and if it is so simple, why the hell was it not including in the AXE033 manual.

Also... because the AXE033 is a parallel interface to the LCD display, ive been using a memory varible for each number, so two numbers to cover the units & the tens columb.

I think i might have worked a way to combine the two decimal numbers, but i need some help with converting the decimal number to the hex eqivalent.

I have some understanding on hex numbers in general, but ive been unable to find any information that linked with the DS1307 & decimal to hex conversion.

Any suggestions or help would be welcome, thanks.
NOS9
 

BeanieBots

Moderator
Welcome to the forum.

The data from a DS1307 is in BCD (binary coded decimal) format.
BCD is a subset of HEX and does NOT require any conversion. Any BCD number is exactly the same as HEX. However, not all HEX numbers are the same as BCD because some will be invalid.

There is a command "BCDTOASCII" which will help you convert BCD values to ASCII values for sending to an LCD display.

If you want decimal numbers (not suitable for LCD display) then it's just a question of taking each nibble of the BCD number. Or, take the ASCII values from BCDTOASCII and subtract "0" to convert from ASCII to decimal.

I've used the DS1307 chip extensively and have never needed to convert decimal to BCD to use it. Simply send/write your data as HEX.

To send 12 (maybe for the hours) simply send $12 which is BCD for 12.
 

ValueAdd

Senior Member
If you were to look at the DS1307 datasheet that clearly states the values are in BCD format. http://www.sparkfun.com/datasheets/Components/DS1307.pdf
DESCRIPTION
The DS1307 Serial Real-Time Clock is a low-power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM.
A search of this forum also provides many example of extracting the BCD into data to sent to an LCD display such as the AXE033.
By way of just a couple of examples, look here: http://www.picaxeforum.co.uk/showthread.php?t=13909
and maybe post 8 at http://www.picaxeforum.co.uk/showthread.php?t=7745
 

NOS9

New Member
Hi,

It looks like i wasn't clear enough... ValueAdd i have already got the conversion stage covered for converting the BCD date from the clock, and have got that information displayed on the LCD display.

Im at the stage where by i am wanting to set the clock with new adjusted time values stored in varibles. Ive adjusted the time values already converted from the values retrieved from the clock, and now need to combine the values and send them to the clock.

Im having trouble with the clock, because i thought the data was BCD as you stated, and that as you also stated, i read on another topic that BCD input is the same as a decimal input. However when i added two varibles together, and sent the value to the clock, when i did a debug the value seemed to have been recieved as a hex value, so i wanted to know how to convert to hex to try it as an idea.

My clock code is big, because ive done graphical touches for my LCD output, flashing numbers and stuff, so im not going to display my whole code, but here is the bit i was trying out.

Code:
let hourtemp=0 'varible to 0
let h01=h01-48 'set hours tens to subtract 48.
let h10=h10-48 'set hours units to subtract 48.
let hourtemp=hourtemp+h10*10+h01 'mutiplied the tens columb by 10, then added the units to the tens.
I think my varibles have ascii numbers stored in them, so i subtracted 48 as i added that number so the correct numbers were displayed on my LCD display, then i multiplied the tens numbers, and then added the minutes.

I am probably doing something wrong, but i did try googling really hard, which believe it or not, usually helps... but the AXE033 manual do not really cover about programming the clock.... its as if they expect you to manually input BCD values and then program the clock.

Any help would be welcome, even if it turns out i needed sleep and its something simple, thanks.

NOS9
 

eclectic

Moderator
Just for background reading perhaps,

P.E. > Wizards >AXE 110 datalogger > Set DS1307 Date /time > OK

Then look at the program that is generated.

e
 

Attachments

Chavaquiah

Senior Member
so im not going to display my whole code
No, indeed. That would take all the fun of guessing what might be wrong with it. :rolleyes:

I think my varibles have ascii numbers stored in them
You think?!

DecimalNumber = 23
Tens = DecimalNumber / 10
Units = DecimalNumber // 10
BCDNumber = Tens * 16 + Units = 35 = $23

ASCII_Tens = "2"
ASCII_Units = "3"
Tens = ASCII_Tens - "0"
Units = ASCII_Units - "0"
BCDNumber = Tens * 16 + Units = 35 = $23
 

westaust55

Moderator
The change you need is in red. a couple for text changes in comments in green

et hourtemp=0 'varible to 0
let h01=h01-48 'set hours units to subtract 48.
let h10=h10-48 'set hours tens to subtract 48.
let hourtemp = h10*16+h01 'mutiplied the "tens" column by 16 to get it into the high nybble, then added the units to the tens.

By way of explanation, you must move the digit from the low nybble to the high nybble for the "tens" part of the time/date.
this involves moving four bits to the left. 2 to the power of 4 (2^4) = 16 so we multiply by 16 and not 10
 
Last edited:

NOS9

New Member
Thank you for your direction & help guys, i will make the editions to my code.

NOS


Update: Thank you for your suggestions Chavaquiah & Westaust55, it worked first time with no problems. It made perfect sense, and i expected i was overlooking something, thank you picaxe forum.

NOS
 
Last edited:
Top