HELP, with example of frequency measurement Error

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi.

I have a problem with CY8CKIT-059 I'm testing the example https://community.cypress.com/docs/DOC-10228 that everyone refers to when it comes to measuring frequency of a signal, but when I run the example the only thing I get are very different frequency data we are talking about very exaggerated deviations as you can see in the capture,

I find it very strange and so I ask the community to have if I can reasonably measure frequencies <1Khz +-1Hz of precision square signal.

before testing the psoc 5LP I have worked with the Psoc 4 cy8ckit-147 and I have successfully performed the frequency meter with a reasonable accuracy, but with the 5LP

I am not able. I've read practically all the post related to the subject, I've downloaded several examples but I didn't get any clarity about it.

Thank you.

Psoc5lp.JPG

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.

grantorax,

try the attached project (tested on KIT-059), works OK here. Re: Arduino - it is friendly for tinkering at home due to the huge library codebase, but I can't imagine having it  on professional equipment.

/odissey1

Frequency meter_01b.png.png

Frequency meter_01a.png

View solution in original post

0 Likes
35 Replies
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

grantorax,

In the example provided the period was calculated incorrectly.

Replace

PWM_windowPeriod = PWM_windowPeriod / PWM_FREQ;

with

PWM_windowPeriod = (PWM_windowPeriod+1) / PWM_FREQ;

/odissey1

0 Likes
Anonymous
Not applicable

Hello, User_342122993, thanks you.

that change you suggest I already have done and the result is the same, the problem is that the measures for a stable signal is very disparate, for a frequency of 500Hz the result by the uart of the 5LP goes from 410Hz to 550Hz what really is very bad, what I do not know if it is a code problem or is a problem of the PsoC 5LP not understand how something so simple can give so many problems, I am rethinking to continue using Psoc ...   because something I've already done with ARDUINO in a super simple and fast way here is costing me a life, and user-engineers are not to waste time solving things that the manufacturer should have left solved before taking the product to the general market.

0 Likes
lock attach
Attachments are accessible only for community members.

grantorax,

try the attached project (tested on KIT-059), works OK here. Re: Arduino - it is friendly for tinkering at home due to the huge library codebase, but I can't imagine having it  on professional equipment.

/odissey1

Frequency meter_01b.png.png

Frequency meter_01a.png

0 Likes

The difference is visible, but not obvious.

Where is the mistake?F2.jpg

0 Likes

Evgeniy,

I started with original code provided by <grantorax> and moved ISR from PWM to the Counter and added double-sync to the frequency input pin. Also replaced PWM_WindowPeriod to PWM_WindowPeriod+1 in code as shown in the previous post to make correct timing interval.

I don't understand where freq:504 comes from on the second window. It should be 500 (as in my previous post). Do you use external 500Hz generator or internal clock divider?

/odissey1

0 Likes

Yes, this is an external 504 Hz signal.

I do not understand why the variant from <grantorax> is unstable.

0 Likes
Anonymous
Not applicable

many thanks to both odissey1 and Evgeniy Pavlov

The first thing: the modifications have been substantial since the first project that I publish cypress and I can conclude that this project in the 5LP does not work as they say, which makes me doubt their interest to help the community to use the psoc correctly, leaving this the following is that I have tested the project that you have modified and if it works but as you can see in the image for the frequencies: 100, 200, 400, 500, 800 1000, 1500, 2000Hz show differences of 2,4,6,8,10,14 hz respectively that acomulative error to which it is due? I in the Psoc41xx had achieved resolution of 0.5Hz to 1Khz, it is not possible with the 5LP to get more accuracy or resolution in measurement? I suppose that the FreqDiv_1 block has been used as a test mode, or performs some function within the program?

now for curiosity I have measured the frequency on pin 15.5 and the frequency I measure is 496Hz about 4Hz less than the calculated frequency, these inaccuracies are typical of Psoc?

P.D.: arduino mega, Due or Zero are just as good as psoc for more professional things the big difference as you say is that they have a great community, personally I think Psoc is a powerful microcontroller and very versatile, but poorly powered by cypress, which is a shame.

I send my thanks to all of you.

5LP_1.JPG

0 Likes

grantorax,

Do you provide test frequency from the external function generator or from the PSoC?

If frequency generator is external, then there will be deviation from the "true" frequency due to PSoC IMO clock accuracy (~3%). The KIT-095 is a budget board, which has no reference XTAL, as a result, the actual MASTER_CLK frequency deviates from the value reported on the screen, and measured frequency will differ from the one provided by the external generator. It is possible to to lock the master frequency of the PSoC by soldering 24MHz XTAL to the board between P15_0 and P15_1 (check manual).

To avoid this hassle, I provided test frequency from the same board using Clock_Test and a Divider. In that case, both generator clock and frequency meter clock are referenced to the same MASTER_CLK, so the frequency reading must be exact. The lowest frequency the Clock_Test can provide is 24MHz / 65535 = 366.2 Hz. To go lower, a frequency Divider (or PWM) can be used. It is better to use explicit divider (e.g. MASTER_CLK/24000) instead of letting PSoC to find closest divider for your frequency.

Another way to improve reference clock accuracy is to use USBUART component instead of UART. Then PSoC cab be configured to use the 48MHz clock from USB bus, so that MASTER_CLK accuracy will be 0.25%. This approach is useful if KIT-059 is being used as an external dongle for PC.

/odissey1

P.S. What board did you use with PSoC4 frequency counter?

0 Likes
Anonymous
Not applicable

Hello odessy1

Yes, I'm measuring an external frequency of a signal generator, because the utility I want to give is to measure a square signal from 10Hz to 1Khz with a resolution of at least 0.5Hz, something I have already done in an arduino mega with a resolution of 0.1Hz and a psoc 4100PS with a resolution of 0.5Hz I imagined that with the 5LP would gain in resolution and ease of design but I see that not.

I understand what you say about the precision of the internal oscillator of the psoc, although in the Psoc creator itself says to be of a "+-1% accuracy", even so I do not understand how with the CY8CKIT-147 which is a 4100PS which I think has no external oscillator, I can get much more precision in frequency measurements, with a more basic hardware configuration and a code a little more elaborate as you can see I'm even doing two measurements "simultaneously" without much trouble,  I have tried to move the project from 4100 to 5LP but we have the problem that the blocks and code are incompatible with 5LP, and with the resolution I have now in this project is not enough and I get angry to think that if with 4100Ps I could do it because with the 5LP that supposed mind is better we are not able, with this one thinks that instead of improving it seems that cypress is worse in its developments making increasingly difficult something that before was done more simply. If any of the Great Engineers of cypress can give solution to this problem.

41_codigo.JPG41_codigo1.JPG

4100ps.JPGpsoc_relog.JPG

0 Likes

First of all: resolution != accuracy. Its perfectly possible to measure a frequency to 0.1 of even 0.01Hz resolution / precision. Precision just tells you how many digits your result has. But accuracy means how exact to reality your result is. And when your reference frequency is only accurate to 1%, a frequency of 1000Hz has only three guaranteed correct digits - no matter how your resolution is, its +-10Hz.

Which means if you measure 1000Hz, with a reference clock accurate to 1%, expect to have the result change from 990 up to 1010Hz. This would be perfectly OK. IN the case of your PSoC4100 you probably were just lucky that the internal clock was accurate enough.

Your resolution, OTOH, depends only on the gate time. With the example above, the gate time is 1s, so the resolution is 1Hz. Increase the gate time to 10s, the resolution will be 0.1Hz.

Since your measurement range involves only low frequencies, it would be better to use the 'period measurement' technique. Use the input signal as gate, and count your reference frequency. That way, you can easily get 0.1Hz resolution for a 1kHz signal, using a 10MHz reference clock (since you can count to 10000 in one period of the input signal).

0 Likes
Anonymous
Not applicable

Hola, user_78878863, thank you for your comment.

I agree with you that is not the same resolution that precision that I already have clear, I need is resolution of 0.1Hz, Accuracy "is an indication of the correctness of a measurement" better than 0.5Hz.

The other issue is that saying "I've been lucky" with the accuaracy 4100 is little to say because what you can think is cypress manufactures its microchips in an unprofessional way leaving margins of error higher than those specified in their product sheets. that "you've been lucky" does not seem to me a good slogan for a company like cypress.

Won't it be a question of the code or the optimization of the block rather than luck?

I find interesting the solution you propose "'period measurement technique", could you give me some example that works and I can try in my 5LP?

Thank you very much for your help.

0 Likes

I'm not a Cypress employee (basically none of the users here is). Even if I would be, please be polite.

Cypress specifies the accuracy of their internal oscillator. Usually this is then a gaussian distribution with guaranteed limits - most of the chips will be quite accurate, but some (sometimes many) can be at the 1% range. Its just random where a specific chips is in this distribution - hence my reference to 'luck' when you said your PSoC4100 was more accurate than the 5LP.

You can, btw., test this by yourself - just route the internal clock to a pin an measure it. If its outside of the spec, you can accuse Cypress of delivering out-of-spec chips.

Until then, when your frequency measurement is more off than it should be (according to the accuracy of the internal clock), you should assume that you have a bug in your project (having build more than one frequency counter with PSoC, I know how easy it is to do something wrong).

When you need a resolution of 0.1Hz, you need to have a gate time of 10s (using a gated timer).

For the period measurement: the circuit is basically the same, but you use the input signal as gate (which was the PWM before), and use a reference clock as input signal to the counter. The rest is just math.

0 Likes

grantorax,

It is my understanding that the PSoC5 board you used has no XTAL. There are two options: 1. it is possible to trim IMO clock frequency to bring it to e.g. exactly 3MHz (and MASTER_CLK to 24MHz). Please check PSoC5 TRM for details. 2. I suggest simple approach: bring BUS_CLOCK frequency to some digital output pin and measure it (say, result is 23.821 MHz). Now, when calculating output frequency, adjust it by coefficient ( 23.821 / 24.0 ). Best solution: buy PSoC5 FreeSoc2 board (Sparkfun), which has XTAL already installed.

/odissey1

0 Likes

grantorax,

also you can easily stabilize IMO frequency on KIT-059 to within 0.07% (see e.g. Digikey 490-17946-1-ND ) by using external ceramic resonator, attached to P15_0 and P15_1 (see pics below) - no capacitors required. The ceramic resonators available at 0.07% accuracy with pitch 0.05", which can be directly soldered between pins on the KIT-059 board. I happened to have 4.18MHz ceramic resonator of radial form-factor ($0.2 @ Digikey), works fine, see settings below.

/odissey1

ceramic_4.19MHz_KIT-059_1a.png 

ceramic_4.19_clocks_1a.png

ceramic_4.19MHz_1a.png

0 Likes

There is an interesting video from Hendrik Lipka:

a higher resolution frequency counter

0 Likes
Anonymous
Not applicable

Good morning, everybody,

The first thing to thank you for your help, the truth is that I am learning many things with you, thank you.

odissey1: You have reason but simple and fast it will be to obtain a good and stable frequency for the Psoc with a crystal of quartz or with an oscillator like the one that you have me for the following test I have mounted a crystal of 32.768Khz in the pins 15.2 and 15.3 and a 24Mhz crystal with its two 22pf capacitors on pins 15.0 and 15.1 I have redirected the Master_Clk and Xtal_32Khz signals to their respective output pins I have measured them with the frequency meter and the 24Mhz gave exact and the clock 32.770Khz but it may be the fault of my meter which is not very precise .... Well what I mean is that it seems that the frequencies are fine.

Evgeniy Pavlov: thank you very much for that example looks very good project, I had seen it on youtube but did not have the project, now I have modified it for the 5LP and tested it with the additions of the crystals as in the original project, but I have not been able to measure any frequency nor get any signal at the output of the DAC, the only thing I see is the image and I do not know why, I measured the main frequency of 24Mhz and 32.77Khz and are correct, proposals are accepted ....

psoc_fre.JPG

0 Likes

Let me remind you: this is an old project there is not in it    isr_xxx_StartEx(...)

You will have to repeat the additions to the interrupt text for PSoC5 by copying from PSoC3 files

isr_InputOverflow.c       CY_ISR(isr_InputOverflow_Interrupt)  {...here..}

isr_RefOverflow.c         CY_ISR(isr_RefOverflow_Interrupt)    {...here..}

isr_InputStop.c         CY_ISR(isr_InputStop_Interrupt)

at the beginning of these files add:

extern int status;

0 Likes

As the author of that project: you need to be careful with the hardware project. This was written for PSoC5, not 5LP, and the 5LP (and newer versions of Creator) are more strict. You will get some warnings about timing / sync problems, so you need sync block at the right places.

I should look into this and upload the current version of this, it seems to be a looked-after project.

The '1001' in the output means 'STATUS_REF_OVERFLOW' which means 'input frequency too low'. So at least 'isr_RefOverflow.c' is still correct, and this event gets triggered.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hello

Evgeniy Pavlov: yes , if I believe that I have modified all the files of the interruptions as in the example.

user_78878863: Thank you for your important help, I find a very interesting project by how you measure the frequency using reciprocal counter, here attached what I could modify of your project to make it work the truth is that the part that interests me is the frequency meter, all help will be well received do you know with what accuracy and resolucion works your project?

You use two crystals one of 24Mhz and another one for the clock of 32.768Khz this last one I believe that you don't use it in the frequency meter, isn't it?

Greetings.

0 Likes

The precision of a reciprocal counter is: input_frequency / reference_counter_period. So for 1kHz, and a 16bit counter, its 1000/65536=0.015Hz.

The 32kHz crystal is only used for a test signal, its not needed for the counter as such 8IIRC). But when this crystal is missing you have no test signal.

0 Likes
Anonymous
Not applicable

using the example that user_342122993 has uploaded and with the modification of the XTAL_24Mhz, that has suggested me odissey1 that is to say using an external crystal with two capacitors of 22pf and adjusting the oscillators of the Psoc I have obtained much more stability in the measure of frequency and a resolution of +-1Hz up to 6Khz what has improved much with respect to using it without external oscillator, the only thing is that I don't know how to show more precision in the measure so that I can show xxx.x with an accuarycy of +-0.2Hz

user_78878863ok, If I cut it in half, I get twice as much precision.?

Thanks you.

0 Likes

What do you mean with "cut in half"?

For a reciprocal counter, the precision is always a percentage of the frequency (so the absolute value in Hz changes). To get higher precision, increase the period of the counters (counters with more bits)

For a gate-based frequency counter, its always a fixed value in Hz (but its ratio to the input frequency changes), and it depends. To increase the precision, make the gate time higher.

0 Likes
Anonymous
Not applicable

Hi.

I mean if 1000/65536 = 0.015Hz then 500/65536= 0.0076Hz?

Thanks you.

0 Likes

Yes, thats what I meant to explain.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

hello

I continue testing with the example of the frequency meter and now I have duplicated the circuit so that I can measure two independent frequencies "freq - rpm", and as you can see in the images exchanging the frequencies you can see that the second meter "rpm" is measuring wrong, and I see nothing wrong I tried with different priority and the same in the interruptions of the counters but without improvements, what I think is that the sprintf function and the printed output series are very slow and that makes the counter does not "count" correctly, I appreciate if you have any way to solve it.

5lp_2timer.JPG

5lp_2timer_1.JPG

0 Likes

Read the capture registers in the ISR. This should not actually make a difference (since the capture is a one-time event), but it might help. I had similar issues in the past where it seemed that the capture values changed afterwards.

(And if you do so, mark the variables holding the values as 'volatile')

0 Likes
lock attach
Attachments are accessible only for community members.

grantorax,

Attached is demo project showing frequency measurement using custom 24-bit reciprocal counter. This is a draft version of component, provided as-is, no documentation, testing and debugging is required. To save resources design utilizes only one 24-bit Counter and a Timer. Frequency range as shown 8Hz - 12MHz, result accuracy ~10^6 in entire frequency range independent from polling time (limitations apply), which can be varied from 0.01 to 2.55 sec.

Attached is a project with embedded component (FreqMeter). Project is build on CYKIT-059 board with external 24MHz XTAL oscillator attached. Project utilized additional custom components (DDS32 and QuadDec_SW) for test frequency generation, and KIT-059 library for annotation. These components are not required if external frequency source is available.

/odissey1

P.S. Attached updated version (_02) and version with extra libraries stripped off (no signal generator).

DDS32:  DDS24: 24-bit DDS arbitrary frequency generator component

QuadDec_SW:  Quad Decoder with Button Switch component for rotary shaft encoders

KIT-059:  Annotation library for CY8CKIT-059 Prototyping Kit

Frequency meter_2x_1a.png

Frequency meter_2x_5a.png

Frequency meter_2x_4a.png

Frequency meter_2x_4b.pngFrequency meter_2x_3a.png

0 Likes
Anonymous
Not applicable

Hello user_78878863

Thank you very much for devoting your time to spend your magnificent work to PSOC 5LP I have tested it and tell you that it works very well, but you need external signal generator for ijecto a frequency into a Ref_Clock pin, can i use a external crystal oscillator expl: 12Mhz and use Xtal frequency to input this pin? and REF_PERIOD is a 1/13000000.0 ? in ms seconds?, and thank you for your help, Thank you.

Hello user_342122993

I really have to say that I was impressed with your reciprocal frequency meter project and how well explained you have it, I have been able to test it right now, and it works well, plus the accuracy and resolution are excellent much better than I need, in short "excellent".

The only problem I see is that if in freq1=400hz and I put a frequency lower than Freq2=600hz I see that the psoc does not give the data of Ferq1 until 15 or 20 Freq2 cycles, but if I keep Freq1 > Freq2 everything goes well, it is as if Freq2 had preference and that the priorities of the interruptions are at 7 all, maybe this is the reason?

apart from this I am seeing the code FreqMeetr_1.c and I see that these at many levels above what I know of the Psoc .... which I am very happy to have people like you in this forum is a great help for beginners like me, just return to thank you for the time you are devoting to me, Thank you.

I have a doubt, there is some function in Psoc to create a periodical task as for example to execute a function or to read a data of periodical form every x time without using hardware components and that is not blocking...

0 Likes

grantorax,

I updated FreqMeter project files (above) to include SmallSync component into the project. Attached also is a project without frequency generator component (only FreqMeter) for easy start, this project needs external signal generator to operate.

The FreqMeter uses software timer using interrupt to poll Counter on regular intervals (timing is not critical here as it provides the minimal acquisition time). Take a look on its API *.c file for implementation (100Hz clock - isr - count++) .

>The only problem I see is that if in freq1=400hz and I put a frequency lower than Freq2=600hz I see that the psoc does not give the data of Ferq1 until 15 or 20 Freq2 cycles, but if I keep Freq1 > Freq2 everything goes well, it is as if Freq2 had preference and that the priorities of the interruptions are at 7 all, maybe this is the reason?

- The component is not 100% tested, I will look into it. I see some slow response on the edge of the low frequency limit (~6-8 Hz) when Timer overflows. Theoretically, the interrupt timing is not important here, as both the Timer and the Counter are latching the counter value in the hardware registers, so you have plenty of time to retrieve the data before the next period (~0.5 sec). Something to research.

/odissey1

P.S. The user_78878863 name is Hendrik Lipka, the author of the original reciprocal counter project.

0 Likes

grantorax,

>The only problem I see is that if in freq1=400hz and I put a frequency lower than Freq2=600hz I see that the psoc does not give the data of Ferq1 until 15 or 20 Freq2 cycles, but if I keep Freq1 > Freq2 everything goes well, it is as if Freq2 had preference and that the priorities of the interruptions are at 7 all, maybe this is the reason?

-I also see similar behavior when INP1 and INP2 are adjacent. Since input pins are set to high impedance digital, there is strong cross-talk between them. As soon I move inputs far apart (e.g. INP1 to Port3 and INP2 to Port0) the cross-talk disappears and all data displayed correctly. Alternatively, try to put some 5-10K resistors to the GND on inputs 1 and 2. This should solve issue. 

/odissey1

0 Likes

My project has some documentation in the main.c file. You can change the reference clock to another frequency as you wish, there is just a constant in the code defining which value it is. Changing it will affect the refresh speed and the maximum / minimum measurable frequency. When you want you can also route this signal to an internal clock.

0 Likes

Cool work coming up with that component. When I'm back home from my business trap I will take a deeper look at it. (although from a look at the component schematic it does look like a regular gated counter).

0 Likes

hli,

it is basic implementation of the reciprocal counter. I didn't test/debug it thoroughly (much more work needed), so can not comment on the difference with your implementation. So far I noticed that the Timer does not work in 32-bit mode.

To save resources, I eliminated PWM window gate, instead used an interrupt with a crude software counter to set counting window time minimum. This woks, but the minimum frequency has doubled, as compared to PWM gate. For example, if with PWM it was 4Hz, now it is 8Hz.

/odissey1

0 Likes

I added an updated project for the PSoC5LP to https://hendriklipka.de/hardware/freqcounter.html . It still targets the -050 kit, but it should be easy to target the -059 kit (or change the external reference clock to the internal one).

0 Likes
JoSi_3317786
Level 2
Level 2
First like received First like given

It's a part of my project (measuring the water flow from a sensor and the RPM from a hall sensor)

Dividing the input signal (flow_sensor) and and measuring its period, I can achieve up to 2 ppm of error up to 999Hz

A high frequency prescaler gives a high resolution too.flow.jpg

0 Likes