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