Generating Music Using 8254 PIT On PC: Experiment #7

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

Experiment #7

Generating Music using 8254 PIT on PC

Birzeit University
Faculty of Engineering and Technology
Department of Electrical and Computer Engineering

Objective:
The objective is to understand, configure and test the 8253/4 Programmable Interval Timer (PIT)
devices, on the personal computer.

Prelab
1. Review the PIT different modes and configuration from your microprocessor book and/or
the 8253/4 datasheets.
2. Review the material below on the PITs in the personal computer.
3. Review the simple I/O mode of PPI (mode 0 of 8255).
4. Prepare all necessary code for parts A and B.

Introduction

In the PC there is a single clock used to synchronize activities of all peripheral chips connected
to the CPU. The clock, which has the highest frequency in the system, belongs to the CPU. There
are functions within the PC that require a clock with a lower frequency. The PIT (8253/54) is
used to bring down the frequency to the desired level for various uses such as the beep sound in
the PC. The 8254 PIT provides three independent channel timers that are programmed using the
control (command) register of the PIT.

The Peripherals in Your PC

Fig. 1 shows the port address decoding in the PC. Fig. 2 shows the PPI connections with address
lines while Fig. 3 shows the PIT connections with the address lines.

1
Figure 1

Figure 2

Figure 3

2
Task 1: Find the port addresses for the PPI and the PIT and fill them in the following table.

Port Address in PC
PPI Port A
PPI Port B
PPI Port C
PPI Command register
PIT Counter 0
PIT Counter 1
PIT Counter 2
PIT Command register

Programming of the PIT


Fig. 4 shows the configuration of the control word to program the PIT counters. You can read
more about this in the Microprocessor textbook.

Figure 4

3
System Timer Modes
The system timer has six modes as shown in the following table.

Mode Name
0 Interrupt on Terminal Count
1 Hardware Re-triggerable One-Shot
2 Rate Generator
3 Square Wave Generator
4 Software Triggered Strobe
5 Hardware Re-triggerable Strobe

PIT connections in the PC


Fig. 5 shows the PIT connections in your PC (it shows the connections of each counter of the
PIT).

Figure 5

In this experiment, we will use the 8254 timer 2 which is connected with the speaker. We will
use it to produce different beep sounds and to generate music. Note that counter 2 clock (i.e. clk2
is connected with 1.19 MHz) while Gate 2 is connected with PB0 of the PPI (i.e. bit 0 of port B
in the PPI) and out2 is connected with speaker. For counter 2 to be enabled then gate 2 should be
logic 1 otherwise it will be disabled. Also note that PB1 is connected with out2 to the speaker
through NAND gate, thus PB1 should be 1 to enable out2 to go the speaker.

4
PROCEDURE
A. Generating Beep using debug
1. Start debug.

2. Program the command register such that counter 2 to generate a square wave (mode 3)
O PIT_CTRL_Register , Control_Word

3. Program counter 2 to produce a frequency of 10 KHz


O ,
O ,

4. Enable Gate2 and the speaker


O ,

5. Disable Gate2 and the speaker


O ,

6. Repeat the above steps to produce beep with frequency 1 KHz, 5 KHz and 15 KHz.

B. Using TASM to produce beep sounds

Copy the following code to a text editor to make an Assembly code. Use TASM and TLINK to
produce the exe file.

.MODEL SMALL
.STACK 1000H

.DATA
COUNT EQU 200D
T EQU 500

.CODE
START:
MOV AL,0B6H
OUT 43H,AL
MOV AX,COUNT
OUT 42H,AL
MOV AL,AH
OUT 42H,AL
MOV AL, 00000011B
OUT 61H,AL
MOV CX,T
DELAY1:
PUSH CX
MOV CX,20000
DELAY2:
LOOP DELAY2
POP CX
LOOP DELAY1
MOV AL,00000000B;DISABLE GATE
OUT 61H,AL
MOV AX,4C00H
INT 21H
END START

5
1. What does the code above do?

2. Explain the task of each block.

3. Change the code to produce a beep sound of 3 KHz for 5 seconds (approximately)

4. Change the code to produce a beep sound of 12 KHz for 2 seconds (approximately).

C. Generate Music on your PC

Fig. 6 shows some piano notes and frequencies. The following table shows how to play “Happy
Birthday” on the PC.

Lyrics Notes Freq. (Hz) Duration


hap C4 262 ½
py C4 262 ½
birth D4 294 1
day C4 262 1
to F4 349 1
you E4 330 2
hap C4 262 ½
py C4 262 ½
birth D4 294 1
day C4 262 1
to G4 392 1
you F4 349 2
hap C4 262 ½
py C4 262 ½
birth C5 523 1
day A4 440 1
dear F4 349 1
so E4 330 1
so D4 294 3
hap B4b 466 ½
py B4b 466 ½
birth A4 440 1
day F4 349 1
to G4 392 1
you F4 349 2

6
Figure 6

Complete the following Assembly code to play “Happy Birthday”.

7
.MODEL SMALL
.STACK 1000H
.DATA
T EQU 50

.CODE
TONE MACRO DIV,DUR
MOV AL,0B6H
OUT 43H,AL
MOV AX,DIV
OUT 42H,AL
MOV AL,AH
OUT 42H,AL
MOV AL,00000011B
OUT 61H,AL
MOV CX,DUR
CALL DELAY1
MOV AL,00000000B
OUT 61H,AL
CALL DELAY2
ENDM

.STARTUP
TONE 4553,T ; HAP (C4)
TONE 4553,T ; PY (C4)
TONE 4057,2*T ; BIRTH (D4)
TONE 4553,2*T;C4
TONE 6409,2*T;F4
TONE 3606,4*T;E4
TONE 4553,2*T;C4
TONE 4553,2*T;C4
TONE 4057,2*T;D4
TONE 4553,2*T;C4
TONE 3035,2*T;G4
TONE 3409,4*T;F4
TONE 4553,T;C4
TONE 4553,T;C4
TONE 2275,2*T;C5
TONE 2704,2*T;A4
TONE 3409,2*T;F4
TONE 3608,2*T;E4
TONE 4057,6*T;D4
TONE 2553,T;B4B
TONE 2553,T;B4B
TONE 2704,2*T;A4
TONE 3409,2*T;F4
TONE 3035,2*T;G4
TONE 3409,4*T;F4
MOV AH,4CH
INT 21H

DELAY1 PROC NEAR


D1:
PUSH CX
MOV CX,38000
D2:
LOOP D2
POP CX
LOOP D1
RET
DELAY1 ENDP

DELAY2 PROC NEAR


MOV CX,65000
D3:
LOOP D3
RET
DELAY2 ENDP
END

8
D. Using Keyboard as Piano keys

In this task you will use the keyboard keys to generate different tones. Use the keys as in the
following table
Keyboard Key Tone Duration
(approximately)
A A4 500 ms ( ½ t)
B B4b 500 ms ( ½ t)
C C4 500 ms ( ½ t)
D D4 500 ms ( ½ t)
E E4 500 ms ( ½ t)
F F4 500 ms ( ½ t)
G G4 500 ms ( ½ t)
H C5 500 ms ( ½ t)

Use the keyboard to play “Happy birthday” music.

You might also like