#pragma config OSC=INTIO2, WDT=OFF, LVP=OFF, DEBUG=ON
#include <p18f1320.h>
void interrupt_at_high_vector(void);
void high_isr (void);
void MyDelay(int xD);
void MyFlash (char qTimes, char xSpeed);
unsigned char ReadEEPROM(unsigned char address);
void WriteEEPROM(unsigned char address,unsigned char data);
void MyToggle(char MyAddr);
#pragma code /* return to the default code section */
#pragma interrupt high_isr
void high_isr (void)
{
INTCONbits.INT0IF=0; // clear the flag and return BREAKPOINT and watch
MyToggle(0x01);
}
void main(void)
{
OSCCON = 0x72; // 8MHz
ADCON1 = 0xFF;
INTCON2 = 0b00000101; // falling edge, high priority
INTCON = 0b11010000; // Setting Bit 3 to 1 gives a automatic interrupt
TRISB = 0b00100101;
ADCON1bits.PCFG0 = 0;
TRISA = 0b10111110;
while(1){
char MyFlashVal;
MyFlashVal = ReadEEPROM(0x01);
if (MyFlashVal = 0b00000001){
MyFlash(1,100);
} else {
LATA=0b00000001; // Turn LED on
MyDelay(200);
LATA=0b00000000; // Turn LED off
MyDelay(200);
}
}
}
void MyDelay(int xD)
{
int countD = xD * 100;
while (countD > 1)
{
countD = countD - 1;
}
}
void MyToggle(char MyAddr)
{
char MyTmp;
MyTmp = ReadEEPROM(MyAddr);
if(MyTmp = 0b00000001){
WriteEEPROM(MyAddr, 0b00000000);
} else {
WriteEEPROM(MyAddr, 0x00000001);
}
}
void MyFlash (char qTimes, char xSpeed)
{
int x = qTimes;
while(x != 0)
{
TRISA = 0b10111110;
LATA = 0b00000001;
MyDelay(xSpeed);
LATA = 0b01000000;
MyDelay(xSpeed);
TRISA = 0b00111111;
LATA = 0b01000000;
MyDelay(xSpeed);
LATA = 0b10000000;
MyDelay(xSpeed);
TRISA = 0b01111110;
LATA = 0b10000000;
MyDelay(xSpeed);
LATA = 0b00000001;
MyDelay(xSpeed);
x--;
}
TRISA = 0b10111110;
}
unsigned char ReadEEPROM(unsigned char address){
EECON1=0; //ensure CFGS=0 and EEPGD=0
EEADR = address;
EECON1bits.RD = 1;
return(EEDATA);
}
void WriteEEPROM(unsigned char address,unsigned char data){
char SaveInt;
SaveInt=INTCON; //save interrupt status
EECON1=0; //ensure CFGS=0 and EEPGD=0
EECON1bits.WREN = 1; //enable write to EEPROM
EEADR = address; //setup Address
EEDATA = data; //and data
INTCONbits.GIE=0; //No interrupts
EECON2 = 0x55; //required sequence #1
EECON2 = 0xaa; //#2
EECON1bits.WR = 1; //#3 = actual write
INTCON=SaveInt; //restore interrupts
while(!PIR2bits.EEIF); //wait until finished
EECON1bits.WREN = 0; //disable write to EEPROM
}
#pragma code high_vector=0x08
void interrupt_at_high_vector(void)
{
_asm GOTO high_isr _endasm // brakpoint here
}