標題: 基于stm32F103VET6藍牙小車程序與APP [打印本頁]

作者: 我是華夏人    時間: 2021-9-23 20:52
標題: 基于stm32F103VET6藍牙小車程序與APP
各個引腳都有相對應(yīng)的連接解釋。又不懂的鐵子們可以私聊我。

引腳分配

HC05與開發(fā)板的連接線,使用杜邦線連接:

        HC05_TXD     <--->    PA3          //串口2接收引腳
        HC05_RXD      <--->     PA2            //串口2發(fā)送引腳
        HC05_KEY     <--->     PB14        //普通GPIO、輸出
        HC05_INT      <--->     PB13        //普通GPIO、輸入
        
        HC05_VCC    <--->     接5V或3.3V
        HC05_GND     <--->     接地線         
        
調(diào)試串口(TTL-USB TO USART):
CH340的收發(fā)引腳與STM32的發(fā)收引腳相連。
    RX<--->PA9
    TX<--->PA10
單片機源程序如下:
  1. #include "stm32f10x.h"
  2. #include "./usart/bsp_usart.h"
  3. #include "./usart/bsp_usart_blt.h"
  4. #include "./systick/bsp_SysTick.h"
  5. #include "./hc05/bsp_hc05.h"
  6. #include "./led/bsp_led.h"
  7. #include "./key/bsp_key.h"
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include "MOTOR.h"
  11. BLTDev bltDevList;
  12. extern ReceiveData DEBUG_USART_ReceiveData;
  13. extern ReceiveData BLT_USART_ReceiveData;
  14. /**
  15.   * @brief  主函數(shù)
  16.   * @param  無
  17.   * @retval 無
  18.   */
  19. int main(void)
  20. {        
  21.         char hc05_name[30]="HC05_SLAVE";
  22.         char hc05_nameCMD[40];
  23.           //初始化systick
  24.         SysTick_Init();
  25.         SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk;
  26.         
  27. MotorInit();//控制電機函數(shù)
  28.         USART_Cmd(USART1, ENABLE); //使能串口
  29.         USART_Config();
  30.         
  31.         LED_GPIO_Config();
  32.         Key_GPIO_Config();
  33.         
  34.         HC05_INFO("**********HC05模塊AT指令測試實驗************");
  35.         
  36.         if(HC05_Init() == 0)
  37.                 HC05_INFO("HC05模塊檢測正常。");
  38.         else
  39.         {
  40.                 HC05_ERROR("HC05模塊檢測不正常,請檢查模塊與開發(fā)板的連接,然后復(fù)位開發(fā)板重新測試。");
  41.                 while(1);
  42.         }
  43.                
  44.         /*各種命令測試演示,默認不顯示。
  45.          *在bsp_hc05.h文件把HC05_DEBUG_ON 宏設(shè)置為1,
  46.          *即可通過串口調(diào)試助手接收調(diào)試信息*/        
  47.         
  48.         HC05_Send_CMD("AT+VERSION?\r\n",1);
  49.         
  50.         HC05_Send_CMD("AT+ADDR?\r\n",1);
  51.         
  52.         HC05_Send_CMD("AT+UART?\r\n",1);
  53.         
  54.         HC05_Send_CMD("AT+CMODE?\r\n",1);
  55.         
  56.         HC05_Send_CMD("AT+STATE?\r\n",1);        

  57.         HC05_Send_CMD("AT+ROLE=0\r\n",1);
  58.         
  59.         /*初始化SPP規(guī)范*/
  60.         HC05_Send_CMD("AT+INIT\r\n",1);
  61.         HC05_Send_CMD("AT+CLASS=0\r\n",1);
  62.         HC05_Send_CMD("AT+INQM=1,9,48\r\n",1);
  63.         
  64.         /*設(shè)置模塊名字*/
  65.         sprintf(hc05_nameCMD,"AT+NAME=%s\r\n",hc05_name);
  66.         HC05_Send_CMD(hc05_nameCMD,1);

  67.         HC05_INFO("本模塊名字為:%s ,模塊已準備就緒。",hc05_name);
  68.         while(1)
  69.         {        
  70.           if(DEBUG_USART_ReceiveData.receive_data_flag == 1)
  71.                 {                        
  72.                         DEBUG_USART_ReceiveData.uart_buff[DEBUG_USART_ReceiveData.datanum] = 0;
  73.                         if(strstr((char *)DEBUG_USART_ReceiveData.uart_buff,"AT"))//如果數(shù)據(jù)是以AT開頭的,就把KEY置高,設(shè)置藍牙模塊
  74.                         {
  75.                                 BLT_KEY_HIGHT;
  76.                                 delay_ms(20);
  77.                                 Usart_SendStr_length(BLT_USARTx,DEBUG_USART_ReceiveData.uart_buff,DEBUG_USART_ReceiveData.datanum);        
  78.                           Usart_SendStr_length(BLT_USARTx,"\r\n",2);        
  79.                                 BLT_KEY_LOW;
  80.                         }else if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"4"))//在這里可以自己定義想要接收的字符串然后處理
  81.                         {
  82.                                 Turnfront(); //小車前進
  83.                                 LED_GREEN;
  84.                         }
  85.                  if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"0"))
  86.                         {
  87.                         
  88.                            Stop();//小車停止
  89.                            LED_RED;
  90.                         }
  91.                 if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"1"))
  92.                         {
  93.                         
  94.                            Turnleft();//小車向左
  95.                            LED_BLUE
  96.                         }
  97.                 if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"2"))
  98.                         {
  99.                         
  100.                            Turnright();//小車向右
  101.                            LED_YELLOW
  102.                         }
  103.                 if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"3"))
  104.                         {
  105.                         
  106.                            Turnback();//小車后退
  107.                            LED_PURPLE
  108.                         }
  109.                         else
  110.                         {
  111.                                 BLT_KEY_LOW;
  112.                                 Usart_SendStr_length(BLT_USARTx,DEBUG_USART_ReceiveData.uart_buff,DEBUG_USART_ReceiveData.datanum);
  113.                         }
  114.                         DEBUG_USART_ReceiveData.receive_data_flag = 0;                //接收數(shù)據(jù)標志清零
  115.                         DEBUG_USART_ReceiveData.datanum = 0;               
  116.                 }
  117.                 if(BLT_USART_ReceiveData.receive_data_flag == 1)
  118.                 {
  119.                         BLT_USART_ReceiveData.uart_buff[BLT_USART_ReceiveData.datanum] = 0;
  120.                         if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"4"))//在這里可以自己定義想要接收的字符串然后處理
  121.                         {
  122.                                 Turnfront(); //小車前進
  123.                                 LED_GREEN;
  124.                         }
  125.                  if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"0"))
  126.                         {
  127.                         
  128.                            Stop();//小車停止
  129.                            LED_RED;
  130.                         }
  131.                 if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"2"))
  132.                         {
  133.                         
  134.                            Turnleft();//小車向左
  135.                            LED_BLUE
  136.                         }
  137.                 if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"1"))
  138.                         {
  139.                         
  140.                            Turnright();//小車向右
  141.                            LED_YELLOW
  142.                         }
  143.                 if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"3"))
  144.                         {
  145.                         
  146.                            Turnback();//小車后退
  147.                            LED_PURPLE
  148.                         }
  149.                         else
  150.                         {                        
  151.                                 Usart_SendStr_length(DEBUG_USARTx,BLT_USART_ReceiveData.uart_buff,BLT_USART_ReceiveData.datanum);
  152.                           Usart_SendStr_length(DEBUG_USARTx,"\r\n",2);                                
  153.                         }
  154.         clean_rebuff();
  155.                 }
  156.         }
  157. }

  158. /*********************************************END OF FILE**********************/
復(fù)制代碼

所有資料51hei附件下載:
STM32代碼: 藍牙小車.7z (192.11 KB, 下載次數(shù): 45)
安卓apk: 3.配套軟件.rar (22.1 KB, 下載次數(shù): 32)






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1