各个引脚都有相对应的连接解释。又不懂的铁子们可以私聊我。
引脚分配
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)
安卓apk:
3.配套软件.rar
(22.1 KB, 下载次数: 32)
|