gnupic: Thread: pulse counting using timer1


[<<] [<] Page 1 of 1 [>] [>>]
Subject: pulse counting using timer1
From: Phil ####@####.####
Date: 2 Apr 2004 07:18:48 +0100
Message-Id: <200404021615.50560.phil@spiderweb.com.au>

Hello everyone,

Thank you for reading this and forgive me if this isn't an appropriate 
question for this list.

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.

-- 
Regards,
Phil.


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

Subject: Re: pulse counting using timer1
From: Snail Instruments ####@####.####
Date: 4 Apr 2004 20:22:17 +0100
Message-Id: <3.0.6.16.20040404200625.46372554@pop.iol.cz>

>Your code looks good; I can't think of a reason why it wouldn't work. 

Should also disable comparators: CMCON=7. Otherwise inputs RA0, 1 always read zero.

Josef


Subject: Re: pulse counting using timer1
From: Phil ####@####.####
Date: 5 Apr 2004 00:43:07 +0100
Message-Id: <200404050942.58448.phil@spiderweb.com.au>

On Sun, 4 Apr 2004 08:06, Greg Hill wrote:
> 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.

Thanks for the replies Josef and Greg,

A couple of hours after posting my question I discovered the answer and that 
was to move the T1CON initialisation outside of the code enclosed by the 
STATUS instructions. Unfortunately I didn't notice that a reply to my own 
question returned to myself, and not the list, until after I'd logged off.

Now that the counter works I've found that the circuit is very sensitive to 
electrical noise. Turning on or off nearby appliances causes the count to 
advance, sometimes by hundreds and this is with the PIC circuit being battery 
powered and port C0 grounded.

I'll have a search for the frequency counter project.

-- 
Regards,
Phil.


Subject: Re: pulse counting using timer1
From: Laurence Withers ####@####.####
Date: 5 Apr 2004 00:59:23 +0100
Message-Id: <200404050058.50054.lwithers@users.sf.net>

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Monday 05 April 2004 00:42, Phil wrote:
> Now that the counter works I've found that the circuit is very
> sensitive to electrical noise. Turning on or off nearby appliances
> causes the count to advance, sometimes by hundreds and this is with
> the PIC circuit being battery powered and port C0 grounded.

Have you decoupled the PIC? (A 0.1uF capacitor across the supply pins, 
located physically close to the chip, should do the job). Also, you 
could experiment with decoupling RC0 as well -- just run a capacitor 
from the pin to ground. Finally, try to use the same technique to 
dampen noise at whatever source your pulses are coming from.

The idea is that at higher frequencies (i.e. the noise you want to 
avoid), the capacitor's impedance falls and so it drains away some of 
the noisy signal.

Bye for now,
- -- 
Laurence Withers, ####@####.#### ####@####.####
http://xmlpcbrender.sf.net/    http://lw-gui.sf.net/
http://pgp.dtype.org:11371/pks/lookup?op=get&search=0x04A646EA
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAcKE5UdhclgSmRuoRAnYkAKCNmSyYeSABI7cb7y6IAJG4SO474ACgon0M
HQexoi6QreyzJZsNbQUJVsg=
=H2nP
-----END PGP SIGNATURE-----
Subject: Re: pulse counting using timer1
From: Pete Harlow ####@####.####
Date: 6 Apr 2004 13:35:30 +0100
Message-Id: <4072A359.3070500@thales-transportservices.com>

I had some spurious behaviour on one 16F877 design that turned out to be 
that I had not disabled Low Voltage Programming. Make sure this is off 
in your configuration word.

Regards,

Pete.

Phil wrote:
> Now that the counter works I've found that the circuit is very sensitive to 
> electrical noise. Turning on or off nearby appliances causes the count to 
> advance, sometimes by hundreds and this is with the PIC circuit being battery 
> powered and port C0 grounded.

This e-mail and any attachment may contain confidential and privileged material intended for the addressee only. 
If you are not the addressee, you are notified that no part of the e-mail or any attachment may be disclosed, 
copied or distributed, and that any other action related to this e-mail or attachment is strictly prohibited, 
and may be unlawful. 
If you have received this e-mail by error, please notify the sender immediately by return e-mail, and delete 
this message. Thales, its subsidiaries and/or its employees shall not be liable for the incorrect or incomplete 
transmission of this e-mail or any attachments, nor responsible for any delay in receipt.
Subject: Re: pulse counting using timer1
From: Phil ####@####.####
Date: 7 Apr 2004 03:48:12 +0100
Message-Id: <200404051029.58598.phil@spiderweb.com.au>

On Mon, 5 Apr 2004 09:58, Laurence Withers wrote:
> On Monday 05 April 2004 00:42, Phil wrote:
> > Now that the counter works I've found that the circuit is very
> > sensitive to electrical noise. Turning on or off nearby appliances
> > causes the count to advance, sometimes by hundreds and this is with
> > the PIC circuit being battery powered and port C0 grounded.
>
> Have you decoupled the PIC? (A 0.1uF capacitor across the supply pins,
> located physically close to the chip, should do the job).

Thanks for the reply Laurence,

I had already tried adding 0.1uf capacitors to every likely place on the board 
without any noticeable affect. No doubt the problem is due to the prototype 
board that I'm using to test ideas. It's one of those boards that has a 
matrix of sockets for the components to plug into.

However, I hadn't tried adding a capacitor from the input pin to ground, as 
you suggested, because the counter advanced even with this pin grounded right 
at the PIC.

Anyway to make a long story shorter, I followed you advice and the counter now 
works perfectly! Now to move onto the next "insurmountable" problem.

-- 
Regards,
Phil.


Subject: Re: pulse counting using timer1
From: Phil ####@####.####
Date: 9 Apr 2004 05:06:13 +0100
Message-Id: <200404071431.35249.phil@spiderweb.com.au>

On Tue, 6 Apr 2004 22:32, Pete Harlow wrote:
> I had some spurious behaviour on one 16F877 design that turned out to be
> that I had not disabled Low Voltage Programming. Make sure this is off
> in your configuration word.
>

Thanks for the reply Pete,

This is my configuration word:

__CONFIG _WDT_OFF & _XT_OSC & _CP_OFF & _LVP_OFF

However, I'm not sure that LVP is actually turned off. I'm using pikdev and 
it's programming window shows that code protection is turned on even though 
my config word sets code protection to off. The way that I understand it, the 
configuration panel shows LVP as off.

Perhaps pikdev's programming window doesn't reflect the contents on the config 
word. I found pikdev's config settings confusing when I first began 
experimenting with PICs; I'll experiment with the config settings a little 
more.

-- 
Regards,
Phil.


[<<] [<] Page 1 of 1 [>] [>>]


Powered by ezmlm-browse 0.20.