'PICAXE 20X2 defaultspeed 8MHz
'Test I2C bus with DS1307 RTC and 24Lc256 EEPROM
#terminal 9600
symbol sec = b0
symbol mins = b1
symbol hour = b2
symbol day = b3
symbol day_mo = b4
symbol month = b5
symbol year = b6
symbol control = b7
symbol pm_am = b8
symbol out1 = b9
symbol out2 = b10
symbol out3 = b11
symbol out4 = b12
symbol out5 = b13
symbol out6 = b14
symbol DS1307 = b15
symbol _24LC256 = b16
let DS1307 = %11010000 'hex D0, dec 208
let _24LC256 = %10100000 'hex A0, dec 160
let control = %00010000 'Enable output at 1Hz
'Set 1Hz SQW
hi2csetup i2cmaster, DS1307, i2cslow, i2cbyte
hi2cout 7, (control)
pause 10
hi2cin $7,(control)
pause 300 'control won't print without this pause
SerTxd("Control ",#control)
pause 1000
main:
hi2csetup i2cmaster, DS1307, i2cslow, i2cbyte
hi2cin $0,(sec,mins,hour,day,day_mo,month,year)
gosub Time
gosub Date
pause 2000 'to see time
hi2csetup i2cmaster, _24LC256, i2cslow, i2cword '24lc256
gosub read_24lc256
goto main
'********
'* Time *
'********
Time:
pm_am ="P" : if hour < $12 then :pm_am = "A" : endif 'indicate pm or am
if hour = $20 or hour = $21 then : let hour = hour - $6 : endif
if hour > $12 then : let hour = hour - $12 : endif '24 to 12 hour format
if hour = $0 then : hour = $12 : endif
bcdtoascii hour,out1,out2
bcdtoascii mins ,out3,out4
bcdtoascii sec ,out5,out6
SerTxd(CR, LF,out1,out2,":",out3,out4,":",out5,out6," ",pm_am,"M -- ")
return
'********
'* Date *
'********
Date:
bcdtoascii month,out1,out2
bcdtoascii day_mo ,out3,out4
bcdtoascii year ,out5,out6
SerTxd(out1,out2,"/",out3,out4,"/",out5,out6, CR, LF, CR, LF)
return
'****************
'* Read 24LC256 *
'****************
Read_24lc256:
'read the first page (0-63) plus one
'first page data:
' "abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz 1234567891"
'All other bytes = 0
for w1=0 to 64 'w1 = 64 should give null byte = blank print
hi2cin w1, (b0)
SerTxd( b0) 'print all on 1 line
pause 20
next w1
SerTxd(CR, LF )
SerTxd( "Done ", CR, LF )
return