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 on 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 wire 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.

Of course it was necessary to test the performance of the library: To do this a small test program (using a slightly modified library admittedly: the count_, total and totalup member variables were moved to public access) was written. This used mbeds on-board PWM generation to characterize the stability and accuracy of the system:

Import programpwm_duty_measurement

A program which test the PWMAverage library internally: just connect pins 23, 29 and 30 short and check it on the terminal

The results show that accuracy varies, however shows pretty good accuracy in the default configuration of the test program: PWM at 100kHz. Study shows that re-coding the ISR in assembler may be a good idea, as this seems to vary in length slightly in use. This is the only cause of inaccuracy (other than rounding errors) in the program. If 10ppm or less accuracy was needed, re-writing the ISR in assembler would be essential.


All wikipages