Istvan Cserny
/
03_pwm_music
Pulse Width Modulation
/ 03_pwm_music
- This program plays the Oranges and Lemons music
- on a piezo buzzer tied between D3 (PTA12) pin and GND
- Hardware requirements:
- - FRDM-KL25Z board
- - Piezo buzzer tied between D3 (PTA12) pin and GND
- Note: The + pin of the buzzer connects to D3, of course...
- This is an example program borrowed from the
- Pulse Width Modulation section of ARM mbed Course Material
- Authors: Rob Toulson and Tim Wilmshurst
- Link: https://developer.mbed.org/cookbook/Course-Notes
- /
main.cpp@0:7d30529dcdc6, 2015-10-21 (annotated)
- Committer:
- icserny
- Date:
- Wed Oct 21 11:51:16 2015 +0000
- Revision:
- 0:7d30529dcdc6
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icserny | 0:7d30529dcdc6 | 1 | /** 03_pwm_music |
icserny | 0:7d30529dcdc6 | 2 | * This program plays the Oranges and Lemons music |
icserny | 0:7d30529dcdc6 | 3 | * on a piezo buzzer tied between D3 (PTA12) pin and GND |
icserny | 0:7d30529dcdc6 | 4 | * |
icserny | 0:7d30529dcdc6 | 5 | * Hardware requirements: |
icserny | 0:7d30529dcdc6 | 6 | * - FRDM-KL25Z board |
icserny | 0:7d30529dcdc6 | 7 | * - Piezo buzzer tied between D3 (PTA12) pin and GND |
icserny | 0:7d30529dcdc6 | 8 | * Note: The + pin of the buzzer connects to D3, of course... |
icserny | 0:7d30529dcdc6 | 9 | * |
icserny | 0:7d30529dcdc6 | 10 | * This is an example program borrowed from the |
icserny | 0:7d30529dcdc6 | 11 | * Pulse Width Modulation section of ARM mbed Course Material |
icserny | 0:7d30529dcdc6 | 12 | * Authors: Rob Toulson and Tim Wilmshurst |
icserny | 0:7d30529dcdc6 | 13 | * Link: https://developer.mbed.org/cookbook/Course-Notes |
icserny | 0:7d30529dcdc6 | 14 | */ |
icserny | 0:7d30529dcdc6 | 15 | #include "mbed.h" |
icserny | 0:7d30529dcdc6 | 16 | PwmOut buzzer(D3); |
icserny | 0:7d30529dcdc6 | 17 | float frequency[]={659,554,659,554,550,494,554,587,494,659,554,440}; //frequency array |
icserny | 0:7d30529dcdc6 | 18 | float beat[]={1,1,1,1,1,0.5,0.5,1,1,1,1,2}; //beat array |
icserny | 0:7d30529dcdc6 | 19 | |
icserny | 0:7d30529dcdc6 | 20 | int main() { |
icserny | 0:7d30529dcdc6 | 21 | while (1) { |
icserny | 0:7d30529dcdc6 | 22 | for (int i=0; i<=11; i++) { |
icserny | 0:7d30529dcdc6 | 23 | buzzer.period(1/(frequency[i])); // set PWM period |
icserny | 0:7d30529dcdc6 | 24 | buzzer=0.5; // set duty cycle |
icserny | 0:7d30529dcdc6 | 25 | wait(0.5*beat[i]); // hold for beat period |
icserny | 0:7d30529dcdc6 | 26 | } |
icserny | 0:7d30529dcdc6 | 27 | } |
icserny | 0:7d30529dcdc6 | 28 | } |