我買了ESP 8266 01做控制用,但是在主循環里面我用了while死循環,結果詭異的是在判斷完SendDataFlage等于1,執行完這個If判段語句后,他的while死循環就執行不下去了,就在這里斷掉了,最離譜的是我做的串口判斷函數,在接收到666666601,1,之后執行完蜂鳴器響之后,也不執行向下語句了,我在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 i,d;
- 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("666666601,ok\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("666666601,ok\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,"666666601,1"))
- {
-
- ES=0;
- TI=1;
- printf("AT+CIPSEND=15\r\n");
-
- ms_delay(1000) ;
- printf("%c\n666666601,on\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\n666666601,off\r\n");
-
- //while(!TI)
- TI=0;
- Recive_state = 0;
- ES=1;
- SendDataFlage=1;
- BUZZ =1;
- goto start1;
- }
-
- else if(strstr(Recive_table,"666666601,ok"))
- {
-
- 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_table,0,20*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;
- }
- }
復制代碼
|