#picaxe 20m2
'#no_data
#com 4
#terminal 4800
Symbol hrs = b0 ' Hours $00-$23
Symbol hrs.bit5 = bit5 ' 0=am, 1=pm (for 12 hour mode)
Symbol hrs.bit6 = bit6 ' 0=24, 1=12 hour mode
Symbol am_pm = bit8 ' am or pm (for 12 hour mode)
Symbol mode = bit9 ' 12 or 24 hrs mode
Symbol AM = 0
Symbol PM = 1
Symbol Mode12 = 1
Symbol Mode24 = 0
Symbol mins = b2 ' Minutes $00-$59
Symbol secs = b3 ' Seconds $00-$59
Symbol month = b4 ' Month $01-$12
Symbol day = b5 ' Day $01-$31
Symbol year = b6 ' Year $00-$99
Symbol dow = b7 ' Day of Week $00-$06 ( 0=Sun, 1=Mon ... 6=Sat )
Symbol control = b8 ' SQW I/O control
Symbol decval = b9
Symbol bcdval = b10
Symbol idx = b11
Symbol idy = b12
Symbol cursor = b13 'LCD cursor pos
'Sparkfun LCD constants
symbol lcd_cmd = $FE 'command prefix
symbol lcd_cmd2 = $7C 'special command prefix
symbol clrLCD = $01 'Clear entire LCD screen
symbol displayOff = $08 'Display off
symbol displayOn = $0C 'Display ON
symbol noCurs = $0C 'Make cursor invisible
symbol ulCurs = $0E 'Show underline cursor
symbol blkCurs = $0D 'Show blinking block cursor
symbol curpos = $80 'set cursor + position (row 1=0 TO 15, row 2 = 64 TO 79)
symbol scrollRight = $1C
symbol scrollLeft = $18
symbol curRight = $14
symbol curLeft = $10
symbol LCD = C.0
EEPROM 0,("SunMonTueWedThuFriSat")
hsersetup B9600_4,0
PAUSE 1000 ; give time for PE terminal Screen to initialize
HI2cSetup I2cMaster, $D0, I2cSlow, I2cByte
hserout 0,(lcd_cmd, displayOn)
pause 2000
hserout 0,(lcd_cmd, clrLCD, lcd_cmd, noCurs)
sertxd ("Enter 'y' to set date", cr, lf)
serrxd [5000,ShowTime],("y")
gosub SetClock
ShowTime:
reconnect
HI2cIn $0, (secs, mins, hrs, dow, day, month, year)
if secs = 128 then 'clock not set
gosub SetClock
end if
mode = hrs.bit6 'get 12/24 hour bit
if mode = Mode12 THEN
am_pm = hrs.bit5 'get am_pm bit (HIGH = p.m.)
hrs.bit5 = 0
hrs.bit6 =0
' hrs = hrs & %00011111 'clear bits 5 and 6
else
' hrs = hrs & %00111111 'clear bit 6
hrs.bit6 =0
endif
gosub Show_LCD
pause 1000
goto ShowTime
end
Show_LCD:
cursor = curpos + 2
hserout 0,(lcd_cmd, cursor)
bcdval=hrs : gosub BCD2DEC : if decval < 10 then hserout 0,("0") end if : hserout 0,(#decval, ":")
bcdval=mins : gosub BCD2DEC : if decval < 10 then hserout 0,("0") end if : hserout 0,(#decval, ":")
bcdval=secs : gosub BCD2DEC : if decval < 10 then hserout 0,("0") end if : hserout 0,(#decval, " ")
if mode = mode12 then
if am_pm = AM then
hserout 0,("a.m.")
else
hserout 0,("p.m.")
endif
endif
cursor = curpos + 66
hserout 0,(lcd_cmd, cursor)
idx = dow-1
idx = idx * 3
for idy = 0 to 2
read idx, decval
idx = idx + 1
hserout 0,(decval)
next
hserout 0,(" ")
bcdval=month : gosub BCD2DEC : if decval < 10 then hserout 0,("0") end if : hserout 0,(#decval, "/")
bcdval=day : gosub BCD2DEC : if decval < 10 then hserout 0,("0") end if : hserout 0,(#decval, "/")
bcdval=year : gosub BCD2DEC : if decval < 10 then hserout 0,("0") end if : hserout 0,(#decval)
return
SetClock:
sertxd ("Year (e.g.11)", cr, lf)
serrxd #decval : gosub DEC2BCD : year = bcdval
sertxd ("Month (1-12)", cr, lf)
serrxd #decval : gosub DEC2BCD : month = bcdval
sertxd ("Day (0-31)", cr, lf)
serrxd #decval : gosub DEC2BCD : day = bcdval
sertxd ("DayOfWeek: 1=SUN 2=MON 3=TUE 4=WED 5=THU 6=FRI 7=SAT", cr, lf)
serrxd #dow
sertxd ("Mode (1=12hr 0=24hr)", cr, lf)
serrxd #mode
if mode = 0 then
sertxd ("HH: 00-23",cr,lf)
else
sertxd ("HH: 01-12",cr,lf)
end if
serrxd #decval : gosub DEC2BCD : hrs = bcdval
sertxd ("Minute (00-59)", cr, lf)
serrxd #decval : gosub DEC2BCD : mins = bcdval
if mode = 1 then
sertxd ("0=AM 1=PM",cr,lf)
serrxd #am_pm
end if
secs = $00
control = 0 ' disable SQW output
IF mode = Mode12 THEN
hrs = hrs | %01000000 '$40 'set bit 6 high for 12 hr mode
end if
IF am_pm = PM THEN
hrs = hrs | %00100000 '$20 'set bit 5 high for PM
end if
HI2cOut $0, (secs, mins, hrs, dow, day, month, year, control)
return
DEC2BCD:
bcdval = decval/10*16
bcdval = decval//10 + bcdval
return
BCD2DEC:
decval = bcdval/16*10
decval = bcdval//16 + decval
return