我买了ESP 8266 01做控制用,但是在主循环里面我用了while死循环,结果诡异的是在判断完SendDataFlage等于1,执行完这个If判段语句后,他的while死循环就执行不下去了,就在这里断掉了,最离谱的是我做的串口中断
判断函数,在接收到6666666011,之后执行完蜂鸣器响之后,也不执行向下语句了,我在memset函数那里做了蜂鸣器响的功能,结果却发现语句完全没有执行到这里。我最后单独运行串口中断接受函数或SendDataFlage函数,while循环语句就很顺畅的执行了,实在找不到那里出错了,求大家指点。
#include <reg52.h> #include <string.h> #include <stdio.h> #include <intrins.h> #include <math.h> #include <stdarg.h> #include "usart.h" #include "wifi.h" sbit BUZZ = P2^3; //蜂鸣器控制引脚 sbit Pow = P1^0; sbit Pow1 = P0^0; unsigned char SendDataFlage = 0; unsigned char id; char Recive_table[20]=""; //接收缓冲最大20个字节 //unsigned char data Recive_table[20]; char Recive_state = 0; //接收完成标志 int main (void) { /********************功能初始化***********************/ Uart_Init();//串口初始化,波特率为9600 Pow = 1; ms_delay(1000) ; ms_delay(1000) ; //BUZZ=0; ms_delay(1000) ; ms_delay(1000) ; ms_delay(1000) ; //BUZZ=1; ES = 0; TI = 1; // printf("AT+CIPSTART=\"TCP\"\"192.168.1.2\"8080\r\n"); BUZZ=0; ms_delay(1000) ; // printf("AT+CIPSTART=\"TCP\"\"192.168.1.2\"8080\r\n"); //BUZZ=1; ms_delay(1000) ; // printf("AT+CIPSTART=\"TCP\"\"192.168.1.2\"8080\r\n"); //BUZZ=0; ms_delay(1000) ; ms_delay(1000) ; ms_delay(1000) ; // printf("AT+CIPSTART=\"TCP\"\"192.168.1.2\"8080\r\n"); BUZZ=1; ms_delay(1000) ; ms_delay(1000) ; ms_delay(1000) ; printf("AT+CIPSTART=\"TCP\"\"192.168.1.2\"8080\r\n"); //BUZZ=0; ms_delay(1000) ; printf("AT+CIPSEND=12\r\n"); ms_delay(1000) ; //BUZZ=1; printf("666666601ok\r\n"); //BUZZ=0; while(!TI); TI = 0; Recive_state = 0; //EA = 1; ES = 1; ms_delay(1000) ; ms_delay(1000) ; BUZZ=1; SendDataFlage=1; BUZZ=0; ms_delay(1000) ; BUZZ=1; start1: while(1) { ms_delay(100) ; if(SendDataFlage==1) { SendDataFlage=0; ES = 0; TI = 1; printf("AT+CIPSEND=12\r\n"); BUZZ=0; ms_delay(1000) ; printf("666666601ok\r\n"); while(!TI); TI = 0; Recive_state = 0; ES = 1; PS = 1; BUZZ=1; ms_delay(1000) ; ms_delay(1000) ; } if(Recive_state == 1) { if(strstr(Recive_table"6666666011")) { ES=0; TI=1; printf("AT+CIPSEND=15\r\n"); ms_delay(1000) ; printf("%c\n666666601on\r\n"); BUZZ =0; Pow = 0; Pow1 = 0; for(d=0;d<=5;d++) { for(i=0;i<=30;i++) { ms_delay(1000) ; } } Pow = 1; Pow1 = 1; printf("AT+CIPSEND=16\r\n"); ms_delay(1000) ; printf("%c\n666666601off\r\n"); //while(!TI) TI=0; Recive_state = 0; ES=1; SendDataFlage=1; BUZZ =1; goto start1; } else if(strstr(Recive_table"666666601ok")) { ES=0; TI=1; BUZZ =0; ms_delay(1000) ; BUZZ =1; while(!TI) TI=0; Recive_state = 0; ES=1; SendDataFlage=1; goto start1; } RI = 0; ES = 0; TI=0; Recive_state = 0; ES = 1; SendDataFlage=1; goto start1; } BUZZ=0; memset(Recive_table'\0'20); //memset(Recive_table020*sizeof(char)); Recive_state = 0; TI=0; ES=1; //打开接收标志位 PS = 1; SendDataFlage=1; ms_delay(1000) ; BUZZ = 1; goto start1; } } /****************************************************/ /****************************************************************** 函 数: void Uart_Interrupt() interrupt 4 功 能: 串口中断函数将收到的字符存到Recive_table[]数组中 参 数: 无 返回值: 无 *******************************************************************/ void Uart_Interrupt() interrupt 4 { static char i=0; //因为是一位一位接收,所以用static if(RI==1) { ES = 0; RI=0; Recive_table[i]=SBUF; i++; if((Recive_table[i-1] == '\n')) { Recive_table[i]='\0'; i=0; Recive_state = 1; } ES = 1; } if(TI) { TI = 0; ES = 1; } }
在不执行的地方,使用串口输出一下当前的标志位的值,看看是在延时,还是某个标志位没有更新而无法运行。
感觉你的标志位的判断与更新与同时的逻辑有问题的