Shop OBEX P1 Docs P2 Docs Learn Events
i use DS1302 timer and want to make a calculation — Parallax Forums

i use DS1302 timer and want to make a calculation

maverhmaverh Posts: 7
edited 2009-03-16 21:54 in BASIC Stamp
Hi,

We read the datetime value from DS1302 it work fine
GOSUB ReadRTCBurst
DEBUG 2,1,1,"jaar=", HEX2 Year," maand=", HEX2 Month," dag=",HEX2 Date," uur=", HEX2 Hours," min=", HEX2 Minutes," sec=",HEX2 Seconde
Now we want to make a calculation with the datetime variables
······· IF·Minutes=15 THEN
········· temp = ((Month * 1736) - 1736) + ((Date * 56) - 56) + (((Hours-6) * 4) - 4) + 1
······· ENDIF
······· IF Minutes=30 THEN
········· temp = ((Month * 1736) - 1736) + ((Date * 56) - 56) + (((Hours-6) * 4) - 4) + 2
······· ENDIF
······· IF Minutes=45 THEN
········· temp = ((Month * 1736) - 1736) + ((Date * 56) - 56) + (((Hours-6) * 4) - 4) + 3
······· ENDIF
······· IF Minutes=60 THEN
········· temp = ((Month * 1736) - 1736) + ((Date * 56) - 56) + (((Hours-6) * 4) - 4) + 4
······· ENDIF
······· IF (Minutes=15) OR (Minutes=30) OR (Minutes=45) OR (Minutes=60) THEN
········· HIGH 11:SEROUT 10\11,16780,[noparse][[/noparse]"TIM ",DEC temp]
······· ENDIF

The issue is that if Minutes=15 then minutes=special character
I can debug Minutes with HEX and the value is ok.
How can i change it to a decimal number




Seconde VAR· Byte
Minutes VAR· Byte
Hours· VAR· Byte
Date· VAR· Byte
Month· VAR· Byte
Day· VAR· Byte
Year· VAR· Byte



'==================== DS1302 Real-Time Clock Subroutines ===================
PrintIt:
· 'Prints zero (0) terminated text from EEPROM
· READ Idx,Value··· 'Get next character
· IF Value = 0 THEN Finished· 'Make sure it's not a binary 0
· DEBUG Value····· 'Display it on screen
· Idx = Idx + 1
· GOTO PrintIt
Finished:
RETURN

WriteRTCRAM:
·'Write to DS1302 RAM Register
· HIGH RTCCS
· SHIFTOUT Dta, Clk, LSBFIRST, [noparse][[/noparse]%0\1,RTCCmd\5,%11\2,Value]
· LOW RTCCS
RETURN

WriteRTC:
·'Write to DS1302
· HIGH RTCCS
· SHIFTOUT Dta, Clk, LSBFIRST, [noparse][[/noparse]%0\1,RTCCmd\5,%10\2,Value]
· LOW RTCCS
RETURN

ReadRTCBurst:
·'Read all time-keeping registers in one burst
· HIGH RTCCS
· SHIFTOUT DTA, Clk, LSBFIRST, [noparse][[/noparse]%1\1,BrstReg\5,%10\2]
· SHIFTIN DTA, Clk, LSBPRE, [noparse][[/noparse]Seconde,Minutes,Hours,Date,Month,Day,Year]
· LOW RTCCS
RETURN

ReadRTCRAM:
·'Read DS1302 RAM Register
· HIGH RTCCS
· SHIFTOUT DTA, Clk, LSBFIRST, [noparse][[/noparse]%1\1,RTCCmd\5,%11\2]
· SHIFTIN DTA, Clk, LSBPRE, [noparse][[/noparse]Value]
· LOW RTCCS
RETURN

SetTimeAndDate:
· 'Write time values into all time-keeping registers, being sure to clear
· 'the write-protect bit in CtrlReg before the write, and set the
· 'write-protect bit after the write
· FOR Idx = 0 TO 8
··· LOOKUP Idx,[noparse][[/noparse]0,$00,Minutes,Hours,Date,Month,Day,Year,128],Value
··· LOOKUP Idx,[noparse][[/noparse]CtrlReg, SecReg, MinReg, HrsReg, DateReg, MonReg, DayReg, YrReg, CtrlReg],RTCCmd
··· GOSUB WriteRTC
· NEXT
RETURN

Comments

  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-03-16 21:54
    minutes = minutes.nb1 * 10 + minutes.nib0
    hours = hours.nib1 * 10 + minutes.nib0

    and so on, converts BCD to decimal.

    Watch out for a negative result in (hours-6)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.