第三次实验报告姓名:陈柯吉学号:2012141441338第三次实验8253定时器/计数器与接口实验------乐曲程序计算机是如何产生音乐和声响的呢?原来在计算机中有一个可编程时间间隔定时器8253,它能根据程序提供的计数值和工作模式,产生各种形状和各种频率的计数/定时脉冲,提供给系统的各个部件使用。它还可以产生不同频率的脉冲作为扬声器的声源。在8253定时器内部有3个独立工作的计数器:Counter0,Counter1,Countre2,每个计数器都分配有一个端口地址,分别是40H,41H,42H。8253内部还有一个公用的控制寄存器,端口地址为43H,端口地址输入到8253的CS,A1,A0端,分别对三个计数器和控制器寻址。对8253编程时,先要设定控制字,以选择计数器,确定工作模式和计数值的格式,每个计数器由三个引脚与外部联系,CLK为时钟输入端,GATA为门控制信号输入端,OUT为计数/定时信号输入端。每个计数器是以倒计数的方式计数,也就是说,从计数初值开始逐次减1,直到减为0为止。端口地址与计数器的关系:40H选中计数器0:作为定时器为系统日时钟提供计时基准41H选中计数器1:作为定时器使用42H选中计数器2:用来控制扬声器发声43H公用的控制器:分别对三个寄存器和控制器寻址那么,如何触动扬声器发出声音呢?原来PC机的主音箱上装有一只小喇叭,由定时器8253和并行接口芯片8255(可编程外围接口芯片)控制其发声,8255包括三个8位寄存器,两个用于输入功能,一个用于输出功能。输入寄存器分配的I/O端口地址为60H和62H,输出寄存器分配的I/O端口地址为61H。8253定时器计数器2连接到扬声器,其电路如下:8253DRIVER1.9318MHZCLK喇叭OUT28255PB0GATE2PB1装入计数器2的计数初值为533H(1.19318MHz/896Hz=1331=533H),这样,得到的控制字为10110110B=0B6H,即选中计数器2,读/写,工作方式三(方波发声器),二进制。计数器2的初始化程序为:MOVAL,0B6HOUT43H,AL与门门22门1门1)计算计数值程序段:MOVDI,给定频率MOVDX,12HMOVAX,34DCHDIVDIAX中即为计数值。2)打开扬声器发声,8255PB0,PBI送出高电平:INAL,61HORAL,3OUT61H,AL3)关闭扬声器,停止发声:INAL,61HANDAL,0FCHOUT61H,AL两只老虎的音频表定义在数据段中,如下:STACKSEGMENTDW100DUP(?)STACKENDSDATASEGMENTMUSICDW2DUP(262,294,330,262);频率表,1231;1231DW2DUP(330,349,392);345;345;DW2DUP(392,440,392,349,330,262);565431;565431dw2dup(294,196,262),0;251;251,0表示结束TIMEDW10DUP(250*50),500*50,250*50,250*50,500*50;节拍表DW2DUP(120*50,120*50,120*50,120*50,250*50,250*50)DW2DUP(250*50,250*50,500*50)NEQU32;32个音符DATAENDS二.实验目的:学习8253计数器2输出方波信号用以驱动扬声器发声的原理,通过程序设置计数器2的输出波形的频率和延续时间,控制扬声器的音调和发生长短。三.实验内容:1.设计程序让微机演奏一段两只老虎的乐曲。2.思考如何让PC机演奏一遍,两遍以及数遍这段两只老虎的乐曲,并修改程序实现。四.实验报告:整理出运行正确的源程序清单(加以中文注释),并给出详细程序流程图。开始输入1~9的数字选择循环次数打开扬声器调用延时子程序关闭扬声器判断是1或2调用SONG子程序否循环次数=0?判断是ESC吗吗输入一个字符判断是1~7,Q~U,A~J吗根据预先设置好的频率赋值给变量否12结束是否结束关闭扬声器打开扬声器调用延时子程序STACKSEGMENTDW100DUP(?)STACKENDSDATASEGMENTMUSICDW2DUP(262,294,330,262);频率表,1231;1231DW2DUP(330,349,392);345;345;DW2DUP(392,440,392,349,330,262);565431;565431dw2dup(294,196,262),0;251;251,0表示结束TIMEDW10DUP(250*50),500*50,250*50,250*50,500*50;节拍表DW2DUP(120*50,120*50,120*50,120*50,250*50,250*50)DW2DUP(250*50,250*50,500*50)NEQU32;32个音符MDB?SAYDB'Pleaseinputthetimes(1~9)youwant:','$'SAY2DB'1.TIGERS2.PIANO:','$'SAY3DB'ERROR','$'DATAENDSCODESEGMENTASSUMECS:CODE,DS:DATA,ES:DATA,SS:STACK;说明代码段、数据段、附;加段、堆栈段START:MOVAX,DATA;设置数据段的段地址MOVDS,AXMOVES,AXAGA:LEADX,SAY2MOVAH,9INT21H;输出“1.TIGERS2.PIANO”MOVAH,1INT21H;输入一个字符CMPAL,31HJETIGERS;与一比较,相等则转TIGERSCMPAL,32HJEPIANO2;与二比较,相等则转PIANOMOVDL,0AH;以下三行输入一个回车MOVAH,2INT21HLEADX,SAY3MOVAH,9INT21H;显示输出'ERROR'JMPAGATIGERS:LEADX,SAYMOVAH,9INT21H;输出“TINGJIBIAN:$”CALLINPUTA:CALLSONGDECMJNZA;M自减1JMPQUIT2PIANO2:CALLPIANO;---------------------------------------------------------------------------------------------------------------------;子程序INPUT;输入;---------------------------------------------------------------------------------------------------------------------INPUTPROCMOVAH,1INT21H;输入一个字符LEASI,MSUBAL,30H;将ASCII码转为16进制数MOV[SI],ALRETINPUTENDP;---------------------------------------------------------------------------------------------------------------------;---------------------------------------------------------------------------------------------------------------------;子程序SONG;播放音乐;---------------------------------------------------------------------------------------------------------------------SONGPROCMOVAL,0B6HOUT43H,AL;选择计数器2LEASI,MUSICLEADI,TIMEMOVCX,N;循环次数SONG2:PUSHCXPUSHDIMOVDI,[SI]MOVDX,12HMOVAX,34DCHDIVDI;设置计数初值OUT42H,ALMOVAL,AHOUT42H,ALINAL,61HORAL,3OUT61H,AL;打开扬声器POPDIMOVCX,20000CALLDELAY;调用延时子程序INAL,61HANDAL,0FCHOUT61H,AL;关闭扬声器ADDSI,2ADDDI,2POPCXLOOPSONG2SONGENDP;---------------------------------------------------------------------------------------------------------------------;---------------------------------------------------------------------------------------------------------------------;子程序DELAY;延时子程序;---------------------------------------------------------------------------------------------------------------------DELAYPROCPUSHAXPUSHBXMOVBX,[DI]DELAY1:AGAIN:LOOPAGAINDECBXJNZDELAY1POPBXPOPAXRETDELAYENDP;---------------------------------------------------------------------------------------------------------------------;---------------------------------------------------------------------------------------------------------------------;子程序piano;---------------------------------------------------------------------------------------------------------------------PIANOPROCMOVDL,0AHMOVAH,2INT21HLP:MOVAH,1INT21H;输入一个字符HIGH1:CMPAL,31HJNEHIGH2MOVDI,523JMPSOUNDHIGH2:CMPAL,32HJNEHIGH3MOVDI,587JMPSOUNDHIGH3:CMPAL,'3'JNEHIGH4MOVDI,659JMPSOUNDHIGH4:CMPAL,'4'JNEHIGH5MOVDI,698JMPSOUNDHIGH5:CMPAL,'5'JNEHIGH6MOVDI,784JMPSOUNDHIGH6:CMPAL,'6'JNEHIGH7MOVDI,880JMPSOUNDHIGH7:CMPAL,'7'JNEMIDDLE1MOVDI,988JMPSOUNDMIDDLE1:CMPAL,'Q'JNEMIDDLE2MOVDI,262JMPSOUNDMIDDLE2:CMPAL,'W'JNEMIDDLE3MOVDI,294JMPSOUNDMIDDLE3:CMPAL,'E'JNEMIDDLE4MOVDI,330JMPSOUNDMIDDLE4:CMPAL,'R'JNEMIDDLE5MOVDI,349JMPSOUNDMIDDLE5:CMPAL,'T'JNEMIDDLE6MOVDI,392JMPSOUNDMIDDLE6:CMPAL,'Y'JNEMIDDLE7MOVDI,440JMPSOUNDMIDDLE7:CMPAL,'U'JNELOW1MOVDI,49