You are on page 1of 4

1.0.

0 IMPLEMENTING PULSE WIDTH MODULATION FOR THE BUZZER

We decided to implement a buzzer in our system, where each key pressed on the keypad would cause a
different tone to sound. In order to use the buzzer, a technique called Pulse Width Modulation (PWM) would
be required. To drive a buzzer, a string of ‘high’ and ‘low’ voltages would be sent to the buzzer through a
digital pin. To vary the frequency, different proportions of high and low voltages (duty cycle) would be needed.

1.1.0 Using Loops and Delays to Vary Frequency

At first, we tried to vary the frequency by using loops and delays. We encountered a few major problems.
Below are the two attempts using loops and delay

Figure 1.1

1.1.1 Example 1
Here, the period of the PWM waveform is kept constant (x+y=255), where (x/255)*100%
would be the duty cycle. However, at higher duty cycles (>20%), the buzzer stops working
properly, producing a clicking sound.

This was probably due to the period being too long, being 10.2ms plus the delay
from using the FOR loop, thus an attempt was made to shorten the period, using two
methods.

The first being decreasing x+y. While this worked (x+y=100), this would grant us
very little resolution and thus very inaccurate frequencies for the tones generated.

Second being using shorter delays, which would allow us to shorten the period as
well as maintain resolution. Delay10TCYx() is a function where it delays for a multiple of 10
Nop(). Nop() means no-operation and is a delay of exactly one cycle. The header file had no
function where it delays for a multiple of Nop() and making our own function would require
an understanding of machine code and thus this attempt was abandoned.

to control the duration for which the desired frequency was heard. However. Belows is the code used for initialisation and changing the ‘duty cycle’ of the tone. Then.2 Example 2 Here. which isn’t good for hearing and thus impractical. While this worked. meaning that for a set amount of loops. Notice that we are not exactly manipulating the duty cycle of the PWM waveform (done by setting PR2 and manipulating CCPR1L. as well as the description of what each line of code does. when we varied the time for which the waveform is ‘low’ (through the use of delays). which matches what our PWM code was doing. producing a clicking sound. Finding the amount of FOR loops needed for each tone would be tedious and impractical. this was not really a problem due to:  For each consecutive tone or semitone. while the tone function toggles the PWM pin on and off as well as changes the frequency and duration of the heard tone.2. we toggle the pin putting out the PWM waveform ON and OFF (by using the TRIS register) with a variable delay in between the toggles. the time taken for each FOR loop also changes.2 The tone_int function initialises the ECCP module. we found hat the PIC18F46K20 had an ECCP module. However.0 USING THE ECCP (ENCHANCED CAPTURE/COMPARE/PWM) MODULE After some research. allowing us to generate the PWM signals in a more efffecient and practical manner compared to using loops and delays. the buzzer stops working properly. This is due to the problem mentioned in Example 1 where at higher duty cycles (>20%). . the change in sound frequency increases. 1. 1. thus changing the frequency of the PWM instead of it’s duty cycle. resolution was still good. higher frequencies would sound for a shorter time and vice versa. We worked around it by setting CCPR1L instead and varying PR2. with a much better resolution. Figure 1. it had an effect of causing a reduced degree of resolution in the high frequencies with smaller changes in PR2 causing large changes in frequencies.  For the range of frequencies that we desired the buzzer to sound. we fixed the time ‘high’ and varied ‘low’. We also vary the tone when the pin is OFF so that no artefacts are heard while buzzer is in operation. This attempt worked much better.1. Resolution only deteriorates significantly at very high frequencies.

01098901 683 85 0.002 0.004 0.00571429 357 162 0.3 .3. So. we would still have to find the correlation between the duty cycle and sound frequency. and PWM frequency being measured. since even after calculating the duty cycle for each individual signal. Table 1.0 DETERMINING PR2 VALUES FOR DIFFERENT FREQUENCIES From Figure 1. we can assume that the frequency of sound being produced by the buzzer would be roughly equal to the frequency of the PWM waveform being sent to the buzzer.264 The data points is charted and a line of best fit (dotted) is also drawn and its equation shown.01 0. 1/PR2 being calculated.00775194 483 120 0.00892857 556 104 0.00617284 385 150 0.1 PR2 1/PR2 PWM frequency (Hz) 175 0.00714286 446 129 0.00833333 519 112 0. we skip the calculation of duty cycles and directly relate PR2 and sound frequency through the use of an oscilloscope to measure the PWM frequency which.4935 800 600 400 200 0 0 0. as assumes.014 1/PR2 .1/PR2 vs PWM frequency 1000 PWM frequency (Hz) y = 61740x + 4.2.01176471 731 79 0. roughly equal to sound frequency for varying amounts of PR2. Also.01351351 838.008 0.006 0.00961538 599 97 0.854 74 0.1.3.01030928 641 91 0. The following data was gathered with PR2 being varied.012 0. Graph 1.01265823 785.00666667 416 140 0. we see that:  CCPR1L =1  PR2 = variable Since we are manipulating the frequency of the PWM waveform instead of directly manipulating it’s duty cycle.

and the features of the PIC1846K20 was gained from implementing this part of our system.7659668 142 b B 4 466. we use “#define” so that we can assign each PR2 values to each of their respective tones to simplify generating specific tones for the user.29459654 94 F5 698.25 94.20497398 79 b A 5 830. especially one where timing is important and multiple tones are needed.5 59. there is minimal margin of error and thus we can conclude that PR2 is inversely proportional to PWM frequency.4.54189612 67 B5 987.96683053 89 b G 5 739.3.99 79.9302223 106 b E 5 622.25106993 59 Now that we have the PR2 values.7329003 134 B4 493.25 119.1579549 126 C5 523. So. .94329545 84 G5 783. the following equation is derived: 61740 ?= ? − 4. A better understanding of PWM implementation.79006973 63 C6 1046.0 Conclusions for PWM and Buzzer Implementing loops and delays were a bad idea.0153762 119 b D 5 554. as they are very unwieldly to use effectively in a system. The ECCP was much better though it took a lot of time going through the data sheets for the register that were needed. Thus.37 112.16 133. from the line of best fit. 1.25 99. buzzers.As can be seen.33 105.2 Frequency PR2 Note PR2 (Hz) rounded A4 440 141.46 88.99 83. Table 1.73522197 75 A5 880 70. Now we need to determine the values of PR2 for the tones desired.33 66. now we have the equation linking PR2 and frequency.2797574 112 D5 587.94229118 100 E5 659. we can derive the following values of PR2 from tones and their frequencies. With data gathered from a source(1).77 62.61 74.4935 Where y=PR2 and x=PWM frequency.88 126.51917947 71 Bb5 932.