找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2643|回复: 7
打印 上一主题 下一主题
收起左侧

ESP8266的单片机程序,一页版

[复制链接]
跳转到指定楼层
楼主
ID:290320 发表于 2018-3-11 10:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
单片机源程序如下:
  1. #include <reg52.h>

  2. #define uint unsigned int
  3. #define uchar unsigned char

  4. sbit lcd0=P1^0;
  5. sbit lcd1=P1^1;
  6. sbit lcd2=P1^2;
  7. sbit lcd3=P1^3;
  8. sbit lcd4=P1^4;
  9. sbit lcd5=P1^5;
  10. sbit lcd6=P1^6;
  11. sbit lcd7=P1^7;
  12. sbit key_config_Ap=P2^1;
  13. sbit key_config_Air=P2^3;

  14. uchar usartbuf[50]={0};
  15. uchar sn,checksum,usrtlen,usarrtflag,num_usart;
  16. uchar send_3_config,send_7_error,send_8_dev,send_9_dev;

  17. /*wifi模组请求设备信息,mcu回复*/
  18. uchar mcu_send_1[75]={
  19. 0xff,0xff,0x00,0x47,0x02,0x55,0x00,0x00,
  20. 0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x34,
  21. 0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x34,
  22. 0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x31,
  23. 0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x31,
  24. 0x34,0x39,0x33,0x35,0x33,0x31,0x33,0x36,
  25. 0x30,0x66,0x38,0x66,0x34,0x32,0x37,0x64,
  26. 0x38,0x64,0x33,0x34,0x35,0x39,0x36,0x62,
  27. 0x33,0x37,0x33,0x62,0x38,0x61,0x39,0x30,
  28. 0x00,0x00,0x55};                                                                                                                                               

  29. /*wifi模组与设备mcu心跳,mcu回复*/
  30. uchar mcu_send_2[9]={0xff,0xff,0x00,0x05,0x08,0x55,0x00,0x00,0x55};

  31. /*设备mcu通知wifi模组进入配置模式,mcu发送*/
  32. uchar mcu_send_3[10]={0xff,0xff,0x00,0x06,0x09,0x55,0x00,0x00,0x11,0x55};

  33. /*设备mcu重置wifi,mcu发送*/
  34. uchar mcu_send_4[9]={0xff,0xff,0x00,0x05,0x0b,0x55,0x00,0x00,0x55};

  35. /*wifi模组向设备通知工作状态变化,mcu回复*/
  36. uchar mcu_send_5[9]={0xff,0xff,0x00,0x05,0x0e,0x55,0x00,0x00,0x55};

  37. /*wifi模组请求重启mcu,mcu回复*/
  38. uchar mcu_send_6[9]={0xff,0xff,0x00,0x05,0x10,0x55,0x00,0x00,0x55};

  39. /*非法消息通知,mcu回复*/
  40. uchar mcu_send_7[10]={0xff,0xff,0x00,0x06,0x12,0x55,0x00,0x00,0x11,0x55};

  41. /*wifi模组读取设备当前状态,mcu回复*/
  42. uchar mcu_send_8[11]={0xff,0xff,0x00,0x07,0x04,0x55,0x00,0x00,0x03,0x11,0x55};

  43. /*设备向wifi主动上报当前状态,mcu发送*/
  44. uchar mcu_send_9[11]={0xff,0xff,0x00,0x07,0x05,0x55,0x00,0x00,0x04,0x11,0x55};

  45. /*wifi模组控制设备,mcu回复*/
  46. uchar mcu_send_10[9]={0xff,0xff,0x00,0x05,0x04,0x55,0x00,0x00,0x55};

  47. /*mcu通知wifi进入可绑定模式,mcu发送*/
  48. uchar mcu_send_12[9]={0xff,0xff,0x00,0x05,0x15,0x55,0x00,0x00,0x55};

  49. /**********************************************************************/
  50. /**********************************************************************/
  51. /**********************************************************************/
  52. /**********************************************************************/
  53. /**********************************************************************/
  54. /**********************************************************************/
  55. /**********************************************************************/
  56. /**********************************************************************/

  57. /*延时函数*/
  58. void Delay_ms(unsigned char z)
  59. {
  60.         unsigned char x,y;
  61.         for(x=z;x>0;x--)
  62.                 for(y=110;y>0;y--);
  63. }

  64. /*初始化函数*/
  65. void Usart_Init()
  66. {
  67.         TMOD=0x21;                                        //定时器0工作方式1,定时器1工作方式2
  68.         TH0=(65536-10000)/256;
  69.         TL0=(65536-10000)%256;
  70.         TH1=0xfd;
  71.         TL1=0xfd;
  72.         TR0=1;
  73.         TR1=1;
  74.         REN=1;
  75.         SM0=0;
  76.         SM1=1;
  77.         EA=1;
  78.         ES=1;       
  79. }

  80. /*mcu发送数据程序*/
  81. void Usart_SendArrang(uchar *dat,uchar len)
  82. {
  83.         unsigned char i;
  84.         for(i=0;i<len;i++)
  85.         {
  86.                         ES=0;
  87.                         SBUF=dat[i];
  88.                         while(!TI);
  89.                         TI=0;
  90.                         ES=1;
  91.         }
  92. }

  93. /*定时器0中断*/
  94. void Timer0_Int() interrupt 1
  95. {
  96.         TH0=(65536-10000)/256;
  97.         TL0=(65536-10000)%256;
  98.         num_usart++;
  99. }

  100. /*串口中断函数*/
  101. void Usart_Int() interrupt 4
  102. {
  103.         num_usart=0;
  104.         ET0=1;
  105.         usartbuf[usrtlen++]=SBUF;
  106.         if(usrtlen==2)
  107.         {
  108.                 if(usartbuf[0]!=0xff||usartbuf[1]!=0xff)
  109.                 {
  110.                         usrtlen=0;
  111.                 }
  112.         }
  113.         RI=0;
  114. }


  115. /*改变设备状态*/
  116. void Control_Mcu()
  117. {
  118.         switch(usartbuf[9])
  119.         {
  120.                 case 0x01:
  121.                         if(usartbuf[10]==0x01)
  122.                         {
  123.                                 lcd0=0;
  124.                         }
  125.                         else if(usartbuf[10]==0)
  126.                         {
  127.                                 lcd0=1;
  128.                         }
  129.                         break;

  130.                 case 0x02:
  131.                         if(usartbuf[10]==0x02)
  132.                         {
  133.                                 lcd1=0;
  134.                         }
  135.                         else if(usartbuf[10]==0)
  136.                         {
  137.                                 lcd1=1;
  138.                         }
  139.                         break;

  140.                 case 0x04:
  141.                         if(usartbuf[10]==0x04)
  142.                         {
  143.                                 lcd2=0;
  144.                         }
  145.                         else if(usartbuf[10]==0)
  146.                         {
  147.                                 lcd2=1;
  148.                         }
  149.                         break;

  150.                 case 0x08:
  151.                         if(usartbuf[10]==0x08)
  152.                         {
  153.                                 lcd3=0;
  154.                         }
  155.                         else if(usartbuf[10]==0)
  156.                         {
  157.                                 lcd3=1;
  158.                         }
  159.                         break;

  160.                 case 0x10:
  161.                         if(usartbuf[10]==0x10)
  162.                         {
  163.                                 lcd4=0;
  164.                         }
  165.                         else if(usartbuf[10]==0)
  166.                         {
  167.                                 lcd4=1;
  168.                         }
  169.                         break;

  170.                 case 0x20:
  171.                         if(usartbuf[10]==0x20)
  172.                         {
  173.                                 lcd5=0;
  174.                         }
  175.                         else if(usartbuf[10]==0)
  176.                         {
  177.                                 lcd5=1;
  178.                         }
  179.                         break;

  180.                 case 0x40:
  181.                         if(usartbuf[10]==0x40)
  182.                         {
  183.                                 lcd6=0;
  184.                         }
  185.                         else if(usartbuf[10]==0)
  186.                         {
  187.                                 lcd6=1;
  188.                         }
  189.                         break;

  190.                 case 0x80:
  191.                         if(usartbuf[10]==0x80)
  192.                         {
  193.                                 lcd7=0;
  194.                         }
  195.                         else if(usartbuf[10]==0)
  196.                         {
  197.                                 lcd7=1;
  198.                         }
  199.                         break;
  200.         }
  201. }

  202. /*处理串口数据*/
  203. void Usart_Communication()
  204. {
  205.         uchar i;
  206.         if(num_usart==10)
  207.         {
  208.                 ET0=0;
  209.                 num_usart=0;
  210.                 usarrtflag=1;
  211.                 REN=0;
  212.         }
  213.         if(usarrtflag)
  214.         {
  215.                 sn=usartbuf[5];
  216.                 checksum=0;
  217.                 switch(usartbuf[4])
  218.                 {
  219.                         case 0x01:
  220.                                 mcu_send_1[5]=sn;
  221.                                 for(i=2;i<74;i++)
  222.                                 {
  223.                                         checksum=checksum+mcu_send_1[i];
  224.                                 }
  225.                                 checksum=checksum%256;
  226.                                 mcu_send_1[74]=checksum;
  227.                                 Usart_SendArrang(mcu_send_1,75);
  228.                                 break;

  229.                         case 0x07:
  230.                                 mcu_send_2[5]=sn;
  231.                                 for(i=2;i<8;i++)
  232.                                 {
  233.                                         checksum=checksum+mcu_send_2[i];
  234.                                 }
  235.                                 checksum=checksum%256;
  236.                                 mcu_send_2[8]=checksum;
  237.                                 Usart_SendArrang(mcu_send_2,9);
  238.                                 break;       

  239.                         case 0x0a:
  240.                                 break;

  241.                         case 0x0c:
  242.                                 break;

  243.                         case 0x0d:
  244.                                 mcu_send_5[5]=sn;
  245.                                 for(i=2;i<8;i++)
  246.                                 {
  247.                                         checksum=checksum+mcu_send_5[i];
  248.                                 }
  249.                                 checksum=checksum%256;
  250.                                 mcu_send_5[8]=checksum;
  251.                                 Usart_SendArrang(mcu_send_5,9);
  252.                                 break;

  253.                         case 0x0f:
  254.                                 mcu_send_6[5]=sn;
  255.                                 for(i=2;i<8;i++)
  256.                                 {
  257.                                         checksum=checksum+mcu_send_6[i];
  258.                                 }
  259.                                 checksum=checksum%256;
  260.                                 mcu_send_6[8]=checksum;
  261.                                 Usart_SendArrang(mcu_send_6,9);
  262.                                 break;       

  263.                         case 0x11:
  264.                                 mcu_send_7[5]=sn;
  265.                                 send_7_error=usartbuf[8];
  266.                                 mcu_send_7[8]=send_7_error;
  267.                                 for(i=2;i<9;i++)
  268.                                 {
  269.                                         checksum=checksum+mcu_send_7[i];
  270.                                 }
  271.                                 checksum=checksum%256;
  272.                                 mcu_send_7[9]=checksum;
  273.                                 Usart_SendArrang(mcu_send_7,10);
  274.                                 break;

  275.                         case 0x03:
  276.                                 if(usartbuf[3]=0x06&&usartbuf[8]==0x02)
  277.                                 {
  278.                                         mcu_send_8[5]=sn;
  279.                                         send_8_dev=~P1;
  280.                                         mcu_send_8[9]=send_8_dev;
  281.                                         for(i=2;i<10;i++)
  282.                                         {
  283.                                                 checksum=checksum+mcu_send_8[i];
  284.                                         }
  285.                                         checksum=checksum%256;
  286.                                         mcu_send_8[10]=checksum;
  287.                                         Usart_SendArrang(mcu_send_8,11);
  288.                                         break;
  289.                                 }
  290.                                 if(usartbuf[3]=0x08&&usartbuf[8]==0x01)
  291.                                 {
  292.                                         Control_Mcu();                                                                //收到数据后,mcu控制设备函数
  293.                                        
  294.                                         mcu_send_10[5]=sn;
  295.                                         for(i=2;i<8;i++)
  296.                                         {
  297.                                                 checksum=checksum+mcu_send_10[i];
  298.                                         }
  299.                                         checksum=checksum%256;
  300.                                         mcu_send_10[8]=checksum;
  301.                                         Usart_SendArrang(mcu_send_10,9);                        //mcu回复

  302.                                         mcu_send_9[5]=sn;
  303.                                         send_9_dev=~P1;
  304.                                         mcu_send_9[9]=send_9_dev;
  305.                                         for(i=2;i<10;i++)
  306.                                         {
  307.                                                 checksum=checksum+mcu_send_9[i];
  308.                                         }
  309.                                         checksum=checksum%256;
  310.                                         mcu_send_9[10]=checksum;
  311.                                         Usart_SendArrang(mcu_send_9,11);                        //mcu主动上报设备状态                                       
  312.                                         break;       
  313.                                 }

  314.                         case 0x06:
  315.                                 break;

  316.                         case 0x16:
  317.                                 break;       
  318.                 }
  319.                 usrtlen=0;
  320.                 usarrtflag=0;
  321.                 REN=1;
  322.         }       
  323. }

  324. /*按键配置设备入网*/
  325. void Key_Usart_Config()
  326. {
  327.         uchar i;
  328.         checksum=0;
  329.         if(key_config_Ap==0)                                                                        //SoftAp配置模式
  330.         {
  331.                 Delay_ms(10);
  332.                 if(key_config_Ap==0)
  333.                 {       
  334.                         REN=0;
  335.                         sn=0x00;
  336.                         mcu_send_3[5]=sn;
  337.                         for(i=2;i<9;i++)
  338.                         {
  339.                                 checksum=checksum+mcu_send_3[i];
  340.                         }
  341.                         checksum=checksum%256;
  342.                         mcu_send_3[9]=checksum;
  343.                         send_3_config=1;
  344.                         mcu_send_3[8]=send_3_config;
  345.                         Usart_SendArrang(mcu_send_3,10);
  346.                         while(key_config_Ap==0);
  347.                         REN=1;       
  348.                 }       
  349.         }

  350.         if(key_config_Air==0)                                                                        //AirLink配置模式
  351.         {
  352.                 Delay_ms(10);
  353.                 if(key_config_Air==0)
  354.                 {       
  355.                         REN=0;
  356.                         sn=0x00;
  357.                         mcu_send_3[5]=sn;
  358.                         for(i=2;i<9;i++)
  359.                         {
  360.                                 checksum=checksum+mcu_send_3[i];
  361.                         }
  362.                         checksum=checksum%256;
  363.                         mcu_send_3[9]=checksum;
  364.                         send_3_config=2;
  365.                         mcu_send_3[8]=send_3_config;
  366.                         Usart_SendArrang(mcu_send_3,10);
  367.                         while(key_config_Air==0);
  368.                         REN=1;       
  369.                 }       
  370.         }       
  371. }

  372. /*主函数*/
  373. void main()
  374. {
  375.         Usart_Init();

  376.         while(1)
  377.         {
  378.                 Key_Usart_Config();
  379.                 Usart_Communication();
  380.         }
  381. }
复制代码

所有资料51hei提供下载:
main.rar (1.85 KB, 下载次数: 34)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:259979 发表于 2018-3-11 21:36 来自手机 | 只看该作者
谢谢分享。
回复

使用道具 举报

板凳
ID:239647 发表于 2019-10-28 09:04 来自手机 | 只看该作者
yi1liang 发表于 2018-3-11 21:36
谢谢分享。

请问有stm32一页的吗
回复

使用道具 举报

地板
ID:515684 发表于 2019-10-29 08:59 | 只看该作者
好东西啊
回复

使用道具 举报

5#
ID:613618 发表于 2019-10-29 11:27 来自手机 | 只看该作者
多谢分亨
回复

使用道具 举报

6#
ID:637393 发表于 2019-11-7 13:26 | 只看该作者
谢谢分享 正需要
回复

使用道具 举报

7#
ID:166284 发表于 2019-11-10 11:13 | 只看该作者
具体51的啥单片机啊,1T还是12T的? 还有哪些16进制的数组怎么来的啊,比如mcu_send_1[75],mcu_send_2[],mcu_send_3[],mcu_send_4[],mcu_send_5[],mcu_send_6[];还有你的原理图呢?实物连接图呢?好哥哥 ,能发一份吗
回复

使用道具 举报

8#
ID:166284 发表于 2019-11-10 11:18 | 只看该作者
哥,原理图和实物图呢?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表