by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
main.cpp@0:1b9092423ff2, 2012-08-31 (annotated)
- Committer:
- robt
- Date:
- Fri Aug 31 16:08:26 2012 +0000
- Revision:
- 0:1b9092423ff2
by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robt | 0:1b9092423ff2 | 1 | /*Program Example 4.7: Plays the tune "Oranges and Lemons" on a piezo buzzer, using PWM |
robt | 0:1b9092423ff2 | 2 | */ |
robt | 0:1b9092423ff2 | 3 | #include "mbed.h" |
robt | 0:1b9092423ff2 | 4 | PwmOut buzzer(p21); |
robt | 0:1b9092423ff2 | 5 | |
robt | 0:1b9092423ff2 | 6 | //frequency array |
robt | 0:1b9092423ff2 | 7 | float frequency[]= {659,554,659,554,440,494,554,587,494,659,554,440}; |
robt | 0:1b9092423ff2 | 8 | float beat[]= {1,1,1,1,1,0.5,0.5,1,1,1,1,2}; //beat array |
robt | 0:1b9092423ff2 | 9 | int main() |
robt | 0:1b9092423ff2 | 10 | { |
robt | 0:1b9092423ff2 | 11 | while (1) { |
robt | 0:1b9092423ff2 | 12 | for (int i=0; i<=11; i++) { |
robt | 0:1b9092423ff2 | 13 | buzzer.period(1/(2*frequency[i])); // set PWM period |
robt | 0:1b9092423ff2 | 14 | buzzer=0.5; // set duty cycle |
robt | 0:1b9092423ff2 | 15 | wait(0.4*beat[i]); // hold for beat period |
robt | 0:1b9092423ff2 | 16 | } |
robt | 0:1b9092423ff2 | 17 | } |
robt | 0:1b9092423ff2 | 18 | } |
robt | 0:1b9092423ff2 | 19 |