
Originally Posted by
Melanie
Convert BCD to decimal... assume variable RTCSeconds holds the seconds in BCD format...
First let's deal with the TENS value (Bits 4,5 and 6)...
DecimalSeconds=RTCSeconds & $70
DecimalSeconds=DecimalSeconds>>4
DecimalSeconds=DecimalSeconds*10
And finally add-in the UNITS value...
DecimalSeconds=DecimalSeconds+(RTCSeconds & $0F)
----
To convert Decimal back to BCD, reverse the Process...
RTCSeconds=DecimalSeconds DIG 1
RTCSeconds=RTCSeconds<<4
RTCSeconds=RTCSeconds+(DecimalSeconds DIG 0)
----
Incorporate the above into a General Purpose Subroutine and use the same single routine to convert Seconds, Minutes, Hours etc etc.
Melanie
Thanks a lot. I was breaking my head from last few days to learn this. You have really explained it very nicely.
BUT
I am facing slight problem in my simulation (I am using proteus).
My code looks like this when program starts for the first time:
Code:
Rtcget var byte[8]
Rtcsec var Rtcget[0]
Rtcmin var Rtcget[1]
Rtchr var Rtcget[2]
Rtcday var Rtcget[3]
read 30,check
if check=0 then
pause 50
I2CWRITE SDA,SCL,$D0,$00,[$00,$00,$12,$01,$00,$00,$00,$90]
pause 50
write 30,1
endif
I2CREAD SDA,SCL,$D1,$00,[STR Rtcget\8]
pause 50
sec=Rtcsec & $70
sec = sec>>4
sec=sec*10
sec=sec +(Rtcsec & $0F)
minu=Rtcmin & $70
minu = minu>>4
minu=minu*10
minu=minu +(Rtcmin & $0F)
hrs=Rtchr & $30
hrs=hrs>>4
hrs=hrs*10
hrs=hrs+(Rtchr & $0F)
if Rtcday=$01 then days=1
if Rtcday=$02 then days=2
if Rtcday=$03 then days=3
if Rtcday=$04 then days=4
if Rtcday=$05 then days=5
if Rtcday=$06 then days=6
if Rtcday=$07 then days=7
& when user changes the time it looks like this
Code:
Rtcmin=minu DIG 1
Rtcmin=Rtcmin<<4
Rtcmin=Rtcmin+(minu DIG 0)
Rtchr=hrs DIG 1
Rtchr=Rtchr<<4
Rtchr=Rtchr+(hrs DIG 0)
if days=1 then Rtcday=$01
if days=2 then Rtcday=$02
if days=3 then Rtcday=$03
if days=4 then Rtcday=$04
if days=5 then Rtcday=$05
if days=6 then Rtcday=$06
if days=7 then Rtcday=$07
I2CWRITE SDA,SCL,$D0,$00,[$00,Rtcmin,Rtchr,Rtcday,$00,$00,$00,$90]
PAUSE 20
The problem is that during simulation, the frequency of the pulse changes once I set the time (coming from DS1307 - I want 1Hz)
Please point out the problem in my code if any, or is it the simulation software which is not correct (Proteus 6.9)
Bookmarks