counting ttl freq.s using a picaxe ?

For my next projec t i like to build a simple freq counter using 7-segment displays.

I know i can use the count command to count the pulses per period and calculate the freq. from it.

I also know the limitation on speed and thus max freq to count.

Has anybody ever build such kind of a project and will share his findings ?
 

andrewpro

New Member
I built a frequency counter for an oscillator that I built around a max038 function generator IC...I dind't do it in the normal fasion though. It's got a sync pulse output that is always at 50% duty cycle and has a rising edge in concurrence with the 0 crossing of the wave it's generating.

The picaxe cant measure the frequencies I was making with this thing directly, so instead I used the pulsein command and a frequency divider to bring the pulse length down to where I could easily measure it with the picaxe, then computed the frequency using (lengthofpulse*2)/dividercount.

This allowed me to accurately measure all the way up ot the 20+Mhz that the oscillator is capable of without worrying about the frequency counting limitations of the picaxe itself. In essence, I wasn't measuring the frequencydirectly, but the wavelength which was made long enough for the picaxe to measure using the dividers. It's much faster to do it this way than to actually count the pulses, ebcause you only need one half of a wavelength (period) to get a exact measure of the frequency, and the update speed is only confined by how much processing you do before you take the next wavelength measurement.

Edited by - andypro on 3/31/2005 7:56:48 PM
 
hmmzz, not a bad idea :)

if i understand you correct instead of measure the puls count you meaure the length of one low to high or high to low transition with the pulsin command and multiply this by two (50% duty cycle)

this way you get the lenght of one cycle in units. at 4 Mhz clock speed a unit is 10 uS long and a cycle of 24 units = 4166 hz (1 000 000 / 240 uS).

I use the ds1307 with a SQW out set to 4096 hz and get this measurement (4166 hz). could the difference have something to do with the build in osc of the 18X and would the accuracy be more closer to the real thing if i use a 28 x with a 4 Mhz osc ?

this way of measuring will only work with a duty cycle of 50 % (500 mS high and 500 mS low to get 1 hz) ?

What is your opinion about first measuring the low to high time (pulsin 0,1,w0) and then measure the high to low time (pulsin 0,0,w0) and then combine them ? would this way enable you to measure non 50 % duty cycle signals ?

thanks for your response
 

andrewpro

New Member
The way I ahve it set up is that a counter is used as a divider (it's actually a programable divider chip) to bring the count down to where the picaxe could read it.

The way I ahve it set up, it will only work with 50% duty cycle. However, I only measure half of the wave for speed purposes. If you measure the whole wave..i.e. the time from one high pulse to the next high pulse, you get the full length of the wave swaping a flip flop once. The time high that the flip flop Q or /Q is high would be exactly one full cycle, no matter the duty cycle of the waveform, and you could use this time to measure the frequency with simple maths. Just instead of multiplying by two like I do (because I'm only measuring half of the waveform) you dont multiply at all.

This will give you the period of one full wave, and the frequency of a wave is 1/time for one wavelength (or full period) and this will give you your frequency of the entire period of the waveform.

Edit: Forgot the second part of your question. The internal oscillator isn't as accurate as it could be. This will give you a slight variation even form the same technical frequency, from time to time, dependant on alot of factors. So yes, there is a %error. using an external oscillator picaxe can give you a more accurate measurement if that's what your after.

Edited by - andypro on 3/31/2005 11:50:48 PM
 
I'll try and test my little test program with a 28X to see if the values are more accurate.

Which diveder chip are you using ? a 4059 ?
 
Hmmzz, strange

i use a ds1307 to generate a pulse on the SQWE pin of the chip. this signal is feeded to a input pin (input 5, not the 4 adc pins) and i'm trying to measure the puls time with this program:

i2cslave %11010000, i2cslow, i2cbyte
writei2c 0,($10, $48, $17, $07, $20, $03, $05, $11)
i2cslave %11010000, i2cslow, i2cbyte ‘ set slave details

main:
pulsin 5,0,w1
sertxd (#w1,"-")
goto main

It seems i don't get any value in w1, the freq of the pulse is 4096 hz and the pulse is generated because putting a led on this the SQWE output flashes.

The whole thing worked on a 18X..
 
Changing from a 18X to 28X did not had any effect on the accuracy of the measurement. It seams the higher the freq. is the less accurate the measurement is. for example:

feeding the 28X with a 4.096 Khz signal result in an 10 - 20 hz difference. (Recalculated by hand using the pulsin value and a calculator because the Pic doesn't support fractal calculations).

My current setup:

A DS1307 is used as a signal generator supplying 4.096 Khz signal, this signal is given to a 4059 programmable divider set to a divide value of 50 and the output of this value is given to a flipflop dividing the signal by 2 and generates a nice signal with a 50% duty cycle. this signal is given to a PICAXE to measure with the pulsin
command

So the 4.096 Khz input signal provided by the DS1307 is divided by 100 and the dived signal is a 40.96 hz with a 50 % duty cycle.

Anyone has a solution for making more accurate readings ?




 

andrewpro

New Member
It's probably just an inaccuracy built into the picaxe itself. Not intentionally built in, just in the way it works. 10 hz is only like a 2-3 percent error, which really isn't that bad, given the circumstances.

Also, you have to remember there may be an inaccuracy in the crystal your using on the rtc itself. IT's also possible that there is an error in the propogation of the divider chain, some compression could be taking place...there are ALOT of places where the errors can add up.

One way to help overcome the error is to use the clock output as the reference to a PLL, driving a VCO, and measure that pulse, but that's getting kind of out of hand because then you have new places for errors to add up.

It may be possible to use an oscilloscope if you can get access to one) to measure the actual output pulse, prior to the picaxe taking the measurement, then use that to program in a "fudge factor" for the picaxe after comparing it to the measurements you've taken.

Or...you could just accept the error for what it is and live with it.
 

BeanieBots

Moderator
I may be wrong but I'm fairly certain that the output of a 4059 programmable divider even after divide by two, is not a perfect 50% duty. Could this be the problem?
 

hippy

Technical Support
Staff member
The non-50% duty can be overcome to some extent by reading a high period and then a low period using PULSIN and adding the two together. Providing the frequency is stable between the two, the reading should be fairly accurate.

Don't forget that the resolution of the PULSIN also has an effect as already mentioned. Results are returned in 10uS units, so if a pulse is just a bit longer than 10uS it can read back as 20uS.

A 4.096kHz high period should be 122.07uS; that would likely be read as 130.00uS, 3.846kHz, that's a 6% error.

Through a divide by 100, that becomes 12207.03uS read as 12210.00uS, which should have a much lower impact on accuracy.
 

andrewpro

New Member
Beaniebots said:

"I may be wrong but I'm fairly certain that the output of a 4059 programmable divider even after divide by two, is not a perfect 50% duty. Could this be the problem?"

That's what the point of the final flip flop was. When I did it, the MAX038 chip always puts out a "perfect" 50% duty cycle at the zero crossing, so I only had to measure half the wave. By putting in the last flip flop, he's measuring the whole wave, from one high to the next, not just the high time of the wave.

Edit: I was just re-reading and what hippy said sparked a new idea. If you want to get a higher accuracy, why not overclock your 28? Double the frequency, double the resolution (to 5us) double again and you get a 2.5us resolution.

Edited by - andypro on 4/6/2005 4:14:24 PM
 
Sadly, i don't have any access to a Scope but when i measure the freq generated by the RTC with my digital multimeter it nicely say's 4.096 Khz and after dividing it by 100 using the 4059 it also says 40.96 hz so i don't think there are any accuracy problems with the RTC clock.

After reading Hippy's post it confirmed my idea that the accuray problem is caused by rounding up the fractal numbers of a pulsin measurement.

My plan is to let the 28X drive the 4059 divider to automatic raise the divide factor after the pulsin result is less than a certain value. (the lower the freq. the higher the accuray) and to make some routines to make it posible to do some fractal calculations for calclating and displaying the freq. (using // to get the remainder value and repeat the calucation on this value multiplied by 10)

And A PLL driven by a VCO, ehhr i know about the meanings of these words and i know about what these components do but for me this is more advanced stuff and i'm a programmer with a minimal electronics background so this is a littlebit to complex for me (for now).

I Also thought about overclocking the PICAXE to get a finer resolution, for this i need a 8 or 16 Mhz oscilator which i have to order online at thechsuplies (i can't find these things in the netherlands, maybe some dutch reader can give me some info on this) and due to the shipping costs i have to order more stuff. maybe next month :)

I'll try some idea's you all gave and will post the results back !

Just as my previous project, there are many already build freq counters, my multimeter even has a simple one and this thing was much cheaper than all the time and components i've spent on this project but i want to see if it's posible and try to overcome some limitations on the picaxe. I know there are faster PICs which can be more accurate but for me it's the game of doing something difficult which makes it fun ! (and not to forget the learning aspect).

O, BTW beanybots posted something about the duty cycle generated from the 4059. For this reason i use a flipflop as a divide by two, this way i have a nice 50% duty cycle.
 
Top