Helpful ReplyHot!pulse counting with pic16F1508

Author
Tim K
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2018/04/26 11:51:20
  • Location: 0
  • Status: offline
2018/05/14 20:45:48 (permalink)
0

pulse counting with pic16F1508

I'm trying to wrap my head around how to measure the frequency of a train of square wave pulses that can range from 0 to 300hz.  the pulse source is a speed sensor.. it's lowest moving input would be 2hz.. 0hz when stopped.
 
am I right in thinking the best way is to configure a timer to increment on an incoming pulse, and a second timer to set up a fixed interval?
the microchip page says the 16f1508 has:  4 Capture/Compare/PWM Peripherals  but I don't see anything in the 1508 docs about a capture mode like the 16f627/8 does...
 
 
#1
jack@kksound
code tags!
  • Total Posts : 2758
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: pulse counting with pic16F1508 2018/05/15 10:46:40 (permalink) ☄ Helpfulby Tim K 2018/05/16 17:10:13
+1 (1)
There are a few dufferent ways to do this with this pic. It is unfortuante that it does not have input capture mode capability on the PWM modules, that feature makesa good hardware solution.
You can use a timer (timer2) set for a periodic interrupt (the period will define your resolution of determining the unknown frequency) and then either poll the input signal for start/stop (detect the rising or falling edges) or use an external interrupt input (IOC maybe or use the comparator module to generate an interupt). In the ISR for the timer keep a running count of "ticks" - +1 each interrupt- and use this to get period of rising edge to rising edge. If you use an interrupt to detect the "edges" then just subtract "current ticks" from "last ticks" to get period in the ISR. If you poll the input then do the subtract after detecting the input edge in the poll function/routine.
#2
coffee critic
Super Member
  • Total Posts : 238
  • Reward points : 0
  • Status: offline
Re: pulse counting with pic16F1508 2018/05/15 11:04:54 (permalink) ☄ Helpfulby Tim K 2018/05/16 17:09:56
+1 (1)
Another method is to use feed the signal into timer 0 and use the overflow a a source for timer 1 gate. The frequency can then be calculated from the count + overflow of timer 1.  The other thing to consider is the tolerance of the internal oscillator of the 15F1508.  This could add significant error to the calculation. See figure 29-6 of the data sheet for more details.  You might want to consider using a watch crystal on the secondary oscillator as a more stable frequency reference. You will need to run the math to see if there is enough resolution at 32.768kHz.      

n_*$
#3
Tim K
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2018/04/26 11:51:20
  • Location: 0
  • Status: offline
Re: pulse counting with pic16F1508 2018/05/16 18:03:49 (permalink)
0
ok....  let me see if I have this figured out.. I set up Timer0 to count everytime a low to high transition occurs on T0CKI
 
i then setup timer2 Period register (PR2) for an interval.. say.. 250ms  and for it generate an interrupt every 250ms..
then with each interrupt, I copy the timer1 count to a memory location and reset the timer0 count.
 
would that work?
 
#4
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: pulse counting with pic16F1508 2018/05/16 18:16:24 (permalink)
+1 (1)
It's more accurate to just leave timer0 running, and subtract the previous count next time you read it.
That avoids you possibly missing a count that arrives between the read and the clear.

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#5
Tim K
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2018/04/26 11:51:20
  • Location: 0
  • Status: offline
Re: pulse counting with pic16F1508 2018/05/16 20:56:15 (permalink)
0
like this?

ISR

BANKSEL TMR0

MOVF TMR0,W
MOVWF VSS3 ;store timer value in vss3

MOVF VSS2,W ;previus count in Vss2
SUBWF VSS3,W ;subtract vss2, from vss3 and leave in W

MOVWF VSS ;copy subtraction into VSS

MOVF VSS3,W
MOVWF VSS2 ;move current coutnt to ;vss2

banksel PIR1
BCF PIR1,TMR2IF ;clear interrupt flag

retfie

 
right now with the timer2 prescaler set to 64:1, i get 0x98 at 1khz, and 0x30  at 100k
with FOSC at 2mHz, and PR2 set at 255
(using a fixed freq clock generator right now)
 
#6
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: pulse counting with pic16F1508 2018/05/16 21:14:38 (permalink)
0
KC8OYE
...
i get 0x98 at 1khz

0x98 = 152
152 counts at 1kHz should take 0.152 seconds
 
If Fosc = 2MHz, PR2 = 0xFF, Prescaler=1:64, it should roll over at 2MHz / 4 / 64 / 256 = 30.51Hz = 0.0328s
So, the count is about 4.6 times too big.
Something is seriously wrong. Are you sure it's a nice digital square wave reaching your PIC input pin?
If it's a sine wave centred around ground, or a square wave with lots of ringing, you could get all sorts of weird effects.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#7
Tim K
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2018/04/26 11:51:20
  • Location: 0
  • Status: offline
Re: pulse counting with pic16F1508 2018/05/16 21:23:18 (permalink)
0
hrrm... i will double check to make sure it IS running at 2mHz.. 
 
MOVLW b'01100010' ;bit 7 n/a, 6-3 1101 = 4mHz, 2n/a, 1-0 1x = internal oscilator
MOVWF OSCCON
 
1100 = 2mHz.. .hrrm. should be at 2mHz..  i will push it up later, but to do that I have to re-adjust all my timing loops for the LCD display.
 
it's late..that's enough for tonight.. Thanks a BUNCH for the help!
 
 
#8
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: pulse counting with pic16F1508 2018/05/16 21:28:13 (permalink)
0
The error is not a power of two, so I don't think the Fosc selection is the problem.
I'm still suspicious about what sort of signal you are injecting into the PIC to count.
If it is a sig-gen, it's probably centred around ground if you have not specifically set a DC offset.
post edited by qɥb - 2018/05/16 21:59:29

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#9
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: pulse counting with pic16F1508 2018/05/17 00:00:18 (permalink)
0
Pedantic aside, "2mHz" is two milli-hertz, or one clock pulse every 500 seconds.
Case matters a lot when using SI units.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#10
Tim K
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2018/04/26 11:51:20
  • Location: 0
  • Status: offline
Re: pulse counting with pic16F1508 2018/05/18 17:48:07 (permalink)
0
qɥb
Pedantic aside, "2mHz" is two milli-hertz, or one clock pulse every 500 seconds.
Case matters a lot when using SI units.
 


whooops.. you are right... make that 2MHz ;)
 
#11
Tim K
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2018/04/26 11:51:20
  • Location: 0
  • Status: offline
Re: pulse counting with pic16F1508 2018/05/18 20:14:11 (permalink)
0
it is a clean, positive-going only square wave.
the selector switch between 1hz, 1khz, and 100khz was very dirty tho.. had to work it a few times to get a stable 1khz signal out of it.
 
is there a minimum voltage that has to be met to be considered a positive going pulse?
even my WaveTek SigGen can only generate 1.5-2v p2p
 
 
#12
Tim K
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2018/04/26 11:51:20
  • Location: 0
  • Status: offline
Re: pulse counting with pic16F1508 2018/05/18 20:32:13 (permalink)
0
alright.. i think I nailed it down..  using my good WavTek SigGen set for positve only square wave...  igot this:
 
2Khz - 1 count
4KHz - 2 counts
6khz  - 3 counts
8KHz - 4 counts
10Khz - 5 counts
12KHZ - 6 counts
 
1/2K = 5E-4
(1/4k) * 2 counts = 5E-4
(1/6k) * 3 counts = 5E-4
so ... the period for the freq counter is obviously 5e-4  or 100uS
interestingly enough...   2MHz / 4 / 256  = 1953.125hz or 5.12e-4.... so it looks like the problem is that the Timer2 pre-scaler isn't doing what I want it to..
 
 
#13
Tim K
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2018/04/26 11:51:20
  • Location: 0
  • Status: offline
Re: pulse counting with pic16F1508 2018/05/18 22:33:26 (permalink)
0
*GROAN*
 
*FACE PALM*
 
*DOH*
 
i'm not sure how or why.. but I had the PreScaler on timer0 enabled... for.. ya ready...1:64!!!
 
DOH!!!! (I thought I had the prescaler turned off, I may have put the 1 in the wrong bit)
 
now that the prescaler is turned of.. I'm getting a count of.. ya ready..... 0x1F = 31
which is more or less right.. since this freq gen is a tad off freq (it's about 900 and change)
 
#14
Tim K
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2018/04/26 11:51:20
  • Location: 0
  • Status: offline
Re: pulse counting with pic16F1508 2018/05/18 22:59:35 (permalink)
0
ok.. now I need to dust off the 'ole high school algebra skills..
because.... Speed (in mph) = ((count * 4) / 8 * 2.73 / 3.73 * 60^2) / 746
 
that is:  count * 4 (since I count for .250 seconds)  / 8 pulses per rev of the sensor * 2.73 (gear reduction of the speedo drive) / 3.73 (gear reduction of the rear axle) * 60 seconds * 60 minutes   then divide the whole mess by 746 revolutions / mile of my rear tires..
 
isn't this fun? LOL
 
post edited by Tim K - 2018/05/18 23:27:28
#15
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: pulse counting with pic16F1508 2018/05/19 01:20:52 (permalink)
0
KC8OYE
is there a minimum voltage that has to be met to be considered a positive going pulse?
even my WaveTek SigGen can only generate 1.5-2v p2p

To be guaranteed it will work, it needs to swing below 0.2 * VDD = 1V
and above 0.8 * VDD = 4V.
That is a 3 volt swing.
It will most probably work for a much smaller swing too, that is just the voltages you must reach to sure it will work.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#16
Tim K
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2018/04/26 11:51:20
  • Location: 0
  • Status: offline
Re: pulse counting with pic16F1508 2018/05/19 13:25:30 (permalink)
0
i'm thinking a compartor in front of it might be a good idea as signal conditioning.  the amplitude varies as does the frequency ... it might have issues at low speed.
 
#17
Tim K
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2018/04/26 11:51:20
  • Location: 0
  • Status: offline
Re: pulse counting with pic16F1508 2018/05/19 14:58:45 (permalink)
0
found an elegant solution :)
 
I changed the counting period to .440 seconds.. that means 1 count = 1 mph
by using Timer2 POSTscaler at 1:14, and setting PR2 at 0xF6, I get 
 
2e^6 /4/64/246/14 = 2.268hz or .440832 seconds :)
 
 
#18
Jump to:
© 2018 APG vNext Commercial Version 4.5