/////////////////////////////////// // // program_10 project // // created 7/13/21 by tj // rev 0 // /////////////////////////////////// // // Use PWM to generate notes for duration // // . // /////////////////////////////////// #include "mbed.h" #include #define DUTY 0.5 // 50% #define T_WAIT 5000000 // in us - 0.5s // function prototypes void get_input(int * freq, int * length); void output_note(int per, int length); // Global HARDWARE Objects // Create a PWM object to drive pin D7 PwmOut Audio_Out(D7); int main(void){ setbuf(stdout, NULL); // splash printf("program_10 - example for EE2905\n"); printf("Using Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); // working variables int frequency; // hz int period; // us int duration; // ms // setup PWM Audio_Out.write(DUTY); // continuously check the reostat and update the PWM while(1){ // get input get_input(&frequency, &duration); // convert freq to period in us period = 1000000/frequency; //update the PWM output_note(period, duration); }// end while return 0; }// end main void get_input(int * freq, int * length){ // get values printf("Please enter a frequency in Hz and a duration in us: "); scanf("%i %i", freq, length); return; }// end get_input void output_note(int per, int length){ Audio_Out.resume(); Audio_Out.period_us(per); wait_us(length); Audio_Out.suspend(); return; }// end output_note