问题:
在串口数据接收中,如果SUBF,接收1,在LCD1602上显示 :TURN ON ,如果是0 ,显示TRUN OFF, 其他的话显示ERROR.
但实际运行中,能显示接受到的数据,但是显示TURN ON ,和TURN OFF时,只会显示ERROR.求高手解答。
程序如下:
/*******************************
*从串口中得到数据。
如果得到01,显示TURN ON
如果得到00,显示TRUN OFF
如果是其他,显示ERROR
在lcd1602上显示出来、
*********************************/
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define nop _nop_()
#define NOP _nop_()
sbit RS = P2^6; //LCD1602 数据命令选择
sbit RW = P2^5;//LCD1602 读写选择
sbit EP = P2^7; //使能端
typedef bit BOOL;
uchar flag, a, i; //flag为串口接收标志位。 a接受SBUF
uchar code table[] = {"I GET "};
uchar code ON[]={"TURN ON"};
uchar code OFF[] = {"TURN OFF"};
uchar code OTHER[] = {"ERROR"};
void delay(uchar ms)
{
uchar i;
while(ms--)
{
for(i=0; i<250; i++)
{
nop;
nop;
nop;
nop;
}
}
}
/*测试LCD1602是否忙*/
BOOL lcd_bz(void)
{
BOOL result;
RS = 0;
RW = 1;
EP = 1;
NOP;
NOP;
NOP;
NOP;
result = (BOOL)(P0 &0X80);
EP = 0;
return result;
}
/*lcd1602写命令*/
void lcd_wcmd(uchar cmd)
{
while(lcd_bz());
RS = 0;
RW = 0;
EP = 0;
P0= cmd;
nop;
nop;
nop;
nop;
EP = 1;
nop;
nop;
nop;
nop;
EP = 0;
}
/*LCD1602写数据*/
void lcd_wdat(uchar dat)
{
while(lcd_bz());
RS = 1;
RW = 0;
EP = 0;
nop;
nop;
P0 = dat;
nop;
nop;
nop;
nop;
EP = 1;
nop;
nop;
nop;
nop;
EP = 0;
}
/*清屏*/
void lcd_clr(void)
{
lcd_wcmd(0x01);
delay(5);
}
/*LCD1602初始化*/
void lcd_init(void)
{
delay(30);
lcd_wcmd(0X38);
delay(5);
lcd_wcmd(0X0C);
delay(5);
lcd_wcmd(0X06);
delay(5);
lcd_wcmd(0X01);
delay(5);
}
/*串口初始化*/
void serial_init(void)
{
TMOD = 0X20;
TH1 = 0XFD;
TL1 = 0XFD;
TR1 = 1;
REN = 1;
SM0 = 0;
SM1 = 1;
EA = 1;
ES = 1;
}
/*主函数*/
void main(void)
{
lcd_init();
serial_init();
while(1)
{
lcd_wcmd(0x01);//清屏指令
i = 0;
lcd_wcmd(0x80);
lcd_wdat(SBUF);
if(SBUF == 1)
{
i = 0;
lcd_wcmd(0x80 + 0X40);
while(ON[i] != ' ')
{
lcd_wdat(ON[i]);
i++;
}
}
else if(SBUF == 0)
{
i = 0;
lcd_wcmd(0x80 + 0x40);
while(OFF[i] != ' ')
{
lcd_wdat(OFF[i]);
i++;
}
}
else
{
i = 0;
lcd_wcmd(0x80 + 0x40);
while(OTHER[i] != ' ')
{
lcd_wdat(OTHER[i]);
i++;
}
}
if(flag)
{
ES = 0;
for(i=0; i<6; i++)
{
SBUF = table[i];
while(
ti == 0);
TI = 0;
}
SBUF = a;
while(!TI);
TI = 0;
ES = 1;
flag = 0;
}
}
}
void serial() interrupt 4
{
RI = 0;
a = SBUF;
flag = 1;
}
一周热门 更多>