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/