Basic speaker testing on a STM32L053R8 board. Based on other examples and improved using commands sent from PC over serial-USB (t=Tone, s=Sweep, p=Police, h=Song, space=Exit). A small 8-Ohm speaker directly plugged on GND/PA_0 (PWM out) makes a low but clearly audible sound.
main.cpp@1:7fdec2560f78, 2015-01-09 (annotated)
- Committer:
- ffxx68
- Date:
- Fri Jan 09 18:00:42 2015 +0000
- Revision:
- 1:7fdec2560f78
- Parent:
- 0:59717cf94c6a
fixed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ffxx68 | 0:59717cf94c6a | 1 | #include "mbed.h" |
ffxx68 | 0:59717cf94c6a | 2 | |
ffxx68 | 0:59717cf94c6a | 3 | // Notes |
ffxx68 | 0:59717cf94c6a | 4 | #define A4 440 |
ffxx68 | 0:59717cf94c6a | 5 | #define B4b 446 |
ffxx68 | 0:59717cf94c6a | 6 | #define C4 261 |
ffxx68 | 0:59717cf94c6a | 7 | #define C4_1 130 |
ffxx68 | 0:59717cf94c6a | 8 | #define C5 523 |
ffxx68 | 0:59717cf94c6a | 9 | #define D4 293 |
ffxx68 | 0:59717cf94c6a | 10 | #define D4b 277 |
ffxx68 | 0:59717cf94c6a | 11 | #define E4 329 |
ffxx68 | 0:59717cf94c6a | 12 | #define F4 349 |
ffxx68 | 0:59717cf94c6a | 13 | #define G4 392 |
ffxx68 | 0:59717cf94c6a | 14 | |
ffxx68 | 0:59717cf94c6a | 15 | const int notes[] = {C4_1,C4,D4,C4,F4,E4,C4_1,C4,D4,C4,G4,F4,C4_1,C4,C5,A4,F4,E4,D4,B4b,B4b,A4,F4,G4,F4}; |
ffxx68 | 0:59717cf94c6a | 16 | const int intervals[] = {4, 4, 8, 8, 8, 10, 4, 4, 8, 8, 8, 10, 4, 4, 8, 8, 8, 8, 8, 4, 4, 8, 8, 8, 12}; |
ffxx68 | 0:59717cf94c6a | 17 | |
ffxx68 | 0:59717cf94c6a | 18 | // speaker sound effect demo using PWM hardware output |
ffxx68 | 0:59717cf94c6a | 19 | PwmOut speaker(PA_0); |
ffxx68 | 0:59717cf94c6a | 20 | // serail port to PC (over USB) |
ffxx68 | 0:59717cf94c6a | 21 | Serial pc(USBTX, USBRX); // tx, rx |
ffxx68 | 0:59717cf94c6a | 22 | |
ffxx68 | 0:59717cf94c6a | 23 | int main() |
ffxx68 | 0:59717cf94c6a | 24 | { |
ffxx68 | 0:59717cf94c6a | 25 | int i; |
ffxx68 | 0:59717cf94c6a | 26 | char c = 0; |
ffxx68 | 0:59717cf94c6a | 27 | |
ffxx68 | 0:59717cf94c6a | 28 | // Intro ( ta-dah @ 500Hz ) |
ffxx68 | 0:59717cf94c6a | 29 | speaker.period(1.0/500.0); // 500hz period |
ffxx68 | 0:59717cf94c6a | 30 | speaker =0.5; //50% duty cycle - max volume |
ffxx68 | 0:59717cf94c6a | 31 | wait(.05); |
ffxx68 | 0:59717cf94c6a | 32 | speaker=0.0; // turn off audio |
ffxx68 | 0:59717cf94c6a | 33 | wait(.05); |
ffxx68 | 0:59717cf94c6a | 34 | speaker =0.5; //50% duty cycle - max volume |
ffxx68 | 0:59717cf94c6a | 35 | wait(.5); |
ffxx68 | 0:59717cf94c6a | 36 | speaker=0.0; // turn off audio |
ffxx68 | 0:59717cf94c6a | 37 | |
ffxx68 | 0:59717cf94c6a | 38 | pc.printf (" \n"); |
ffxx68 | 0:59717cf94c6a | 39 | pc.printf ("t=Tone, s=Sweep, p=Police, h=Song\n"); |
ffxx68 | 0:59717cf94c6a | 40 | |
ffxx68 | 0:59717cf94c6a | 41 | while(c != ' ') { |
ffxx68 | 0:59717cf94c6a | 42 | |
ffxx68 | 0:59717cf94c6a | 43 | c=pc.getc(); |
ffxx68 | 0:59717cf94c6a | 44 | pc.printf("%c",c); |
ffxx68 | 0:59717cf94c6a | 45 | wait(.1); |
ffxx68 | 0:59717cf94c6a | 46 | |
ffxx68 | 0:59717cf94c6a | 47 | switch (c) |
ffxx68 | 0:59717cf94c6a | 48 | { |
ffxx68 | 0:59717cf94c6a | 49 | case 't': |
ffxx68 | 0:59717cf94c6a | 50 | pc.printf ("Tone...\n"); |
ffxx68 | 0:59717cf94c6a | 51 | // generate a short 150Hz tone using PWM hardware output |
ffxx68 | 0:59717cf94c6a | 52 | // something like this can be used for a button click effect for feedback |
ffxx68 | 0:59717cf94c6a | 53 | for (i=0; i<10; i++) { |
ffxx68 | 0:59717cf94c6a | 54 | speaker.period(1.0/150.0); // 500hz period |
ffxx68 | 1:7fdec2560f78 | 55 | speaker =0.25; //25% duty cycle - mid range volume |
ffxx68 | 0:59717cf94c6a | 56 | wait(.02); |
ffxx68 | 0:59717cf94c6a | 57 | } |
ffxx68 | 0:59717cf94c6a | 58 | speaker=0.0; // off |
ffxx68 | 0:59717cf94c6a | 59 | break; |
ffxx68 | 0:59717cf94c6a | 60 | |
ffxx68 | 0:59717cf94c6a | 61 | case 's': |
ffxx68 | 0:59717cf94c6a | 62 | pc.printf ("Sweep...\n"); |
ffxx68 | 0:59717cf94c6a | 63 | // sweep up in frequency by changing the PWM period |
ffxx68 | 0:59717cf94c6a | 64 | for (i=0; i<8000; i=i+100) { |
ffxx68 | 0:59717cf94c6a | 65 | speaker.period(1.0/float(i)); |
ffxx68 | 1:7fdec2560f78 | 66 | speaker=0.25; |
ffxx68 | 0:59717cf94c6a | 67 | wait(.02); |
ffxx68 | 0:59717cf94c6a | 68 | } |
ffxx68 | 0:59717cf94c6a | 69 | speaker=0.0; // off |
ffxx68 | 0:59717cf94c6a | 70 | break; |
ffxx68 | 0:59717cf94c6a | 71 | |
ffxx68 | 0:59717cf94c6a | 72 | case 'p': |
ffxx68 | 0:59717cf94c6a | 73 | pc.printf ("Police...\n"); |
ffxx68 | 0:59717cf94c6a | 74 | // two tone police siren effect - two periods or two frequencies |
ffxx68 | 0:59717cf94c6a | 75 | // increase volume - by changing the PWM duty cycle |
ffxx68 | 1:7fdec2560f78 | 76 | for (i=0; i<10; i=i+2) { |
ffxx68 | 0:59717cf94c6a | 77 | speaker.period(1.0/969.0); |
ffxx68 | 0:59717cf94c6a | 78 | speaker = float(i)/50.0; |
ffxx68 | 0:59717cf94c6a | 79 | wait(.5); |
ffxx68 | 0:59717cf94c6a | 80 | speaker.period(1.0/800.0); |
ffxx68 | 0:59717cf94c6a | 81 | wait(.5); |
ffxx68 | 0:59717cf94c6a | 82 | } |
ffxx68 | 0:59717cf94c6a | 83 | speaker=0.0; // off |
ffxx68 | 0:59717cf94c6a | 84 | break; |
ffxx68 | 0:59717cf94c6a | 85 | |
ffxx68 | 0:59717cf94c6a | 86 | case 'h': |
ffxx68 | 1:7fdec2560f78 | 87 | pc.printf ("Happy birthday...\n"); |
ffxx68 | 0:59717cf94c6a | 88 | for (int i=0;i<=24;i++) { |
ffxx68 | 0:59717cf94c6a | 89 | speaker.period(1.0/notes[i]); |
ffxx68 | 1:7fdec2560f78 | 90 | speaker=0.25; |
ffxx68 | 0:59717cf94c6a | 91 | wait(0.8*intervals[i]/10); |
ffxx68 | 0:59717cf94c6a | 92 | } |
ffxx68 | 0:59717cf94c6a | 93 | speaker=0.0; // off |
ffxx68 | 0:59717cf94c6a | 94 | break; |
ffxx68 | 0:59717cf94c6a | 95 | } |
ffxx68 | 0:59717cf94c6a | 96 | } |
ffxx68 | 0:59717cf94c6a | 97 | pc.printf ("Bye...\n"); |
ffxx68 | 0:59717cf94c6a | 98 | } |