[STM32F4] 【STM32F469I试用】实战

[复制链接]
 楼主| gxy5057232 发表于 2015-12-23 20:44 | 显示全部楼层 |阅读模式
本帖最后由 gxy5057232 于 2015-12-24 09:47 编辑

申请开发板时准备用做一个智能家居的控制中心,需要的时间太长,很快就要到发实战的截至日期了,就先做了个DEMO功能出来,不然板子就得被收回了~
主要功能:
    通过手机或电脑在局域网内控制 469 Discovery 的5个LED,包括 LD1 LD2 LD3 LD4 LD7, LD7。

硬件组成:
   STM32F469 Discovery + LBP100 WIF模块
   LBP100 WIF模块由上海汉枫生产,可以通过 smart link 软件来配置其连接到路由器,具体的资料可以从其官网下载。外观如下:
    IMG_20151223_174616.jpg LPB100_PIN.jpg



通过漆包线连接排针插到 STM32F469 Discovery 的Arduino uno connector接口,电路如下:
模块控制引脚连接:
LPB100_CON.jpg

电源引脚:

LPB100_POWER9.jpg
实物图:
IMG_20151223_174750.jpg

USB转UART接口用来监测WIFI模块向MCU返回的数据。
IMG_20151223_174951.jpg
使用 ST Link 的虚拟串口作为调试信息打印接口。

软件主要流程:
1初始化LED
2配置WIFI模块
3搭建TCP SERVER
4解析、执行指令

主函数:
  1. int main(void)
  2. {
  3.     debugInit();
  4.     debug_log("*****************21ic*****************\r\n");
  5.     debug_log("*    STM32F469IN_Discovery Board ʵսÈÕ¼Ç 1    *\r\n");
  6.     debug_log("*      ---------- ÖÇÄÜLED Demo ----------      *\r\n");
  7.     debug_log("************************************************\r\n");
  8.     debug_log("all led init\r\n");
  9.     all_led_Init();
  10.     debug_log("wifi init\r\n");
  11.     wifiInit();
  12.     while(1)
  13.     {
  14.         ledControlDetect();
  15.         delay(500);
  16.     }
  17. }
wifi配置过程:
  1. void wifiInit(void)
  2. {
  3. LPB100_IO_init();            //³õʼ»¯IO   
  4. LPB100_RELOAD_PIN_SET();     //À­¸ß nReload ·ÀÖ¹WIFIÄ£¿é»Ö¸´³ö³§ÉèÖà  
  5. LPB100_reset();              //¸´Î»WIFIÄ£¿é
  6.         
  7.     //µÈ´ý WIFI ready
  8.     while(1 == LPB100_READY_PIN_STA())   //LPB100 Æô¶¯Íê³Éºó Ready PIN Êä³ö0
  9.     {
  10.     debug_log("wait wifi ready...\r\n");
  11.     delay(5000);
  12.     }  
  13.    
  14.     debug_log("wifi is ready\r\n");

  15.    
  16.     if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0))    // userKey °´ÏÂ WIFI»Ö¸´³ö³§ÉèÖÃ
  17.     {
  18.         debug_log("clear LPB100\r\n");
  19.         LPB100_RELOAD_PIN_RESET();
  20.         delay(30000);
  21.         LPB100_RELOAD_PIN_SET();
  22.         delay(10000);
  23.         
  24.         //µÈ´ý WIFI ready
  25.         while(1 == LPB100_READY_PIN_STA())   //LPB100 Æô¶¯Íê³Éºó Ready PIN Êä³ö0
  26.         {
  27.             debug_log("wait wifi ready...\r\n");
  28.             delay(5000);
  29.         }
  30.         debug_log("wifi is ready\r\n");
  31.     }

  32.     wifi_link_detect();     //wifi Á¬½Ó¼ì²â
  33.     wifi_tcpServer_init();  //¾ÖÓòÍø TCP SERVER ÉèÖÃ
  34.     intoDirectTranfser();   //wifi Ä£¿é»Øµ½Í¸´«Ä£Ê½
  35.     debug_log("wifi init ok\r\nmain loop\r\n");
  36. }

简单的写了WIFI模块的连接检测
  1. void wifi_link_detect(void)
  2. {
  3.     uint8_t sta = 0;
  4.     uint16_t height_count = 0;
  5.     uint16_t low_count = 0;
  6.     uint32_t total_count = 0;
  7.    
  8.     debug_log("link detect\r\n");
  9.    
  10.     while(1)
  11.     {
  12.         switch(sta)
  13.         {
  14.             case 0:
  15.             {
  16.                 if(LPB100_LINK_PIN_STA() == 0) // link pin Ϊ0
  17.                 {
  18.                     low_count++;
  19.                     height_count = 0;
  20.                     if(low_count > 15)               //link Á¬Ðø15´ÎΪ 0  ÈÏΪÁ¬½Óµ½Â·ÓÉÆ÷
  21.                     {
  22.                         debug_log("wifi link ok\r\n");
  23.                         return;
  24.                     }                 
  25.                 }
  26.                 else
  27.                 {
  28.                     low_count = 0;
  29.                     height_count++;
  30.                     if(height_count == 30)               //Á¬Ðø30´ÎΪ1 ÈÏΪÁ¬½Ó²»µ½Â·ÓÉÆ÷ ½øÈë smart link
  31.                     {
  32.                         LPB100_RELOAD_PIN_RESET();
  33.                         delay(1000);
  34.                         LPB100_RELOAD_PIN_SET();
  35.                         low_count = 0;
  36.                         height_count = 0;
  37.                         sta = 1;
  38.                         debug_log("\r\nsmart link\r\n\r\n");
  39.                     }               
  40.                 }
  41.             }break;
  42.             case 1:   
  43.             {
  44.                 if(LPB100_LINK_PIN_STA() == 0) // link pin Ϊ 0
  45.                 {
  46.                     low_count++;
  47.                     height_count = 0;
  48.                     if(low_count > 15)               //link Á¬Ðø15´ÎΪ0 ÈÏΪÁ¬½Óµ½Â·ÓÉÆ÷
  49.                     {
  50.                         debug_log("\r\nsmart link ok\r\n\r\n");
  51.                         return;
  52.                     }               
  53.                 }
  54.                 else
  55.                 {
  56.                     low_count = 0;
  57.                     height_count++;               
  58.                 }
  59.             }break;
  60.         }
  61.         total_count++;
  62.         delay(500);
  63.         if((total_count%4) == 0)
  64.             debug_log("total:%d low:%d height:%d\r\n",total_count,low_count,height_count);
  65.     }
  66. }
TCP SERVER设置代码:
  1. //ÉèÖÃWIFI×öTCP SERVER
  2. void wifi_tcpServer_init(void)
  3. {
  4.     uint8_t ipMode;
  5.     uint32_t ip[4];
  6.     uint32_t mask[4];
  7.     uint32_t gateway[4];
  8.    
  9.     //½øÈëATÖ¸Áîģʽ
  10.     intoATmode();
  11.    
  12.     //¹Ø±Õ»ØÏÔ
  13.     delay(500);
  14.     wifi_printf("AT+E=off\n");
  15.    
  16.    
  17.     if(wifi_read_wann(&ipMode,ip,mask,gateway))
  18.     {
  19.         debug_log("%c\r\n",ipMode);
  20.         if(ipMode == 'D')
  21.         {
  22.             debug_log("IP_MODE:DCHP\r\n");
  23.         }
  24.         else
  25.         {
  26.             debug_log("IP_MODE:static\r\n");
  27.         }
  28.         debug_log("ip:%d.%d.%d.%d\r\n",ip[0],ip[1],ip[2],ip[3]);
  29.         debug_log("mask:%d.%d.%d.%d\r\n",mask[0],mask[1],mask[2],mask[3]);
  30.         debug_log("gateway:%d.%d.%d.%d\r\n",gateway[0],gateway[1],gateway[2],gateway[3]);
  31.         
  32.         //ÉèÖÃ
  33.         delay(500);
  34.         wifi_printf("AT+NETP=TCP,SERVER,8899,%d.%d.%d.%d\n",ip[0],ip[1],ip[2],ip[3]);
  35.     }
  36.     else
  37.     {
  38.         debug_log("get wann err\r\n");
  39.     }
  40. }
协议解析代码:
  1. //"ledNum":"1","sta":"0"  
  2. void ledControlDetect(void)
  3. {
  4.     uint32_t val;
  5.     uint32_t ret;
  6.    
  7.     uartScanf(&ret,500,"ledNum":"%d",&val);
  8.     if(!ret)
  9.     {
  10.         return;
  11.     }
  12.    
  13.     switch(val)
  14.     {
  15.         case 1:
  16.         {
  17.             uartScanf(&ret,500,"sta":"%d",&val);
  18.             if(ret)
  19.             {
  20.                 debug_log("LED1 ");
  21.                 if(val == 0)
  22.                 {
  23.                     LD1_CLOSE();
  24.                     debug_log("close\r\n");
  25.                 }
  26.                 else if(val == 1)
  27.                 {
  28.                     LD1_OPEN();
  29.                     debug_log("open\r\n");
  30.                 }
  31.                 wifi_printf("LED1 OK\r\n");
  32.             }

  33.         }break;
  34.         case 2:
  35.         {
  36.             uartScanf(&ret,500,"sta":"%d",&val);
  37.             if(ret)
  38.             {
  39.                 debug_log("LED2 ");
  40.                 if(val == 0)
  41.                 {
  42.                     LD2_CLOSE();
  43.                     debug_log("close\r\n");
  44.                 }
  45.                 else if(val == 1)
  46.                 {
  47.                     LD2_OPEN();
  48.                     debug_log("open\r\n");
  49.                 }
  50.                 wifi_printf("LED2 OK\r\n");
  51.             }
  52.         }break;
  53.         case 3:
  54.         {
  55.             uartScanf(&ret,500,"sta":"%d",&val);
  56.             if(ret)
  57.             {
  58.                 debug_log("LED3 ");
  59.                 if(val == 0)
  60.                 {
  61.                     LD3_CLOSE();
  62.                     debug_log("close\r\n");
  63.                 }
  64.                 else if(val == 1)
  65.                 {
  66.                     LD3_OPEN();
  67.                     debug_log("open\r\n");
  68.                 };
  69.                 wifi_printf("LED3 OK\r\n");
  70.             }
  71.         }break;
  72.          case 4:
  73.         {
  74.             uartScanf(&ret,500,"sta":"%d",&val);
  75.             if(ret)
  76.             {
  77.                 debug_log("LED4 ");
  78.                 if(val == 0)
  79.                 {
  80.                     LD4_CLOSE();
  81.                     debug_log("close\r\n");
  82.                 }
  83.                 else if(val == 1)
  84.                 {
  85.                     LD4_OPEN();
  86.                     debug_log("open\r\n");
  87.                 }
  88.                 wifi_printf("LED4 OK\r\n");
  89.             }
  90.         }break;
  91.         case 7:
  92.         {
  93.             uartScanf(&ret,500,"sta":"%d",&val);
  94.             if(ret)
  95.             {
  96.                 debug_log("LED7 ");
  97.                 if(val == 0)
  98.                 {
  99.                     LD7_CLOSE();
  100.                     debug_log("close\r\n");
  101.                 }
  102.                 else if(val == 1)
  103.                 {
  104.                     LD7_OPEN();
  105.                     debug_log("open\r\n");
  106.                 }
  107.                 wifi_printf("LED7 OK\r\n");
  108.             }
  109.         }break;
  110.         default:wifi_printf("err cmd\n");break;
  111.     }

  112. }
为了和HTTP协议兼容 指令格式采用了json格式: "ledNum":"1","sta":"0"  通过 scanf()进行解析。

软件运行LOG:
360截图20151223201718346.jpg

smart link 完成 TCP SERVER 建立完成

360截图20151223202502318.jpg

smart link软件:
Screenshot_2015-12-23-19-05-48.png

手机端发指令:
Screenshot_2015-12-23-19-06-36.png

收到恢复后 对应的LED亮起:
IMG_20151223_190706.jpg

Screenshot_2015-12-23-19-07-20.png

IMG_20151223_190732.jpg


gejigeji521 发表于 2015-12-23 23:23 | 显示全部楼层
设置的这些都是通过代码实现的?真是棒极了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

61

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部

4

主题

61

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部