标题:
16按键值单片机双机通信显示
[打印本页]
作者:
123789taa
时间:
2018-6-19 10:36
标题:
16按键值单片机双机通信显示
AT89C51单片机双机通信
QQ截图20180619103515.png
(42.72 KB, 下载次数: 31)
下载附件
2018-6-19 10:38 上传
主机程序
#include<reg51.h>
#define shuchu0 P0
#define shuchu2 P2
#define KEY P1
//unsigned char code num[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x7f};
unsigned char code num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; //共yang数码管
unsigned char KeyValue; //返回按键值
int i;
void Delay10ms(unsigned int c); //延时10ms
void KeyDown(); //检测按键函数
void main(void)
{
while(1)
{
unsigned char temp=0;
KeyDown(); //运行按键扫描函数,返回按键值KeyValue=0,1,2到15
TMOD=0x20; //设置定时器T为方式2
TH1=0xfd; //波特率9600
TL1=0xfd;
SCON=0x40; //串口初始化方式1发送,不接受
PCON=0x00; //SMOD=0,方式2,波特率=187.5kbit/s 波特率仅与SMOD的值有关 波特率选择位
TR1=1; //启动T1
i=KeyValue;
if(i<=9)
{
shuchu0 = num[0];
shuchu2 = num[KeyValue];
}
else
{
i=i-10;
shuchu0 = num[1];
shuchu2 = num[i];
}
while(1){
temp=KeyValue; //读入按键值
SBUF=temp; //数据串行口发送
while(TI==0); //如果TI=0,位发送完,循环等待
TI=0; //数据发送完成,把TI清零
break;
}
}
}
/*******************************************************************************
* 函 数 名 : KeyDown
* 函数功能 : 检测有按键按下并读取键值
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void KeyDown(void)
{
char a = 0;
KEY=0x0f;
if(KEY!=0x0f)//读取按键是否按下
{
Delay10ms(1);//延时10ms进行消抖
if(KEY!=0x0f)//再次检测键盘是否按下
{
//测试列
KEY=0X0F;
switch(KEY)
{
case(0X07): KeyValue=0;break;
case(0X0b): KeyValue=4;break;
case(0X0d): KeyValue=8;break;
case(0X0e): KeyValue=12;break;
}
//测试行
KEY=0XF0;
switch(KEY)
{
case(0X70): KeyValue=KeyValue+3;break;
case(0Xb0): KeyValue=KeyValue+2;break;
case(0Xd0): KeyValue=KeyValue+1;break;
case(0Xe0): KeyValue=KeyValue;break;
}
while((a<50) && (KEY!=0xf0)) //检测按键松手检测
{
Delay10ms(1);
a++;
}
}
}
}
void Delay10ms(unsigned int c)
{
unsigned char a, b;
for (;c>0;c--)
{
for (b=38;b>0;b--)
{
for (a=130;a>0;a--);
}
}
}
从机程序
#include<reg51.h>
unsigned char code num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; //共yang数码管
unsigned char i;
#define shuchu0 P0
#define shuchu2 P2
void main(void)
{
while(1){
unsigned char temp=0;
TMOD=0x20; //设置串口为方式1接收,REN=1,REN允许串行接收位
TH1=0xfd;
TL1=0xfd;
SCON=0x50;
PCON=0x00;
TR1=1; //启动T1
while(1)
{
while(RI==0); //若TI为0,未接收到数据
RI=0; //接收到数据,把RI清零
temp=SBUF; //读取数据存入temp中
i=temp;
if(i<=9)
{
shuchu0 = num[0];
shuchu2 = num[i];
}
else
{
i=i-10;
shuchu0 = num[1];
shuchu2 = num[i];
}
}
}
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1