Interrupts

This code will blink an LED while listening for a button press. Not amazingly impressive but difficult to do with Arduino and the delay() function (while the delay macro is executing the microchip is kind of useless and can’t be listening to the state of a button). Be sure to debounce your button in hardware if you use this code (check out the Debounce page on this site).

Just a note that the INT0 pin on the Attiny84 is PB2 and I have LEDS on PA1 and PA2.

Also a quick primer on bit twiddling:


BYTE |= (1<<i); // set a bit

BYTE = (1<<i) | (1<<j); // set two bits

BYTE &= ~(1<<i); // clear a bit

BYTE ^= (1<<i); //toggle a bit (change its state to the opposite)

Here is the demo code but it does not work reliably and needs to be rewritten!:

/*
 * Attiny84 interrupt.c
 *
 * Created: 2/1/2019 12:58:37 PM
 * Author : FablabDigiscope
 */

#include <avr/interrupt.h>
#include <util/delay.h>

ISR(INT0_vect)
{
	if (PINB & (0b00000100)) // if this works out as non-zero it will execute...
	{
	PORTA |= 0b00000010; // set interrupt LED on
	PORTA &= ~0b00000100; // clear blinking LED bit
	}
	else
	{
	PORTA &= ~(0b00000010); // clear interrupt LED bit
	}
}


int main (void)
{
 PINB = 0b00000100; // pull up resistor for PB2
 DDRA = 0b00000110; // two LEDS output

 GIMSK |= (1 << INT0); // enable the INT0 external interrupt on pin PB2
 sei(); //enable global interrupts

 while (1)
 {

	 PORTA ^= 0b00000100; //toggle the blinking light
	 PORTA &= ~0b00000010; // turn off the interrupt light
	 _delay_ms(200);

 }
}