Go Down

Topic: Problem with TIFR0 Register using Timer0 (Atmega328p) (Read 1 time) previous topic - next topic

Darksaga28

So, here's the deal. I've got a problem with calculating the number of overflows on a While loop. Here's part of the code:

TIMSK0= 0x01; // enables timer0 interrupts
TCCR0B= 0x01; // no prescaler... (Confused about using TCCR0A or TCCR0B? Help in this too please)

So, I want to check how many times Timer0 overflows when a digital pin is HIGH. Suppose that pin is called ''Pincheck''. So here's what I thought:

Check:
if (digitalRead(Pincheck) ==HIGH) {
 
  TCNT0=0; // begin to count
}
else {
  goto Checkr;
}
 
while (digitalRead(Pincheck) == HIGH){
    if ((TIFR0, TOV0) ==1) {
     Overflow +=1;
   }
   
  }

I'm not sure what's wrong, but I think is the way I check TIFR0's pin ''TOV0'', the one that sets when Timer0 overflows. I think this because all the other stuff runs well. Anyone knows what could be wrong with the code?
I did the same algorithm using Bascom and worked out really well, but I need this on Arduino for a project. Thanks in advance.

Coding Badly


Timer 0 is used for millis (and its ilk).  I suggest using timer 2 instead (which is almost identical to timer 0).

PaulS

Code: [Select]
    if ((TIFR0, TOV0) ==1) {
I'd also suggest that you explain what you think this is doing. The comma operator rarely does what a lot of people seem to think it does.

Darksaga28

I've done this different ways, none of them seems to work.

How would you check Timer0/1/2 overflows? The thing is that everytime it overflows, I've got to increment a variable.

Darksaga28

I've just started programming on Arduino, so that's why I still don't feel comfortable on it. I had been programming on Bascom for years.
In fact, in Bascom to read and check TOVx is as easy as this:

If TIFR.0 =1 then
statement
EndIf

But I'm really interested in learning how to program this on Arduino, because of other features I may use in the future that Arduino has.

Coding Badly

Code: [Select]
if ( TIFR2 & (1 << TOV2) )
{
 // timer 2 overflowed
}


The ampersand (&) is a bitwise-and.  The double-less-than is a bitwise-left-shift.  C(++) expressions the evaluate to non-zero are automatically treated as true.

"1 << TOV2" shifts a single bit into the correct position for the TOV2 flag.  "TIFR2 &" isolates the bit representing the TOV2 flag.

Darksaga28

Ok guys, thanks for the help. I finally got it right, both ways. Using the Timer interrupt function, and ''manually'' checking TIFR2's TOV2, to see when an overflow has place.


Go Up