Hi, I just spend a little time wondering why my timer interrupt fires immediately until I cleared the interrupt flag manually before enabling. Same thing with an external interrupt, when I first use it it's ok. Before enabling the second time I have to clear the flag. Maybe this is a standard procedure but I never needed to do that before, is this normal or what could be the reason ?
Thank you
Mat

what is your point ? I know how to clear the flag, the question is why do I have to do it. I thought it should be clear when enabling the interrupt and be cleared when the ISR is executed. Or am I wrong there ?

But you're right here's the code. It starts off with a blinking (standby). When a (low active) button is pressed (falling edge) the green led and an SSR is switched on. Then the next while waits for the button to be pressed and held. If it is held long enough (5s @8MHz) the green led and the SSR are switched off and the program returns to the first blinking while. If the button is released earlier then the timer interrupt is disabled again and everything stays on. Without the clearing of the bits in "GIFR" and "TIFR" it doesn't work. The defines aren't listed here.

ok, I didn't understand it like that, but you're right, no word about the interrupt having to be enabled for the flag-set. So I could start the timer clock when enabling the interrupt, but now it's working this way. Thanks to all for clearing this up.

??? There >>IS<< "word" about what Cliff said, and what your quoted question asked.

Quote:

â€¢ Bit 0 â€“ TOV1: Timer/Counter1, Overflow Flag
The setting of this flag is dependent of the WGM13:0 bits setting. In Normal and CTC modes, the TOV1 Flag is set when the timer overflows. Refer to Table 15-4 on page 137 for the TOV1 Flag behavior when using another WGM13:0 bit setting.

TOV1 is automatically cleared when the Timer/Counter1 Overflow Interrupt Vector is executed. Alternatively, TOV1 can be cleared by writing a logic one to its bit location.

What part of the phrase in bold is ambiguous? You will find similar wording for [nearly? cannot think of an exception] AVR interrupt flags of that type.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

In fact I was going to quote exactly the text that Lee quoted but amongst the first three PDFs I looked at (which all had the same text) I couldn't find one that wasn't "secure" and that would let me copy/paste. :-(

yes the flag setting is independant of the interrupt enable bit. If it wasn't we could never poll for a condition. Therefore it is always a wise idea to clear an interrupt flag just before enabling the interrupt source. This will prevent accidental firings of the interrupt immediately after the interrupt is enabled.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

ok, then we have settled that. :) Thank you ! Strange and pure luck obviously that I never needed to do that before. Maybe because I never disabled-enabled ints before, but just switched them on...
Just off topic, if I click on "message icons" or "smiles" I get a little window saying "hacking attempt 1" but no icons or something. What is that supposed to mean ?