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

256                          8. INTERMEDIATE LEVEL PROJECTS

                                 k = 3;
                                 break;
                             case 'g':                           // Is it 'g' ?
                                 k = 4;
                                 break;
                             case 'h':                           // Is it 'h' ?
                                 k = 5;
                                 break;
                             case 'j':                           // Is it 'j' ?
                                 k = 6;
                                 break;
                             case 'k':                           // Is it 'k' ?
                                 k = 7;
                                 break;
                         }

                         pwm.period(Periods[k]);                 // Start the PWM
                         pwm.pulsewidth(DutyCycle[k]);           // 50% Duty Cycle
                         wait_ms(200);                           // Play for 200ms
                         pwm.pulsewidth(0.0);                    // Stop PWM
                     }


                     int main()
                     {
                         MyPC.attach(&ISR, Serial::RxIrq);       // Attach Serial Int

                     //
                     // Calculate the periods (in seconds), Duty Cycles (50%) before
                     // entering the loop
                     //
                         for(int k = 0; k < MaxNotes; k++)
                         {
                             Periods[k] = 1.0f / Notes[k];
                             DutyCycle[k] = Periods[k] / 2.0f;
                         }

                         while(1)                                    // Wait for ISR
                         {
                         }
                     }

                 FIG. 8.124, CONT’D


                 use a PWM waveform with the frequency set to about 50Hz (period¼20ms), then the
                 viewer will not notice the flashing effect. As the duty cycle is varied, the brightness of
                 the LED will change. For example, a higher duty cycle will result in brighter light since
                 the average voltage is higher. Similarly, a lower duty cycle will result in dimmer light. The
                 PDL of the project is shown in Fig. 8.125.
   265   266   267   268   269   270   271   272   273   274   275