Using EEPROM to store data after power down
The data memory of the PIC18F4550 is volatile. This means that information stored in this memory is lost when the power is switched off. In many cases this is not a problem, but sometimes it's useful that certain data, e.g. configuration data, is saved even when the board is powered down. To this end, the PIC18F4550 contains 256 bytes of non-volatile EEPROM memory besides the volatile data memory. This memory can be used to store non-volatile information.
The program below demonstrates how the EEPROM memory is used. The program changes the byte at address 0 by means of the push buttons. The north button increments the byte, the south button decrements the byte.
The most important parts of the program are the readEepromByte and writeEepromByte functions. The readEepromByte function reads one of the 256 EEPROM bytes while the writeEepromByte function writes a value to a given address in the EEPROM memory. For more information on the internal details of these functions please refer to chapter 7 of the PIC18F4550 datasheet
.
Test the program in the following way: set a value using the buttons, switch the power off, and switch the power back on again. The value you have just set should appear unchanged on the LCD display.
#include <p18f4550.h> #include "lcd.h" #include "delay50ms.h" // Fuses configuration #pragma config PLLDIV = 5 // Divide by 5 (20 MHz oscillator input) #pragma config FOSC = HSPLL_HS // HS oscillator, PLL enabled, HS used by USB #pragma config IESO = OFF // Oscillator Switchover mode disabled #pragma config PWRT = OFF // PWRT disabled #pragma config BOR = OFF // Brown-out Reset enabled in hardware only (SBOREN is disabled) #pragma config WDT = OFF // HW Disabled - SW Controlled #pragma config WDTPS = 32768 // 1:32768 #pragma config MCLRE = ON // MCLR pin enabled; RE3 input pin disabled #pragma config LVP = OFF // Disable low-voltage programming #pragma config CCP2MX = ON // Config3h #pragma config PBADEN = OFF // PORB digital IO on powerup #define byte unsigned char #define PRESSED 0 #define S_N PORTBbits.RB4 #define S_S PORTBbits.RB5 #define BYTE_ADDR 0 #define TRUE 1 void writeEepromByte(byte addr, byte data) { INTCONbits.GIE = 0; // Disable interupts EECON1bits.EEPGD = 0; // Select the EEPROM memory EECON1bits.CFGS = 0; // Access the EEPROM memory EECON1bits.WREN = 1; // Enable writing EEADR = addr; // Set the address EEDATA = data; // Set the data EECON2 = 0x55; // Write initiate sequence EECON2 = 0xaa; // Write initiate sequence EECON1bits.WR = 1; // Start writing while (!PIR2bits.EEIF) ; // Wait for write to finish PIR2bits.EEIF = 0; // Clear EEIF bit INTCONbits.GIE = 1; // Enable interupts } byte readEepromByte(unsigned char addr) { EECON1bits.EEPGD = 0; // Select the EEPROM memory EECON1bits.CFGS = 0; // Access the EEPROM memory EEADR = addr; // Set the address EECON1bits.RD = 1; // Start reading return EEDATA; // Return the read value } void main(void) { TRISB = 0xFF; // Configure port B as inputs INTCON2bits.NOT_RBPU = 0; // Enable internal pullup resistors // Initiate the LCD initLCD(); clearLCD(); while (TRUE) { // Write value stored in EEPROM to LCD setLineOnLCD(1); appendIntToLCD(readEepromByte(BYTE_ADDR)); // Change value stored in EEPROM with north and south button if (S_N == PRESSED) { writeEepromByte(BYTE_ADDR, readEepromByte(BYTE_ADDR) + 1); } if (S_S == PRESSED) { writeEepromByte(BYTE_ADDR, readEepromByte(BYTE_ADDR) - 1); } Delay10KTCYx (200); } }
- Type:

Your shopping cart