Page 269 - ARM Based Microcontroller Projects Using MBED
P. 269

8.37  PROJECT 31—VARYING THE LED BRIGHTNESS           255
               /***************************************************************************

                                           ELECTRONIC ORGAN
                                           ================
               In this project a buzzer is connected to PWM port PA_8. The program is an
               electronic organ that can play the notes for one octave. The following keys
               are used on the keyboard for the notes:
               a:C5, s:D5 d:E5 f:F5 g:G5 h:A5 j:B5 k:C6
               Pressing a key plays the corresponding note for 200ms.

               Author: Dogan Ibrahim
               Date  : September 2018
               File  : EOrgan
               ****************************************************************************/
               #include "mbed.h"
               Serial MyPC(USBTX, USBRX);
               PwmOut pwm(PA_8);
               #define MaxNotes 8

               //
               // Define the musical note (C5 - C6) frequencies
               //
               float Notes[MaxNotes] = {523.25,587.33,659.25,698.46,783.99,880.0,
                                        987.77,1046.5};

               float Periods[MaxNotes];
               float DutyCycle[MaxNotes];

               //
               // Serial interrupt service routine. The program jumps here when
               // a key is pressed on the keyboard
               //
               void ISR()
               {
                   char c;
                   int k;

                   c=MyPC.getc();                          // Get a key from keyboard

                   switch (c)
                   {
                       case 'a':                           // Is it 'a' ?
                           k = 0;
                           break;
                       case 's':                           // Is it 's' ?
                           k = 1;
                           break;
                       case 'd':                           // Is it 'd' ?
                           k = 2;
                           break;
                       case 'f':                           // Is it 'f' ?
            FIG. 8.124  Program listing.
                                                                                     (Continued)
   264   265   266   267   268   269   270   271   272   273   274