Document

advertisement
9. Making Music on the PC
Background
Until now, we have utilized the microprocessor, the memory, the monitor, and the keyboard. In this lab,
we will learn how to interact with devices connected to the output ports of the microprocessor. The devices
are the 8253/54 programmable interval timer (PIT) and the 8255 programmable peripheral interface (PPI).
Inside the PC system, there is a single clock that synchronizes all activities of the peripheral chips
connected to the CPU. The PIT is used to reduce the frequency of the clock for use with various devices.
The 8255 is connected to the output of channel 2 on the 8253/54 to control the PCs built in speaker. We
will use the input of the 2 devices to change the tone of the PC speaker to create music.
Objectives : Learn to:
A. Control the PC’s built in speaker
B. Generate square waves of various frequencies
C. Transfer data to the output ports and
D. Read data from the input ports
Pre-Lab
Verify your hardware capabilities by typing in the instructions in section Testing with Debug on page 195
in your text.
Explain how the tone changes if you increase and decrease the divisor by 100.
What time delay will result (in seconds) when a call is made to BIOS int 15h, service 86h with
cx:dx=00200000h?
Give an x86 instruction sequence that inputs data from port 57h, sets bits 6 and 7 without changing the
other bits and then outputs this data back to the same port.
Lab
A. Initializing the 8253/54
The 8253 chip was replaced by the 8254 in later generation 80x86 processors. As with all Intel products,
the 8254 is backwards compatible, thus the instructions written for the 8253 are compatible to the 8254.
The 8253/54 (8254) timer is selected using 3 control lines. The control lines must be set in order to choose
which of the three counters is requested. Since each of the counters can be programmed separately, we will
concentrate on counter 2, the one that controls the PC speaker. In order to produce sound, we must set the
channel (counter) function as a square wave. There is a one-byte control word that is used to initialize the
counters. The control word has bits D7 - D0 where D7 is the most significant bit.
D7 and D6 are the counter select bits.
D5 and D4 contain the divisor for the input frequency. The three options are read/write the MSB only,
read/write the LSB only, and read/write the LSB first followed by the MSB.
D7
D6
0
0
0
1
1
0
1
1
task
D5
D4
task
0
0
latch cnt
0
1
LSB only
counter 2
1
0
MSB only
illegal
1
1
LSB MSB
counter 0
counter 1
D3, D2, D1 are used for mode selection. The six possible modes determine the shape of the waveform.
D3
D2
D1
Mode
0
0
0
0 Interrupt on terminal count
0
0
1
1 Programmable one-shot
x
1
0
2 Rate generator
x
1
1
3 Square Wave generator
1
0
0
4 Software triggered strobe
1
0
1
5 Hardware triggered strobe
D0 is reserved to choose between a binary representation of the divisor (0) or a BCD representation (1).
The largest number for binary is 216 and the largest BCD number is 104. The counter is loaded with zeros
to get the highest count.
What will the value (in hex) for the control word in order to select counter 2 to output a square wave?
Assume that we are using a huge divisor.
The clocks of the 8254 are connected to a constant frequency of 1.1931817 MHz. In order to create music,
we must reduce this frequency to one in audio range and output it from Port 42H. In the Prelab, we played
a middle C which has a frequency of 261.6. We set our divisor to 4561 (11D1H). The control word was
set to B6H for binary option, square wave, LSB then the MSB, counter 2. Example 9.1 shows the code to
output the control word to port 43H the dedicated port for the control word and output the divisor to 42H to
activate the speaker.
Example 9.1
MOV Al,0B6H
;control word
OUT 43H,AL
;43H control word port
MOV AL,D1H
;low byte of divisor
OUT 42H,AL
;LSB first
MOV AL. 11
;high byte
OUT 42H, AL
;MSB next
The speakers are turned on using Port 61H. The two least significant bits of the port must be set to 1.
However the other bits must not be changed. Example 9.2 shows how to turn the speaker on and off.
Example 9.2
IN AL,61H
;get the current speaker setting
MOV AH,AL
;save it for later
OR AL,03H
;set the 2 LSB to 1 other no change
OUT 61H,AL
;turn on the speaker
CALL DELAY
;duration of note
MOV AL,AH
;restore the original setting of the port
OUT 61H,AL
turn off the speaker;
Now, all we need is a module to play music that has the duration of a note or a rest. We can use bit 4 of
port 61 as a timer. It toggles every 15.085 microseconds. If we monitor that bit, we can loop for a certain
duration. We can create a 125 millisecond delay by multiplying 8289 x 15.08 microseconds = 125 ms.
Let’s consider 125 ms as equal to 1 length. Example 9.3 shows a subroutine for a delay of 125 ms (1
length).
Example 9.3
DELAY
PROC NEAR
MOV CX, 8289
;8289x15.08=125 ms
PUSH AX
;save AX on the stack
IN AL, 61H
;get the current setting for port 61H
AND AL, 10H
;check bit 4
CMP AL,AH
;determine if it changed
JE DURA
;if not equal, not 15 micro yet
MOV AH, AL
;save the new setting for port 61H
LOOP LENGTH
;decrement CX and continue until 0
DURA:
POP AX
RET
DURA
ENDP
By changing the value of CX, we can create delays for shorter or longer periods. We now have everything
that we need to play a song. Use the following table to play “Row Row Row Your Boat”. Get creative,
you and your neighbor play it as a round and one of you go up an octave. To get the notes, you may use the
table provided on the diskette in the book or you may program them in your code. There is a complete
listing on page 194 Table 5.1. Use the DELAY procedure in Example 9.3. Assume that one length equals
125 ms. Repeated calls to the function creates multiples of 125 ms. The number after the note indicates
the octave. That is, G4 is G in octave 4 or 196 Hz.
Table 9.1 Row Your Boat
Words
Notes
Length
row
G4
4
row
G4
4
row
G4
4
your
A4
2
boat
B4
4
Gent
B4
4
ly
B4
2
down
C5
4
the
C5
2
stream
D5
3
mer
G5
1
ri
G5
1
ly
G5
1
mer
F5
1
ri
F5
1
ly
F5
1
mer
D5
1
ri
D5
1
ly
D5
1
life
C5
4
is
B4
4
but
A4
3
a
G3
1
dream
F3
3
B. Viewing Shapes of 8254
The counters can be programmed to output various waveforms by changing the mode. The IBM BIOS has
preloaded the counters. Connect the oscilloscope and view the output waveforms for each of the counters.
Set the control register for each of the six modes and view the waveforms on the oscilloscope. Be sure to
determine the port for the oscilloscope. Divide the waves by 5000 to ensure better viewing.
Lab Report
A. Describing What You Learned
1.
Answer all of the ‘‘Think About It’’ questions above.
B. Applying What You Learned
Describe the relationship between the divisor, the frequency, and the timer. Explain the effects both visual
and auditory.
Download