/*
Filename: intEx1
Author: Jhaved Jesus II Bucaling
Date: 4/18/24
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);
void pcint1_init(void);
void pcint2_init(void);
void pcint3_init(void);
volatile unsigned char PinChange0 = 0; //global variable, initialised to False
volatile unsigned char PinChange1 = 0; //global variable, initialised to False
volatile unsigned char PinChange2 = 0;//global variable, initialised to False
volatile unsigned char counter = 0; // Global variable for debugging code
int main(void)
{//--- User initializations.
DDRB = 0xF0; // NOTE: configure only PB0 (PCINT0) as input
PORTB = 0x0F; // activate only its pull up resistor, and turn off LED
DDRH = 0xF0; // NOTE: configure only PB0 (PCINT8) as input
PORTH = 0x0F;// activate only its pull up resistor, and turn off LED
DDRK = 0xF0; // NOTE: configure only PB0 (PCINT16) as input
PORTK = 0x0F;// 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
pcint1_init();// initialise registers to enable PCINT8
pcint2_init();// initialise registers to enable PCINT16
while(1)
{
if(PinChange0) // Check if pin change detected
{
PORTA |= 0x80;
_delay_ms(1000);
PORTA &= ~0x80;
PinChange0 = 0; // Reset pin change detection
}
if(PinChange1) // Check if pin change detected
{
PORTA |= 0x40;
_delay_ms(1000);
PORTA &= ~0x40;
PinChange1 = 0; // Reset pin change detection
}
if(PinChange2) // Check if pin change detected
{
PORTA |= 0x20;
_delay_ms(1000);
PORTA &= ~0x20;
PinChange2 = 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)|(1 << PCINT1); // Port B, Pin53
PCIFR |= (1 << PCIF0);
sei(); // SREG = 1
}
void pcint1_init(void)
{
cli(); // SREG = 0
PCICR |= (1 << PCIE1);
PCMSK1 |= (1 << PCINT8); // Port H, pin 0
PCIFR |= (1 << PCIF1);
sei(); // SREG = 1
}
void pcint2_init(void)
{
cli(); // SREG = 0
PCICR |= (1 << PCIE2);
PCMSK2 |= (1 << PCINT16)|(1 << PCINT17); // Port K, pin 8
PCIFR |= (1 << PCIF2);
sei(); // SREG = 1
}
//interrupt-service routine
ISR(PCINT0_vect)
{
PinChange0 = 1;
PCIFR = PCIFR | (1 << PCIF0);// re-arm the interrupt – IMPORTANT!!!
}
ISR(PCINT1_vect)
{
PinChange1 = 1;
PCIFR = PCIFR | (1 << PCIF1);// re-arm the interrupt – IMPORTANT!!!
}
ISR(PCINT2_vect)
{
PinChange2 = 1;
PCIFR = PCIFR | (1 << PCIF2);// re-arm the interrupt – IMPORTANT!!!
}