问题:
在串口数据接收中,如果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;
}
我改了一下程序你参考一下。单片机接收到数据才改变现状的显示状态。不接收到数据一直保持显示。
- /*******************************
- *从串口中得到数据。
- 如果得到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^0; //LCD1602 数据命令选择
- sbit RW = P2^1;//LCD1602 读写选择
- sbit EP = P2^2; //使能端
- 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)
- {
- 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;
- lcd_wcmd(0x80);
- lcd_wdat(a);
- if(a == 1)
- {
-
- i = 0;
- lcd_wcmd(0x80 + 0X40);
- while(ON[i] != ' ')
- {
- lcd_wdat(ON[i]);
- i++;
- }
- }
- else if(a == 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++;
- }
- }
- flag = 0;
- }
- }
- }
- void serial() interrupt 4
- {
- RI = 0;
- a = SBUF;
- flag = 1;
- }
复制代码同样原因,你是使用死循环输出数据,结果就如 依然落叶飘零 所说一样;
调试的话可以让发送端只发送0和1,你就能看到效果了
一周热门 更多>