EEPROM Memory.pdf
(
369 KB
)
Pobierz
AVR EEPROM Memory Read/Write Cookbook
BETA version 0.00 2/23/08
This is the beta version of a tutorial that, once it has been sufficiently reviewed and
commented in the forum, will be posted in the tutorials section. I’m hoping some folks
will try this out and tell me what wasn’t clear, what didn’t work, and what typos they
found.
AVR EEPROM Memory Read/Write Cookbook
Joe Pardue 2/23/08 beta version 0.00
This ‘tutorial’ is actually a cookbook - by which I mean that I give you proven and tested
recipes for how to use the AVR EEPROM. I use the EEPROM functions from avr-libc
which has it’s own documentation of these functions. Also, I can heartily recommend
Dean Camera’s tutorial on these EEPROM functions which discusses how to use the read
functions:
[TUT] [C] Using the EEPROM memory in AVR-GCC
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=38417
I felt that this cookbook was needed since neither of the above documents provides
detailed recipes for using the EEPROM functions with real devices.
Introduction to AVR Memory Types
The AVR has EEPROM (Electrically Erasable Programmable Read Only Memory) to
provide users with non-volatile memory for storing data. Technically the Flash Memory
is also electrically erasable and programmable, but it is written and erased in pages [and
is much cheaper than EEPROM which is written and erased in bytes]. While Flash can be
used for non-volatile data storage, you risk trashing your program and AVR Flash is rated
for 10,000 read write cycles (a lot, but not if the data is changed at computer rates), while
the AVR EEPROM is rated at 100,000 read write cycles.
To help get our heads around the meaning of the write/erase cycles think about updating
a bit of data once per hour. You could safely do this for just over 416 days with Flash
Memory, and 4166 days (just under 11 ½ years) with EEPROM. You can see that doing
update every minute would quickly exhaust both memory types (166.6 hours for Flash
and 1666 hours or 69 days for EEPROM).
Flash Memory is intended for occasional updates of program memory or for changing
data at a rate that won’t exhaust the memory within the expected lifetime of the system
you are designing. If you are designing a system to last 10 years, then you could safely
write to a Flash Page 1000 times per year or 2.74 times per day. For the EEPROM we
saw above that hourly writes are permissible.
Typical Memory for AVRs:
AVR
SRAM Bytes
EEPROM Bytes
Flash Bytes
Cost *
ATmega48
512
256
4096
2.58
ATmega88
1024
512
8192
3.87
ATmega168
1024
512
16384
4.11
ATmega164
1024
512
16384
4.82
ATmega324
2048
1024
32768
6.02
ATmega644
4096
2048
65536
7.87
* Price at DigiKey Jan-April 2008 Catalog – DIP package
EEPROM _Test Software:
WinAVR and AVRStudio
We will be developing the EEPROM_Test software using the WinAVR toolset for the
GCC compiler along with Atmel’s AVRStudio. You can get the latest and greatest
version of WinAVR at:
http://winavr.sourceforge.net
and AVRStudio from
www.atmel.com
.
EEPROM _Test is a program written for the ATmega644 and the AVR Butterfly that
allows a PC terminal program, in the case shown here: Smiley Micros Developer
Terminal, to read and write the AVR EEPROM using byte, word, and block functions
from avr-libc (which comes with WinAVR).
We will test this code using the Smiley Micros Developer Terminal and XML files
written to ease the testing process by automatically sending commands and data streams.
You can find the Developer Terminal and the source code for EEPROM_Test at
www.smileymicros.com
downloads menu link: ‘EEPROM Test’.
To simplify things we will keep all the AVR C code and header type information in a
single source code module along with the USART routines.
What we’ll send and what we’ll expect to receive:
Read Byte
Send command byte 0x62 (‘b’)
Send address byte
Receive byte
Write Byte
Send command byte 0x42 (‘B’)
Send address byte
Send byte to write
Read Word
Send command byte 0x77 (‘w’)
Send address high byte
Send address low byte
Receive word high byte
Receive word low byte
Write Word
Send command byte 0x57(‘W’)
Send address high byte
Send address low byte
Send word high byte
Send word low byte
Read Block
Send command byte 0x6b (‘k’)
Send size byte - limit to <= 80
Send EEPROM address start high byte
Send EEPROM address start low byte
Receive number of bytes specified by size
Write Block
Send command byte 0x4b (‘K’)
Send size byte - limit to <= 80
Send EEPROM address start high byte
Send EEPROM address start low byte
EEPROM_Test Source Code:
/*****************************************************
Include and declarations from UART_Test
******************************************************/
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
void USART_Init();
void USART_Transmit( unsigned char );
unsigned char USART_Receive( void );
void USART_Transmit_PROGMEM(const char *);
int main(void)
{
uint8_t c;
uint16_t Address;
uint8_t ByteValue;
uint16_t WordValue;
USART_Init();
while(1)
{
c = USART_Receive();
if( c == 'b') // Read Byte
{
Address = USART_Receive();
USART_Transmit(eeprom_read_byte((uint8_t*)Address));
}
else if(c == 'B') // Write Byte
{
Address = USART_Receive();
ByteValue = USART_Receive();
eeprom_busy_wait();
eeprom_write_byte((uint8_t*)Address,ByteValue);
}
else if(c == 'w') // Read Word
{
Address = (USART_Receive() << 8);
Address |= USART_Receive();
WordValue = eeprom_read_word((uint16_t*)Address);
USART_Transmit(WordValue>>8);
USART_Transmit(WordValue);
}
else if(c == 'W') // Write Word
{
Address = (USART_Receive() << 8);
Address |= USART_Receive();
WordValue = (uint16_t)(USART_Receive()<<8);
WordValue |= (uint16_t)USART_Receive();
eeprom_busy_wait();
eeprom_write_word((uint16_t*)Address,WordValue);
}
else if(c == 'k') // Read Block
{
//NOTE: We are limiting this to 80 bytes for this test
// this code SHOULD work for the full EEPROM if the
// array is set to the EEPROM size - not tested though.
uint8_t LocalBlock[80];
uint16_t size;
uint16_t Address;
size = (USART_Receive() << 8);
size |= USART_Receive();
Address = (USART_Receive() << 8);
Address |= USART_Receive();
// Limit size to 80 due to LocalBlock array size
if(size > 80) {
USART_Transmit('?');
break;
}
eeprom_read_block((void*)LocalBlock,(const void*)Address,size);
int i;
for(i=0;i<size;i++)
{
USART_Transmit(LocalBlock[i]);
}
}
else if(c == 'K') // Write Block
{
// Receive number of bytes specified by size
// NOTE: We are limiting this to 80 bytes for this test
// this code SHOULD work for the full EEPROM if the
// array is set to the EEPROM size - not tested though.
uint8_t LocalBlock[80];
uint16_t size;
uint8_t rec;
uint16_t Address;
size = (USART_Receive() << 8);
size |= USART_Receive();
Address = (USART_Receive() << 8);
Address |= USART_Receive();
int i = 0;
for(i = 0; i < size; i++)
{
LocalBlock[i] = USART_Receive();
if(i >= 80) break; // Make sure we don't go over 80
}
eeprom_write_block((const void*)LocalBlock,(void*)Address,size);
}
else USART_Transmit('?');
}
}
/*****************************************************
Functions from UART_Test
Plik z chomika:
apuln2
Inne pliki z tego folderu:
WinHex 16.1 [PL] [Keygen].rar
(1952 KB)
snd-reversingwithlena-tutorials.rar
(142938 KB)
SoftIce v4.2.7.rar
(2937 KB)
Americas Army 3 Hack.rar
(1051 KB)
W32Dasm 8.rar
(1553 KB)
Inne foldery tego chomika:
(1)
► ELEKTRONIKA - ksiażki
►Aaa Programy PC
►PlayStation 3
04 METODA SILVY programy sukcesu cd-4
Zgłoś jeśli
naruszono regulamin