Counter Question
frank freedman
Posts: 1,983
in Propeller 1
Playing with the counters today. The app note is a bit fuzzy on the logic levels. If I read correctly, the edge mode will only cause the counter to increment by one at each rising or falling edge. The question I have concerns causing the counter to increment from rising edge to rising edge of a square wave and I don't care about the duty cycle, only the total duration of the cycle. In this way, I need only take 1/sysclock * number of sysclock pulses the counter accumulated to give me the period of the waveform. The purpose of this is to take a LM331 set up to provide 1khz/volt to monitor and ultimately regulate the current through a part by monitoring the voltage drop across a series resistance feeding the part. The current will be provided by an external DAC or maybe the S/D described in the counter app notes/PEK docs.
Thx
Frank
Thx
Frank
Comments
The choices would be to use WAIT to sync to each edge, and capture count on same-edges, and you might want to do that over N periods.
or, you could run 2 counters, one HI gated, one LOW gated, and the sum read on an edge, is the period. the Ratio H/(L+H) is the duty cycle.
There is also this thread covering high precision reciprocal counters, most useful when your source is also high precision.
http://forums.parallax.com/discussion/123170/propbasic-reciprocal-frequency- counter-0-5hz-to-40mhz-40mhz-now
The LM331 is ancient and expensive, and not very accurate.
You might instead use a small MCU (sub 40c) with ADC and PWM mode, and send PWM coded info over the isolated link.
I could have used an ADC much the same way as when I did the ADC object a few years ago, but wanted to see if I could get a faster conversion rate than with an ADC that I have on hand. The LM331 has been around a while, but TI seems to be keeping them current. I looked at Analog Devices same functional part, and it was multiple times the price of the LM331(like almost $10.00!!) Also, the LM331 can operate from higher supply and input voltages while giving whatever logic level out I happen to pull it up to. Nice thing about hobby level, I can explore and learn from doing and even more from others comments. Gotta say parallax peeps rock!!
An alternative combination could be to combine a HC(T)4046 Oscillator and a Current Output Current Sense amplifier - the Current sense amp goes direct from millivolts to uA, including level shift, and the 4046 oscillator is a FlipFlip with S-R Ramps. You would choose a 50% duty cycle as the ideal operating point, and at that point the R*C charge time is equal to the I/C charge time. C & Threshold variations are nulled.
This is what I use to PWM at 20Khz. TC represents the cycle time. phsa and phsb represent the duty cycle.
The only advantage a gated counter gives you is not having to wait accurately - so long as you read the PHS
during the time its not changing and before the next edge, you'll get the time for that half cycle. So its good
for timing a single pulse, but hard to use for measuring a repeating period unless you synchronize, in which
case CNT does the job.
The jitter comes from the LM331(seen on my scope, a Tek 2247A), and as noted could be better if not breadboarded and a better layout with low drift parts was used rather than this quick hack from the most basic data sheet example. One side note, I also have a Digilent Analog Explorer v2, and started this project using the digital LSA mode. It sucked surprisingly bad on capture, so I have a bit of learning on that device to do.
Thanks for the suggestion on using CNT.
This should work. Call it with a pointer to the variable that will hold the cycle timing. On starting, put the pin number to monitor in this variable.
Ran the code you posted last night. Worked as advertised. Will definitely be keeping that one for future use. Tested both methods with a 3V 4khz square wave provided by a HP3312A function generator. Will do additional testing at much higher frequencies when I get a chance. Forget statistical hocus pocus. Jon's code works better at >1Mhz than the counter version which seems to fall off pretty fast beyond 100khz.
Thank you again Jon,
Frank
-Phil
Here is the final solution I derived from the original object.
I don't really need a high speed pulse counter at the moment.
But I sure appreciate the beautiful PASM example!
Thanks
That's a little puzzling, as both are hardware based ?
One code measures the whole period, whilst the other code measures a half-cycle, so the whole period one has ~double the time -> half the jitter (assuming 50% duty). That's just 2:1 difference.
You could also measure over some small number (8?/16?) of whole cycles, if the input frequency is high and you need best precision.
First, JMG, yes, I found the half cycle discrepancy yesterday when I realized that I was only getting the half cycle. Did not notice it as my function generator has a fixed duty cycle. Otherwise I would have had to chase down why the count for a given half cycle would have made little sense.
Jon, Thanks for yet another example of how to solve this one. I have last night, implemented a single counter hybrid based on your last use of the cnt and my counter based code. I will post the code here, but am getting a bit of an issue where I have to stop and restart the terminal when I change frequency from the function generator. My counts and resultant frequency are pretty close, but the serial thing is kind of annoying.