I have a pic18f27J53 which has HLVD. I have everything working the way I want except that each event interrupts 4 times. That doesn't seem right. I have been able to code around it but just does not seem like I should have to.
Has anyone use HLVD?
#include <xc.h>
#define LOW_POWER_SETPOINT 0b1001
#define NORMAL_POWER_SETPOINT 0b1010
#define _XTAL_FREQ 8000000
unsigned interruptCount = 0;
#define DETECT_LOW_POWER 0
#define DETECT_NORMAL_POWER 1
unsigned char detectPowerMode = 0;
unsigned char enterSleep = 0;
void main(void)
{
OSCCONbits.IRCF0 = 1;
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF2 = 1;
TRISAbits.TRISA0 = 0;
TRISAbits.TRISA1 = 0;
LATAbits.LATA0 = 1;
OSCCONbits.IDLEN = 0;
HLVDCONbits.HLVDEN = 0; // Disable HLDV
HLVDCONbits.HLVDL = LOW_POWER_SETPOINT; // Set point to low value
HLVDCONbits.VDIRMAG = detectPowerMode; // Interrupt when voltage drops
PIR2bits.HLVDIF = 0; // Clear the HLVD interrupt flag
HLVDCONbits.HLVDEN = 1; // Enable HLDV
RCONbits.IPEN = 1;
INTCONbits.GIEH = 1; // Enable general interrupts high
PIE2bits.HLVDIE = 1; // Enable low voltage detect interrupt
while(1)
{
LATAbits.LATA1 = 0;
__delay_ms(100);
LATAbits.LATA1 = 1;
__delay_ms(100);
if (enterSleep)
Sleep();
}
return;
}
__interrupt(high_priority) void interrupts_highPriority(void)
{
if (PIR2bits.HLVDIF == 1)
{
PIR2bits.HLVDIF = 0;
interruptCount++;
if (interruptCount<4)
return;
interruptCount=0;
if (detectPowerMode == 1)
{
LATAbits.LATA0 = 1;
detectPowerMode = DETECT_LOW_POWER;
HLVDCONbits.HLVDEN = 0;
HLVDCONbits.HLVDL = LOW_POWER_SETPOINT;
HLVDCONbits.VDIRMAG = detectPowerMode;
HLVDCONbits.HLVDEN = 1;
enterSleep=0;
}
else
{
LATAbits.LATA0 = 0;
detectPowerMode = DETECT_NORMAL_POWER;
HLVDCONbits.HLVDEN = 0;
HLVDCONbits.HLVDL = NORMAL_POWER_SETPOINT;
HLVDCONbits.VDIRMAG = detectPowerMode;
HLVDCONbits.HLVDEN = 1;
enterSleep =1;
}
}
}