Laboratory 18: Producing Music With The PC Objective: Part 1: Playing A Musical Scale

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

LABORATORY 18: PRODUCING MUSIC WITH THE PC Objective

Learn how to: Program the 8253 timer that supplies the speaker data to produce the tones of a musical scale. Analyze, execute, and modify a program that automatically plays a musical scale. Modify the musical scale program so that it plays a melody. Analyze, execute, and modify a program that accepts keyboard inputs 1 through 8 to play the musical scale. NOTE: Earlier PCs had a speaker driven by circuitry on the motherboard, lf your PC does not have this type of speaker, lab will not produce the expected tone.

can make any delay that is a multiple of 50 ms by simply changing the contents of DL. For instance, to make a 250 ms delay for sounding quarter notes, the value 05H is loaded into DL. Following is the listing of such a routine DELAyI:

Part 1: Playing a Musical Scale


he PC s speaker driver circuit can be used to generate the tones or notes o~,a musical instmmant. Figure L18. I lists the notes and corresponding frequencies for one complete octave (the 8 keys from one C to the next C) starting at what is called Middle C on a piano keyboard. Here Middle C is identified as a 523.25 Hz tone and the D key that follows by a 587.33 Hz tone. The higher frequency tone is said to have a higherpitch. Notice that musical notes E, F, G, A, and B represent Increases m frequency and pitch up through the next C, which is at twice the frequency of Middle C or 1046.50 Hz. If we sound each of these tones in order, starting with Middle C, we have played what is called a C major scale.

MOV DELA[2: NOP NOP LOOp DEC JNZ RET

CX,2870H

;Delay count for 50 ms

DELAY~ DL DELAy~

;Repeat the 50 ms delay ;till repeat count = 0

Musical Key C D E F G A B C

Frequency 523.25 587.33 659.26 698.46 783.99 880.00 987.77 1046.50

DMsor 08E2H 07EAH 070DH 06A8H 05EEH 0548H 04B5H 0471H

The number 2870H implements a 50 ms delay on an 8088-based PC that runs at 5 MHz. If you are using a PC with another processor, 8086, 80286, 80386, 80486, or a Pentium processor, or an 8088-based PC that is muning at a different clock rate, this number will need t6 be changed to maintain the delay at 50ms. We can extend the software techniques of playing a single half note to a sequence of tones corresponding to the keys of a musical keyboard. For instance, a program could be written to automatically play a musical scale, such as a C-major scale. To do this, we can set up a table in memory oft he counts for the tones of the notes in one octave of the scale. Then, one count after the other is loaded into the timer. After each count is loade the t" of a second, and then the sneaker is ..,,,~,~ ~,-. prowae a sllant period between tones, t.,~,~,, uu to ., d,. liner and speaker are turned to ~n,,,a ,~ . ,- a quarter ~ n o ...... ~e tone for Here we will begin our study of music generation on the PC with a program that and modify the program Check offeae ,- ,,o ~ ....... plays a mustcal scale. We will analyze, run, ~ h step ~,~ ,, ~ comptetea.

Check

Step Procedure Examine the data segment and constant definitions in the program in Figure L18.2, Assume that it is run on a 5 MHz PC. a. What type ofstatemants are used to define the counts for the tone divisor table9 How many tones does the program generate? --~ What is the first divisor in the table? ---------------What musical key does it represent? ~ What is the last divisor and its musical key~ ~ How is the end of the tone count table marked9 ~ b. What is the name of the repeat count used to set the duration of a tone? Find the value assigned to this constant in the program9 How long is each tone sounded for? ~ --~ Find the value assigned to this constant in the program. __ Ex2~el~2eg is the silent period? ---------------a 5 MHz PC. part of the program in Figure L18.2 labeled Generate the tones. Assume that it is run on a. What is the value of the control word written to the 8253 timer9 Which counter is being loaded? __ --~ What mode of operation is specified.9 _--~ In this mode what type of signal is produced at the OUT pin of the counter9 -----------------Is the Counter set for BCD or binary counting? ~ What is the address of the control word register of the 82539 ----------------What is the function of the GATE input? ~ --~

FIGURE L18.1 Table of divisors for 8253 to generate various musical Iones.
To produce any note in a musical scale, we simply program the 8253 timer that produces the speaker output with the appropriate count and then enable the timer and speaker. The l. 19 MHz clock input is divided by the count to produce the desired fm quency at the output. Another note can be sounded by simnly loadin a diffe of Laboratory 15 we 1 ..... a ~. . "g rent div~sor (count) into the ti,.o~, ~ , . ~u,,,~u ,~ow to generate a soecific freow~eno~, t,~n .... L . ; ........ I~ m ~-arts 2 and 3 o~quence can oe used in our music programs ~ ..... ~ ..~ at toe spea~er or the PC. Th~s same histruction However, to sound more like a By of music, the tone must last the tone is duration. For instance, the sca e can ~e as a sequence of quarter notes. notequarter note we mean thatfor a ~pecific ro played by producmg a delay betweeu notes ~,~- ~ ,_ , p duced for Just 0he-quarter of a see , ~t,~y ~an oe ~- ,~- o~, " . ond. Moreovel; tn me original IBM PC the clock si-nal out,,-~oetter recognized - o ~,~,~ oy u~e oz.~ timer ts appbed to the speaker through a NAND gate The sec no tnpntthethis gate is controlled is Sounded c~ .~. ., , ~- ....at address-~lH this control bit loaic t speaker and a tone from hit 1 of the outpnt hurt ". y malting B " clock to nroduccd T~,;o ~o .~._ , . --~. ,.,n toe omer nano, ff ~t ~s made log~c 0 the clock si--o :~ ~ nterval rome tone will be t~med using a software delay routine similar to the one u sed ~Jatxt written in"- 2 of Laboratorv 15 -~ ~ in the urn ....... Part rot the software delay routine, we will use a general subrontine that can generate a variable delay. This program will produce a fixed delay equal to 50 ms that is made variable by multiplying it by a number that g~ts placed in register DL. In this way, we

c. What is the name of the repeat count that is used to set the duration of the silence between tones?

91

b. What does the instruction sequence that follows do?

TITLE

I~BORATORY 18 Part 1 (LI8PI,ASM) PAGE ,132 SEGMENT STACK ~STACK

EX,|BX] EXIT
c. Is the most significant or least significant byte of the divisor count loaded into the 8253 first? d. What does the instruction sequence that follows do?

STACK_SEG

DATA_SEG Table DW DW DW

SEGMENT 8E2H,7EAH, 70DH,6ASH, 5EEH, 548H,4B5H,471H 471H,4B5H,548H,SEEH,6ASH, 70DH, TE/~,SE2H 0000 ;End of data marker ENDS ;Repeat counter for a to~e duration ;Repeat counter for a silence

RPTCTRI = i0 RPTCTR2 = 20 CODE_SEG LI8PI PROC ASSUME

IN MOV OR OUT
Explain to the bit level.

AL, AH,AL AL,3 61H,AL

SEGMENT ~CODE FAR CS:CODE_SEG, SS:STACK_SEG, DS:DATA_SEG ;To return to DEBUG program put retul~n address on the stack PUSH MOV PUSH DS AX, 0 ~D~ i~plements Laboratory 18 Part AX, DATA_SEG DS, AX BX, TableI ;Set up data segment ;Set up divisor pointer

e. What register is used to pass the tone duration count parameter to the time delay routine? What instruction initiates the time delay routine? List the events that take place in the part of the program that is labeled Turn tone off7

;Followin~ code MOV MOV LEA

;Generate the to~es MOV OUT MOV CMP JZ OUT MOV O~T IN MOV OR OUT MOV CALL At, 0B6H 43H, AL AX, [BX] ~, 0 EXIT 42N, AL At, AH 42H, AL At, 6iH AN, AL At, 3 61H, AL DL, RPTCTRI DELJ~y ;Set up timer ;Get divisor ;End of data? ;Yes ;Load LSByte of divisor ;Load MSByte of divisor ;Read port 61H ;Save its contents ;E~able timer and ~ gate ;Set up the repeat counter for tone ;i00 msec (5 MHz PC) X RPTCTRI

An executable version of the program in Figure L18.2 is available on the Programs Diskette in file L I 8P 1 .EXE. Run this program using the the command C: \DOS>A:L%&P%.EXE Describe what you hear..

; Turn tone off

NOTE: The numbers used in this program for the tone and repeat count implement the correct durations for the tone and silence duration based on the 8088 microprocessor in the original IBM PC, which runs at 5 MHz. ~/you are using a PC with another processor, 8086, 80286. 80386, 80486, or Pentium<O processor or an 8088-based PC that is running at a different clock frequency, the music produced by this plvgram will be played too fast. This is because the software generated delay will be much shorter This can be compensated for by either increasing the number loaded into CX in the DELAY subroutine or by increasing the values of the repeat counts for the tone duration and silence between tones,

MOV OUT MOV CALL ADD OMp EXIT: RET

At, AH 61H, AL DL, RPTCTR2 DELAY BX, 2 TONE

;Restore port 61H ;Set up the r~Peat counter for silence ;100 msec (5 MHz PC) x RPTCTR2 ;Point to next divisor value ;Continue with next tone ;Return

;Delay = Repeat counh x 100 msec on a 5 MHz PC DELAY PROC NEAR CX, 50EOH DELAY2 DL DELAYI ;Delay count for i00 rosec

5.

6.

What is the clock speed at which your PC runs? Compute the values for the repeat connter and the silence counter or delay count for your PC so as to produce each tone for 0.25 s with 0.75 s of silence between tones. _ Modify the program using the results obtained in step 5, reassemble, relink, and generate L18P1A.EXE. Rerun the new program to verify that it works correctly. Describe the difference between what was heard in step 4 and what you now bear. Rewrite the program produced in step 6 to make it interactive with the user. That is, have it display a message asking the user to enter the "U" or "D" key to play the up scale or down scale, respectively. The program must also be modified so that just the up scale or down scale is played according to the key input. Save the executable file as L18P1B.EXE. Run the new program to verify its operation.

DELAY1: MOV DELAY2: NOP NOP LOOP DEC JNZ RET DELAY ~NDP LI8PI ENDP CODE_SEG

;Repeat the 100 msec delay ;till re,eat count = 0

E~S

FIGURE L18.2 Program for Laboratory 18.

92

93

Part 2: Playing a Musical Melody


In the last section, we executed a program that automatically plays a musical scale. Here the program will be modified to permit it to play simple musical melodies. To do this, we simply build the data table with the sequence of notes ueeded t play the piece of music. That is, by arranging the counts for the tones of the scale in the appropriate order it is possible to generate mnsie. For instance, if the fi~st tNee notes in the melody are E, E, and F, the first three entries in the table would be 070DH, 070DH, and 06ASH. When run, the program plays the sequence of tones defined by the note table; thereby, playing the melody. Here we wilt analyze, execute, and modify a program that is written to play a melody.

Part 3: Program for Playing Individual Notes for Keyboard Inputs


The music programs we have worked with so far were all table driven. That is, they played a sequence of tones that were listed in a table in memory. Here we will work with a program that plays the notes of a scale in response to keyboard inputs. This will permit any melody to be played without any modification to the program. The notes of the song are keyed in one after the other from the keyboard.

Check Check
Step 1. Procedure Print out the program in file LISP2.ASM. From the contents of the data table in the program, make list of the notes in the order they at-e played.

Step Procedure 1. Set the PC to print what is displayed on the screen and print out the program in file L18P3.ASM with a TYPE command. a. From the contents of the data table in the program, make a list of the notes in the order their counts are stored in memory. b. Give an overview of the operation of the part of the program identified as Wait for keyboard character down to the comment Generate the tone.

__

Run the program in file L18P2.EXE. Does it play a melody? Can you name the song? NOTE: Again, the numbers used in the program for the tone and repeat count implement the correct durations for the tone and silence durations based on the SO88 microprocessor in the riginal IBM PC, which runs at 5 MHz. If you are using a PC with another processor, 8086, 80286, 80386, 80486, or Pentium processor, or a PC that is running at a different clock frequency, the music produced by this program will be played too fast. This is because the software generated delay will be much shorter This can be compensated for by either increasing the number loaded into CX in the DELAY subroutine or by increasing the values of the repeat counts for the tone duration and silence between tones.

3. 4.

If necessary, modify the program as done in step 6 of Part 1 of this laboratory exercise. Call the files for the modified program L18P2A.XXX. Rerun the executable file. Modify the data table in the program of file L 18P2.ASM so that they represent the melody. C (high) BAGFGA~GABGAGFEFC (high) BAGFG~FG~BG~GFEF

c. Which key must be entered to terminate execution of the program? Which keys of the keyboard are used to play the notes of the scale? Which key plays C0ow)? C(high)? What musical note is played when key 2 is depressed? d. What equation is used to calculate the offset of the count for the note from the keycode? In which register is the offset of the count for the note to be played from the table passed to the Generate the tone routine? Run the program in file L18P3.EXE directly from DOS. Depress the 1 through 8 keys in that order. what happens?

Here C(high) stands for the C that is coded as the count 0471H. Save the new program in the file LISP2B.ASM. Assemble, link, and then rerun the program. Can you name the new music piece that is played? 5. Rqpeat step 4 for the following melody

EEEEEEEGC (low) DEFFFFFEEEDDEDGEEEEEEEGC (low)DEFFFFFEEEGGFDC (low)


Here C(low) stands for the C that is coded as the count 08E2H.
What is this melody? NOTE:Again, the number used in the progn2m for the repeat count implements the correct duration for the tone duration bused on the 8088 microprocessor in the original IBM PC, which runs at 5 MHz. If you are using a PC with another processor, 8086, 80286, 80386, 80486, or Pentium processor, or an 8088-bused PC that is running at a diffkrent clock frequency, the tone produced by this program will be sounded for too short a period. This is because the software generated delay will be much shorten This. can be compensated for by either increasing the number loaded into CX in the DELAY submutine or by increasing the values of the repeat count for the tone duration.

3. 4.

If necessary, modify the program as done in step 6 of Part 1 of this laboratory exercise. Call the files for the modified program LI 8P3A.XXX. Renan the executable file. Try out the keyboard by playing the melody used in step 4 of Part 2 of this laboratory exercise. It is repeated here.

C (high) BAGFGAFGABGAGFEFC (high) BAGFGAFGABGAGFEF

94

95

Repeat step 4 for the melody that follows, which was used in step 5 of Part 2.

EEEEEEEGC(Iow)DEFFFFFEEEDDEDGEEEEEEGC(Iow)DEFFFFFEEEGGFDC(Iow)
Modify the program so as to add a message that is displayed when the program is brought up saying Enter 1 to 8 to play a tone or (~3) to terminate. Cal! the files for the modified program L18P3B.XXX. Rerun the executable file. Write a program that allows you to save a sequence of keystrokes (musical notes) entered from the " keyboard and play them back by pressing P"on the keyboard, One should be able to replay as many times as one wishes until (~1) is pressed to terminate the program. Assemble, link, and execute to verify the operation of the new program. Save the file as L18P3C.XXX.

Interface Circuits: Construction, Testing, and Troubleshooting

LABORATORY 19: USING THE PC LABS 0N-BOARD INPUT/OUTPUT INTERFACE CIRCUITS


Objective
Learn how to: Use the DEBUG INPUT and OUTPUT commands to input the settings of the switches and light the LEDs of the PC~LAB. Analyze, modify, and run programs that light the LEDs of the PCp,LAB. Analyze, modify, write, and run programs that light the LEDs of the PCp~LAB based on the setting of its switches. Analyze, modify, and run programs that produce tones at the speaker of the PCp~LAB. NOTE: For this laboratory exercise, the PCIxLAB must be attached to your PC. Also, the INT/EXT switch must be set to the INTposition so that the on-board 1/0 interface circuitry is enabled for operation.

Part 1: Simple Input/Output with the INPUT and OUTPUT DEBUG Commands
Here we begin our laboratory study of the on-board circuits of the PCp,LAB. This lab focuses on how to exercise the input! output resource (LEDs, switches, and speaker) through software. In this part of the lab we will use the INPUT and OUTPUT commands of DEBUG to read the settings of the switches and light the LEDs. Check offeach step as it is completed. Check Step Procedure

1. Ensure that the INT/EXT switch is set for operation of the on-board circuitry. 2. Bring up the DEBUG program 3. Set all eight switches to the ON position. Write an INPUT command that will read their state. Issue this command to the PCp,LAB. What value is displayed on the screen? Reset all of the switches to the OFF position and then read their state with another INPUT command. What value is now displayed on the screen? Set switch So, S l, $2, and $3, to ON and the rest to OFE Again read their state with an INPUT cormnand. What value is displayed on the screen?

96 97

You might also like