PDA

View Full Version : Use of Timer1 with 16F819



Barry Johnson
- 23rd January 2005, 05:29
I am having difficulty getting the following program to work. PIC is the 16F819. Using an ext. osc running at 16 MHz. When I run the program, it prints the "starting" and "running," then nothing more. This program has no actual purpose other than to help me understand how to proper use Timer1. Once running, I intend to use it in an actual project. I loaded the TMR1 register with $00FF as an arbitrary value. I know having a print command in the interrupt handler isn't wise, but it is just for debug at present.

Any guidance will be most appreciated.

Thanks,
Barry


' PicBasic Pro program that uses the Timer1
' interrupt for a "real-time clock."
' PIC is a 16F819

include "modedefs.bas" 'include serout defines

DEFINE OSC 16

loops VAR WORD

' initialize interrupts
INTCON = $00 ' disable and clear global interrupts, peripheral interrupts

PIR1 = $00 ' clear interupt flags

INTCON = $C0 ' enable global and peripheral interupts
PIE1 = $01 ' Enable TMR1 overflow interrupt

TMR1H = $00 ' load timer1 with $00FF
TMR1L = $FF
TRISB = %00000010 'All port b output except pin 1 (RX) is input

T1CON = %00000011 ' Turn on Timer1, prescaler = 1, ext. clock on

serout 2, T2400, ["starting", 10, 13]

on interrupt goto inthand

Pause 500
serout 2, T2400, ["running", 10, 13]

loops = 0


loop: ' endless loop with 1 ms pause
pause 1
goto loop


inthand:
PIR1 = $00 ' clear interrupt flags
PIE1 = $01 ' enable TMR1 interrupt
TMR1H = $00 ' load timer1 with $00FF
TMR1L = $FF
loops = loops + 1 ' increment counter of interrupts
serout 2, T2400, ["loops = ", #loops, 10, 13]
resume

End

Bruce
- 23rd January 2005, 21:19
If you're trying to use the internal clock (Fosc/4) for the TMR1 clock source, then you'll need T1CON = %00000001 VS T1CON = %00000011.

You'll also want to place DISABLE just prior to entering your int handler.

With ONINT you can do pretty much whatever you like in the int handler.

You can tinker with this code below. It's tested on a 16F627A @20MHz.

@ DEVICE HS_OSC,LVP_OFF,WDT_OFF,MCLR_OFF
DEFINE OSC 16 ' Tested at 20
include "modedefs.bas" 'include serout defines

Loops VAR WORD ' Loops value
loops = 0 ' Start clear

TRISB = %00000010 'All port b output except pin 1 (RX) is input

INTCON = 0
PIR1 = $00 ' clear interupt flags

serout 2, T2400, ["starting", 13, 10] ' Before interrupts are enabled

TMR1H = $00 ' load timer1 with $00FF
TMR1L = $FF
T1CON = %00000001 ' TMR1 on, prescaler=1, clock=(Fosc/4)
INTCON = $C0 ' Enable global and peripheral interupts
PIE1 = $01 ' Enable TMR1 overflow interrupt

on interrupt goto inthand

loop: ' endless loop with 1 ms pause
pause 1
goto loop

DISABLE
inthand:
loops = loops + 1 ' increment counter of interrupts
serout 2, T2400, ["loops = ", #loops, 13, 10]
TMR1H = $00 ' load timer1 with $00FF
TMR1L = $FF
PIR1 = $00 ' clear interrupt flags
RESUME
ENABLE

End

Barry Johnson
- 25th January 2005, 16:25
Thanks Bruce. I understand now what my error was and the code works.