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

8.35 PROJECT 29—MELODY MAKER                     249
            8.35.6 The PDL
              When playing a melody, each note is played for certain duration and with a certain
            frequency. In addition, a certain gap is necessary between two successive notes. The fre-
            quencies of the musical notes starting from middle C (i.e., C4) are given below. The harmonic
            of a note is obtained by doubling the frequency. For example, the frequency of C5 is
            2 262¼524Hz.



            Notes  C4     C4#   D4     D4#    E4     F4     F4#   G4   G4#   A4   A4#    B4
            Hz     261.63  277.18  293.66  311.13  329.63  349.23  370  392  415.3  440  466.16  493.88
              In order to play the tune of a melody, we need to know its musical notes. Each note is
            played for certain duration and there is a certain time gap between two successive notes.
            The next thing we want is to know how to generate a sound with a required frequency
            and duration. In this project, we will be generating the classic Happy Birthday melody
            and thus we need to know the notes and their durations. These are given in the table below
            where the durations are in units of 400ms (i.e., the values given in the table should be mul-
            tiplied by 400 to give the actual durations in milliseconds).


            Note   C4 C4 D4 C4 F4 E4 C4 C4 D4 C4 G4 F4 C4 C4 C5 A4 F4 E4 D4 A4# A4# A4 F4 G4 F4
            Duration 1  1  2  2  2  3  1  1  2  2  2  3  1  1  2  2  2  2  2  1  1  2  2  2  4

              The PDL of the project is shown in Fig. 8.120. Basically, two arrays are used to store the
            notes and their corresponding durations.



            8.35.7 Program Listing

              The program listing (program: Melody) is shown in Fig. 8.121. At the beginning of the pro-
            gram, PWM port PA_8 is assigned to variable pwm and the frequencies and durations of the
            melody are stored in two arrays called Frequency and Duration, respectively. Array Period
            stores the periods of the notes (the PWM function pwm.period requires the periods rather
            than the frequencies to be entered), array Durations stores the duration of each note in sec-
            onds (the PWM function pwm.pulsewidth requires the pulse width to be in seconds and not
            in milliseconds), and array DutyCycle stores the Duty Cycle of each PWM note as 50% (i.e.,
            Period/2). Before the main program loop, the periods of the waveforms, duty cycles, and their
            durations are calculated so that the main program loop does not have to spend any time to do
            these calculations. Inside the program loop, the melody frequencies are generated with the
            required durations. Notice that the PWM waveform is stopped by setting its pulsewidth
            to 0. A small delay (100ms) is introduced between each note. The melody is repeated after
            3s of delay.
   258   259   260   261   262   263   264   265   266   267   268