石家庄铁道大学四方学院集中实践报告书课题名称双机串行通信设计姓名邢志杰学号20127019系、部电气工程系专业班级方1210-4指导教师马丽2015年7月3日※※※※※※※※※※※※※※※※※※※※※※※※2012级单片机接口课程设计一、设计任务及要求:设计任务:双机串行通信设计设计要求:1、两片单片机利用串行口进行串行通信:串行通信的波特率可从键盘进行设定,可选的波特率为1200、2400、4800和9600bit/s。串行口工作方式为方式1的全双工串行通信。2、两个单片机之间进行通讯波特率的设定,最终归结到对定时计数器T1计数初值TH1、TL1进行设定。故本题目本质上是通过键盘扫描得到设定的波特率,从而载入相应的T1计数初值TH1、TL1实现的。3、要求发送方读入按键值,发送到接收方,接收方接受数据并显示在数码管上。4、要求做出实物。二、指导教师评语:三、成绩指导教师签名:年月日目录第1章设计目的······································································错误!未定义书签。第2章设计要求······································································错误!未定义书签。第3章硬件电路设计································································错误!未定义书签。3.1系统框图·····························································································13.2STC89C52单片机最小系统·······································································23.3按键电路·····························································································33.4主电路设计··································································错误!未定义书签。第4章软件程序设计························································································44.1主程序流程图·······················································································44.2键盘扫描子程序流程图···········································································54.3从机主程序流程图·················································································64.4从机中断子程序流程图···········································································74.5程序调试·····························································································84.6双机串行通信源程序··············································································9第5章结论···································································································13参考文献········································································································131第1章设计目的1.1设计目的(1)掌握单片机实际系统的开发步骤。(2)了解串行通信的原理;了解数码管显示的工作原理;了解键盘扫描的工作原理;对双机串行通信软件编程、调试、相关硬件设备的使用技能等方面得到真正的实践机会,把软硬件结合,克服其中的种种问题,提高编程能力。第2章设计要求2.1设计要求(1)两片单片机利用串行口进行串行通信:串行通信的波特率可从键盘进行设定,可选的波特率为1200、2400、4800和9600bit/s。串行口工作方式为方式1的全双工串行通信。(2)两个单片机之间进行通讯波特率的设定,最终归结到对定时计数器T1计数初值TH1、TL1进行设定。故本题目本质上是通过键盘扫描得到设定的波特率,从而载入相应的T1计数初值TH1、TL1实现的。(3)要求发送方读入按键值,发送到接收方,接收方接受数据并显示在数码管上。(4)要求做出实物。第3章硬件电路设计3.1系统框图2图3-1系统框图3.2STC89C52单片机最小系统89C52共有四个八位的并行双向口,即有32根输入输出口线。各口的每一位均由锁存器、输出驱动器和输入缓冲器组成。VCC(40引脚):电源电压VSS(20引脚):接地图3-2STC89C52引脚图P0端口(P0.0~P0.7,39~32引脚):P0口是一个漏极开路的8位双向I/O口。作为输出端口,每个引脚能驱动8个TTL负载,对端口P0写入“1”时,可以作为高阻抗输入。在访问外部程序和数据存储器时,P0口也可以提供低8位地址和8位数据的复用总线。此时,P0口内部上拉电阻有效。在FlashROM编程时,P0端口接收指令字节;而在校验程序时,则输出指令字节。验证时,要求外接上拉电阻。按键电路单片机1AT89C52单片机2AT89C52显示电路3P1端口(P1.0~P1.7,1~8引脚):P1口是一个带内部上拉电阻的8位双向I/O口。P1的输出缓冲器可驱动(吸收或者输出电流方式)4个TTL输入。对端口写入1时,通过内部的上拉电阻把端口拉到高电位,这是可用作输入口。P1口作输入口使用时,因为有内部上拉电阻,那些被外部拉低的引脚会输出一个电流。P1口特点是输出锁存器,输出时没有条件。输入缓冲,输入时有条件,即需要先将该口设为输入状态,先输出1。此外,P1.0和P1.1还可以作为定时器/计数器2的外部技术输入(P1.0/T2)和定时器/计数器2的触发输入(P1.1/T2EX)。P3口为准双向口。可以字节访问,也可以位访问。P3.0---RXD,串行输入口。P3.1---TXD,串行输出口。P3.2---INT0,外部中断0的请求。P3.3---INT1,外部中断1的请求。P3.4---T0,定时器/计数器0外部计数脉冲。P3.5---T1,定时器/计数器,1外部计数脉冲。P3.6---WR,外部数据存储器写选通。P3.7---RD,外部数据存储器读选通。RST(9引脚):复位输入。当输入连续两个机器周期以上高电平时为有效,用来完成单片机单片机的复位初始化操作。ALE(30引脚):地址锁存控制信号(ALE)是访问外部程序存储器时,锁存低8位地址的输出脉冲。XTAL1(19引脚):振荡器反相放大器和内部时钟发生电路的输入端。XTAL2(18引脚):振荡器反相放大器的输入端。STC89C52引脚图如图3-2所示。3.3按键电路445673210图3-3按键电路图本设计按键采用矩阵键盘,键盘连接主机的P2口,主机从矩阵键盘接收信息,通过串行输出口输出到从机,从机从串行输入口接收信息并把信息显示在数码管上。3.4主电路设计aabbccddeeffgghh001122335566abcdefg4477P3P3P3P3hP10P11P12P1345673210P10P11P12P13D02D13D24D35D46D57D68D79Q019Q118Q217Q316Q415Q514Q613Q712LE11OE1U474HC573S3S0XTAL218XTAL119ALE30EA31PSEN29RST9P0.0/AD039P0.1/AD138P0.2/AD237P0.3/AD336P0.4/AD435P0.5/AD534P0.6/AD633P0.7/AD732P1.0/T21P1.1/T2EX2P1.23P1.34P1.45P1.56P1.67P1.78P3.0/RXD10P3.1/TXD11P3.2/INT012P3.3/INT113P3.4/T014P3.7/RD17P3.6/WR16P3.5/T115P2.7/A1528P2.0/A821P2.1/A922P2.2/A1023P2.3/A1124P2.4/A1225P2.5/A1326P2.6/A1427U1AT89C52XTAL218XTAL119ALE30EA31PSEN29RST9P0.0/AD039P0.1/AD138P0.2/AD237P0.3/AD336P0.4/AD435P0.5/AD534P0.6/AD633P0.7/AD732P1.0/T21P1.1/T2EX2P1.23P1.34P1.45P1.56P1.67P1.78P3.0/RXD10P3.1/TXD11P3.2/INT012P3.3/INT113P3.4/T014P3.7/RD17P3.6/WR16P3.5/T115P2.7/A1528P2.0/A821P2.1/A922P2.2/A1023P2.3/A1124P2.4/A1225P2.5/A1326P2.6/A1427U2AT89C52D02D13D24D35D46D57D68D79Q019Q118Q217Q316Q415Q514Q613Q712LE11OE1U374HC573C133pFC233pFX1CRYSTALR110kC433pFC533pFX2CRYSTALR210kC610uFR310kC310uF图3-4主电路图第4章软件程序设计5设计思路为:主机通过键盘扫描程序确认是否有键按下,若有键按下则将按键号对应的显示代码发送给从机,并判断是否是波特率按键,若是则进行波特率调整,若无键按下,则继续进行键盘扫描。从机主程序动态显示缓冲区内的数据。中断子程序接收数据并判断是否是波特率按键所对应的显示代码,若是则进行波特率调整,然后将数据保存到缓冲区。初始波特率都为9600。4.1主程序流程图键盘连接到甲机的P2口,通过按键扫描确定键值,从机与主机通过串行输入口P3.0和串行输出口P3.1相连接,实现两机之间的串行通信。图4-1主机主程序流程图4.2键盘扫描子程序流程图开始设置串行口工作方式及波特率检测按键确定按键号并发送波特率按键调整波特率YYNN6矩阵式键盘扫描的方法常用的有两种,一种是逐列送0,依次读回行;另一种为反转法。本程序采用前者,程序流程图如下:图4-2键盘扫描子程序流程图4.3从机主程序流程图从机主程序动态显示缓冲区的4个数据。从机的P1口经锁存器74HC573连接数码管位码,P2口经锁存器74HC573连接数码管的段码。从机与主机通过串行输入口P3.0和串行输出口P3.1相连接,利用中断来接收主机发来的数据,并根据接收的数据来判断是否需要进行波特率调整。从机主程序流程图如下:开始P2口列置0,行置1读回P2口得键号行值是否变化调整行,列值结束YN7图4-3从机主程序流程图4.4从机中断子程序流程图开始初始化送段码,送位码显示完?调整指针YN8图4-3从机中断子程序流程图4.5程序调