![](/media/cache/profiles/d3b154a6afe99c7d4f2b3fa23962a134.jpg.50x50_q85.png)
Testing PWM with a speaker
main.cpp@0:b82c05c12d48, 2014-02-20 (annotated)
- 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?
User | Revision | Line number | New 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 | } |