Reading from a DS1307 RTC: compare BCD with decimal

parto

Member
Hi,

I am hoping to get some help reading data from a DS1307 real-time-clock and using the data in a PICAXE program. I know how to set the clock. But how do I read the year data and use it? I'd like to say in the program, "if year > 2016 then". But how do I compare my decimal "2016" with what comes out of the clock in BCD format?

Thanks!
 

westaust55

Moderator
If you are using an X1 or X2 PICAXE part then there is the BCDtoBIN command to help you.
For other parts you will need to do the calculation:

Year = BCDyear / 16 * 10 ; extract tens part
Year = BCDyear // 10 + Year; extract units part and combine

There are other 1 line ways to do the above calculation however the 2-line code shows clearly how the conversion is done.

Assuming you are only interested in years post/since 2000 then as the DS1307 only gives a 2 digit number you will need to add 2000 if you wish to work with the year as a full 4 digit number.
 

parto

Member
I have the 08M2 and I got confused seeing the BCDtoBIN command in the examples I found. I will plug in your suggestions; thank you!
 

hippy

Technical Support
Staff member
It is possible to treat BCD just like any other number, except use hexadecimal when doing it. For example for year > 2016 ...

If year > $16 Then
 

parto

Member
Thanks hippy, I think you're saying that in the same way that I put the numbers into the clock to set the time initially: "let hour = $10", I can read them back out: "If hour > $14 Then". This makes sense intuitively and would make things easier; I really have no need to get rid of the dollar sign.

The formulas that westaust55 supplied work well and it was good to see how that was done. I'll try using just the dollar signs and see how that goes.

I haven't a clue why, after working through understanding conversions between decimal, binary, and BCD formats, there is hexadecimal format also involved, but I'll work on that next....

Thanks again!
 

hippy

Technical Support
Staff member
BCD numbers are just a subset of hexadecimal numbers; $00-$09, $10-$19, $20-$29 etc.
 

westaust55

Moderator
Virtually all computers work only in binary numbers (i.e. %xxxxxxxx in PICAXE code).
We humans typically have some difficulty working in binary so the computer programs display and take data values in decimal with which we are familiar.

For those writing the code, hexadecimal is used as an easy way to represent the binary values.
The first (most significant) 4 bits known as a Nybble are represented by one digit (0-9 & A -F) and the next 4 bits are represented by another hexadecimal digit and so on. To identify a number as hexadecimal either $ or "0x" is fairly universally added as a prefix to the number.
It is still not easy for us humans to work in hexadecimal (try and mentally do some maths) but the conversion back and forth between binary and hexadecimal if easy. It is also faster (less pen/key strokes) to write or type a value in hexadecimal than in binary (eg $A5 rather than %10100101).

For programmers binary is still a good way to be able to see exactly what value (1 or 0) is assigned to each bit/function for a port or (function) register.

BCD (binary coded decimal) is a way of having a binary digit (i.e. 0 to 9 rather than 0 to F) encoded in each nybble so that a byte comprising 2 nybbles = 1 byte can hold a representation for decimal numbers 00 to 99. From early years discrete logic chips such as the 4000 series and 74xx series had specify 1 digit/nybble to 7-segment drivers to display values in decimal on 7-segment displays so we humans could read the output values more easily then reading a binary value displayed on 8 or 16 separate LEDs . Very early computers did often just display data on the front in binary and take input via 8 or 16 (or 32) switches.
 
Last edited:
Top