如题,单片机串口发送数据会连续发送几十条数据,求解!- #include <AT89X52.h>
- sbit E=P2^3;
- sbit RW=P2^4;
- sbit RS=P2^5;
- sbit key1=P1^0;
- sbit key2=P1^1;
- sbit key3=P1^2;
- unsigned char disbuf,j,write,key,keyf;
- void ckzd (void) interrupt 4
- {
- if(RI==1)
- {
- disbuf=SBUF;
- RI=0;
- }
- if(TI==1) TI=0;
- }
- //**************************************液晶启动代码**********
- void Delay(unsigned int t) //延时
- {
- unsigned int k;
- for(k=0;k<t;k++);
- }
- void LCD1602_busy(void)//检测忙状态
- {
- P0_7=1;
- RS=0;
- RW=1;
- E=1;
- while(P0_7==1);
- E=0;
- }
- void Write_com(unsigned char combuf)
- {
- RS=0;
- RW=0;
- P0=combuf;
- E=1;
- E=0;
- }
- void Write_busycom(unsigned char combuf)//检测忙状态并写指令
- {
- LCD1602_busy();
- Write_com(combuf);
- }
- void Write_busydata(unsigned char databuf)//检测忙状态并写数据
- {
- LCD1602_busy();
- RS=1;
- RW=0;
- P0=databuf;
- E=1;
- E=0;
- }
- void LCD1602_Write_address(unsigned char x,unsigned char y)//写地址
- {
- x&=0x0f;
- y&=0x01;
- if(y==0)
- Write_busycom(x|0x80);
- else
- Write_busycom((x+0x40)|0x80);
- }
- void LCD1602_init(void)//液晶初始化
- {
- Delay(1500); //调用延时函数
- Write_busycom(0x38); //8位数据总线,两行显示模式,5*7点阵显示
- Delay(500); //调用延时函数
- Write_busycom(0x38); //8位数据总线,两行显示模式,5*7点阵显示
- Delay(500); //调用延时函数
- Write_busycom(0x38); //8位数据总线,两行显示模式,5*7点阵显示
- Write_busycom(0x38); //8位数据总线,两行显示模式,5*7点阵显示
- Write_busycom(0x08); //显示功能关,无光标
- Write_busycom(0x01); //清屏
- Write_busycom(0x06); //写入新的数据后,光标右移,显示屏不移动
- Write_busycom(0x0C); //显示功能开,无光标
- }
- void LCD1602_Disp(unsigned char x,unsigned char y,unsigned char buf)
- {
- LCD1602_Write_address(x,y); //先将地址信息写入
- Write_busydata(buf); //再写入要显示的数据
- }
- //********************************液晶结束代码*************
- void main()
- {
- LCD1602_init();
- TMOD&=0x0F;
- TMOD|=0x20;
- SCON=0x50;
- TH1=0xFA;
- TL1=0xFA;
- TR1=1;
- EA=1;
- ES=1;
- while(1)
- {
- //液晶显示读取到的串口接收数据
- LCD1602_Disp(0,0,'R');
- LCD1602_Disp(1,0,':');
- LCD1602_Disp(2,0,'0');
- LCD1602_Disp(3,0,'x');
- //高四位
- j=disbuf>>4;
- if(j<10) j=j+'0';
- else j=j-10+'A';
- LCD1602_Disp(4,0,j);
- //低四位
- j=disbuf&0x0F;
- if(j<10) j=j+'0';
- else j=j-10+'A';
- LCD1602_Disp(5,0,j);
- //************************
- //液晶显示串口欲发送数据
- LCD1602_Disp(0,1,'W');
- LCD1602_Disp(1,1,':');
- LCD1602_Disp(2,1,'0');
- LCD1602_Disp(3,1,'x');
- //高四位
- j=write>>4;
- if(j<10) j=j+'0';
- else j=j-10+'A';
- LCD1602_Disp(4,1,j);
- //低四位
- j=write&0x0F;
- if(j<10) j=j+'0';
- else j=j-10+'A';
- LCD1602_Disp(5,1,j);
- if(key1==0) key=1;
- else if(key2==0) key=2;
- else if(key3==0) key=3;
- switch(key)
- {
- case 1:write=write+1;key=0;break;
- case 2:write=write-1;key=0;break;
- case 3:SBUF=write;TI=0;key=0;break;//会发送几十次
- }
-
- }
- }
复制代码
|