Shop OBEX P1 Docs P2 Docs Learn Events
Code for bs2p40 w/DS1307 timestamp clock w/user control — Parallax Forums

Code for bs2p40 w/DS1307 timestamp clock w/user control

NewBeeNewBee Posts: 35
edited 2010-06-14 15:50 in BASIC Stamp
Hi,

Comments requested.· This project started with StampWorks 2.1 exercise 33.· My work here still contains· few lines from the example code.· This is not perfect but it seems to be a frequent topic so here it is.· Things about this code:

1.· It is large (honestly I don't know yet how much more the 2p40 will do.· Therefore improvements are solicied.)
2.· The DS 1307 only reserves 1 byte to store the year and·therefore·only provides for a 2 digit year indicator as in '09 '10 '99 and so on.· to fix this short coming I used a SRAM register to facilitate an upper byte for the year indicator.
3.· There is no decrement so adjustments to the clock parameters wrap around.· This is a problem when incrementing past the end of the decade and in to the next century by mistake.
4.· This·program only·increments the lower byte of the year indicator.· Enabling adjustment of the upper byte will resolve item # 3 above.
3.· The original exercise used the modulus operator to control numeric clock parameters thus allowed simple code for increment and decrement, but I had problems applying the same technique to "MOY" and use of· "// 12."· I worked with it for a while but gave up and moved on.· I don't know what space might be saved by uding the modulus operator as compare to the current technique.

Enjoy,

Bob

' =========================================================================
'
'·· File....... Date & Time.BSP
'·· Purpose.... Real-time-clock interfacing
'·· Author.....
'·· E-mail.....
'·· Started.... 09 June 2010
'·· Updated.... 14 June 2010
'
'·· {$STAMP BS2p}
'·· {$PBASIC 2.5}
'
' =========================================================================

'
[noparse][[/noparse] Program Description ]
'
' This program demonstrates the access and control of an external real-
' time-clock chip, the DS1307.

'
[noparse][[/noparse] I/O Definitions ]
SDA············ PIN···· 0······················ ' I2C serial data line
SCL············ PIN···· 1······················ ' I2C serial clock line
Spkr··········· PIN···· 2······················ ' speaker output
BtnBus········· VAR···· INH···················· ' input pins 8 - 15

'
[noparse][[/noparse] Constants ]

DS1307········· CON···· %1101 << 4

'
[noparse][[/noparse] Variables ]
control········ VAR···· Byte··················· ' SQW I/O control
DecConvert····· VAR···· Byte
DateArray······ VAR···· Byte(9)················ ' DateArray(0) = Seconds
··············································· ' DateArray(1) = Minutes
··············································· ' DateArray(2) = Hours
··············································· ' DateArray(3) = DOW
··············································· ' DateArray(4) = Date
··············································· ' DateArray(5) = Month
··············································· ' DateArray(6) = year
··············································· ' DateArray(7) = control
btns··········· VAR···· Byte··················· ' debounced button inputs
btnProg········ VAR···· btns.BIT7·············· ' enable programing the clock
btnbk·········· VAR···· btns.BIT6·············· ' Back button (not enabled/intrgrated)
btnYear········ VAR···· btns.BIT5·············· ' +/- year
btnMonth······· VAR···· btns.BIT4·············· ' +/- month
btnDate········ VAR···· btns.BIT3·············· ' +/- date
btnDay········· VAR···· btns.BIT2·············· ' +/- day
btnHr·········· VAR···· btns.BIT1·············· ' +/- hours
btnMn·········· VAR···· btns.BIT0·············· ' +/- minutes
pntr··········· VAR···· Byte··················· ' ee pointer
cntr··········· VAR···· Byte··················· ' Counter
char··········· VAR···· Byte··················· ' character for display
Dirty·········· VAR···· Bit···················· ' control set_clock occurances
'
[noparse][[/noparse] EEPROM Data ]
DayNames······· DATA··· "SunMonTueWedThuFriSat"
'
[noparse][[/noparse] Initialization ]
Reset:
· #IF ($STAMP < BS2P) #THEN
··· #ERROR "This code requires BS2P or greater"
· #ENDIF
Setup:
· DEBUG CLS,
······· "DS1307 Clock set up and control", CR,
······· "
", CR
'First deteermine if the clock was previously set
· I2CIN SDA, DS1307, 08, [noparse][[/noparse]decconvert]························· ' retrieve clock registers
· IF decconvert < 20 THEN····································· ' clock not set
··· I2COUT SDA, DS1307, 08, [noparse][[/noparse]20]······························ ' write 2 MSB for year······· '
··· DTMFOUT Spkr, 100, 100, [noparse][[/noparse]1, 2, 3]························· ' simple alert for debugging
··· Reset_Clock:
··· GOSUB Get_Buttons········································· ' scan buttons
' load the date array with the clock defaults to be used
··· datearray(0) = 00········································· ' sec
··· datearray(1) = 50········································· ' min
··· datearray(2) = 13········································· ' 13:00 hours
··· datearray(3) = 07········································· ' DOW
··· datearray(4) = 12········································· ' DOM
··· datearray(5) = 06········································· ' MOY
··· datearray(6) = 10········································· ' year (Lower byte)
··· datearray(7) = 0·········································· ' initialize the control bit
··· datearray(8) = 20········································· ' SRAM Address. This byte is
······························································ ' has 2 uses, 1. indicates the
······························································ ' clock has been set and 2.
······························································ ' it is the upper byte for
······························································ ' setting of the year
··· GOSUB set_clock
· ELSE························································ 'clock already set
··· DTMFOUT Spkr, 10, 10, [noparse][[/noparse]1, 2, 3, 4, 5, 6]·················· 'Simple alert for debugging
· ENDIF
· Dirty = 0··················································· ' clear the flag
'
[noparse][[/noparse] Program Code ]
Main:
· GOSUB Get_Clock············································· ' read DS1307
· 'line 1 prints th time and line 2 prints the date
· DEBUG CRSRXY, 0, 12,
······· DEC2 datearray(2), ":", DEC2 datearray(1), ":",
······· DEC2 datearray(0), CR,
······· DEC2 datearray(5), "/", DEC2 datearray(4), "/",
······· DEC2 datearray(8), DEC2 datearray(6), CR
· DEBUG CRSRXY, 0, 14
· GOSUB print_day·············································· ' turn DOW # into text
· GOSUB Get_Buttons············································ ' check if buttons pressed
· SELECT btns
··· CASE = %10000001··········································· ' adjust minutes
····· datearray(0) = $00······································· ' set seconds back to 0
····· DateArray(1) = DateArray(1) + 1·························· ' Increment the array
····· IF DateArray(1) >= 59 THEN······························· ' if more than 59 sec
······· DateArray(1) = 0······································· ' set back to 0
····· ENDIF
····· Dirty = 1················································ ' set the flag
··· CASE = %10000010··········································· ' adjust hours
····· DateArray(2) = DateArray(2) + 1·························· ' increment the value
····· IF DateArray(2) >= 24 THEN······························· ' if more than 24 hrs
······· DateArray(2) = 0······································· ' set back to 0
····· ENDIF
····· Dirty = 1················································ ' set the flag
··· CASE = %10000100··········································· ' adjust DOW
····· DateArray(3) = DateArray(3) + 1·························· ' increment the value
····· IF DateArray(3) >= 8 THEN································ ' if more than 7
······· DateArray(3) = 1······································· ' set back to 1
····· ENDIF
····· Dirty = 1················································ ' set the flag
··· CASE = %10001000··········································· ' adjust DOM
····· DateArray(4) = DateArray(4) + 1·························· ' increment the value
····· IF DateArray(4) >= 32 THEN······························· ' if more than 31
······· DateArray(4) = 1······································· ' Set back to 1
····· ENDIF
····· Dirty = 1················································ ' set the flag
··· CASE = %10010000··········································· ' adjust MOY
····· DateArray(5) = DateArray(5) + 1·························· ' increment the value
····· IF DateArray(5) >= 13 THEN······························· ' if more than 12
······· DateArray(5) = 1······································· ' set back to 1
····· ENDIF
····· Dirty = 1················································ ' set the flag
··· CASE = %10100000··········································· ' adjust the year
····· datearray(6) = datearray(6) + 1·························· ' increment the value
····· IF DateArray(6) >= 100 THEN······························ ' if moe than 99
······· DateArray(6) = 0······································· ' set back to 0
······· datearray(8) = datearray(8) + 1························ ' increment the upper byte
····· ENDIF
····· Dirty = 1················································ ' set the flag
· ENDSELECT
· IF Dirty = 1 THEN············································ ' a button was pressed and
··· Dirty = 0·················································· ' date or time has changed
··· GOSUB Set_Clock············································ ' so update DS1307
· ENDIF
· GOTO Main

'
[noparse][[/noparse] Subroutines ]
Get_Buttons:
· btns = %11111111············································· ' enable all eight inputs
· FOR cntr = 0 TO 7
··· btns = btns & ~BtnBus······································ ' test inputs
· NEXT
RETURN
Print_Day:
· pntr = DayNames + ((datearray(3) - 1) * 3)··················· ' point to 1st char
· FOR cntr = 0 TO 2············································· ' print 3 letters
··· READ (pntr + cntr), char···································· ' read letter
··· DEBUG char················································· ' print it
· NEXT
· RETURN
' Do a block WRITe To clock registers
Set_Clock:
· FOR cntr = 0 TO 8
··· IF cntr < 8 THEN············································ 'Don't convert Ram Data
····· DecConvert· = (datearray(cntr) / 10 << 4) + (datearray(cntr) // 10) ' decimal to BCD
····· I2COUT SDA, DS1307, cntr, [noparse][[/noparse]DecConvert]···················· ' update clock registers
··· ELSE
····· I2COUT SDA, DS1307, 08, [noparse][[/noparse]datearray(8)]···················· ' update year upper byte
··· ENDIF
· NEXT
· RETURN
' Do a block read from clock registers
Get_Clock:
· FOR cntr = 0 TO 8
··· I2CIN SDA, DS1307, cntr, [noparse][[/noparse]DecConvert]························· ' retrieve clock registers
··· IF cntr < 8 THEN·············································· ' Don't convert Ram Data
····· datearray(cntr) = DecConvert.NIB1 * 10 + DecConvert.NIB0···· ' BCD to decimal
··· ELSE
····· I2CIN SDA, DS1307, 08, [noparse][[/noparse]datearray(8)]······················· ' get year upper byte
··· ENDIF
· NEXT
· RETURN
·
Sign In or Register to comment.