各個引腳都有相對應的連接解釋。又不懂的鐵子們可以私聊我。
引腳分配
HC05與開發板的連接線,使用杜邦線連接:
HC05_TXD <---> PA3 //串口2接收引腳
HC05_RXD <---> PA2 //串口2發送引腳
HC05_KEY <---> PB14 //普通GPIO、輸出
HC05_INT <---> PB13 //普通GPIO、輸入
HC05_VCC <---> 接5V或3.3V
HC05_GND <---> 接地線
調試串口(TTL-USB TO USART):
CH340的收發引腳與STM32的發收引腳相連。
RX<--->PA9
TX<--->PA10
單片機源程序如下:
- #include "stm32f10x.h"
- #include "./usart/bsp_usart.h"
- #include "./usart/bsp_usart_blt.h"
- #include "./systick/bsp_SysTick.h"
- #include "./hc05/bsp_hc05.h"
- #include "./led/bsp_led.h"
- #include "./key/bsp_key.h"
- #include <string.h>
- #include <stdlib.h>
- #include "MOTOR.h"
- BLTDev bltDevList;
- extern ReceiveData DEBUG_USART_ReceiveData;
- extern ReceiveData BLT_USART_ReceiveData;
- /**
- * @brief 主函數
- * @param 無
- * @retval 無
- */
- int main(void)
- {
- char hc05_name[30]="HC05_SLAVE";
- char hc05_nameCMD[40];
- //初始化systick
- SysTick_Init();
- SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk;
-
- MotorInit();//控制電機函數
- USART_Cmd(USART1, ENABLE); //使能串口
- USART_Config();
-
- LED_GPIO_Config();
- Key_GPIO_Config();
-
- HC05_INFO("**********HC05模塊AT指令測試實驗************");
-
- if(HC05_Init() == 0)
- HC05_INFO("HC05模塊檢測正常。");
- else
- {
- HC05_ERROR("HC05模塊檢測不正常,請檢查模塊與開發板的連接,然后復位開發板重新測試。");
- while(1);
- }
-
- /*各種命令測試演示,默認不顯示。
- *在bsp_hc05.h文件把HC05_DEBUG_ON 宏設置為1,
- *即可通過串口調試助手接收調試信息*/
-
- HC05_Send_CMD("AT+VERSION?\r\n",1);
-
- HC05_Send_CMD("AT+ADDR?\r\n",1);
-
- HC05_Send_CMD("AT+UART?\r\n",1);
-
- HC05_Send_CMD("AT+CMODE?\r\n",1);
-
- HC05_Send_CMD("AT+STATE?\r\n",1);
- HC05_Send_CMD("AT+ROLE=0\r\n",1);
-
- /*初始化SPP規范*/
- HC05_Send_CMD("AT+INIT\r\n",1);
- HC05_Send_CMD("AT+CLASS=0\r\n",1);
- HC05_Send_CMD("AT+INQM=1,9,48\r\n",1);
-
- /*設置模塊名字*/
- sprintf(hc05_nameCMD,"AT+NAME=%s\r\n",hc05_name);
- HC05_Send_CMD(hc05_nameCMD,1);
- HC05_INFO("本模塊名字為:%s ,模塊已準備就緒。",hc05_name);
- while(1)
- {
- if(DEBUG_USART_ReceiveData.receive_data_flag == 1)
- {
- DEBUG_USART_ReceiveData.uart_buff[DEBUG_USART_ReceiveData.datanum] = 0;
- if(strstr((char *)DEBUG_USART_ReceiveData.uart_buff,"AT"))//如果數據是以AT開頭的,就把KEY置高,設置藍牙模塊
- {
- BLT_KEY_HIGHT;
- delay_ms(20);
- Usart_SendStr_length(BLT_USARTx,DEBUG_USART_ReceiveData.uart_buff,DEBUG_USART_ReceiveData.datanum);
- Usart_SendStr_length(BLT_USARTx,"\r\n",2);
- BLT_KEY_LOW;
- }else if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"4"))//在這里可以自己定義想要接收的字符串然后處理
- {
- Turnfront(); //小車前進
- LED_GREEN;
- }
- if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"0"))
- {
-
- Stop();//小車停止
- LED_RED;
- }
- if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"1"))
- {
-
- Turnleft();//小車向左
- LED_BLUE
- }
- if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"2"))
- {
-
- Turnright();//小車向右
- LED_YELLOW
- }
- if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"3"))
- {
-
- Turnback();//小車后退
- LED_PURPLE
- }
- else
- {
- BLT_KEY_LOW;
- Usart_SendStr_length(BLT_USARTx,DEBUG_USART_ReceiveData.uart_buff,DEBUG_USART_ReceiveData.datanum);
- }
- DEBUG_USART_ReceiveData.receive_data_flag = 0; //接收數據標志清零
- DEBUG_USART_ReceiveData.datanum = 0;
- }
- if(BLT_USART_ReceiveData.receive_data_flag == 1)
- {
- BLT_USART_ReceiveData.uart_buff[BLT_USART_ReceiveData.datanum] = 0;
- if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"4"))//在這里可以自己定義想要接收的字符串然后處理
- {
- Turnfront(); //小車前進
- LED_GREEN;
- }
- if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"0"))
- {
-
- Stop();//小車停止
- LED_RED;
- }
- if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"2"))
- {
-
- Turnleft();//小車向左
- LED_BLUE
- }
- if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"1"))
- {
-
- Turnright();//小車向右
- LED_YELLOW
- }
- if(strstr((char *)BLT_USART_ReceiveData.uart_buff,"3"))
- {
-
- Turnback();//小車后退
- LED_PURPLE
- }
- else
- {
- Usart_SendStr_length(DEBUG_USARTx,BLT_USART_ReceiveData.uart_buff,BLT_USART_ReceiveData.datanum);
- Usart_SendStr_length(DEBUG_USARTx,"\r\n",2);
- }
- clean_rebuff();
- }
- }
- }
- /*********************************************END OF FILE**********************/
復制代碼
所有資料51hei附件下載:
STM32代碼:
藍牙小車.7z
(192.11 KB, 下載次數: 45)
2021-9-23 21:50 上傳
點擊文件名下載附件
安卓apk:
3.配套軟件.rar
(22.1 KB, 下載次數: 32)
2021-9-23 20:49 上傳
點擊文件名下載附件
|