Page 265 - ARM Based Microcontroller Projects Using MBED
P. 265
8.36 PROJECT 30—ELECTRONIC ORGAN 251
/***************************************************************************
MELODY MAKER
============
In this project a buzzer is connected to PWM port PA_8. The program plays
the melody HAPPY BIRTHDAY. Array Frequency stores the frequencies of the
notes, array Period stores the periods of the notes, array Duration
stores the durations of each note in units of 400ms. Array Durations
stores the duration of each note in seconds, array DutyCycle stores the
Duty Cycle of each PWM note as 50% (i.e. Period / 2)
Author: Dogan Ibrahim
Date : September 2018
File : Melody
****************************************************************************/
#include "mbed.h"
#define MaxNotes 25
PwmOut pwm(PA_8);
int Frequency[MaxNotes] = {262,262,294,262,349,330,262,262,294,262,
392,349,262,262,524,440,349,330,294,466,
466,440,349,392,349};
int Duration[MaxNotes] = {1,1,2,2,2,3,1,1,2,2,2,3,1,1,2,2,2,2,
2,1,1,2,2,2,3};
float Period[MaxNotes];
float DutyCycle[MaxNotes];
float Durations[MaxNotes];
int main()
{
//
// Calculate the periods (in seconds), Duty Cycles (50%), and
// Durations (in seconds)before entering the loop
//
for(int k = 0; k < MaxNotes; k++)
{
Period[k] = 1.0f / Frequency[k];
DutyCycle[k] = Period[k] / 2.0f;
Durations[k] = 400 * Duration[k] / 1000.0;
}
//
FIG. 8.121 Program listing.
(Continued)