Richard Kuo
/
NTOUEE-mbed-PWM_playnote
mbed PWM play note to a speaker
main.cpp@0:32bc20854e98, 2016-10-20 (annotated)
- Committer:
- rkuo2000
- Date:
- Thu Oct 20 12:41:50 2016 +0000
- Revision:
- 0:32bc20854e98
mbed PWM play note to a speaker
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rkuo2000 | 0:32bc20854e98 | 1 | // using NuMaker-PFM-NUC472 PWM0 output to a speaker |
rkuo2000 | 0:32bc20854e98 | 2 | #include "mbed.h" |
rkuo2000 | 0:32bc20854e98 | 3 | |
rkuo2000 | 0:32bc20854e98 | 4 | #define _C5 523 // C5 = 523.25Hz |
rkuo2000 | 0:32bc20854e98 | 5 | #define _D5 587 // D5 = 587.33Hz |
rkuo2000 | 0:32bc20854e98 | 6 | #define _E5 659 // E5 = 659.26Hz |
rkuo2000 | 0:32bc20854e98 | 7 | #define _F5 698 // F5 = 698.46Hz |
rkuo2000 | 0:32bc20854e98 | 8 | #define _G5 784 // G5 = 783.99Hz |
rkuo2000 | 0:32bc20854e98 | 9 | #define _A5 880 // A5 = 880.00Hz |
rkuo2000 | 0:32bc20854e98 | 10 | #define _B5 988 // B5 = 987.77Hz |
rkuo2000 | 0:32bc20854e98 | 11 | #define _C6 1046 // C6 = 1046.50Hz |
rkuo2000 | 0:32bc20854e98 | 12 | #define _D6 1175 // D6 = 1174.66Hz |
rkuo2000 | 0:32bc20854e98 | 13 | #define _E6 1319 // E6 = 1318.51Hz |
rkuo2000 | 0:32bc20854e98 | 14 | #define _F6 1397 // F6 = 1396.91Hz |
rkuo2000 | 0:32bc20854e98 | 15 | #define _G6 1568 // G6 = 1567.98Hz |
rkuo2000 | 0:32bc20854e98 | 16 | #define _A6 1760 // A6 = 1760.00Hz |
rkuo2000 | 0:32bc20854e98 | 17 | #define _B6 1976 // B6 = 1975.53Hz |
rkuo2000 | 0:32bc20854e98 | 18 | |
rkuo2000 | 0:32bc20854e98 | 19 | PwmOut pwm0(PF_9); // PWM0 pin |
rkuo2000 | 0:32bc20854e98 | 20 | |
rkuo2000 | 0:32bc20854e98 | 21 | int main() |
rkuo2000 | 0:32bc20854e98 | 22 | { |
rkuo2000 | 0:32bc20854e98 | 23 | int i =0; |
rkuo2000 | 0:32bc20854e98 | 24 | int note[15] = { // array of notes |
rkuo2000 | 0:32bc20854e98 | 25 | _C5, _D5, _E5, _F5, _G5, _A5, _B5, |
rkuo2000 | 0:32bc20854e98 | 26 | _C6, _D6, _E6, _F6, _G6, _A6, _B6}; |
rkuo2000 | 0:32bc20854e98 | 27 | |
rkuo2000 | 0:32bc20854e98 | 28 | for (i=0; i<14; i++) { |
rkuo2000 | 0:32bc20854e98 | 29 | pwm0.period_us(1000000/note[i]); // set period per note |
rkuo2000 | 0:32bc20854e98 | 30 | pwm0.pulsewidth_us(1000000/note[i]/2); // set duty cycle to 50% |
rkuo2000 | 0:32bc20854e98 | 31 | Thread::wait(100); |
rkuo2000 | 0:32bc20854e98 | 32 | } |
rkuo2000 | 0:32bc20854e98 | 33 | pwm0.pulsewidth_us(0); // set dutycycle = 0% to stop |
rkuo2000 | 0:32bc20854e98 | 34 | } |