Trong hướng dẫn này, chúng ta sẽ nghiên cứu cách sử dụng EEPROM bên trong của AVR. EEPROM thường được sử dụng khi một số loại lưu trữ vĩnh viễn trong thời gian thực được yêu cầu.
ATmega32 chứa 1024 byte bộ nhớ EEPROM dữ liệu. Nó được tổ chức như một không gian dữ liệu riêng biệt. Các byte dữ liệu EEPROM được đánh địa chỉ tuyến tính trong khoảng từ 0 đến 1023.
EEPROM Registers
EEDR | |||||||
---|---|---|---|---|---|---|---|
D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
MSB | - | - | - | - | - | - | LSB |
EECR (EEPROM Control Register)
This registers controls all EEPROM operations.
EECR | |||||||
---|---|---|---|---|---|---|---|
D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
- | - | - | - | EERIE | EEMWE | EEWE | EERE |
• Bits 7 to 4 – Reserved Bits
These bits are reserved bits and will always read as zero.
• Bit 3 – EERIE: EEPROM Ready Interrupt Enable
To enable the EEPROM Ready Interrupt write one to EERIE ( first set the I bit in SREG )and zero to disable it. The EEPROM Ready interrupt generates a constant interrupt when EEWE is cleared.
• Bit 2 – EEMWE: EEPROM Master Write Enable
You must write one to EEMWE to enable EEPROM write operation. If EEMWE is zero, setting EEWE will have no effect.
• Bit 1 – EEWE: EEPROM Write Enable
EEWE is the write strobe to the EEPROM. To write the value into the EEPROM this bit must be written to one after you set up address and data correctly . Before that the EEMWE bit must be set to one, otherwise no EEPROM write takes place.
• Bit 0 – EERE: EEPROM Read Enable
It is the read strobe to the EEPROM. Write one to the EERE after setting up the correct address in the EEAR Register to trigger the EEPROM read.
You should poll the EEWE bit before starting the read operation. If a write operation is in progress, it is neither possible to read the EEPROM, nor to change the EEAR Register.
EEAR (EEPROM Address Register)
• Bits 15..10 – Reserved Bits
These bits are reserved bits and will always read as zero.
• Bits 9..0 – EEAR9..0: EEPROM Address
Tthe EEPROM address, where you want to write data or read data from, write it here.
EEAR | |||||||
---|---|---|---|---|---|---|---|
15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
- | - | - | - | - | - | EEAR9 | EEAR8 |
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
EEAR7 | EEAR6 | EEAR5 | EEAR4 | EEAR3 | EEAR2 | EEAR1 | EEAR0 |
EEPROM Read/Write Procedure:
Follow following steps to do EEPROM read/write operation :
Write Operation :
1. Wait till previous write operation is completed(i.e. wait till EEWE becomes zero).
2. Load the EEPROM address into EEAR at which the data has to be stored.
3. Load the data into EEDR which has to be stored in EEPROM.
4. Set the EEMWE (EEPROM Master Write Enable).
5. Within four clock cycles after 4th step, set EEWE(Eeprom Write Enable) to trigger the EEPROM Write Opeartion
Read Operation :
1. WAit for completion of previous Write operation.
2. EEWE will be cleared once EEPROM write is completed.
3. Load the EEPROM address into EEAR from where the data needs to be read.
4. Trigger the EEPROM read operation by setting EERE (EEPROM Read Enable).
5. Wait for some time (about 1ms) and collect the read data from EEDR.
Code
- #include <avr\io.h> // io.h contains the defnition of all ports and SFRs //
- #include "uart.h" //User defined UART library which contains the UART routines
- #include "eeprom.h" //User defined library which contains eeprom routines
- /* start of the main program */
- void main()
- {
- unsigned int eeprom_address=0x00;
- unsigned char write_string[] = {"Hello World"}, read_string[15];
- /* Initialize the Uart before Transmitting/Receiving any data */
- UART_Init();
- while(1)
- {
- UART_TxString("\n\rWrite : "); //Print the message on UART
- UART_TxString(write_string); //Print the String to be written
- EEPROM_WriteString(eeprom_address,write_string); // Write the String at memory Location 0x00
- UART_TxString("\tRead : "); //Print the message on UART
- EEPROM_ReadString(eeprom_address,read_string); // Read the String from memory Location 0x00
- UART_TxString(read_string); //Print the read String
- }
- }
Storing Numbers
#include "uart.h" #include "eeprom.h" uint16_t writeNum_16 = 12345; uint32_t writeNum_32 = 12345678; uint16_t readNum_16; uint32_t readNum_32; #define num_16_address 0x00 #define num_32_address 0x02 // As num_16 takes two bytes, new address will start from +2 location int main(void) { UART_Init(9600); EEPROM_WriteNBytes(num_16_address,(uint8_t *)&writeNum_16,2); //write 2-bytes of data(writeNum_16) at 0x00. EEPROM_WriteNBytes(num_32_address,(uint8_t *)&writeNum_32,4); //write 4-bytes of data(writeNum_32) at 0x02. EEPROM_ReadNBytes(num_16_address,(uint8_t *)&readNum_16,2); //Read 2-bytes of data from 0x00 into readNum_16 EEPROM_ReadNBytes(num_32_address,(uint8_t *)&readNum_32,4); //Read 4-bytes of data from 0x02 into readNum_32 UART_Printf("num_16 = %5u num_32=%8U",readNum_16,readNum_32); while (1); return 0; }