gnupic: pulse counting using timer1
Subject:
Re: pulse counting using timer1
From:
Greg Hill ####@####.####
Date:
3 Apr 2004 23:07:06 +0100
Message-Id: <Pine.LNX.4.44.0404031500490.30710-100000@hillnet.us>
Phil -
I'm doing a project with tmr1 on the 876a as well. I've ported the code in
the Microchip app note about implementing a frequency counter in software
from the 16Cxx chip they used to the 876A I'm using.
Your code looks good; I can't think of a reason why it wouldn't work. One
thing I noticed is that you turn on the timer before configuring its clock
source and synchronization bits. While I don't know for certain, it seems
possible to me that the timer module might ignore the settings of those
bits while it is turned on (ie, maybe the timer module itself reads the
configuration bits only when it is first turned on). Try specifically
turning the timer off, then setting the configuration bits, and then bsf
t1con,tmr1on to enable the timer module.
I haven't use gpsim for quite a while, so I couldn't say whether it
simulates the tmr1 module properly or not.
Greg
On Fri, 2 Apr 2004, Phil wrote:
> I'm attempting to count pulses using a 16F876A PIC and timer1. The way I
> understand timer1's use is that a rising edge on port C0 causes TMR1L to
> increment and overflow into TMR1H. So far that doesn't happen and I suspect
> that there is more to it than just setting a few bits in the
> T1CON register.
>
> This is my initialisation code:
>
> clrf PORTB ; Initialise Port B
> clrw ; Clear W register
> bsf STATUS,RP0 ; Select Register Bank 1
> movwf TRISB ; Set port B to all outputs
> movlw b'00000001'
> movwf TRISA ; Port A0 input the rest outputs
> movlw b'00000011'
> movwf TRISC ; Port C0 & C1 inputs the rest outputs
> bsf T1CON,0 ; Enable timer1
> bsf T1CON,1 ; External clock input from C0
> bsf T1CON,2 ; Don't sync external clock input
> movlw 0x06
> movwf ADCON1 ; Set port A to digital i/o
> bcf STATUS,RP0 ; select Register Bank 0
>
> The remainder of the code reads TMR1L and TMR1H (I took this from the
> Microchip's docs) and then displays the result on a LCD display. The counter
> registers are always 0 and the display code actually works.
>
> Is there more to it? Should double clicking port C0 from within gpsim
> increment the counter? Are there other registers that I have to deal with?
>
> I have searched for example code without success and this is my first PIC
> project so go easy on the flames.
>