Atmel dataflash memory is attractive for use with the BASIC Stamp in data logging or other memory intensive applications.
Here is a simple getting started demo of some of the main commands required to access the RAM memory and to program and read the flash memory .
' Demo of the AT45D0xx
' under control of the Stamp II
' (c) 1999 Tracy Allen, Electronically Monitored Ecosystems.
cmd var byte ' AT45D0xx command, to be sent by the Stamp
page var word ' page of 264 bytes, 1024 pages in the AT45D020
adrs var byte ' which byte on page
' bytes 256-263 not used
ix var byte ' general purpose index
x var byte ' general purpose byte
' here are the connections to hardware control lines:
atcs con 8 ' AT45D0xx chip select line
sck con 9 ' AT45D0xx clock line
sdo con 10 ' AT45D0xx data output line
sdi con 11 ' AT45D0xx data input line
sdip var in11 ' AT45D0xx data status
' here is demo data that will be stored in the dataflash:
msg data "this is a test.. 1.. 2.. 3.."
outs=%0000000111100001
'fedcba9876543210
dirs=%1111011111111111
main:
'------------------------------------------------
' first write some data to one of the RAM buffers
cmd=$84 ' write to buffer 1 command
adrs=0 ' write at the beginning of the page
gosub sndcmd
for ix=0 to 27 ' 28 byte message
read msg+ix,x ' read it from BS2 eeprom
debug x ' show it on screen
shiftout sdo,sck,msbfirst,[x\8] ' put it in the AT45D0xx
next
debug cr
high atcs ' finish the command
'
'------------------------------------------------
' now read back the data from the RAM buffer
cmd=$54 ' read from buffer 1
adrs=0 ' read from the beginning of the page
gosub sndcmd
for ix=0 to 27 ' 28 byte message
shiftin sdi,sck,msbpost,[x] ' get it from the AT45D0xx
debug x ' show it on screen
next
high atcs ' finish the command
debug cr
'
'------------------------------------------------
' now erase a flash eeprom page and put the data there
cmd=$83 ' erase flash page & write buffer 1 there
page=5 ' choose page 5 for the demo
debug hex4 page,cr
gosub sndcmd ' do it, erase and program page 5!
high atcs ' finish the command
gosub waitready ' wait for it to finish writing
'pause 20 ' alternative to wait routine, hard pause
'
'------------------------------------------------
' now read data directly from the flash page
cmd=$52 ' read page direct command
page=5 ' same page as above
adrs=0 ' top of the page
gosub sndcmd
for ix=0 to 27 ' read 28 bytes
shiftin sdi,sck,msbpost,[x\8]
debug x
next
high atcs ' finish the command
debug cr,cr
'
pause 1000
end ' end of the demo program
'------------------------------------------------
' subroutine sends the command
' some commands require more bytes than others
' some commands require a lot of extra clocking bits
' This routine asserts chip select, but it is up to the
' calling routine to bring the chip select back high.
sndcmd:
low atcs
shiftout sdo,sck,msbfirst,[cmd\8,page<<1\16,adrs\8]
if cmd>$56 then sndend
shiftout sdo,sck,msbfirst,[0\8]
if cmd>$52 then sndend
shiftout sdo,sck,msbfirst,[0\16,0\8]
sndend:
return
'------------------------------------------------
' subroutine waits for the chip to finish writing to flash page
' it has a timeout loop
' note that it is necessary to wait before starting another
' write to the flash
' however, the program could start sending data to the
' alternate RAM buffer #2 even while the write is in progress.
waitready:
low atcs
shiftout sdo,sck,msbfirst,[$ae\9]
ix=255
wr1:
pause 1
ix=ix-1
if sdip-1*ix then wr1
high atcs
if ix=0 then error
return
error:
debug "dataflash write error",cr
return