/*
Filename: intEx1.01
Author:Jesus Franco
Date: 07/04/2023
Description: Turn on a led
*/
#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:3
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.
unsigned char SWITCHVAL;
unsigned char MAXSWITCH;
DDRB = 0x00; // configure PB0:3
PORTB = 0x00;
DDRA = 0xFF; // All Port A configured as output
pcint0_init(); // initialise registers to enable PCINTn
// Intitialise serial port 0
Serial.begin(115200);
while(1){
//Activate led for 1 second
if(PinChange == 1) // Check if pin change detected
{
SWITCHVAL=PINB ;
MAXSWITCH = SWITCHVAL & 0x0F;
switch (MAXSWITCH)
{case 0x01:
Serial.println("Botton 1 pressed");
_delay_ms(400);
break;
case 0x02:
Serial.println("Botton 2 pressed");
_delay_ms(400);
break;
case 0x04:
Serial.println("Botton 3 pressed");
_delay_ms(400);
break;
case 0x08:
Serial.println("Botton 4 pressed");
_delay_ms(400);
break;
SWITCHVAL=0x00;
}
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)|(1<<PCINT1)|(1<<PCINT2)|(1<<PCINT3);
// 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!!!
}