Resetting just one interrupt flag in TIFR

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you think there is a simple way to reset just one timer interrupt flag (as TOV2 for example) in TIFR?

 

I have ATmega8A and I faced this situation while an external interrupt routine (as for INT0) is executing and in which TOV2 flag only should be reset in case it was set but didn’t have the chance to be served yet. The program also runs Timer0 and Timer1.

 

Thank you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can you explain whats happening and what do you expect? Show your code too.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There recurring problem is that if you do:
TIFR |= 0x01;
You may reset more than one bit. Doing |= is, off course wrong - you should be diong only =

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Mandar, I am afraid my question is not related to code in general.

 

I meant let us suppose we have the register TIFR in which more than one flag are set but not served yet (due to the start of a higher interrupt service or their corresponding enable flags are not set). 

And, as you already know, each of the TIFR flags could be reset by setting it to 1 (the MCU hardware will return it to 0 automatically).

So if we like resetting all flags, we just output 0xFF into TIFR.  

 

From knowing this, it seems impossible (for ATmega8 in the least) to reset directly one flag only in TIFR. But there might be a trick that I have missed.

Of course, there are always indirect ways (but related to code) to solve this issue when necessary.

 

Kerim

 

Last Edited: Sat. Jun 11, 2016 - 08:04 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

By RESET you mean clear, right? In that case why don't you clear TOV2 (in your case) by writing 1 to it, as mentioned in datasheet.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But TIFR (of ATmega8) is not a bit-addressable register.

Hmmm... Now I got it... writing 0 to the other flags keeps them as they were. I had the impression they will be cleared too. It seems I am getting old sooner than my expectation :( Sorry.

Last Edited: Sat. Jun 11, 2016 - 02:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Look, TOV2 in TIFR gets cleared after execution of corresponding ISR, else you can clear it manually by writing 1 to it. i.e. For Atmega8

 

TIFR|=0x40;

This will clear only TOV2 flag only and will keep rest of bits unaffected.

 

OTOH,

 

TIFR=0x40;

This will clear TOV2 and disturb setting of other bits as well.

 

Best method is, always use use bit names instead of direct assigning value. e.g.

 

TIFR|=(1<<TOV2);

 

Of course you need to use ORing, ANDing when required.

 

Hope this clears your doubt.

Last Edited: Sat. Jun 11, 2016 - 08:29 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

TIFR|=(1<<TOV2)
NOT correct!
TIFR=(1<<TOV2);
Is the correct method.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, Kartman,

The instruction TIFR=(1<<TOV2); is the correct one here.

In assembly, I write it as:

   LDI r16, 1<<TOV2

   OUT TIFR, r16

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:

TIFR|=(1<<TOV2)
NOT correct!

Did I miss any in my reply?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes |= is wrong because if other bit are 1 they will also be cleared. (it works like reset on a SR flip-flop)