发送
- #include <reg51.h>
- #define DPIO_DIG P0
- #define DPIO_PLACE P2
- sbit key1=P1^0;
- sbit key2=P1^1;
- sbit key3=P1^2;
- sbit key4=P1^3;
- unsigned char code LED[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0x39,0xff};//共阳极数码管显示的从0~c,即段码,最后一个表示全灭
- unsigned char LEDBuf[]={0xc0,0xc0,0xc0,0xc0};//数据显示缓冲区
- unsigned char code PLACE_CODE[]={0x01,0x02,0x04,0x08};//存放位选的数组,同时放在rom存储器
- unsigned char KeyNum=0;
- unsigned int Numcon=0,num=0;
- bit flag=0;
- void DelayXms(unsigned int t)
- {
- unsigned int i,j;
- for(i=t;i>0;i--)
- for(j=120;j>0;j--);
- }
- void Key_Scan()
- {
- static unsigned char i;
- static bit sign=0;
- if(!key1||!key2||!key3||!key4)
- {
- if(++i>=10 && sign==0)
- {
- sign=1;
- if(!key1)KeyNum=1;
- if(!key2)KeyNum=2;
- if(!key3)KeyNum=3;
- if(!key4)KeyNum=4;
- }
- }
- else
- {
- i=0;
- sign=0;
- }
- }
- /*----------------------------
- 串口发送一个字节数据
- ----------------------------*/
- void SendData(unsigned char dat)
- {
- SBUF = dat;
- while(!TI);
- TI = 0;
- }
- void Disply()
- {
- static unsigned char i;
- DPIO_DIG=0xff;//消隐
- DPIO_PLACE=PLACE_CODE[i];//送位选
- DPIO_DIG =LEDBuf[i];//送段码
- i=++i%4;
- }
- void main()
- {
- PCON &= 0x7F; //波特率不倍速
- SCON = 0x50; //8位数据,可变波特率
- TMOD &= 0x0F; //清除定时器1模式位
- TMOD |= 0x20; //设定定时器1为8位自动重装方式
- TL1 = 0xFD; //设定定时初值
- TH1 = 0xFD; //设定定时器重装值
- ET1 = 0; //禁止定时器1中断
- TR1 = 1; //启动定时器1
- while(1)
- {
- Key_Scan();
- switch(KeyNum)
- {
- case 1 :if(Numcon<9999)Numcon++;KeyNum=0;break;
- case 2 :if(Numcon>0)Numcon--;KeyNum=0;break;
- case 3 :Numcon=0;KeyNum=0;break;
- case 4 :Numcon=88;KeyNum=0;break;
- }
- if(Numcon!=num)//数据有变化
- {
- num=Numcon;
- LEDBuf[0]=LED[num/1000%10];
- LEDBuf[1]=LED[num/100%10];
- LEDBuf[2]=LED[num/10%10];
- LEDBuf[3]=LED[num%10];
- SendData(0xaa);//数据头
- SendData(num>>8);//高8位
- SendData(num);//低8位
- }
- Disply();
- DelayXms(1);
- }
- }
复制代码 接收
- #include <reg51.h>
- #define DPIO_DIG P0
- #define DPIO_PLACE P2
- unsigned char code LED[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0x39,0xff};//共阳极数码管显示的从0~c,即段码,最后一个表示全灭
- unsigned char LEDBuf[]={0xc0,0xc0,0xc0,0xc0};//数据显示缓冲区
- unsigned char code PLACE_CODE[]={0x01,0x02,0x04,0x08};//存放位选的数组,同时放在rom存储器
- unsigned char rec_buf[3];
- bit flag=0;
- void DelayXms(unsigned int t)
- {
- unsigned int i,j;
- for(i=t;i>0;i--)
- for(j=120;j>0;j--);
- }
- void Disply()
- {
- static unsigned char i;
- DPIO_DIG=0xff;//消隐
- DPIO_PLACE=PLACE_CODE[i];//送位选
- DPIO_DIG =LEDBuf[i];//送段码
- i=++i%4;
- }
- void main()
- {
- unsigned int Numcon=0;
- PCON &= 0x7F; //波特率不倍速
- SCON = 0x50; //8位数据,可变波特率
- TMOD &= 0x0F; //清除定时器1模式位
- TMOD |= 0x20; //设定定时器1为8位自动重装方式
- TL1 = 0xFD; //设定定时初值
- TH1 = 0xFD; //设定定时器重装值
- ET1 = 0; //禁止定时器1中断
- TR1 = 1; //启动定时器1
- EA=1; //总中断开
- ES=1; //串口中断开
- while(1)
- {
- if(flag)
- {
- flag=0;
- if(rec_buf[0]==0xaa)//数据验证、解析
- {
- rec_buf[0]=0x00;
- Numcon=rec_buf[1]<<8|rec_buf[2];
- LEDBuf[0]=LED[Numcon/1000%10];
- LEDBuf[1]=LED[Numcon/100%10];
- LEDBuf[2]=LED[Numcon/10%10];
- LEDBuf[3]=LED[Numcon%10];
- }
- else//错误
- {
- LEDBuf[0]=0xbf;//'-'
- LEDBuf[1]=0xbf;
- LEDBuf[2]=0xbf;
- LEDBuf[3]=0xbf;
- }
- }
- Disply();
- DelayXms(1);
- }
- }
- void UARTInterrupt() interrupt 4
- {
- static unsigned char i;
- RI=0; //接收中断请求标志位清0
- rec_buf[i]=SBUF; //保存SBUF接收到的数据
- if(rec_buf[0]==0xaa) //验证数据头
- {
- i++;
- if(i>=3)
- {
- i=0;
- flag=1; //数据串接收完成标志置1
- }
- }
- }
复制代码
|