- #include<reg51.h>
- #define uchar flag1,flag2,flag3
- sbit key1=P1^0;
- sbit key2=P2^1;
- sbit key3=P2^2;
- flag1=1,flag2=1,flag3=1;
- unsigned char i;
- unsigned char n;
- unsigned char code Tab[ ]={0xA5,0X20}; //开始扫描命令
- unsigned char a[4]; //接收数据数组
- float b[1]; //提取后的数据数组
- void delay(unsigned int time);
- void Send(unsigned char dat) //发送命令
- {
- SBUF=dat;
- while(TI==0);
- TI=0;
- }
- void UsartInit()
- {TMOD=0x20; //定时器T1工作方式2
- SCON=0x50; //串口工作方式1允许接收(REN=1)
- PCON=0x00; //正常波特率9600
- TH1=0xfd; //初值设定,波特率9600
- TL1=0xfd;
- TR1=1; //打开计数器
- EA=1; //打开总中断
- ES=1;} //打开中断允许中的串口中断允许
- void key_scan() //按键扫描
- {if(!key1){delay(10);if(!key1)flag1=0;}
- if(!key2){delay(10);if(!key2)flag2=0;}
- if(!key3){delay(10);if(!key3)flag3=0;}
- }
- void delay(unsigned int time) //延时
- { unsigned int j = 0;
- for(;time>0;time--)
- for(j=0;j<110;j++);}
- void main()
- {
- UsartInit(); //串口初始化
- key_scan(); //按键扫描
- if(flag1==0&flag2==1&flag3==1) //判断是否开始扫描
- { flag1=1; //清零按键标志
- for (i=0;i<2;i++) //串口发送
- {
- Send(Tab[i]);
- delay(20);
- }
- delay(300);
- n=0;
- while(n<5) //串口接收
- while(RI==0);
- {a[n]=SBUF;
- RI=0;
- n++;}}
-
-
- }
复制代码
|