Regarding ATmega8 Timer1

Status
Not open for further replies.

Embedded_Geek

Full Member level 6
Joined
Jul 5, 2010
Messages
342
Helped
58
Reputation
116
Reaction score
56
Trophy points
1,318
Location
Germany
Visit site
Activity points
2,974
Code:
// F_CLK = 4MHz

// CALCULATIONS 
Timer Frequency = F_CLK = 4MHz
Time Period = 1/4Mhz = 0.25 us
Interrupt period = 0.25us * 40 = 10us
So, according to this every 10us an interrupt will be fired


#include <avr/io.h>
#include <avr/interrupt.h>


int main(void)
{
    

    // Mode 4, CTC on OCR1A	
    TCCR1B |= (1 << WGM12);
    
    // Set interrupt on compare match
    TIMSK |= (1 << OCIE1A);
    
    // Compare value
    OCR1A = 40;

    // Set prescaler to No Prescalar (i.e., F_CLK/1)
    TCCR1B |= (1 << CS10);
    
    sei();
    // enable interrupts


    while (1);
    {
        
    }
}

ISR (TIMER1_COMPA_vect)
{
    // Action to be done every 10us 
}


According to the calculation there must be an interrupt at each 10us, but unfortunately the interrupt is getting fired at each 21us.

*** My fuse setting are perfect since I am able to print debug message via uart at a desired baudrate.

Can anyone help me fix this issue?


Thanks in advance,
 

You are operating at a core clock of 4MHz so each cycle is 0.25us.

Are you sure that you are in and out of the interrupt in 40 cycles (40*0.25=10us) ?
If not then you loose the next interrupt so you are probably executing one interrupt then loosing one then executing one etc.
 
Are you sure that you are in and out of the interrupt in 40 cycles (40*0.25=10us) ?

Thanks Alexan.

Yeah I am sure enough that I am out of the interrupt before the next interrupt is fired. I am sure because actually in my ISR I am only incrementing an integer variable.

I am really shocked at this peculiar behavior. Can you suggest me some ideas in order to find the real cause of the problem.


Thanks in advance,
 

Can you use a debugger or simulation to check the timings?

- - - Updated - - -

I did try your code in AVRstudio simulator with an empty interrupt and the interrupt triggers every 10 or 10.5 us
 
Can you use a debugger or simulation to check the timings?

Thanks alexan.

I have verified the timing using logic analyzer. It comes out to be 21us. I have no clue what's really going wrong.

Waiting for some suggestions to solve out the issue.
 

How do you do this measurment?
I assume that you toggle a pin from the interrupt, are you sure that you are not timing a period (which would be two interrupts)?

Maybe provide the full code with the interrupt content so I can see what is going on
 
How do you do this measurment?
I assume that you toggle a pin from the interrupt, are you sure that you are not timing a period (which would be two interrupts)?

Maybe provide the full code with the interrupt content so I can see what is going on

Thanks Alexan,

I did measure correctly by toggling the port pin. I haven't measured the time period.

As you hinted in your earlier post, you were absolutely correct. My ISR was taking more than 10us. The problem was due to the if-else condition. I was actually reading the port pin status and then negated it to toggle the bit. This particular statement took finite amount of time (i.e., more than 10us), though I don't know why it is taking so much of time. Initially I was only incrementing a variable, that was fine. Only the toggling of output pin created the problem.

Once again thanks for assisting me towards the correct direction.


Oops forgot to mention. I have changed my crystal from 4MHz to 12 MHz.


Do you have any idea why is it creating problem with 4MHz inorder to decode NEC IR protocol. My ISR takes long time so I miss one interrupt each time. But the same code works almost perfectly with a 12MHz crystal. Now I clearly understand the value of assembly language compared to C.
 
Last edited:

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…