I'm trying to design a simple Arduino interrupt service routine that uses a function pointer in it so that it can effectively be modified from the main()
function. The following is my code.
#include <stdio.h>
#include <util/delay.h>
#include "gpio.h" // "gpio.h" is my own library that contains the
// definitions of digital_write, digital_read,
// pin_mode, analog_write, etc.
// It also configures all the timer/counter
// circuits to operate in fast-PWM mode
// with an undivided input clock signal.
// This library has been tested.
/* Two interrupt service routines */
void INT_1(void);
void INT_2(void);
/* Function pointer to choose any one of the above defined ISRs */
void (* interrupt)(void) = NULL;
/* main */
int main(void) {
pin_mode(3, OUTPUT);
pin_mode(4, OUTPUT);
cli();
TIMSK0 |= _BV(TOIE0); // Enable Timer0 overflow interrupt
sei();
while(1)
{
interrupt = INT_1; // For 10 ms, INT_1 executes on interrupt
_delay_ms(10);
interrupt = INT_2; // For next 10 ms, INT_2 executes on interrupt
_delay_ms(10);
}
return 0;
}
ISR(TIMER0_OVF_vect) { // Execute the function pointed to by
// "interrupt" on every overflow on timer 0
if(interrupt != NULL)
{
interrupt();
}
}
void INT_1(void) {
digital_write(3, LOW);
digital_write(4, HIGH);
}
void INT_2(void) {
digital_write(3, HIGH);
digital_write(4, LOW);
}
LEDs are connected to pins 3 and 4. These should light up alternately for 10 milliseconds each. However, on flashing this program onto the Arduino, I see that each LED lights up for approximately 2 seconds. Can anyone tell me why?