Pages: [1]   Go Down
Author Topic: Problem with TIFR0 Register using Timer0 (Atmega328p)  (Read 1007 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 208
Posts: 12932
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49386
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 208
Posts: 12932
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Pages: [1]   Go Up
Jump to: