/*
Filename: intEx1
Author:
Date:
Description:
Code to test external interrupts: every time PCINT0 is triggered,
the red shield LED7 is toggled
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
//user-defined function for the initialisation of the external interrupt PCINT0
void pcint0_init(void);
volatile unsigned char PinChange = 0; //global variable, initialised to False
volatile unsigned char counter = 0; // Global variable for debugging code
int main(void)
{//--- User initializations.
DDRB = 0xFE; // NOTE: configure only PB0 (PCINT0) as input and
PORTB = 0x01; // activate only its pull up resistor, and turn off LED
DDRA = 0xFF; // All Port A configured as output
pcint0_init(); // initialise registers to enable PCINT0
while(1)
{
if(PinChange) // Check if pin change detected
{
PORTA |= 0x80;
_delay_ms(1000);
PORTA &= ~0x80;
PinChange = 0; // Reset pin change detection
}
}
}
//user-defined function for the initialisation of the external interrupt PCINT0
void pcint0_init(void)
{
cli(); // SREG = 0
PCICR |= (1 << PCIE0);
PCMSK0 |= (1 << PCINT0); // Port B, pin 0
PCIFR |= (1 << PCIF0);
sei(); // SREG = 1
}
//interrupt-service routine
ISR(PCINT0_vect)
{
PinChange = 1;
PCIFR = PCIFR | (1 << PCIF0); // re-arm the interrupt – IMPORTANT!!!
}