您的位置 首页 电子

单片机键盘输入及串口发送

includereg52htypedefunsignedcharuchar;typedefunsignedintuint;数码管端口控制sbitSG=P0^0;控制个位sbitSS

#include”reg52.h”

typedef unsigned char uchar;
typedef unsigned int uint;

//数码管端口操控
sbit SG = P0^0;//操控个位
sbit SS = P0^1;//操控十位
unsigned char code rst[6]={0xe4,0xc0,0xe0,0xc0,0xe0,0x22};//界说一个code类型 完成软件复位
uchar tab0[] = {0x81,0xbd,0x92,0x98,0xac,0xc8,0xc0,0x9d,0x80,0x88}; //0~9共阳数码管
uchar tab1[2] ={0xfe,0xfe} ;
uchar SendBuf[10];//发送的数
uchar *p;
uchar t1,t2;
uchar channel;
uchar SendLen=0,BufLen=7;
Timer_0();//数码动态扫描的守时函数
Delay(uint t);//按键去抖的延时函数
uint Key_1();//键盘扫描函数
uint Key_2();//键盘返回值函数
Delay();//延时函数
Reset();//复位函数
Affirm();//承认发送函数
void init_ser1();//串口初始化函数
void SendTo();
void Coding(char,char,char,char,char);
main()
{
ucharn;
init_ser1();//串口初始化函数
P2=0x00;
P3=0x0f;
p=tab1;
Timer_0(); //守时器初始化函数
SendTo();
t1 = 0;
t2 = 0;
while(1)
{

n=Key_2();
if(n!=0xff)
{

//Led=0;
if(*p==0xfe)
{
if(p==tab1)
{
t1 = n;
}
else if(p==tab1+1)
{
t2 = n;
}
//Led=1;
Delay(9000); //特别要害的延时
//Led=0;

*p=tab0[n];
p++;
}
}

}
}
uint Key_1()
{
uint i,j;
if(P2!=0x00)
{
Delay(300);//去除按键颤动
if(P2!=0x00)
{
i=P2;
return(i);
}
}
if(P3!=0xf0)
{
Delay(300);
if(P3!=0xf0)

j=P3;
return(j);
}

}
uint Key_2()
{
switch(key_1()) //依据扫描成果选取值
{
case 0x01:return 1;break;
case0x02:return 2;break;
case0x04:return 3;break;
case0x08:return 4;break;
case0x10:return 5;break;
case0x20:return 6;break;
case0x40:return 7;break;
case0x80:return 8;break;
case0x1f:return 9;break;
case0x2f:Reset(); break;
case0x4f:return 0;break;
case0x8f:Affirm();break;
default:return 0xff;break;
}
}
Delay(uint t)//延时函数
{

while(–t);

}
Reset()//铲除复位函数
{
Delay(20000);
Coding(0,0,0,0,0);
(*((void (*)())rst))();//复位
}
Affirm()//承认发送函数
{


Delay(20000);
channel = t1*10 + t2;
Coding(channel,0,0,0,0);
SendTo();

//flag=1;
}
Timer_0()//守时器0初始化
{
TMOD=TMOD&0xf0|0x01;
TR0=1;
ET0=1;
//EA=1;
}
interrupt_0() interrupt 1//数码管扫描
{
TH0=0xf8;
TL0=0x30;

P1=tab1[0];
SG=1;
SS=0;
Delay(150);

P1=tab1[1];
SG=0;
SS=1;

}

void Coding(char CHANNEL,char Command3,char Command4,char Command5,char Command6)
{
SendBuf[0]=0xFF;
SendBuf[1]=CHANNEL;
SendBuf[2]=Command3;
SendBuf[3]=Command4;
SendBuf[4]=Command5;
SendBuf[5]=Command6;
SendBuf[6]=Command6+Command5+Command4+Command3+CHANNEL;
}

void init_ser1()
{

// REN=1;//答应串行口接纳数据
//
// SM0=0;
// SM1=1;//串行口工作方式为1
//
// TMOD=TMOD&0xf0|0x20;//经过守时器1设置串口波特率
// PCON|=0x0;
// TH1=0xFD;
// TL1=0xFD; //波特率9600
// PS=1;
//
// TR1=1;//发动守时器1
// ES=1;//开串行口中止
// PS=1;
PCON=0X00;
SCON=0X50;
TMOD|=0X20;
TH1=0xFd;
TL1=0xFd;
TI=1;
EA=1;
TR1=1;
ES=1;
}
void Serial(void)interrupt 4
{
if(RI)
{
RI=0;
}
if(TI)
{
TI=0;
//Delay(20);
if(SendLen >= BufLen) BufLen = 0;
if(BufLen)
{
SBUF = SendBuf[SendLen++];
}
}
}
void SendTo(void)
{
BufLen = 7;
SendLen = 0;
SBUF = “” ;
do{}
while(BufLen);
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/dianzi/258282.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部