要求:
1、双机通过串口连接
2、主机接受矩阵键盘按下的值,处理后通过串口发送对应数据给从机
3、从机接收主机发送的数据,通过处理后,LCD实时显示发送的数值,并在“=”发送后开始运算,同时显示结果
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
源程序:
代码:
- /**
- P1:按键
- P2:数据LCD
- P3:RW,RW,EN
- 主从机功能分配:
-
- 主机以字符串形式接收数据,并实时发送给从机,从机实时进行LCD显示
-
- 当主机检测输入为"="时,从机检测接收到"=",从机计算结果,再进行LCD显示
- */
- #include <reg52.h>
- #include <stdio.h>
- #include "Keyscan.h"
- #include "usart.h"
- unsigned char num;
- //unsigned char str;
- // 主机: 以字符串形式接收数据,并实时发送给从机
- void main()
- {
- UsartInit();
- delay(200);
- while(1)
- {
- num=KeyPro();//键盘扫描
- if(num!=0xff) //按键按下
- {
- putchar(num);
- }
- }
- }
代码:
- //主机main函数
- void main()
- {
- UsartInit();
- delay(200);
- while(1)
- {
- num=KeyPro();
- if(num!=0xff)
- {
- putchar(num);
- }
- }
- }
- //从机main函数
- UsartInit();
- LCD_init();
- putstring("Hello,this is calaulator\r\n");
- putstring("He is made from Hebin and Wangshaoji\r\n");
- showstring(0,0,Hello_0);
- showstring(0,1,Hello_1);
- delayms(500);
- while(1)
- {
- showstring(0,0,dis_table0);
- showstring(0,1,dis_table1);
- }
- }
代码和仿真都放在资料中了
注意只支持Proteus7.5版本,其他版本打开都会报错的,dsn文件和代码51hei下载地址: