# Tmr0 16F505

Status
Not open for further replies.

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

#### jpanhalt

##### Well-Known Member
You'll wait a long tome for the IF. The 12(16)F50x do not have interrupts.

#### be80be

##### Well-Known Member
Not much to work with

#### jpanhalt

##### Well-Known Member
I think the only way to detect overflow of TMR0 per se is to poll TMR0. For example, a "movf TMR0,w" or its equivalent in C should set the zero flag in STATUS (i.e., STATUS,2) when the timer rolls over. I have not actually tried that, though. Running it at 1:1 may be more difficult.

If you want counts less than 256, then you can do presets or various subtractions or additions to determine the actual count. At each rollover, you need to repeat any presets.

John

#### Pommie

##### Well-Known Member
If you test bit 7 of TMR0 it will go to zero on an overflow.

Mike.

#### be80be

##### Well-Known Member
Poll the bit I though about that. Trying to bit bang serial in C but i'm learning.
Moved up to a bigger chip. Pic 16F15376 got serial timer2 and pwm working on it
Lets you dim a led using pwm. And read the adc reading over serial.

Status
Not open for further replies.