DC1302 Time Chip Coding Problem
vk1222
Posts: 6
I have a simple code that sets the time and then records the time every 30 seconds or so using a data logger but for some reason the timing is not working. I put together the code using the demo code that the parallax site has and I've hit quite a road bump. I can't figure out what part of the demo code actually makes the timing chip keep time. When I run the demo its fine so its connected properly but when I run my code it logs the same time that I set over and over again. I have put the code here too but if anyone can explain what is wrong with it that keeps it from actually keeping the time. Any help is appreciated.
' {$STAMP BS2e} ' {$PBASIC 2.5} ' -----[ I/O Definitions ]------------------------------------------------- ' -----[ I/O Definitions ]------------------------------------------------- OUTPUT 4 OUTPUT 3 DataIO PIN 0 ' DS1302.6 Clock PIN 1 ' DS1302.7 CS1302 PIN 2 ' DS1302.5 LED PIN 3 LED1 PIN 4 TX PIN 8 ' Transmit Data --> 27937.4 (RXD) RTS PIN 9 ' Request To Send --> 27937.6 (CTS) RX PIN 10 ' Receive Data <-- 27937.5 (TXD) CTS PIN 11 ' Clear To Send <-- 27937.2 (RTS) ' -----[ Constants ]------------------------------------------------------- Baud CON 84 ' Serial Baud Rate 9600 bps (BS2) NumSamples CON 1 ' Number Of Samples To Log WrSecs CON $80 ' Write Seconds RdSecs CON $81 ' Read Seconds WrMins CON $82 ' Write Minutes RdMins CON $83 ' Read Minutes WrHrs CON $84 ' Write Hours RdHrs CON $85 ' Read Hours CWPr CON $8E ' Write Protect Register WPr1 CON $80 ' Set Write Protect WPr0 CON $00 ' Clear Write Protect WrBurst CON $BE ' Write Burst Of Data RdBurst CON $BF ' Read Burst Of Data WrRam CON $C0 ' Write RAM Data RdRam CON $C1 ' Read RAM Data Hr24 CON 0 ' 24 Hour Mode Hr12 CON 1 ' 12 Hour Mode ' -----[ Variables ]------------------------------------------------------- buffer VAR Byte(15) ' Input Buffer index VAR Byte ioByte VAR Byte ' Input/Output Storage reps VAR Byte reg VAR Byte ' Read/Write Address secs VAR Byte secs01 VAR secs.LOWNIB secs10 VAR secs.HIGHNIB mins VAR Byte ' Minutes mins01 VAR mins.LOWNIB mins10 VAR mins.HIGHNIB hrs VAR Byte ' Hours hrs01 VAR hrs.LOWNIB hrs10 VAR hrs.HIGHNIB date VAR Byte month VAR Byte day VAR Byte ' Day year VAR Byte ' Year ' -----[ EEPROM Data ]----------------------------------------------------- Sun DATA "SUN", 0 ' Day Abbreviations Mon DATA "MON", 0 Tue DATA "TUE", 0 ' These data statements could Wed DATA "WED", 0 ' contain the full day name and Thu DATA "THU", 0 ' the code would still work Fri DATA "FRI", 0 ' without change. Sat DATA "SAT", 0 ' -----[ Initialization for Disk Drive]-------------------------------------------------- DEBUG CLS, "Memory Stick Datalogger Demo V1.0", CR, CR, "Initializing..." PAUSE 200 ' Allow Time To Settle HIGH TX ' Initialize Transmit Line LOW RTS ' Take Vinculum Out Of Reset PAUSE 600 ' Allow Time To Settle DEBUG "Done!", CR, "Synchronizing..." DO SEROUT TX\CTS, Baud, ["E", CR] ' Sync Command Character GOSUB Get_Data ' Get Response PAUSE 250 LOOP UNTIL ioByte = $0D ' Wait For Carriage Return DO SEROUT TX\CTS, Baud, ["e", CR] ' Sync Command Character GOSUB Get_Data ' Get Response PAUSE 250 LOOP UNTIL ioByte = $0D ' Wait For Carriage Return DEBUG CLS, "Memory Stick Datalogger Demo V1.0", CR, CR, "Initializing..." PAUSE 200 ' Allow Time To Settle HIGH TX ' Initialize Transmit Line LOW RTS ' Take Vinculum Out Of Reset PAUSE 600 ' Allow Time To Settle DEBUG "Done!", CR, "Synchronizing..." DO SEROUT TX\CTS, Baud, ["E", CR] ' Sync Command Character GOSUB Get_Data ' Get Response PAUSE 250 LOOP UNTIL ioByte = $0D ' Wait For Carriage Return DO SEROUT TX\CTS, Baud, ["e", CR] ' Sync Command Character GOSUB Get_Data ' Get Response PAUSE 250 LOOP UNTIL ioByte = $0D ' Wait For Carriage Return ' -----[ Program Code ]---------------------------------------------------- Main: DEBUG "Done", CR, "Switching to Short Command Mode..." SEROUT TX\CTS, Baud, ["SCS", CR] ' Switch To Short Command Mode GOSUB Get_Data ' Purge Receive Buffer DEBUG "Done!", CR, "Waiting for Memory Stick..." Check_Drive: DO SEROUT TX\CTS, Baud, [CR] ' Prompt Device For Status GOSUB Get_Data ' Purge Receive Buffer IF buffer(0) = ">" THEN ' Check For Ready Prompt EXIT ' If Ready Then Exit Loop ELSEIF buffer(0) = "N" AND buffer(1) = "D" THEN DEBUG "." ' Device Ready But No Memory Stick ELSEIF buffer(0) = "D" AND buffer(1) = "D" AND reps = 0 THEN DEBUG "Connected!", CR, "Accessing..." reps = 1 ' Memory Stick Ready ELSE DEBUG "." ENDIF PAUSE 250 ' Command Retry Delay LOOP DEBUG "Ready!", CR ' -----[ Initialization for Time Chip]-------------------------------------------------- Init: reg = CWPr ' Initialize DS1302 ioByte = WPr0 ' Clear Write Protect GOSUB RTC_Out ' Send Command '------------------[Setting Time]------------------------------------------------------- Start: DO ' DEBUG Menu DEBUG CLS ' Clear The DEBUG Screen DEBUG CRSRXY, 0, 0, "Press 1 to Set Date/Time.", CR DEBUGIN DEC1 reps ' Get 1 Number IF reps = 1 THEN GOSUB Set_Mode GOSUB Set_Time GOTO Record_Data ENDIF LOOP Record_Data: FOR reps = 0 TO 10 SEROUT TX, Baud, [$09, $20, "datafile.txt", CR] GOSUB Get_Data ' Purge Receive Buffer SEROUT TX, Baud, [$08, $20, $00, $00, $00, $10, $0D, CR, HEX2 month, "/", HEX2 day, "/", HEX2 year, "_", HEX2 hrs, ":", HEX2 mins, ",", CR, LF, CR] PAUSE 500 ' Write walked through door after handwash used = 1 ' Close File (MUST CLOSE!) SEROUT TX, Baud, [$0A, $20, "datafile.txt", CR] DEBUG DEC reps PAUSE 30000 NEXT DEBUG CLS DO DEBUG CRSRXY, 0, 0, "Done", CR LOOP ' -----[ Subroutines ]----------------------------------------------------- Get_Data: index = 0 ' Reset Index Pointer DO ' Receive Data SERIN RX\RTS, Baud, 100, Timeout, [ioByte] buffer(index) = ioByte ' Add Received Byte To Buffer index = index + 1 ' Increment Index Pointer IF index > 14 THEN Timeout ' Check For Overflow LOOP Timeout: RETURN Set_Mode: DEBUG CLS ' Clear The DEBUG Screen DEBUG CRSRXY, 0, 0, "Enter 2 digit year, i.e. 05 = 2005:", CR DEBUGIN HEX2 year ' Set Year DEBUG CRSRXY, 0, 1, "Enter 2 digit month, i.e. 03 = March:", CR DEBUGIN HEX2 month ' Set Month DEBUG CRSRXY, 0, 2, "Enter 2 digit date, i.e. 02 = 2nd:", CR DEBUGIN HEX2 date ' Set Date DEBUG CRSRXY, 0, 3, "Enter day of the week as a number 1-7", CR DEBUG CRSRXY, 0, 4, "1=SUN, 2=MON, 3=TUE, 4=WED, 5=THU, 6=FRI, 7=SAT", CR DEBUGIN HEX1 day DEBUG CRSRXY, 0, 6, "Enter 2 digit hour, 00-23:", CR DEBUGIN HEX2 hrs ' Set Hours (24 Hour Mode) DEBUG CRSRXY, 0, 8, "Enter 2 digit minutes, 00-59:", CR DEBUGIN HEX2 mins ' Set Minutes ' Setting clockMode to modeFlag will effectively ' set or clear BIT7 in the hrs variable. ' Setting ampm to ampmFlag will effectively set BIT5 in the ' hrs variable to the proper value. ' This must only be done when modeFlag is set (12 Hour Mode), ' otherwise you can destroy hours above 19 in 24 Hour Mode. DEBUG CRSRXY, 0, 9, "Press ENTER to set time.", CR DEBUGIN reps RETURN ' Send Time/Date To DS1302 Set_Time: ' DS1302 Burst Write HIGH CS1302 ' Select DS1302 SHIFTOUT DataIO, Clock, LSBFIRST, [WrBurst] SHIFTOUT DataIO, Clock, LSBFIRST, [secs, mins, hrs, date, month, day, year, 0] LOW CS1302 ' Deselect DS1302 RETURN Get_Time: ' DS1302 Burst Read HIGH CS1302 ' Select DS1302 SHIFTOUT DataIO, Clock, LSBFIRST, [RdBurst] SHIFTIN DataIO, Clock, LSBPRE, [secs, mins, hrs, date, month, day, year] LOW CS1302 ' Deselect DS1302 RETURN RTC_Out: HIGH CS1302 ' Select DS1302 SHIFTOUT DataIO, Clock, LSBFIRST, [reg, ioByte] LOW CS1302 ' Deselect DS1302 RETURN
Comments
As long as you don't overwrite those registers, the unit will keep time.
I used the same demo code and the system has been running over a year.
You'll have to check your code to make sure you aren't mistakenly changing the registers due to running out of variable space or when you come back from running another program slot.