You are viewing an older revision! See the latest version
Accurately measuring duty cycle
Specification¶
- To measure the average duty cycle in a PWM/pulse train
- To accept a minimum high of 3uS
- To measure to the highest accuracy easily attainable with the mbed
- To measure signals with frequency 1Hz - 100kHz
Implementation¶
The LPC1768 (and indeed many others) include Counter/Timer peripheral blocks which make this task rather easy. They have the facility to capture the timer value on certain pin toggle events (the CAPn.0/1 inputs). The timer can be configured to count up by ones every clock; record when the signal goes low and record when it goes high again, and have interrupts on either of these events.
Unfortunately the two capture functions (cap 1 and 0) can only work on separate pins (with a register for storing the value for each) so to measure both high and low going times one is required to short two pins together. In this example we are using Timer 2 and therefore pins 30 and 29. To test pin 23 was used for generating a PWM signal of various frequencies.
A class was written to do the heavy lifting: PWMAverage
Import library
Public Member Functions |
|
| PWMAverage (PinName cap0, PinName cap1) | |
|
Create a
PWMAverage
object.
|
|
| void | reset () |
|
Reset the counters and values.
|
|
| void | start () |
|
Start the timer.
|
|
| void | stop () |
|
Stop the timer.
|
|
| float | read () |
|
Read the duty cycle measured.
|
|
| double | avg_up () |
|
Read the average length of time the signal was high per cycle in seconds.
|
|
| float | avg_UP () |
|
Read the average length of time the signal was high per cycle in seconds.
|
|
| double | avg_down () |
|
Read the average length of time the signal was low per cycle in seconds.
|
|
| double | period () |
|
Read the average period in seconds.
|
|
| int | count () |
|
Read the number of cycles counted over.
|
|
Here is a test program:
Import programPWMAverage_test
A test program for the PWMAverage library. This program will print the average duty cycle of a signal (1Hz-100kHz) after a button is pressed for a few seconds.
