Ray Liu
/
NuMaker-mbed-PWM0_playnote
Modify the file main.cpp for M487
main.cpp@0:30d5f101ebff, 2017-09-29 (annotated)
- Committer:
- shliu1
- Date:
- Fri Sep 29 05:44:54 2017 +0000
- Revision:
- 0:30d5f101ebff
main.cpp adds the setting of TARGET_NUMAKER_PFM_M487 for M487
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shliu1 | 0:30d5f101ebff | 1 | //NuMaker-PFM-NUC472 PWM0 output note to a speaker |
shliu1 | 0:30d5f101ebff | 2 | #include "mbed.h" |
shliu1 | 0:30d5f101ebff | 3 | |
shliu1 | 0:30d5f101ebff | 4 | #define _C5 523 // C5 = 523.25Hz |
shliu1 | 0:30d5f101ebff | 5 | #define _D5 587 // D5 = 587.33Hz |
shliu1 | 0:30d5f101ebff | 6 | #define _E5 659 // E5 = 659.26Hz |
shliu1 | 0:30d5f101ebff | 7 | #define _F5 698 // F5 = 698.46Hz |
shliu1 | 0:30d5f101ebff | 8 | #define _G5 784 // G5 = 783.99Hz |
shliu1 | 0:30d5f101ebff | 9 | #define _A5 880 // A5 = 880.00Hz |
shliu1 | 0:30d5f101ebff | 10 | #define _B5 988 // B5 = 987.77Hz |
shliu1 | 0:30d5f101ebff | 11 | #define _C6 1046 // C6 = 1046.50Hz |
shliu1 | 0:30d5f101ebff | 12 | #define _D6 1175 // D6 = 1174.66Hz |
shliu1 | 0:30d5f101ebff | 13 | #define _E6 1319 // E6 = 1318.51Hz |
shliu1 | 0:30d5f101ebff | 14 | #define _F6 1397 // F6 = 1396.91Hz |
shliu1 | 0:30d5f101ebff | 15 | #define _G6 1568 // G6 = 1567.98Hz |
shliu1 | 0:30d5f101ebff | 16 | #define _A6 1760 // A6 = 1760.00Hz |
shliu1 | 0:30d5f101ebff | 17 | #define _B6 1976 // B6 = 1975.53Hz |
shliu1 | 0:30d5f101ebff | 18 | |
shliu1 | 0:30d5f101ebff | 19 | #if defined(TARGET_NUMAKER_PFM_NUC472) |
shliu1 | 0:30d5f101ebff | 20 | PwmOut pwm0(PF_9); // PWM0 pin |
shliu1 | 0:30d5f101ebff | 21 | #elif defined(TARGET_NUMAKER_PFM_M453) |
shliu1 | 0:30d5f101ebff | 22 | PwmOut pwm0(PC_6); // PWM0_pin |
shliu1 | 0:30d5f101ebff | 23 | #elif defined(TARGET_NUMAKER_PFM_M487) |
shliu1 | 0:30d5f101ebff | 24 | PwmOut pwm0(PA_5); // PWM0_pin |
shliu1 | 0:30d5f101ebff | 25 | #endif |
shliu1 | 0:30d5f101ebff | 26 | |
shliu1 | 0:30d5f101ebff | 27 | |
shliu1 | 0:30d5f101ebff | 28 | int main() |
shliu1 | 0:30d5f101ebff | 29 | { |
shliu1 | 0:30d5f101ebff | 30 | int i =0; |
shliu1 | 0:30d5f101ebff | 31 | int note[15] = { // array of notes |
shliu1 | 0:30d5f101ebff | 32 | _C5, _D5, _E5, _F5, _G5, _A5, _B5, |
shliu1 | 0:30d5f101ebff | 33 | _C6, _D6, _E6, _F6, _G6, _A6, _B6}; |
shliu1 | 0:30d5f101ebff | 34 | |
shliu1 | 0:30d5f101ebff | 35 | for (i=0; i<14; i++) { |
shliu1 | 0:30d5f101ebff | 36 | pwm0.period_us(1000000/note[i]); // set period per note |
shliu1 | 0:30d5f101ebff | 37 | pwm0.pulsewidth_us(1000000/note[i]/2); // set duty cycle to 50% |
shliu1 | 0:30d5f101ebff | 38 | Thread::wait(100); |
shliu1 | 0:30d5f101ebff | 39 | } |
shliu1 | 0:30d5f101ebff | 40 | pwm0.pulsewidth_us(0); // set dutycycle = 0% to stop |
shliu1 | 0:30d5f101ebff | 41 | } |