QC

Bộ Nhớ EEPROM AVR

 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 (Đăng ký dữ liệu EEPROM)
Thanh ghi này chứa dữ liệu được ghi vào EEPROM trong hoạt động ghi và đối với hoạt động đọc, nó có dữ liệu được đọc ra từ EEPROM.

EEDR
D7D6D5D4D3D2D1D0
MSB------LSB

EECR (EEPROM Control Register)

This registers controls all EEPROM operations.

EECR
D7D6D5D4D3D2D1D0
----EERIEEEMWEEEWEEERE

• 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
15141312111098
------EEAR9EEAR8
76543210
EEAR7EEAR6EEAR5EEAR4EEAR3EEAR2EEAR1EEAR0

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


  1. #include <avr\io.h> // io.h contains the defnition of all ports and SFRs //
  2. #include "uart.h" //User defined UART library which contains the UART routines
  3. #include "eeprom.h" //User defined library which contains eeprom routines
  4.  
  5. /* start of the main program */
  6. void main()
  7. {
  8. unsigned int eeprom_address=0x00;
  9. unsigned char write_string[] = {"Hello World"}, read_string[15];
  10.  
  11. /* Initialize the Uart before Transmitting/Receiving any data */
  12. UART_Init();
  13.  
  14. while(1)
  15. {
  16. UART_TxString("\n\rWrite : "); //Print the message on UART
  17. UART_TxString(write_string); //Print the String to be written
  18. EEPROM_WriteString(eeprom_address,write_string); // Write the String at memory Location 0x00
  19.  
  20. UART_TxString("\tRead : "); //Print the message on UART
  21. EEPROM_ReadString(eeprom_address,read_string); // Read the String from memory Location 0x00
  22. UART_TxString(read_string); //Print the read String
  23. }
  24. }


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;
}


Nap Code vào PY32F003 dùng Stlink

 Nap Code vào PY32F003 dùng Stlink Bước 1: Cài đặt  KeilC v5.39 theo link sau ( chú ý 5.39 keil c mới nạp ok). https://edge07.111.ir.cdn.ir/...