![](http://img.e-com-net.com/image/info8/f7334420553a4683bfc35b975850209c.gif)
PC机的主机箱上装有一只小喇叭,由定时器8253和并行接口芯片8255控制其发音,其电路如下:
8253 DRIVER
1.9318MHZ CLK
OUT2
8255
PB0 GATE2
PB1
8253用定时器2来产生896Hz的音频信号使喇叭发声,我们可以改变定时器2的计数值来改变声音频率,通过发声的延时程序来获得时间长短不同的声音,于是就可以通过编程;来获得声调(频率)和节奏(延时长短),使计算机演奏出乐曲来。如果再把音符1,2,3,4,
. . .
5,6,7,1,2,3,••••••与数字键1,2,3,4,5,6,7,8,9,••••••对应起来,就可以把计算机变成电子琴。
以下给出有关资料:
1.音符 1, 2, 3, 4 ,5 ,6, 7, 1, 2, 3, 4, 5, 6, 7, ⅰ
频率 131 147 165 175 196 220 247 262 294 330 349 392 440 494 523
2.计数值计算公式
计数值=1.19318MHz(时钟频率)/ 给定频率
=1234DCH / 给定频率
8253道地地定时器通道2的端口地址:42H(存放计数值,先送低位字节,后送高位字节)
命令寄存器地址:43H
4.编程参考:
1) 计算计数值程序段:
MOV DI, 给定频率
MOV DX, 12H
MOV AX, 34DCH
DIV DI
AX中即为计数值。
2) 打开扬声器发声,8255PB0,PBI送出高电平:
IN AL 61H
OR AL 3
OUT 61H AL
3)关闭扬声器,停止发声:
IN AL 61H
OR AL 0FCH
OUT 61H AL
二.实验内容:
1. 设计程序让微机演奏一段简单乐曲
2. 利用DOS的键盘管理功能。将微机变为一个具有简单功能的电子琴(选作)。 提问者: 灭世刀 - 助理 二级最佳答案;下面是sound.asm通用发声程序
public soundf
cseg segment para'code'
assume cs:cseg
soundf proc far
push ax
push bx
push cx
push dx
push di
mov al,0b6h
out 43h,al
mov dx,12h
mov ax,348ch
div di
out 42h,al
mov al,ah
out 42h,al
in al,61h
mov ah,al
or al,3
out 61h,al
wait1: mov cx,4815
call waitf
delay: loop delay
dec bx
jnz wait1
mov al,ah
out 61h,al
pop di
pop dx
pop cx
pop bx
pop ax
ret
soundf endp
waitf proc near
push ax
waitf1:
in al,61h
and al,10h
cmp al,ah
je waitf1
mov ah,al
loop waitf1
pop ax
ret
waitf endp
cseg ends
end
;下面是music.asm一个示例乐曲可以在数据段该成别的
extrn soundf:far
stack segment para stack 'stack'
db 64 dup('stack...')
stack ends
dseg segment para 'data'
mus_freq dw 330,294,262,294,3 dup(330)
dw 3 dup(294),330,392,392
dw 330,294,262,294,4 dup(330)
dw 294,294,330,294,262,-1
mus_time dw 6 dup(25),50
dw 2 dup(25,25,50)
dw 12 dup(25),100
dseg ends
cseg segment para 'code'
assume cs:cseg,ss:stack,ds:dseg
music proc far
mov ax,dseg
mov ds,ax
lea si,mus_freq
lea bp,ds:mus_time
freq:
mov di,[si]
cmp di,-1
je end_mus
mov bx,ds:[bp]
call soundf
add si,2
jmp freq
end_mus:
mov ax,4c00h
int 21h
music endp
cseg ends
end music
;下面是piano键盘数字键变成电子琴
extrn soundf:far
stack segment para stack 'stack'
db 64 dup('stack...')
stack ends
dseg segment para 'data'
table dw 262,294,330,349,392,440,494,523
dseg ends
cseg segment para 'code'
assume cs:cseg,ds:dseg,ss:stack
main proc far
mov ax,dseg
mov ds,ax
new_note:
mov ah,0
int 16h
cmp al,0dh
je exit
mov bx,offset table
cmp al,'1'
jb new_note
and ax,0fh
shl ax,1
sub ax,2
mov si,ax
mov di,[bx][si]
mov bx,10
call soundf
jmp new_note
exit:
mov ax,4c00h
int 21h
main endp
cseg ends
end main
第一题masm sound 后masm music 后link sound+music
第二题masm piano 后link sound+music(前提是sound已经masm过) 0回答者: hxl9841007 - 经理 四级
![](http://img.e-com-net.com/image/info8/3315dfc396bf4bfeafce02eb8d4e7b41.gif)