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.
Revision 1:7fdec2560f78, committed 2015-01-09
- Comitter:
- ffxx68
- Date:
- Fri Jan 09 18:00:42 2015 +0000
- Parent:
- 0:59717cf94c6a
- Commit message:
- fixed
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Fri Jan 09 17:46:14 2015 +0000 +++ b/main.cpp Fri Jan 09 18:00:42 2015 +0000 @@ -50,10 +50,9 @@ pc.printf ("Tone...\n"); // generate a short 150Hz tone using PWM hardware output // something like this can be used for a button click effect for feedback - speaker =0.25; //25% duty cycle - mid range volume for (i=0; i<10; i++) { speaker.period(1.0/150.0); // 500hz period - + speaker =0.25; //25% duty cycle - mid range volume wait(.02); } speaker=0.0; // off @@ -62,9 +61,9 @@ case 's': pc.printf ("Sweep...\n"); // sweep up in frequency by changing the PWM period - speaker=0.25; for (i=0; i<8000; i=i+100) { speaker.period(1.0/float(i)); + speaker=0.25; wait(.02); } speaker=0.0; // off @@ -74,7 +73,7 @@ pc.printf ("Police...\n"); // two tone police siren effect - two periods or two frequencies // increase volume - by changing the PWM duty cycle - for (i=0; i<26; i=i+2) { + for (i=0; i<10; i=i+2) { speaker.period(1.0/969.0); speaker = float(i)/50.0; wait(.5); @@ -85,10 +84,10 @@ break; case 'h': - // Play "happy birthday" - speaker=0.25; + pc.printf ("Happy birthday...\n"); for (int i=0;i<=24;i++) { speaker.period(1.0/notes[i]); + speaker=0.25; wait(0.8*intervals[i]/10); } speaker=0.0; // off