Pulse Width Modulation

Dependencies:   mbed

/ 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
  • /

Files at this revision

API Documentation at this revision

Comitter:
icserny
Date:
Wed Oct 21 11:51:16 2015 +0000
Commit message:
First version

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Oct 21 11:51:16 2015 +0000
@@ -0,0 +1,28 @@
+/** 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
+ */ 
+#include "mbed.h"
+PwmOut buzzer(D3);
+float frequency[]={659,554,659,554,550,494,554,587,494,659,554,440}; //frequency array
+float beat[]={1,1,1,1,1,0.5,0.5,1,1,1,1,2};     //beat array
+
+int main() {
+    while (1) {
+        for (int i=0; i<=11; i++) {
+            buzzer.period(1/(frequency[i]));    // set PWM period
+            buzzer=0.5;                         // set duty cycle
+            wait(0.5*beat[i]);                  // hold for beat period
+        }
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Oct 21 11:51:16 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/34e6b704fe68
\ No newline at end of file