home  general  lard  ArdweeNET  MAXX  the QUUB  Gecko  SiiMAN
        embedded systems  PC software

Here's a simple polled EEPROM read and write routine for the ATmege32 (but should work with most controllers). It uses the functions defined in avr/eeprom.h to write a byte into a location then read it back and produce a serial data stream out an IO pin to verify that the correct value was written then read.

Get the code eeprom_write_read_32.txt

Below is a screen grab from my logic analyser showing the following.

PORTB:0, the serialised value read from the EEPROM, in this case 0x22.

PORTB:1, the bit clock so it's easy to see what bits are set.

PORTB:2, goes high when a byte it being transmitted so you can see where bytes start and end if there's a continous stream.

Note that there are two versions of main() in the supplied code because for some reason the functions in eeprom.h differentiate between the addresses a byte can be written to as opposed to where you can write a word.

eeprom_write_byte() expects a byte for an address and is therefore limited to the address range 00-FF. eeprom_write_word() wants a word for the address and can therefore address all of the EEPROM. What the address has to do with the type of data you're writing I have no idea but that's the way it's written and it works so I'll think about it another day.

To swap main()s just remove the x_ from one and add it to the other.

If you don't have an analyser you can still verify that the write has worked by uploading the EEPROM contents from AVR Studio. From the programming dialogue enter the file name to save the EEPROM contents to and click "Read".

Look at the file and you'll see something like this, it's a HEX file format that includes address and checksum information but you can see the 22 at location 0 as programmed by the sample code.



Copyright © 1973-2013 Rob Gray, All rights reserved.
PO Box 450, Gin Gin, QLD, Australia.