be80be
Well-Known Member
I been play with the timer on a 16f505
Trying to figure how to load a counter and test each time it fills up.
Trying to figure how to load a counter and test each time it fills up.
Code:
#include <xc.h>
unsigned long counter; //counter variable to count
//the number of TMR0 overflows
Init(void)
{
TMR0 = 0;//Clear the TMR0 register
/*Configure Timer0 as follows:
- Use the internal instruction clock
as the source to the module
- Assign the Prescaler to the Watchdog
Timer so that TMR0 increments at a 1:1
ratio with the internal instruction clock*/
OPTION = 0B11010111;
}
/*----------------------------------------------------------
Subroutine: main
Parameters: none
Returns: nothing
Synopsys: Main program function
-----------------------------------------------------------*/
main(void)
{
Init(); //Initialize the relevant registers
TMR0 = 0;
while(1)
{
//Poll the T0IF flag to see if TMR0 has overflowed
if (TMR0)
{
++counter;//if T0IF = 1 increment the counter
//variable by 1
//TMR0 = 0;//Clear the T0IF flag so that
//the next overflow can be detected
}
}
}