1

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 :)

rebatoma
  • 734
  • 1
  • 17
  • 32
heyjaynell
  • 47
  • 1
  • 10
  • 1
    http://www.atmel.com/images/atmel-8271-8-bit-avr-microcontroller-atmega48a-48pa-88a-88pa-168a-168pa-328-328p_datasheet_complete.pdf this is the datasheet – heyjaynell Dec 10 '15 at 20:06
  • ^^^ consult data sheet and/or user manual. To notify your 'code', set some flag that you will have to poll or, if you have an OS, signal a semaphore. – Martin James Dec 10 '15 at 20:15

1 Answers1

0

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
}
arduic
  • 659
  • 3
  • 12
  • Do I have to define TIMER1_COMPA_vect? – heyjaynell Dec 10 '15 at 21:14
  • To be honest I did not read the article however I would assume that TIMER1_COMPA_vect is either set in some other file or already defined in that file as the interrupt for the CTC interrupt on TIMSK. Generally the way these programs work you need to inform the system in some way what function to call when the interrupt is fired (ISR). In some cases a second file holds the entire interrupt table. In others there is some kind of a set interrupt function call to make. I suggest reading the detailed article for more info. – arduic Dec 10 '15 at 22:00
  • Figured it out. Thanks so much! Sorry new to this! – heyjaynell Dec 10 '15 at 22:07
  • No worries interrupts are a confusing topic but they are probably one of the most valuable concepts to understand. If you know what a PWM is you will understand how they are built on timer interrupts now hopefully. Anyways, if there is anything you feel I didn't mention here and in the comments to get the answer please list them below so others can find the answer to. – arduic Dec 10 '15 at 22:09
  • 1,000,000 Hz / 64 = 15,625 Hz, not 15,624 Hz. Why does OCR1A require an off-by-one value? –  Jun 20 '20 at 20:53
  • 1
    Ah, the data sheet clarifies: “OCR1A defines the top value of the counter.” The counter starts at 0, not 1, so 1 must be subtracted from the desired frequency. I’m a fan of documenting these details in the code as comments, since they are obscure facts and hard to find in the datasheet. It’s a shame most tutorials don’t do that. –  Jun 20 '20 at 21:14