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@0:59717cf94c6a, 2015-01-09 (annotated)
- Committer:
- ffxx68
- Date:
- Fri Jan 09 17:46:14 2015 +0000
- Revision:
- 0:59717cf94c6a
- Child:
- 1:7fdec2560f78
improved using serial commands
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 | speaker =0.25; //25% duty cycle - mid range volume |
ffxx68 | 0:59717cf94c6a | 54 | for (i=0; i<10; i++) { |
ffxx68 | 0:59717cf94c6a | 55 | speaker.period(1.0/150.0); // 500hz period |
ffxx68 | 0:59717cf94c6a | 56 | |
ffxx68 | 0:59717cf94c6a | 57 | wait(.02); |
ffxx68 | 0:59717cf94c6a | 58 | } |
ffxx68 | 0:59717cf94c6a | 59 | speaker=0.0; // off |
ffxx68 | 0:59717cf94c6a | 60 | break; |
ffxx68 | 0:59717cf94c6a | 61 | |
ffxx68 | 0:59717cf94c6a | 62 | case 's': |
ffxx68 | 0:59717cf94c6a | 63 | pc.printf ("Sweep...\n"); |
ffxx68 | 0:59717cf94c6a | 64 | // sweep up in frequency by changing the PWM period |
ffxx68 | 0:59717cf94c6a | 65 | speaker=0.25; |
ffxx68 | 0:59717cf94c6a | 66 | for (i=0; i<8000; i=i+100) { |
ffxx68 | 0:59717cf94c6a | 67 | speaker.period(1.0/float(i)); |
ffxx68 | 0:59717cf94c6a | 68 | wait(.02); |
ffxx68 | 0:59717cf94c6a | 69 | } |
ffxx68 | 0:59717cf94c6a | 70 | speaker=0.0; // off |
ffxx68 | 0:59717cf94c6a | 71 | break; |
ffxx68 | 0:59717cf94c6a | 72 | |
ffxx68 | 0:59717cf94c6a | 73 | case 'p': |
ffxx68 | 0:59717cf94c6a | 74 | pc.printf ("Police...\n"); |
ffxx68 | 0:59717cf94c6a | 75 | // two tone police siren effect - two periods or two frequencies |
ffxx68 | 0:59717cf94c6a | 76 | // increase volume - by changing the PWM duty cycle |
ffxx68 | 0:59717cf94c6a | 77 | for (i=0; i<26; i=i+2) { |
ffxx68 | 0:59717cf94c6a | 78 | speaker.period(1.0/969.0); |
ffxx68 | 0:59717cf94c6a | 79 | speaker = float(i)/50.0; |
ffxx68 | 0:59717cf94c6a | 80 | wait(.5); |
ffxx68 | 0:59717cf94c6a | 81 | speaker.period(1.0/800.0); |
ffxx68 | 0:59717cf94c6a | 82 | wait(.5); |
ffxx68 | 0:59717cf94c6a | 83 | } |
ffxx68 | 0:59717cf94c6a | 84 | speaker=0.0; // off |
ffxx68 | 0:59717cf94c6a | 85 | break; |
ffxx68 | 0:59717cf94c6a | 86 | |
ffxx68 | 0:59717cf94c6a | 87 | case 'h': |
ffxx68 | 0:59717cf94c6a | 88 | // Play "happy birthday" |
ffxx68 | 0:59717cf94c6a | 89 | speaker=0.25; |
ffxx68 | 0:59717cf94c6a | 90 | for (int i=0;i<=24;i++) { |
ffxx68 | 0:59717cf94c6a | 91 | speaker.period(1.0/notes[i]); |
ffxx68 | 0:59717cf94c6a | 92 | wait(0.8*intervals[i]/10); |
ffxx68 | 0:59717cf94c6a | 93 | } |
ffxx68 | 0:59717cf94c6a | 94 | speaker=0.0; // off |
ffxx68 | 0:59717cf94c6a | 95 | break; |
ffxx68 | 0:59717cf94c6a | 96 | } |
ffxx68 | 0:59717cf94c6a | 97 | } |
ffxx68 | 0:59717cf94c6a | 98 | pc.printf ("Bye...\n"); |
ffxx68 | 0:59717cf94c6a | 99 | } |