Yasushi TAUCHI
/
Sound_PWM_2
PWMによる音だし(疑似Sin波)
main.cpp@0:5a95bead7a47, 2011-12-26 (annotated)
- Committer:
- yueee_yt
- Date:
- Mon Dec 26 12:58:19 2011 +0000
- Revision:
- 0:5a95bead7a47
- Child:
- 1:e1919d5190b3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yueee_yt | 0:5a95bead7a47 | 1 | #define mC 261.626 |
yueee_yt | 0:5a95bead7a47 | 2 | #define mD 293.665 |
yueee_yt | 0:5a95bead7a47 | 3 | #define mE 329.628 |
yueee_yt | 0:5a95bead7a47 | 4 | #define mF 349.228 |
yueee_yt | 0:5a95bead7a47 | 5 | #define mG 391.995 |
yueee_yt | 0:5a95bead7a47 | 6 | #define mA 440.000 |
yueee_yt | 0:5a95bead7a47 | 7 | #define mB 493.883 |
yueee_yt | 0:5a95bead7a47 | 8 | |
yueee_yt | 0:5a95bead7a47 | 9 | #include "mbed.h" |
yueee_yt | 0:5a95bead7a47 | 10 | |
yueee_yt | 0:5a95bead7a47 | 11 | Ticker timer; |
yueee_yt | 0:5a95bead7a47 | 12 | PwmOut sp1(p25); |
yueee_yt | 0:5a95bead7a47 | 13 | float ms[180]; |
yueee_yt | 0:5a95bead7a47 | 14 | float m1; |
yueee_yt | 0:5a95bead7a47 | 15 | |
yueee_yt | 0:5a95bead7a47 | 16 | void sound_out(void) { |
yueee_yt | 0:5a95bead7a47 | 17 | static float j=0; |
yueee_yt | 0:5a95bead7a47 | 18 | j=j+m1; |
yueee_yt | 0:5a95bead7a47 | 19 | if(j>180)j=j-180; |
yueee_yt | 0:5a95bead7a47 | 20 | sp1.write(ms[(int)j]); |
yueee_yt | 0:5a95bead7a47 | 21 | } |
yueee_yt | 0:5a95bead7a47 | 22 | |
yueee_yt | 0:5a95bead7a47 | 23 | int main() { |
yueee_yt | 0:5a95bead7a47 | 24 | float mm[]={mC,mD,mE,mF,mG,mA,mB,mC*2}; |
yueee_yt | 0:5a95bead7a47 | 25 | int i; |
yueee_yt | 0:5a95bead7a47 | 26 | for (i=0;i<180;i++) { |
yueee_yt | 0:5a95bead7a47 | 27 | ms[i]=sin(2*3.1415*(float)i/180.0)/2.0+0.5; |
yueee_yt | 0:5a95bead7a47 | 28 | } |
yueee_yt | 0:5a95bead7a47 | 29 | |
yueee_yt | 0:5a95bead7a47 | 30 | sp1.period_us(10); |
yueee_yt | 0:5a95bead7a47 | 31 | |
yueee_yt | 0:5a95bead7a47 | 32 | for (i=0;i<sizeof(mm);i++) { |
yueee_yt | 0:5a95bead7a47 | 33 | m1=mm[i]*180/10000; |
yueee_yt | 0:5a95bead7a47 | 34 | timer.attach_us(&sound_out,100); |
yueee_yt | 0:5a95bead7a47 | 35 | wait(0.5f); |
yueee_yt | 0:5a95bead7a47 | 36 | } |
yueee_yt | 0:5a95bead7a47 | 37 | sp1.write(0.0f); |
yueee_yt | 0:5a95bead7a47 | 38 | while (1); |
yueee_yt | 0:5a95bead7a47 | 39 | } |