#define F_CPU 8000000UL
#include <avr/io.h> /* Include AVR std. library file */
#include <util/delay.h> /* Include inbuilt defined Delay header file */
#include <avr/interrupt.h>
static uint8_t mode = 0b00000000;
void int5_init(void)
{
// Make sure you have connected a SW between PD1 (External Interrupt) and GND.
cli(); // Clear all the interrupts
PORTE |= (1 << PE5); // External SW PullUp
DDRE &= ~(1 << PE5); // External Interrupt Sensing Port(PE3) Input Port
EICRB |= (1<<ISC50); // External Interrupt 5, Falling Edge Asynchronously Interrupt
EIMSK |= (1 << INT5);; // External Interrupt 5 enable
sei();
}
void Time_Delay(void)
{
TCNT1 = 3906; // TCNT1= for delay of 0.5 seconds
TCCR1B |= (1<<CS12)| (1<<CS10); // Scale down the timer frequency to 8MHz/1024
while ((TIFR1 & (1<<TOV1))==0); // wait for TF1 to roll over
TCCR1B = 0; // Stop Timer
TIFR1 |=(1<<ICF1)|(1<<OCF1C)|(1<<OCF1B)|(1<<OCF1A)|(1<<TOV1);
}
int main()
{
int5_init(); // Initialization of INT5 Interrupt
DDRB |= (1<< PB0); // PORTB.0 will be used as an output.
PORTB &= ~(1 << PB0); // PORTB.0 pulled low
while(1) {
}
}
ISR (INT5_vect, ISR_NOBLOCK)
{
// sei();
// int5_init();
mode = ~mode;
if (mode == 0b00000000) {
PORTB &= ~(1<<0);
} else {
while (mode != 0b00000000) {
PORTB ^= (1<<0);
Time_Delay();
}
}
EIMSK |= (1 << INT5); // External Interrupt 5 enable
}