I am trying to measure frequency of an input analog signal using Comparator (to create digital signal) that feeds into a counter. I am also utilizing a window PWM as capture to the counter.
However, it doesn't show the correct frequency when I connect AC_in and AC_ref to a function generator that produces 50Hz sine wave. I am also using a limiting and biasing external circuit as shown in my Top Design.
It works well with only square wave. I have attached my project below.
Welcome in the forum.
Your counter is not reset after one measure, so the next time you will read 100 as captured value.
Better is: use measure_StartEx() API and provide your own handler in your own file. That avoids editing in generated files.
As a general rule: Global variables that get changed in an interrupt handler must be declared as volatile.
You get design errors because you have put the comparator pin connections at the wrong end of your schematic components:
The pin ends the PSoC and your caps and diodes are outside the PSoC. Confirm that outside connections are drawn in blue lines.
Thank you for your suggestions!
My counter is reset at the rising edge of my capture (which comes from my generated window PWM signal).
I put those external connections there as a reference, I will correct them to the right end.
My problem is however when the connections are in the right order (as you suggested), and I drive my Sine wave signal through, it doesn't show the correct frequency. It shows random values from 01 to 99 on my LCD screen.
It does on the other hand, work well with square wave signals.
What does your comp_test output look like on a logic analyzer? Can there be too much noise on your sine output of function generator?
I have attached the screen from my oscilloscope.
Channel 1 (Yellow) is my input signal which has mean at 2.5V and peak to peak 1.3V
Channel 2 (Green) is the comparator signal.
Both of them have 50 Hz frequency. I have used hysteresis in my comparator to account for some of the noise.
The funny thing is that it now works okay for frequencies ranging from 0 - 60 Hz after I change my input reference pin from 3.1 to 3.2 on Psoc. Higher frequencies couldn't work as reliably.
Does using R2 help to increase the hysteresis? Or do we have to do that through API?
What does increasing counter period do? My signal is only 50-100 Hz so the counter doesnot have to count that much?
I am just using CY8CKIT-059 PSoC® 5LP
I have tried to look at the datasheet for this device and couldn't find the section that has pins and bypass capacitors. Can you refer me to that section?
Would it not matter anyways because I am passing a DC reference input into pin 3.2 so a bypass capacitor would just smooth out the signal?
I found some issues with your program in the area of the matching network on AC in . Resistor R_1 is shorted out by wire from AC_in to the Diode D-1. This entire network is not connected correctly. Pavloven example above is done correctly. I also found an error in your interrupt routine it had two errors. I have commented these and you will need to fix it. As a standard practice you should not make changes to Auto generated parts of the Program.