• EEPROM test system
• EEPROM test program
The internal electrically erasable programmable read only memory block is not strictly speaking a peripheral, as it is internal to the MCU, but it is accessed in a way similar to external devices so it is included in this part. In the 16F877, the EEPROM is a block of 256 bytes of nonvolatile read/write memory. It allows data to be retained while the power is off, which is useful in applications such as an electronic lock where a secure code needs to be stored.
Figure 3.11shows a test circuit that demonstrates its operation. Arbitrary 8-bit codes are set on the switch bank, which are stored, recalled, and displayed on the LED bank. The R/!W (Read/Not Write) input switch is closed to select the Write mode. The switch code is set and the button pressed. This stores the code in the first EEPROM location, address 0. The switch code is then changed and the next code stored in location 1, and so on until a 0 is entered on the switches. As the data are stored, each byte is displayed on the bar graph.
The R/!W switch is then opened to select read mode. As the button is pressed, the same sequence of stored codes is displayed from memory. The nonvolatile data storage is demonstrated by the fact that the test data are retained between successive simulation runs. This can be viewed if the simulation is paused and the EEPROM data window selected from the debug menu. Listing 3.12 is an EEPROM test program.
Listing 3.12. EEPROM Test Program
// EEPROM.C
// Internal data EEPROM test, design file EEPROM.DSN
#include “16F877A.h”
#use delay(clock=4000000)
void main() //////////////////////////////////////////////////////////
{
int writebyte, readbyte;
int maxadd, address;
port_b_pullups(1); // Enable Port B internal pull-ups
if(!input(PIN_C1) // Write memory sequence //////////////////
{
address=0; // First address
do
{ while(input(PIN_C0)){}; // Wait for button
writebyte=input_B(); // Get switch bank data
write_eeprom(address,writebyte); // Write data to EEPROM
readbyte=read_eeprom(address); // Read it back
output_D(readbyte); // Display data on bar graph
while(!input(PIN_C0)){}; // Wait for button release
address++; // Next EEPROM address
} while(writebyte!=0); // Continue until data=00
}
else // Read memory sequence ///////////////////
{
address=0; // First address
do
{ while(input(PIN_C0)){}; // Wait for button
readbyte=read_eeprom(address); // Read data
output_D(readbyte); // Display it on bar graph
while(!input(PIN_C0)){}; // Wait for button release
address++; // Next address
} while(readbyte!=0); // Continue until data=00
while(1); // Done *************************************
}
}
3.144.237.3