I am trying to write a program for a PIC24F mcu that can measure the duty cycle of a pulse width modulated input signal. Has anyone done this? What would be the best approach?
It may depend on exactly which PIC24 part you are using, but some and possibly all PIC24 parts include timer hardware with input capture capability (check your part's data sheet). When configured for input capture, this will copy the timer counter value to a register in an input transition, and then generate an interrupt. Typically, in the interrupt handler, you would copy the input capture register and set the input capture up for the next transition, once you have the first three transitions, you can calculate the duty-cycle, and thereafter update it on every transition, or with perhaps a little less complexity every other transition.
There is a simpler possibility for this problem if you have:
- a spare ADC which suits your meassurement precision requirements
- room/money for this circuit (there might be simpler ones)
Then just meassure the output voltage which is linearly dependent on your duty cycle.