I don't manage to find a solution to measure the duty cycle in a PWM signal . I have to measure firstly a constant duty cycle for one signale, secondly i have to treat a signal with variable duty cycle ( such as a sine turned into a PWM modulation) ? How to solve the problem ?
Easiest approach will be to use a timer that counts on a given internal frequency. The capture input of the timer will save the actual count which you can read-out in a connected interrupt where you reset the timer again.
When using two of these construct you are able to measure both the high-time and low-time of your signal.
Something like this ? D produces a high pulse = period width of PWM inout, its ISR
reads and resets counter.
Set capture on - edge, with ISR enabled in component, capture width
of PWM high time when ISR fires and capture register read.
Resolution clk show = 1 uS measurement resolution, 16 bit counter
can count a period up to ~65K us.
I think you can replace D and its inverter with a T F-F, just did not look at this.
hello Dana , i need the PWM measurement too. I will give your schematic a try, but i miss some detail info about the proper Counter configutration.
maybe you even have this as little creator project at hand to share. b.r. Markus
I have a project, but not working. Seems interrupts not working the way
I wanted, so stumped at the moment, and today have other commitments
so cannot work on it.
The project is PSOC 3 based, runing on -030 board, so you would have to
change device, and should compile into PSOC 5 OK, or so I think.
Note I fixed all re-entrant errors in project, so on PSOC 3 compiles w / no
errors or warnings.
thanks Dana for sharing your project.
i did porting to kit LP050 and some code workaround (and both ISR).
but results are still not the way i want. 😞
i have no possiblity to change PeriodISR to falling edge as mentioned.
the result i want to get for lcd is:1st line: D.C.xx.yy %
2ndline : Frequency:xx.yy kHz
can you pimp my projectcode for doing so ?
my test signal is TTL 1.02kHz fixed frequency and 0.01 to 99.90% variable duty cycle
from a 2D-rotation hallsensor(360° in 12bit-PWM_angle resolution).
need the correct duty-cylcle value for further calculations of position-change speed..
The period ISR taken off the D output, then inverted, is effectively falling
edge, end of the period. The D high time, or its low time for that matter, either width, is =
period of the input signal. So you set the period ISR to rising edge, the D and inverter
take care of the rest.
Attached is a basic project you can modify to suit your needs. Measures pulse width
and duty cycle. It does this not on every input cycle as it is not a pure HW approach
and I chose to do it with one timer. With two timers you could do it on a per cycle basis,
or use Verilog for an optimal; solution.
hello dear dana,
thank you for sharing your simplified project. As it works fine on PSoC5LP so far, I like to port your latest Creator-project to
PSoC4000 ( CY8C4013SXI-410) device for having a very small and low-cost solution for wanted flow-meter application comunicating via I2C to host device (PSoC3 based).
unfortunately there is no logic inside the small PSoC400 device, so i consider to use external inverter
and two pins for inputig the PWM-test signal.
PSoC'in is challenging fun.
best regards Markus
I consider the PSoC42xx device for a more valuable flowmeter with segment LCD drive, also but later to come.
The PSoC4000 device family came to my mind, while it is the tinyest part with low-pincount (SOIC-8) Cypress offers,
also operating from same +5Volt voltage rail as Hall sensor IC.
planned PCB will have round shape with only 17mm diameter carrying a 4-Pin Molex picoblade (1.25mm) Conector, Hallsensor IC (SOIC-8), PSoC and bypass capacitors to fit the flowmeter hardware. Parts will be mounted on both sides of a doublesided PCB.
Size matters in this particular case, to keep design simple and cheap.
Otherwise i would like to use the more powerful PSoC's of course.
I dont want to use an old 8-bit PIC for new design....so i give the PSoC4000 family a try.
have a nice weekend. greetz - Markus
Possibly you could use the pin configurable interrupt (it has a falling edge
selection) to get the - edge interrupt for the high time measurement.
hello dear Dana
I gave your suggestion for the pin with interrupt generation ( falling edge ) a try, but got only ridiculous readings.
any idea what to change to make it work like your solution with inverter included ?
We recommend you to create a new thread with your query with the right key word as subject rather than adding your query as comment to an old thread. You might not get the attention from our contributors when you comment on an old thread and also we recommend to keep the discussion aligned with the question asked in the thread, so that it will be helpful for other members who are searching for answers.