- 打卡等级:常住居民II
- 打卡总天数:87
- 最近打卡:2025-04-30 10:02:32
已绑定手机
高级会员
- 积分
- 878
|
本帖最后由 欢迎哈哈哈3 于 2024-11-21 16:00 编辑
问一下大家,,在主程序中定义了个全局变量flg1,我用串口接收数据,当接受到以+号开始...#号结束的数据时,给标识符flg1赋相关的值来表示,并在主程序中通过LCD1602显示出来,如果不是以+号开始#号结束的数据时,则标识符flg1就继续在主程序中的液晶屏幕上,显示的值不变。
但是为什么我这个程序,当接受的不是以+号开始...#号结束的数据时,主屏幕上显示就变了呢,变成112了,
但是flg1就只存在串口接受程序和主程序显示,其他地方也没有使用flg1这个标识符。为什么会出现112这个数呢,是串口接收数据对全局变量有什么影响吗?
下面是部分程序
主程序:
#include "main.h"
#include "stc8.h"
#include "lcd1602.h"
#include "ESP8266.h"
#include "uart2.h"
#include "string.h"
unsigned char dataBuf[]={0};
unsigned char flg1=0;//定义全局变量
void setinoutport()
{
P0M1=0X00;P0M0=0X00;
P1M1=0X00;P1M0=0X02;
P2M1=0X00;P2M0=0X00;
P3M1=0X00;P3M0=0X02;
P0PU=0XFF;
}
main()
{
unsigned int t_ms=0,t_s=0;
setinoutport();
LCD_Initial();
uart2int();
esp8266int();
for(;;)
{
Print(15,1, t_s%10+'0');
Print(14,1, t_s%100/10+'0');
Print(13,1, t_s/100+'0');
if(flg1>=0 && flg1<=9)//显示接收到正确的数据值的标识
{
Print(6,1, flg1+'0');
Print(4,0, ' ');
Print(5,0, ' ');
Print(6,0, ' ');
}
else
{
Print(6,1, '?');//显示接收不是正确的数据值
Print(4,0, flg1/100+'0'); 显示flg1变化的值
Print(5,0, flg1%100/10+'0');
Print(6,0, flg1%10+'0');
}
Print(1,1, 3+'0');
t_ms++;
if(t_ms==100)
{
t_ms=0;t_s++;
}
if(t_s>10)
{
t_s=0;
sprintf(dataBuf,"Temp:%3.1fC\r\n",32.5);
PrintString("AT+CIPSEND=0,12\r\n",17); //·¢ˍʽ¾ݍ
DelayMs(500);
PrintString(dataBuf,12);
DelayMs(500);
}
}
}
串口接收程序
#include "stc8.h"
#include "main.h"
#include "string.h"
extern unsigned char RX_num=0,RX_i=0;
extern unsigned char RX_buffer[]={0};
unsigned char tbuf=25,i=0;
bit busy2,busy1;
sbit LED2 = P1^6;
sbit LED3 = P1^5;
extern unsigned char flg1;//引用全局变量flg1
void DelayUs2x(unsigned char t)
{
while(--t);
}
void uart2int(void) // 9600bps@11.0592MHz
{
S2CON = 0x50; // 8???,?????
AUXR |= 0x04; // ???2???Fosc,?1T
AUXR |= 0x01;
T2L = 0xE0; // 65536-11059200/9600/4 = FEC7H
T2H = 0xFE; // ??????
AUXR |= 0x10; // ?????2
IE2 = 0x01; // ??????
EA = 1; // ????
}
void PrintString(unsigned char *puts,unsigned char tt)
{
unsigned char NUM=0;
for (; NUM<tt; puts++)
{
S2BUF = *puts;
NUM++;
busy2 = 1;
while(busy2);
}
}
void Uart2(void) interrupt 8
{
IE2=0X00;
if (S2CON & S2RI)
{
S2CON =0x50;
RX_buffer[RX_num] = S2BUF;
if(RX_buffer[0]=='+')
{
RX_num++;
if((RX_buffer[RX_num-3]=='+') && (RX_buffer[RX_num-1]=='#'))
{
if(RX_buffer[RX_num-2]=='1') {LED2=1;flg1=1;RX_num=0;}//如果数据符合要求,则给flg1赋值
if(RX_buffer[RX_num-2]=='2') {LED2=0;flg1=2;RX_num=0; }
}
}
else {RX_num=0;} //如果数据不符合要求,则flg1保持不变
if(RX_num>tbuf) {RX_num = 0;}
}
if (S2CON & S2TI)
{
S2CON =0x50;
busy2 = 0;
}
IE2=0X01;
}
|
|