PWMによる音だし(疑似Sin波)

Dependencies:   mbed

Revision:
0:5a95bead7a47
Child:
1:e1919d5190b3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Dec 26 12:58:19 2011 +0000
@@ -0,0 +1,39 @@
+#define mC 261.626
+#define mD 293.665
+#define mE 329.628
+#define mF 349.228
+#define mG 391.995
+#define mA 440.000
+#define mB 493.883
+
+#include "mbed.h"
+
+Ticker timer;
+PwmOut sp1(p25);
+float ms[180];
+float m1;
+
+void sound_out(void) {
+    static float j=0; 
+    j=j+m1;
+    if(j>180)j=j-180;
+    sp1.write(ms[(int)j]);
+}
+
+int main() {
+    float mm[]={mC,mD,mE,mF,mG,mA,mB,mC*2};
+    int i;
+    for (i=0;i<180;i++) {
+        ms[i]=sin(2*3.1415*(float)i/180.0)/2.0+0.5;
+    }
+
+    sp1.period_us(10); 
+
+    for (i=0;i<sizeof(mm);i++) {
+        m1=mm[i]*180/10000;
+        timer.attach_us(&sound_out,100);
+        wait(0.5f);
+    }
+    sp1.write(0.0f);
+    while (1);
+}