' 20loadI2CSer flash i2c eeprom over serial
#picaxe 20M2 ' leg 8: 0V; leg 7: O0, serout, infraout; leg 6: I1, O1, ADC1; leg
#no_data
#terminal 4800 'turn on terminal after program load
'setfreq M2 ' all baud rates are halved
symbol tcpInPin = c.5 ' b.1
'symbol baudrate=T600_4 ' serial in will be 300 baud
symbol baudrate=T2400_4 ' serial in will be 300 baud
symbol sertxBaud=N2400_4 ' 1200 baud
symbol i2cDevice=%10101110 ' note "111" address
'symbol i2cDevice=%10100000 ' note "000" address
symbol cI2CSpeed=i2cfast
symbol eeprom_Length_Addr=0
symbol htmlLengthAddr=0
symbol eeprom_base=16
symbol htmlBase=16
symbol cHTMLaddrBase=28 ' start of address/length pairs
' for html pages
symbol inputBuffBase=120 ' WARNING! may wrap to b0
'symbol bitFlag=b0
symbol bMatchFlag=bit0
symbol bHTMLEndFlag=bit1
symbol nPages=b15
symbol matchCount=b16 ' if 7, have matched "</html>?
symbol value=b17
symbol n1 = b18
symbol year=b19
symbol month=b20
symbol day=b21
symbol hour=b22
symbol minute=b23
symbol eeprom_addr=w12
symbol addr_low=b24
symbol addr_high=b25
symbol wTemp1=w13
symbol wTemp1Low=b26
symbol wTemp1Hi=b27
symbol i2c_addr=s_w1
symbol i2c_addr_Limit=s_w2
symbol i2c_addr2=s_w3
symbol wHTMLPagePtr=s_w4
symbol wHTMLStartAdr=s_w5
symbol wHTMLlength=s_w6
start:
pause 8000 ' startup time
' high 0
' readtemp b.4,b4
sertxd("20loadI2CSer on ",#tcpInPin,cr,lf)
pause 500 ' startup time
i2cslave %11010000, i2cslow, i2cbyte
' writei2c 0, ($00, $10, $22, $06, $18, $01, $14, $10) ' smhwdmy
wHTMLPagePtr = cHTMLaddrBase
wHTMLStartAdr = htmlBase
poke wHTMLPagePtr, WORD wHTMLStartAdr
gosub getDateTime
gosub loadI2Ceeprom
pause 5000
gosub printTemplate
sertxd("Done",13,10)
main:
do
pause 1000
readtemp b.4,b4
' sertxd ("Temp ",#b4,13,10)
sertxd("Temp ",#b4," ")
pause 1000
loop
getDateTime:
'#rem
i2cslave %11010000, i2cslow, i2cbyte
' writei2c 0, ($00, $06, $17, $01, $09, $12, $13, $10) ' smhwdmy
pause 40
gosub getDate
' sertxd ("Date is ", #year,":",#month,":",#day,13,10)
sertxd("Date is ", #year,":",#month,":",#day,13,10)
gosub getTime
' sertxd ("Time is ", #hour,":",#minute,13,10)
sertxd("Time is ", #hour,":",#minute,13,10)
return
getTime:
readi2c 0, (b1,b2,b3)
' sertxd ("Getting Time3 ", #n1," ",#n2," ",#n3, 13, 10)
bcdtoascii b1, b5, b6
' second = n5-"0"*10+n6-"0"
b4 = b2
bcdtoascii b4, b1, b2
minute = b1-"0"*10+b2-"0"
'sertxd ("n4 n1 n2 minute: ", #n4," ",n1," ",n2," ",#minute, 13, 10)
bcdtoascii b3, b4, b5
hour = b4-"0"*10+b5-"0"
return
getDate: '
readi2c 4, (b1,b2,b3) ' xxxxdmy
' sertxd ("Getting Date ", #n1," ",#n2," ",#n3, 13, 10)
'day = bcdtobin n1
'month = bcdtobin n2
'year = bcdtobin n3
bcdtoascii b1, b5, b6
day = b5-"0"*10+b6-"0"
b4 = b2
bcdtoascii b2, b5, b6
month = b5-"0"*10+b6-"0"
bcdtoascii b3, b5, b6
year = b5-"0"*10+b6-"0"
return
loadI2Ceeprom:
hi2csetup i2cmaster,i2cDevice, cI2CSpeed, i2cword
eeprom_addr=eeprom_base
' sertxd ("Load to ",#eeprom_addr,13,10)
b0 = i2cDevice / 2 & %00000111
sertxd("Load to ",#eeprom_addr,"; I2C device ",#b0,13,10)
' serout tcpOutPin, baudrate, ("14LoadI2C ",13,10)
do
serin tcpInPin, baudrate, b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15
' sertxd (b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15)
sertxd(b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15)
' writei2c eeprom_addr, (b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15)
hi2cout eeprom_addr, (b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15)
eeprom_addr = eeprom_addr + 16
pause 10
if b15 = 0 then
bptr = 15
' b15 = @bptrdec
if b0 > 0 then
do while @bptrdec = 0 and bptr > 0 : dec eeprom_addr : loop
endif
' eeprom_addr = eeprom_addr - 16
hi2cout eeprom_Length_Addr, (addr_low,addr_high)
' sertxd (cr,lf,#eeprom_addr," bytes written",cr,lf)
sertxd(cr,lf,#eeprom_addr," bytes written",cr,lf)
pause 2000
exit
endif
pause 40
loop
return
printTemplate:
'#rem
' i2cslave i2cDevice, i2cfast, i2cword
hi2csetup i2cmaster,i2cDevice, cI2CSpeed, i2cword
value = $fe
i2c_addr = htmlLengthAddr ' Length of html code
hi2cin i2c_addr, (wTemp1Low,wTemp1Hi)
i2c_addr_Limit = wTemp1 + 16 +16 ' don't read past this address
i2c_addr = htmlBase ' start of html with embedded "~nn" variables
sertxd("HTML Template: base, length ",#i2c_addr," ",#wTemp1,cr,lf)
'i2c_addr = 0
do while value <> 0 and value <> $ff and i2c_addr < i2c_addr_Limit
gosub readI2Cbuff
bptr = bptr - 16
for n1 = 1 to 16
if bHTMLEndFlag = 1 then
bHTMLEndFlag= 0
i2c_addr2 = i2c_addr - 16 + bptr - inputBuffBase
hi2cout i2c_addr2, (0) ' null terminate the html code
inc nPages ' add to the web page count
peek wHTMLPagePtr, WORD wHTMLStartAdr
wHTMLlength = i2c_addr2 - wHTMLStartAdr
wHTMLPagePtr = wHTMLPagePtr + 2
poke wHTMLPagePtr, WORD wHTMLlength
wHTMLPagePtr = wHTMLPagePtr + 2
inc i2c_addr2
poke wHTMLPagePtr, WORD i2c_addr2 ' start of next page, if any
endif
bMatchFlag = 0
select matchCount
case 0
if @bptr = "<" then : bMatchFlag=1 : endif
case 1
if @bptr = "/" then : bMatchFlag=1 : endif
case 2
if @bptr = "h" then : bMatchFlag=1 : endif
case 3
if @bptr = "t" then : bMatchFlag=1 : endif
case 4
if @bptr = "m" then : bMatchFlag=1 : endif
case 5
if @bptr = "l" then : bMatchFlag=1 : endif
case 6
if @bptr = ">" then ' complete match
bHTMLEndFlag=1
endif
endselect
if bMatchFlag = 1 then
inc matchCount
else
matchCount = 0
endif
if @bptrinc = 0 then
dec bptr ' point back to null
value = 0
do while n1 <= 16 : @bptrinc = " " : inc n1 loop
endif
next n1
bptr = bptr - 16
sertxd(@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc, _
@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptr)
loop
' write the length of the last web page
i2c_addr2 = i2c_addr - 16 + bptr - inputBuffBase
inc nPages ' add to the web page count
peek wHTMLPagePtr, WORD wHTMLStartAdr
wHTMLlength = i2c_addr2 - wHTMLStartAdr
wHTMLPagePtr = wHTMLPagePtr + 2
poke wHTMLPagePtr, WORD wHTMLlength
sertxd(cr,lf)
'#endrem
return
readI2Cbuff:
bptr = inputBuffBase
hi2cin i2c_addr, (@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc, _
@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc)
i2c_addr=i2c_addr+16
return