SPI EEPROM is external memory which can be added to a PICAXE system which keeps its contents even when power is removed.
SPI EEPROM and other SPI devices are connected to the PICAXE using a three-wire SPI bus (often with an additional one device select signal) and can be controlled with a range of SPI specific commands.
SPI EEPROM is available in a range of storage sizes.
SPI EEPROM
Schematic
PCB
Read and write toan 26LC160 SPI EEPROM using SPI commands
This test program demonstrates SPI interfacing to the EEPROM. Two bytes are written and then read back to verify. This program uses 'spiin' and 'spiout' commands and will be slower than using 'hspiin' and 'hspiout'.
- Code Example:
-
; Input / Output pin setup ;24LC160 pins ; 1 = CS = PICAXE B.7 ; 2 = SO = PICAXE C.4 ; 3 = WP = +5V ; 4 = Vss = 0V ; 5 = SI = PICAXE C.5 ; 6 = SCK = PICAXE C.3 ; 7 = HOLD = +5V ; 8 = Vdd = +5V ; remember PICAXE input connects to EEPROM output and vice versa! symbol cs = B.7 ; chip select is PICAXE output 7 ; 25LC160 Commands symbol WRSR = 1 ; write status register symbol WR = 2 ; write symbol RD = 3 ; read symbol WRDI = 4 ; write disable symbol RDSR = 5 ; read status register symbol WREN = 6 ; write enable ; Variables symbol add_low = b0 ; address low byte symbol add_high = b1 ; address high byte symbol test_data1 = b2 ; dummy test data 1 symbol test_data2 = b3 ; dummy test data 2 symbol SR_Value1 = b4 ; read status register 1 symbol SR_Value2 = b5 ; read status register again Symbol RD_Value1 = b6 ; read data byte 1 Symbol RD_Value2 = b7 ; read data byte 2 init: ; set cs high and wait a while high cs ; chip select high pause 50 ; wait a while ; load some test values add_high = 0 add_low = 25 test_data1 = 55 test_data2 = 99 main: ; write enable low cs spiout C.3,C.5,MSBFIRST_L,(WREN,2) high cs ; write status register to 0 to remove block protect low cs spiout C.3,C.5,MSBFIRST_L,(WRSR,0) high cs pause 5 ; wait write time ; write enable low cs spiout C.3,C.5,MSBFIRST_L,(WREN) high cs ; read status register to see if it has worked ; we expect this value to now be 2 low cs spiout C.3,C.5,MSBFIRST_L,(RDSR) spiin C.3,C.4,MSBPRE_L,(SR_Value1) high cs ; write low cs spiout C.3,C.5,MSBFIRST_L,(WR,add_high,add_low,test_data1,test_data2) high cs pause 5 ; wait write time 5ms ; read low cs spiout C.3,C.5,MSBFIRST_L,(RD,add_high, add_low) spiin C.3,C.4,MSBPRE_L,(RD_Value1, RD_Value2) high cs ; write enable low cs spiout C.3,C.5,MSBFIRST_L,(WREN) high cs ; read status register to see if it has worked ; we expect this value to now be 2 low cs spiout C.3,C.5,MSBFIRST_L,(RDSR) spiin C.3,C.4,MSBPRE_L,(SR_Value2) high cs ; write disable the chip low cs spiout C.3,C.5,MSBFIRST_L,(WRDI) high cs ; display values on screen and then loop debug pause 1000 ; increment test data inc test_data1 inc test_data2 goto main
Copy Code Submit an Example
Read and write toan 26LC160 SPI EEPROM using HSPI commands
This test program demonstrates SPI interfacing to the EEPROM. Two bytes are written and then read back to verify. This program uses 'hspiin' and 'hspiout' commands and will be faster than using 'spiin' and 'spiout'.
- Code Example:
-
; Input / Output pin setup ;24LC160 pins ; 1 = CS = PICAXE B.7 ; 2 = SO = PICAXE C.4 ; 3 = WP = +5V ; 4 = Vss = 0V ; 5 = SI = PICAXE C.5 ; 6 = SCK = PICAXE C.3 ; 7 = HOLD = +5V ; 8 = Vdd = +5V ; remember PICAXE input connects to EEPROM output and vice versa! symbol cs = b.0 ' chip select is connected to PICAXE output 7 ; 25LC160 Commands symbol WRSR = 1 ; write status register symbol WR = 2 ; write symbol RD = 3 ; read symbol WRDI = 4 ; write disable symbol RDSR = 5 ; read status register symbol WREN = 6 ; write enable ; Variables symbol add_low = b0 ; address low byte symbol add_high = b1 ; address high byte symbol test_data1 = b2 ; dummy test data 1 symbol test_data2 = b3 ; dummy test data 2 symbol SR_Value1 = b4 ; read status register 1 symbol SR_Value2 = b5 ; read status register again Symbol RD_Value1 = b6 ; read data byte 1 Symbol RD_Value2 = b7 ; read data byte 2 init: ; set cs high and wait a while high cs ; chip select high ; setup spi mode - mode 1,1 with sample at end, medium speed hspisetup spimode11e,spimedium output b.7 pause 50 ; wait a while ; load some test values add_high = 0 add_low = 25 test_data1 = 55 test_data2 = 99 main: inc b9 ; write enable low cs hspiout (WREN) high cs ; write status register to 0 to remove block protect low cs hspiout (WRSR,0) high cs pause 5 ; wait write time ; write enable low cs hspiout (WREN) high cs ; read status register to see if it has worked ; we expect this value to now be 2 low cs hspiout (RDSR) hspiin (SR_Value1) high cs ; write low cs hspiout (WR,add_high,add_low,test_data1,test_data2) high cs pause 5 ; wait write time 5ms ;read low cs hspiout (RD,add_high,add_low) hspiin (RD_Value1,RD_Value2) high cs ; write enable low cs hspiout (WREN) high cs ; read status register to see if it has worked ; we expect this value to now be 2 low cs hspiout (RDSR) hspiin (SR_Value2) high cs ; write disable the chip low cs hspiout (WRDI) high cs ; display values on screen and then loop debug pause 1000 ; increment test data inc test_data1 inc test_data2 goto main
Copy Code Submit an Example
Create Module
SPI EEPROM can be connected using three generic terminal block create modules to carry the data in, data out and clock signals of the SPI bus.
Bill of Materials
Description | Code | Qty | |
---|---|---|---|
8-pin 0.3" DIP IC Socket | ICH008 | 1 | Buy Now |
100nF minaiature polysester capacitor | CAP001 | 1 | Buy Now |
Simulation
Submit Your Own Code!
You must be logged in to submit code examples. Login now.
Share