qq_42059232 2018-05-05 05:39 采纳率: 0%
浏览 1143
已结题

51单片机加入定时器的定义和中断后,手机APP与单片机只能连接几秒,问题出在哪里

在原来的单片机代码中加入void TF0_isr() interrupt 1和在void InitUART(void)里定义了定时器 ET0=1;TH0=56320/256; L0=56320%256; TR0=1;后,拷到单片机里,手机APP连接WiFi建立链接后,几秒后断开链接,有时甚至无法建立链接。

是串口中断和定时器中断冲突了吗?
请问问题出在哪里,怎么解决

原来正常的情况:单片机和APP连接后,APP能控制LED1、2的亮灭,并显示LED等的状态

#include "DHT11.h"
#include
#include "string.h"
extern unsigned char F16T,F16RH; //温湿度数据
sbit LED1=P3^7;
sbit LED2=P3^6;
#define uchar unsigned char
#define uint unsigned int
unsigned char idata Rxbuff[50],Rxnum;
unsigned char Recwifi_data[5];
char *strx=0;
char clinetid;//连接ID
unsigned char getflag;//获取标志
void Get_Clinet(void);
void InitUART(void) //这是串口和定时器的基本配置,配置他的波特率是9600.这些参数都是标准的。
{

 TMOD|= 0x21;
SCON = 0x50;
TH1 = 0xFD;
TL1 = TH1;
PCON = 0x00;
EA = 1;  //启动中断系统
ES = 1;
TR1 = 1;

ET0=1;//     定时器的定义 
TH0=56320/256;   //计数起点为56320 ==10ms溢出一次  
TL0=56320%256;  
TR0=1;

}
void Clear_Buffer(void)//清空缓存
{
int i;

//  for(i=0;i<50;i++)
//  Rxbuff[i]=0;//
    Rxnum=0;

}

/******************************************************************************/
void delayms(unsigned int x)
{
unsigned int i;
while(x--)
for(i=125;i>0;i--);
}
/*

  • UART 发送一字节 */ void UART_send_byte(char dat) { SBUF = dat; while (TI == 0); TI = 0; }

/*

  • UART 发送字符串 */ void Send_Str(unsigned char *buf) { while (*buf != '\0') { UART_send_byte(*buf++); } }

void ESP8266_SERVER(void)//服务器建立
{

Send_Str("AT\r\n");
delayms(500);
Send_Str("AT\r\n");
delayms(500);
Send_Str("AT\r\n");
delayms(500);
Send_Str("ATE0\r\n");     //关闭回显
delayms(500);
Send_Str("AT+CWSAP=\042ESP8266_001\042,\0421234567890\042,5,3\r\n");      //设置显示名称和密码
delayms(500);
Send_Str("AT+CIPMUX=1\r\n");//启动多连接,建立服务器都需要配置
delayms(500);   
Clear_Buffer(); 
Send_Str("AT+CIPSERVER=1,8888\r\n");//建立服务器
delayms(500);   
strx=strstr((const char*)Rxbuff,(const char*)"OK");//等待建立完成
while(strx==NULL)
{       Clear_Buffer(); 
        delayms(500);
        strx=strstr((const char*)Rxbuff,(const char*)"OK");//等待建立完成
}
Clear_Buffer(); 

}
void Get_Clinet(void)//判断是否获取到了客户端接入
{

   uchar i;

    strx=strstr((const char*)Rxbuff,(const char*)"CONNECT");//返回连接值
        if(strx)
    {
      Clear_Buffer();   
        getflag=1;
    }
    strx=strstr((const char*)Rxbuff,(const char*)"CLOSE");//返回断开连接
        if(strx)
    {
      Clear_Buffer();   
        getflag=0;
    }
    if(getflag)
    {
            strx=strstr((const char*)Rxbuff,(const char*)"+IPD");//有数据返回
          if(strx)//正常获取数据
             {
                 clinetid=strx[5];
                 strx=strstr((const char*)Rxbuff,(const char*)":");//有数据返回
                 for(i=0;i<2;i++)
                 Recwifi_data[i]=strx[1+i];       //获取手机对LED的控制
                  if( Recwifi_data[0]=='1'&&   Recwifi_data[1]=='0'     )//关灯
                  LED1=1;
                  else  if( Recwifi_data[0]=='1'&&   Recwifi_data[1]=='1'       )//开灯
                  LED1=0 ;
                  if( Recwifi_data[0]=='2'&&   Recwifi_data[1]=='0'     )//关灯
                  LED2=1;
                  else  if( Recwifi_data[0]=='2'&&   Recwifi_data[1]=='1'       )//开灯
                  LED2=0 ;  
                 Clear_Buffer();    
             }

}

}
void Send_DATA(uchar buffer)
{
uchar i;
Send_Str("AT+CIPSEND=0,7\r\n");
delayms(300);
Send_Str(buffer);//发送数据
delayms(100);
strx=strstr((const char
)Rxbuff,(const char*)"SEND OK");//返回OK
while(strx==NULL)
{
delayms(100);
strx=strstr((const char*)Rxbuff,(const char*)"SEND OK");//返回OK
}
strx=strstr((const char*)Rxbuff,(const char*)"+IPD");//有数据返回 ,顺便判断是否有数据返回
if(strx)//正常获取数据
{
clinetid=strx[5];
strx=strstr((const char*)Rxbuff,(const char*)":");//有数据返回
for(i=0;i<5;i++)
Recwifi_data[i]=strx[1+i]; //获取上位机设定的报警值
Clear_Buffer();

}
Clear_Buffer();

}
void main()
{
unsigned char Tx_Buf[10];
uchar i;
unsigned char LEDstatus;//灯的状态
delayms(500);
delayms(500);
delayms(1000); //延时一段时间,让WIFI模块稳定
InitUART(); //初始化串口
ESP8266_SERVER();
IP = 0x10;
// PS = 1;
// PT0=0; //初始化ESP8266
while(1)

{

Get_Clinet();
if(getflag)
{
getDHT11(); //获取温湿度值
Tx_Buf[0]='T'; //帧头
Tx_Buf[1]=F16T/10%10+0x30; //将温湿度数据送往发送数组,送给蓝牙模块让手机APP显示
Tx_Buf[2]=F16T%10+0x30;

Tx_Buf[3]=F16RH/10%10+0x30;
Tx_Buf[4]=F16RH%10+0x30;
LEDstatus=LED1;
Tx_Buf[5]=LEDstatus+0x30;//发送灯的状态
LEDstatus=LED2;
Tx_Buf[6]=LEDstatus+0x30;
Send_DATA(Tx_Buf) ;//发送数据

}
}

}

/*****************串口接收中断函数,接收蓝牙模块的数据*********************/
void UARTInterrupt(void) interrupt 4
{

if(RI)
{
ES=0;
RI = 0;
Rxbuff[Rxnum++]=SBUF;
if(Rxnum>=50)
Rxnum=0;
ES=1;
}

}

     void TF0_isr()  interrupt 1  //10ms 进入一次  

{

static int c1;

//执行一条指令12个周期,12/11.059M =1.08507us 10ms需要9216个周期
TH0=56320/256; //重装初值

TL0=56320%256;

    c1++;
    if(c1>400){
    LED1=0;
    }
    if(c1>600)
    {
     LED1=1;
     c1=0;
    }

}

  • 写回答

2条回答

  • QQ-2858498411 2018-05-05 07:34
    关注

    51单片机定时器0做波特率发生器时一般工作于模式2,自动装载初值,否则如果没有精确计算的中断填装就无法通信。
    蓝牙或WiFi与手机的连接应该与串口通信无关,就算断开串口,也应该能连接手机,蓝牙或WiFi电压不稳会断开连接,你试试只对WiFi加电,不连接单片机能不能正常连接手机。

    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3