How often does timer/counter interrupt execute

Discussion in 'Embedded Systems and Microcontrollers' started by Sonoma_Dog, Sep 25, 2008.

  1. Sonoma_Dog

    Thread Starter Active Member

    Jul 24, 2008
    99
    0
    all right, so i have successfully done my first interrupt programming. but i am still not sure how often does the program go into the interrupt. Since i dont have an oscilloscope to verify the timing. so my guesses is all based on calculations.

    I am using a 8bit timer/counter, 16Mhz crystals and the interrupt setting is overflowing with 1024 prescaler.

    in the datasheet, i found this equation: f_ocnx=f_clock_IO/(2*N*(1+OCRnx))

    so i guess my OCRnx is 256, wince i am using a 8 bit counter. And if i am not wrong. my N will be 1024 and f_clock_IO is 16Mhz

    So does it means the interrupt will execute at [f_ocnx= 16Mhz/(2*1024*(1+255))] 30.51Hz, which also mean every 32.7ms.

    is my calulation correct? and also, can anyone tell me why is there a 2 in the denominator?

    Thanks
     
  2. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    764
    Want you know if you are in the ballpark?

    If you have a scope available:

    In the interrupt service routine, set one output pin immediately after the overhead dedicated to save the environment, and immediately BEFORE retrieving the environment, reset it.

    For the different settings you could see how close you are. In all my designs I keep this to know that the ISR is active. Saved a lot of guesswork.
     
  3. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    What chip family are you using?
    Where is clock sourced from, CPU clock or a separate timer clock?
    Without this basic info there is no way to check your calculations.
     
  4. Sonoma_Dog

    Thread Starter Active Member

    Jul 24, 2008
    99
    0
    No, i do not have an scope with me at home, that would make it so much easier. :(



    I am using a ATmage168 chip and i believe i will be using a external clock sources, a crystal 16mhz oscillator.

    thanks!
     
  5. nanovate

    Distinguished Member

    May 7, 2007
    665
    1
    create a variable to be used inside the timer interrupt
    connect a LED to a pin
    toggle the pin when the variable reaches a certain value


    Here is an example using Timer 1 (16 bit) on the ATmega168 with the gcc compiler.
    This will toggle the pin on Port B every second based on a 16MHz clock.
    Code ( (Unknown Language)):
    1.  
    2. #include <avr/io.h>
    3. #include <avr/interrupt.h>
    4.  
    5. volatile unsigned int blink;
    6.  
    7. int main (void)
    8. {
    9.    DDRB |= (1 << 0); // Set LED as output
    10.  
    11.    TCCR1B |= (1 << WGM12); //Set up Timer 1 for CTC mode
    12.  
    13.    TIMSK1 |= (1 << OCIE1A); // Enable the CTC interrupt
    14.  
    15.    sei(); //  Enable global interrupts
    16.  
    17.    OCR1A   = 15625; // Set CTC compare value
    18.  
    19.    TCCR1B |= ((1 << CS10) | (1 << CS11)); // Start timer at Fcpu/64 (250 kHz)
    20.  
    21.    while(1);
    22.    
    23. }
    24.  
    25. ISR(TIMER1_COMPA_vect)
    26. {
    27.    
    28.    blink++;
    29.    if(blink > 15)
    30.   {
    31.        PORTB ^= (1 << 0); // Toggle the LED on PORTB
    32.        blink = 0;
    33.   }  
    34.  
    35. }
    36.  
     
Loading...