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

254                          8. INTERMEDIATE LEVEL PROJECTS

                 8.36.6 Program Listing
                   The program listing (program: EOrgan) is shown in Fig. 8.124. At the beginning of the pro-
                 gram, PWM port PA_8 is assigned to variable pwm and the frequencies of the musical notes
                 C5–C6 are stored in array called Notes. Inside the main program, the serial interrupt is at-
                 tached to function called ISR so that when a key is pressed on the keyboard the program
                 jumps to this function. Also, the periods of the musical notes are calculated and stored in ar-
                 ray called Periods. All the duty cycles are set to 50%. The main program then waits and does
                 nothing else. Inside the ISR the characters typed on the keyboard are read and a switch state-
                 ment is used to find out which character has been typed. Characters a–k are given numbers
                 0–7, respectively. These numbers are used to index array Periods to set the PWM period.
                 A note is played for 200ms and after this time the PWM is stopped, ready for the next key
                 to be accepted.


                 8.36.7 Suggestions for Additional Work

                   Modify the program given in Fig. 8.124 by changing the durations a note is played.
                   Buzzers do not generate clear sounds. Modify the circuit diagram by using an audio am-
                 plifier (e.g., LM386) and a speaker (e.g., 8 ohm) to generate loud and clear musical tones.


                          8.37 PROJECT 31—VARYING THE LED BRIGHTNESS

                 8.37.1 Description

                   In this project, we vary the brightness of the User LED on the development board by
                 sending a PWM waveform to the LED and then varying the duty cycle of this waveform
                 so that the average voltage across the LED varies in relation to the duty cycle. The frequency
                 of the PWM waveform is set to 50Hz and its duty cycle is incremented in 10 steps every
                 second, that is, the brightness is increased every 100ms. When full brightness is reached,
                 it is decreased again in 10 steps every second until the LED if turned OFF. This process
                 is repeated continuously.

                 8.37.2 Aim

                   The aim of this project is to show how PWM can be programmed to vary the brightness of
                 an LED.


                 8.37.3 Project PDL

                   If we turn the LED ON and OFF slowly, then we see a flashing LED. If on the other hand
                 we shorten the ON and OFF times and turn ON for 50% and OFF for 50%, the LED will
                 appear half as bright since half of the average voltage is applied to the LED. The important
                 factor here is the duration. Therefore, if the ON and OFF times are very short, then the
                 viewer will not notice the flashing effect and will see the LED dimmed. In general, if we
   263   264   265   266   267   268   269   270   271   272   273