esp8266加入到c51单片机单通道程序的编写

电子说

1.2w人已加入

描述

  ESP8266连接本地服务器的51单片机汇编程序

    at89c2051单片机,晶振11.096MHZ,通讯速率9600,TCP多连接方式,本地连接地址192.168.1.107,收发数据成功

 

   汇编程序如下:

  这是main.c

  #include

  #include

  #include "usart.h"

  #include "wifi.h"

  char Recive_table[20]="";  //接收缓冲,最大20个字节

  char Recive_state = 0;   //接收完成标志

  int main (void)

  {

  /********************功能初始化***********************/

  Uart_Init();//串口初始化,波特率为9600

  ms_delay(1000) ;

  WIFI_Init(); //wifi初始化

  /****************************************************/

  /**********************主循环************************/

  for(;;)

  {

  ms_delay(10) ;

  if(Recive_state == 1)

  {

  ES=0;  //清空接收标志位

  if((Recive_table[0]=='+')&&(Recive_table[1]=='I')&&(Recive_table[2]=='P'))//接收到的字符串形式为+IPD,x,x:y

  {

  if((Recive_table[3]=='D')&&(Recive_table[6]==','))

  {

  if(Recive_table[9]=='0')

  P1 = 0;

  if(Recive_table[9]=='1')

  P1 = 0xff;

  }

  }

  memset(Recive_table,'\0',20);

  Recive_state = 0;

  ES=1;  //打开接收标志位

  }

  }

  /****************************************************/

  }

  /******************************************************************

  函 数: 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;

  }

  else

  TI = 0;

  }

  这是usart.c

  #include "usart.h"

  /******************************************************************

  函 数: void Uart_Init(void)

  功 能: 串口初始化,波特率为9600(这个不会,上网百度)

  参 数: 无

  返回值: 无

  *******************************************************************/

  void Uart_Init(void)

  {

  TMOD=0x20;

  TH1=0xfD;

  TL1=0xfD;

  TR1=1;

  REN=1;

  SM0=0;

  SM1=1;

  EA=1;

  ES=1;

  }

  这是wifi.c

  #include

  #include "wifi.h"

  /******************************************************************

  函 数: void ms_delay(int t)

  功 能: 毫秒级延时

  参 数: 无

  返回值: 无

  *******************************************************************/

  void ms_delay(int t)

  {

  int i,j;

  for(i=t;i>0;i--)

  for(j=110;j>0;j--);

  }

  /******************************************************************

  函 数: void LED(void)

  功 能: 发送完命令后显示用的函数

  参 数: 无

  返回值: 无

  *******************************************************************/

  void LED(void)

  {

  P1 = 0;

  ms_delay(100);

  P1 = 0xff;

  ms_delay(100);

  }

  /******************************************************************

  函 数: void WIFI_Init(void)

  功 能: wifi初始化(名字:esp8266;密码:1234567890)

  参 数: 无

  返回值: 无

  *******************************************************************/

  void WIFI_Init(void)

  {

  ES = 0;

  TI = 1;

  printf("AT+RST\r\n");

  LED();

  ms_delay(1000) ;

  printf("AT+CWMODE=3\r\n");

  LED();

  ms_delay(1000) ;

  printf("AT+CIPMUX=1\r\n");

  LED();

  ms_delay(1000) ;

  printf("AT+CIPSERVER=1,8080\r\n");

  LED();

  ms_delay(1000) ;

  while(!TI);

  TI = 0;

  ES = 1;

  }

  文章综合来源:CSDN

编辑:ymf

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分