Testing PWM with a speaker

Dependencies:   mbed

Committer:
bcostm
Date:
Thu Feb 20 14:09:54 2014 +0000
Revision:
0:b82c05c12d48
Child:
1:aa7cd19c6a4f
Initial version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 0:b82c05c12d48 1 #include "mbed.h"
bcostm 0:b82c05c12d48 2
bcostm 0:b82c05c12d48 3 DigitalOut my_led(LED1);
bcostm 0:b82c05c12d48 4 InterruptIn my_button(USER_BUTTON);
bcostm 0:b82c05c12d48 5 PwmOut my_pwm(PB_3);
bcostm 0:b82c05c12d48 6
bcostm 0:b82c05c12d48 7 void pressed() {
bcostm 0:b82c05c12d48 8 if (my_pwm.read() == 0.25) {
bcostm 0:b82c05c12d48 9 my_pwm.write(0.75);
bcostm 0:b82c05c12d48 10 }
bcostm 0:b82c05c12d48 11 else {
bcostm 0:b82c05c12d48 12 my_pwm.write(0.25);
bcostm 0:b82c05c12d48 13 }
bcostm 0:b82c05c12d48 14 }
bcostm 0:b82c05c12d48 15
bcostm 0:b82c05c12d48 16 int main()
bcostm 0:b82c05c12d48 17 {
bcostm 0:b82c05c12d48 18 // Set PWM
bcostm 0:b82c05c12d48 19 my_pwm.period_ms(10);
bcostm 0:b82c05c12d48 20 my_pwm.write(0.5);
bcostm 0:b82c05c12d48 21
bcostm 0:b82c05c12d48 22 // Set button
bcostm 0:b82c05c12d48 23 my_button.fall(&pressed);
bcostm 0:b82c05c12d48 24
bcostm 0:b82c05c12d48 25 while (1) {
bcostm 0:b82c05c12d48 26 my_led = !my_led;
bcostm 0:b82c05c12d48 27 wait(0.5); // 500 ms
bcostm 0:b82c05c12d48 28 }
bcostm 0:b82c05c12d48 29 }