You will have to tweak depending on your oscillator speed, but the following will toggle a LED every time the timer rolls over. At 4 Mhz that is 65.535mSec X 8 or approx 524 milliseconds. If you need more accurate timing you can pre-load the counter with a value that gives you the time you want. There is an uncertainty, of course, that is directly dependent on the time it takes to execute your code.
Set up Timer1 with a /8 prescaler by setting
T1CON = 00110001 'Divide by 8 prescaler, no syncronization, enable timer
Then, in your code:
TOP:
IF PIR1.0 = 1 THEN
PIR1.0 = 0
TOGGLE LED
ENDIF
'Put your code here'
GOTO TOP
END




Bookmarks