Addeen
Newbie
I just started learning power management and I need to know, why INT0 is implemented, why void interrupt ISR(void) is implemented.
The code below will blink the LEDs for five times and enter into sleep mode. Upon an INT0 interrupt, the micro-controller resumes execution.
The code below will blink the LEDs for five times and enter into sleep mode. Upon an INT0 interrupt, the micro-controller resumes execution.
C:
// PIC18F4520 Configuration Bit Settings
#pragma config OSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit
#pragma config WDT = OFF // Watchdog Timer Enable bit
#include <xc.h>
#include <stdblib.h>
#define _XTAL_FREQ 4000000
#define SPEAKER PORTBbits.RB3
void interrupt ISR (void);
void main(void)
{
char i;
OSCCON = 0;
ADCON1 = 0x0F; // Configure all ports as digital
TRISA = 0b11110000; // Configure RA<3:0> as outputs (LEDs)
TRISB = 0b11110111; // Configure RB3 as output (speaker)
// Set up for INT0 interrupt
INTCONbits.GIE = 0; // Disable global interrupt
INTCONbits.INT0IF=0; // Clear INT0 interrupt flag
INTCON2bits.INTEDG0=1; // Configure INT0 for rising edge trigger
INTCONbits.INT0IE=1; // Enable INT0 interrupt
INTCONbits.GIE=1; // Clear INT0 interrupt flag
while (1)
{
for (i=0;i<5;i++)
{
PORTA = 0b11111111; // Turn on all LEDs
__delay_ms(300);
PORTA = 0b00000000; // Turn off all LEDs
__delay_ms(300);
}
SLEEP(); // library function to execute sleep instruction
}
}
void interrupt ISR(void)
{
int x;
if (INTCONbits.INT0IF) // Check INT0 flag to confirm if INT0 interrupt trigger
{
for (x=0; x<3000; x++) // Sound the speaker
{
SPEAKER = 1;
_delay(500);
SPEAKER = 0;
_delay(500);
}
INTCONbits.INT0IF=0; // Clear interrupt flag
}
}
Last edited by a moderator: