1键盘扫描实验实验报告一、实验目的1.掌握线反转法键盘扫描原理。2.了解单片机的输入和输出过程,理解单片机的数据采集过程。二、实验内容单片机外接4x4键盘,通过线反转法判断按下的键,并在数码管上显示按键对应的数字。第一行从左到右分别是开关K0,K1,K2,K3,第二行从左到右分别是K4,K5,K6,K7以此类推。当按下Kn时,在数码管上显示数字n。三、实验原理线翻转法:先对行(R0-R3)置0,对列(R4-R7)置1。当有键被按下时,会把按键所在的列的电位从1变0,记录下位置;然后再将行列翻转,记录下按下键的所在行,两数进行或运算,就可以得到一个唯一表示按下键的数字。例如:假定R0-R7分别与单片机的P2.0-P2.7相连。先把R4-R7置1,R0-R3置0(通过指令MOVP2,#0F0H实现)。当键K5被按下时,R5电位被拉低为低电平。此时,P2口表示的数为:11010000(0xD0);然后再置R4-R7为0,R0-R3为1,此时,R1电位被拉低为低电平,此时,P2口表示的数为:00001101(0x0D)。将两数相与取反,得到:00100010。如此类推,可以把十六个开关和得到的数字的关系计算出来,如下表:对应开关二进制十六进制对应开关二进制十六进制K0000100010x11K8000101000x14K1001000010x21K9001001000x24K2010000010x41K10010001000x44K3100000010x81K11100001000x84K4000100100x12K12000110000x18K5001000100x22K13001010000x28K6010000100x42K14010010000x48K7100000100x82K15100010000x882四、实验过程1.连接好单片机及其外围设备电路32.编写汇编程序ORGLJMPKeyLJMPK7:CJNER2,#82H,K8ORG0100HMOVP0,#0F8HInit:CLRP1.3LJMPKeyMOVP0,#0C0HK8:CJNER2,#14H,K9Key:MOVP2,#0F0HMOVP0,#080HMOVA,P2LJMPKeyMOVR1,AK9:CJNER2,#24H,K10MOVP2,#0FHMOVP0,#090HMOVA,P2LJMPKeyORLA,R1K10:CJNER2,#44H,K11CPLAMOVP0,#088HMOVR2,ALJMPKeyJNZKeyProK11:CJNER2,#84H,K12LJMPKeyMOVP0,#083HKeyPro:CJNER2,#11H,K1LJMPKeyMOVP0,#0C0HK12:CJNER2,#18H,K13LJMPKeyMOVP0,#0C6HK1:CJNER2,#21H,K2LJMPKeyMOVP0,#0F9HK13:CJNER2,#28H,K14LJMPKeyMOVP0,#0A1HK2:CJNER2,#41H,K3LJMPKeyMOVP0,#0A4HK14:CJNER2,#48H,K15LJMPKeyMOVP0,#086HK3:CJNER2,#81H,K4LJMPKeyMOVP0,#0B0HK15:CJNER2,#88H,K16LJMPKeyMOVP0,#08EHK4:CJNER2,#12H,K5LJMPKeyMOVP0,#099HK16:LJMPKeyLJMPKeyENDK5:CJNER2,#22H,K6MOVP0,#092HLJMPKeyK6:CJNER2,#42H,K7MOVP0,#082H五、实验结果1.当按下开关Kn时,数码管能够显示对应的数字。并且P2口的实际输出值跟理论值完全符合。(也就是按下K0,K1,…,K15,数码管上会显示0,1,2,…,F)