lbenson
Senior Member
I have a program in which it is useful to save data across downloads while I am doing testing. The only external flash I have is that in the DS1307 Real Time Clock (RTC) chip, 56 bytes in locations 8 through 3F. Putting data there is fairly straightforward, but I thought I would post the following code which tests the process. First it fills scratchpad locations 16-72 (decimal) with A-Z,a-z, and 0-3 (56 characters). Then it moves this data to 1307 locations 8-3F using subroutine copyScratchToRTC. Then it zeros the scratchpad. Then it moves the data back from the 1307 to the scratchpadcopy using subroutine RTCtoScratch. Finally it displays the scratchpad data. I am moving only printable characters, so I use the null character (binary 0) as a string terminator. If the subroutines encounter the null character, they stop moving data at that point; otherwise they move 56 characters. The variable, rNextSensorLoc, is used in my main program to hold the next available scratchpad location for holding data.
Code:
' 28Flash copies 56 bytes from scratchpad to DS1307 & back
#picaxe 28X1
symbol rNextSensorLoc = 0x64
start:
pause 1000
i2cslave %11010000, i2cslow, i2cbyte
ptr = 16
for b0 = "A" to "Z" ' fill scratchpad with data
@ptrinc = b0
next b0
for b0 = "a" to "z"
@ptrinc = b0
next b0
for b0 = "0" to "3"
@ptrinc = b0
next b0
poke rNextSensorLoc, ptr
@ptr = 0 ' terminate string
gosub copyScratchToRTC
ptr = 16
for b0 = 0 to 56
@ptrinc = 0 ' zero out the scratchpad
next b0
gosub copyRTCtoScratch
ptr = 16
sertxd(@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc)
sertxd(@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc)
sertxd(@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc)
sertxd(@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc)
sertxd(@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc)
sertxd(@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc)
sertxd(@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc,@ptrinc)
sertxd(13,10)
end
copyScratchToRTC: ' rtc bytes 8-3F
peek rNextSensorLoc,b2
ptr = 16
if ptr < b2 then
b3 = 8
do while b3 < 64 ' 3F=63
if ptr = b2 then
writei2c b3,(0) ' terminate string
exit
endif
writei2c b3,(@ptrinc)
inc b3
loop
else
writei2c 8,(0) ' terminate string
endif
return
copyRTCtoScratch: ' rtc bytes 8-3F or null terminated
ptr = 16
b3 = 8
do while b3 < 64 ' 3F=63
readi2c b3,(b4)
if b4 = 0 then exit
@ptrinc = b4
inc b3
loop
poke rNextSensorLoc,ptr ' save next location
return