I'm writing a code that needs to check a sensor's input every 0.5s. I want to use an ISR because I want my code to execute until the sensor's input changes.
How would I set up this ISR to execute every 0.5s?
Thanks :)
I would suggest using a Timer interrupt. For an example go here. http://www.avrfreaks.net/forum/tut-c-newbies-guide-avr-timers?page=all
I haven't tested it myself but here is a section of code on that.
#include
#include
int main (void)
{
DDRB |= (1 << 0); // Set LED as output
TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode
TIMSK |= (1 << OCIE1A); // Enable CTC interrupt
sei(); // Enable global interrupts
OCR1A = 15624; // Set CTC compare value to 1Hz at 1MHz AVR clock, with a prescaler of 64
TCCR1B |= ((1 << CS10) | (1 << CS11)); // Start timer at Fcpu/64
for (;;)
{
}
}
ISR(TIMER1_COMPA_vect)
{
PORTB ^= (1 << 0); // Toggle the LED
}