DS1307 Flash Memory Usage

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
 

leftyretro

New Member
Wow, great timing. I just ordered a DS1307 Real Time Clock module and this will come in very handy. Do you know if there are any other general purpose PICAXE routines to support this RTC chip such as loading initial time, etc?

Lefty
 

lbenson

Senior Member
I think those are covered in other threads in the code snippets section. In the Program Editor you can click Help, PICAXE 28/40 Datasheets, i2c tutorial--it has a section on the 1307.

Briefly, to initialize you do:

i2cslave %11010000, i2cslow, i2cbyte
writei2c 0, ($00, $42, $23, $03, $15, $10, $08, $10) ' smhwdmy

The bytes written are seconds, minutes, hours, weekday, day, month, year, control. The hex values show the BCD numbers, e.g., the 15th day of the 10th month of the year '08.

To read:

readi2c 0, (b0,b1,b2,b3,b4,b5,b6) ' smhwdmy

To convert the day of the month to two ascii characters:

bcdtoascii b4, b7, b8

In the above example, b7 would be "1" and b8 would be "5".

To convert those to binary:

b9 = b7 - "0"
b10 = b8 - "0"

To make a single binary number of the two digits:

b11 = b7 - "0" * 10 + b8 - "0"

Enjoy.
 

westaust55

Moderator
As well as a search on this forum for DS1307 have a look at the Rev Ed datasheet/tutorial on i2c at:
http://www.rev-ed.co.uk/docs/axe110_i2c.pdf

If you have an 28X1/40X1 PICAXE then
look in manual 2 at the commands that start with Hi2c . . . .
else
look in manual 2 at the commands that start with i2c . . . . .
endif

:)
 
Last edited:
Top