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