Alpha Beta
Newbie level 2
Hey All,
I'm running into a problem while trying to use the EEPROM on the PIC16F690. I'm using MPLAB IDE v8.36 and a PICKIT 2. I stripped the code down to its essentials to see if I could get that to work. My goal is to be able to have a write function based on an external input that I can read later, even if power is lost at some point.
This test code writes a value (in this case, 0x03) to an address (0x11), reads it into a variable (test), then flashes an LED to show what number was read. When I include both the write and read functions, the LED flashes 3 times, which is expected. However, when I comment out the write function, the read function gets a value of FF and flashes 255 times. I looked under View > EEPROM and it looks like the 0x03 isn't getting stored in the first place.
My questions:
-If the 0x03 isn't getting stored, why does the read function work when the write is not commented out?
-How do I get the write function to correctly store a value?
Any ideas?
I'm running into a problem while trying to use the EEPROM on the PIC16F690. I'm using MPLAB IDE v8.36 and a PICKIT 2. I stripped the code down to its essentials to see if I could get that to work. My goal is to be able to have a write function based on an external input that I can read later, even if power is lost at some point.
This test code writes a value (in this case, 0x03) to an address (0x11), reads it into a variable (test), then flashes an LED to show what number was read. When I include both the write and read functions, the LED flashes 3 times, which is expected. However, when I comment out the write function, the read function gets a value of FF and flashes 255 times. I looked under View > EEPROM and it looks like the 0x03 isn't getting stored in the first place.
My questions:
-If the 0x03 isn't getting stored, why does the read function work when the write is not commented out?
-How do I get the write function to correctly store a value?
Any ideas?
Code:
#include <htc.h>
#define LED RA2
void delay(int);
void eeWrite(char address, char data);
char eeRead(char address);
__CONFIG(INTIO & WDTDIS & PWRTDIS & MCLRDIS & UNPROTECT & UNPROTECT & BORDIS & IESODIS & FCMDIS);
int main(int argc, char *argv[])
{
ANSEL = 0;
ANSELH = 0;
TRISA = 0x30; //--110000 RA5:RA4 are inputs, RA3:RA0 are outputs
PORTA = 0;
int test;
eeWrite(0x11, 0x03); //use first, comment second
test = eeRead(0x11);
for(int i = 0; i < test; i++)
{
LED = 1;
delay(500);
LED = 0;
delay(500);
}
while(1) {}
return 0;
}
void delay(int time)
{
int t=time;
int i=0;
while (t>0)
{
while(i<10)
{
asm("nop");
i++;
}
i = 0;
t--;
}
}
void eeWrite(char address, char data)
{
char INTCON_SAVE;
EEADR = address;
EEDATA = data;
EEPGD = 0; // 0 = Access data EEPROM memory
WREN = 1; // enable writes to internal EEPROM
INTCON_SAVE=INTCON; // Save INTCON register contants
INTCON = 0; // Disable interrupts, Next two lines SHOULD run without interrupts
EECON2=0x55; // Required sequence for write to internal EEPROM
EECON2=0xaa; // Required sequence for write to internal EEPROM
WR = 1; // begin write to internal EEPROM
INTCON=INTCON_SAVE; // Now we can safely enable interrupts if previously used
asm("nop");
while(EEIF==0) {asm("nop");} //wait until write operation is complete
WREN = 0; // Disable writes to EEPROM on write complete
EEIF = 0; //Clear EEPROM write complete flag. (must be cleared in software. So we do it here)
}
// This function reads data from address given in internal EEPROM of PIC
char eeRead(char address)
{
EEADR = address;
EEPGD = 0; // 0 = Access data EEPROM memory
RD = 1; // EEPROM Read
return EEDATA; // return data
}