Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

Re: [AVR-Chat] Re: documentation of timer interrupts vs bugs

Re: [AVR-Chat] Re: documentation of timer interrupts vs bugs

2005-04-21 by Dave Hylands

Hi Chcuk,

> > The reason that writing a 1 to it clears it is because if you write a
> > 1 to some other bit in the same register, you're writing a zero to all
> > of the other bits.
> 
> ?????
> 
> I assume when one writes a 1 to a register bit position one is ORing it into the
> register such as:
> 
> reg |= 0x20;
> 
> and, clearing a bit ...
> 
> reg &= ~0x20;
> 
> so I don't understand your statement that "if you write a 1 to some other bit in
> the same register, you're writing a zero to all of the other bits".  I assume
> you're saying something like:
> 
> reg = 0x02;
> 
> to clear the 0x20 bit (bit position 5) but I wouldn't think that that would be
> normal practice ...

Yeah - my statement wasn't all that clear. You're right as far a
general purpose stuff is concerned.

However, the interrupt flag register is special. It's never safe to do
something like:

TIFR |= 0x20;

You really have to do:

TIFR = 0x20;

This is because setting the bits in the flag register happens in
hardware. There's NO way to atomically do any bit manipulations on
this particular register.

All the software can do is clear the bits (which clears the interrupt).

For example, let's say that you do the following (and writing a zero
cleared an interrupt)

TIFR |= ( 1 << TOV1 ), which generates some assembly like this:

in      r24, 0x38
ori     r24, 0x04
out     0x38, r24

Now lets suppose that a TOV2 interrupt occurs after the "in"
instruction. Whether interrupts are enabled or not, the TOV2 flag will
get set. However the out instruction would write a zero into TOV2
which would clear it. Now you've lost a TOV2 interrupt.

By making it so that the software can only clear interrupts by writing
a 1, and using

TIFR = ( 1 << TOV1 );

you don't disturb any other flag bits.

-- 
Dave Hylands
Vancouver, BC, Canada
http://www.DaveHylands.com/

RE: [AVR-Chat] Re: documentation of timer interrupts vs bugs

2005-04-22 by Chuck Hackett

> From: Dave Hylands
> 
> ....
> However, the interrupt flag register is special.
> ....
> This is because setting the bits in the flag register happens in
> hardware. There's NO way to atomically do any bit manipulations on
> this particular register.
> 
> All the software can do is clear the bits (which clears the interrupt).
> 
> For example, let's say that you do the following (and writing a zero
> cleared an interrupt)
> 
> TIFR |= ( 1 << TOV1 ), which generates some assembly like this:
> 
> in      r24, 0x38
> ori     r24, 0x04
> out     0x38, r24
> 
> Now lets suppose that a TOV2 interrupt occurs after the "in"
> instruction. Whether interrupts are enabled or not, the TOV2 flag will
> get set. However the out instruction would write a zero into TOV2
> which would clear it. Now you've lost a TOV2 interrupt.
> 
> By making it so that the software can only clear interrupts by writing
> a 1, and using
> 
> TIFR = ( 1 << TOV1 );
> 
> you don't disturb any other flag bits.

Hi Dave,

Thanks much for the explanation.  I've always wondered why one writes a "1" to
"clear" and interrupt, your description made it clear (ouch, two puns in one
sentence <vbg>) for the first time.

Cheers,

Chuck Hackett
"Good judgment comes from experience, experience comes from bad judgment"
7.5" gauge Union Pacific Northern (4-8-4) 844
http://www.whitetrout.net/Chuck

Re: documentation of timer interrupts vs bugs

2005-04-22 by arhodes19044

What happens when you write a 1 to a bit in TIFR that is already 
zero.  I suppose it stays zero....

-Tony

P.S.  That means that one can not manually set the interrupt flag to 
create a software controlled interrupt?

-T

--- In AVR-Chat@yahoogroups.com, Dave Hylands <dhylands@g...> wrote:
Show quoted textHide quoted text
>By making it so that the software can only clear interrupts by writing
>a 1, and using
>
>TIFR = ( 1 << TOV1 );
>
>you don't disturb any other flag bits.
> -- 
> Dave Hylands
> Vancouver, BC, Canada
> http://www.DaveHylands.com/

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.