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.

Dependencies:   PWMAverage mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Measure and print the average duty cycle of a signal connected to p29 and p30 (together) while the button (p16) is pulled high
00002 
00003 #include "mbed.h"
00004 #include "PWMAverage.h"
00005 
00006 DigitalOut myled(LED1);
00007 
00008 PWMAverage pa(p29,p30);
00009 
00010 DigitalIn button (p16);
00011 
00012 Timer tmr;
00013 
00014 int main()
00015 {
00016     button.mode(PullDown);
00017     while(1)
00018     {
00019         pa.reset();
00020     
00021         while (!button) {}
00022         pa.start();
00023         tmr.start();
00024         myled=1;
00025     
00026         while (button) {}
00027         pa.stop();
00028         tmr.stop();
00029         myled=0;
00030     
00031         printf("Average dudy cycle over %d us was %.4f\n\r",tmr.read_us(),pa.read());
00032     }
00033 }