#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
volatile unsigned char PinChange = 0;
volatile unsigned char PinState = 0x00;
void pcint0_init(void);
int main(void) {
portSetup();
pcint0_init();
while(1) {
if(PinChange) {
PORTA = PinState;
_delay_ms(1000);
PinState = 0x00;
PORTA = PinState;
PinChange = 0;
}
}
}
void portSetup(void) {
DDRB = 0xF0;
PORTB = 0x0F;
DDRA = 0xFF;
}
void pcint0_init(void) {
cli();
PCICR |= (1 << PCIE0);
PCMSK0 = PCMSK0 | (1 << PCINT0) | (1 << PCINT1) | (1 << PCINT2) | (1 << PCINT3);
PCIFR |= (1 << PCIF0);
sei();
}
ISR(PCINT0_vect) {
PinChange = 1;
PinState = (~PINB & 0x0F);
debounceButton(PinState);
PCIFR |= (1 << PCIF0);
}
void debounceButton(char state) {
_delay_ms(5);
if (state != (~PINB & 0x0F)) {
PinChange = 0;
}
}