Книга: Advanced PIC Microcontroller Projects in C

4.3.1 EEPROM Library

4.3.1 EEPROM Library

The EEPROM library includes functions to read data from the on-chip PIC microcontroller nonvolatile EEPROM memory, or to write data to this memory. Two functions are provided:

• Eeprom_Read

• Eeprom_Write

The Eeprom_Read function reads a byte from a specified address of the EEPROM. The address is of type integer, and thus the function supports PIC microcontrollers with more than 256 bytes. A 20ms delay should be used between successive reads from the EEPROM to guarantee the return of correct data. In the following example, the byte at address 0x1F of the EEPROM is read and stored in variable Temp:

Temp = Eeprom_Read(0x1F);

The Eeprom_Write function writes a byte to a specified address of the EEPROM. The address is of type integer and thus the function supports PIC microcontrollers with more than 256 bytes. A 20ms delay should be used between successive reads or writes to the EEPROM to guarantee the correct transfer of data to the EEPROM. In the following example, number 0x05 is written to address 0x2F of the EEPROM:

Eeprom_Write(0x2F, 0x05);

Example 4.11

Write a program to read the contents of EEPROM from address 0 to 0x2F and then send this data to PORTB of a PIC microcontroller.

Solution 4.11

The required program is given in Figure 4.18. A for loop is used to read data from the EEPROM and then send it to PORT B of the microcontroller. Notice that a 20ms delay is used between each successive read.

/***********************************************************************
                      READING FROM THE EEPROM
                      =========================
This program reads data from addresses 0 to 0x2F of the EEPROM and then
sends this data to PORTB of the microcontroller.
Programmer: Dogan Ibrahim
File:       EEPROM.C
Date:       May, 2007
************************************************************************/
void main() {
 unsigned int j;
 unsigned char Temp;
 TRISB = 0; // Configure PORTB as output
 for (j=0; j <= 0x2F; j++) {
  Temp = Eeprom_Read(j);
  PORTB = Temp;
  Delay_ms(20);
 }
}


Figure 4.18: Program to read from the EEPROM

Оглавление книги


Генерация: 1.128. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз