Hardware Prototyping:Program SCALE1

Program SCALE1

This program will output a musical scale of eight tones. The frequencies for a musical scale from middle C upwards are 262, 294, 330, 349, 392, 440, 494 and 523 Hz. These can be

PIC Microcontrollers-1269

PIC Microcontrollers-1270

translated into a table of delay counts which give the required tone period, since period T ¼ 1/f (s), where f ¼ frequency (Hz). The buzzer on the DIZI board is driven from RB0, so this needs to be toggled at a rate determined by the frequency of each tone. We therefore need to use a counter register or the hardware timer to provide a delay corresponding to half the period of each tone. We have previously seen how to calculate the delay time for a loop. Using a formula for the count value derived from this analysis, figures were calculated for a half cycle of each tone, which were then placed in the data table in SCALE1.ASM. To keep the program simple, each tone will be output for 255 cycles, so we will use another register to count the number of cycles competed during each tone. The scale will then be played over a period of about 5 s. The table of values can later be modified to play a tune in the doorbell program.

Instead of a flowchart, the SCALE1 program source code listing (Program 10.3) has been annotated with arrows to show the execution sequence. This informal method of analysis

PIC Microcontrollers-1271

PIC Microcontrollers-1272

can be used to check the program logic before simulation. The eight tone frequencies are controlled by the value of ‘HalfT’, obtained from the program data table at ‘getdel’. ‘HalfT’ is a counter value, which will give a delay corresponding to half a cycle of the frequency required when the chip is clocked at 4 MHz. The eight tones are selected in turn by the

value of ‘TonNum’, which is initialized to 8. This is used as the program counter offset in the data table fetch operation. It is decremented in the main loop after each tone has finished to select the next. The ‘HalfT’ values are thus selected from the bottom of the table upwards.

The tone is generated in the routine ‘note’, where RB0 is set high, the delay using ‘HalfT’ runs, RB0 is cleared, and the second half cycle delay executed. No OPeration instructions (NOP) have been inserted to equalize the duration of each half cycle. RB0 is toggled 255 times using the ‘Count’ register, which gives a duration of around half a second, depending on which tone is being generated (the lower frequencies are output for longer). The main loop thus selects each of the eight values of ‘HalfT’ in turn, and outputs 255 cycles of each tone.

Leave a comment

Your email address will not be published. Required fields are marked *