HC05与开发板的连接线,使用杜邦线连接:
HC05_TXD <---> PA3 //串口2接收引脚
HC05_RXD <---> PA2 //串口2发送引脚
HC05_KEY <---> PA5 //普通GPIO、输出
HC05_INT <---> PA7 //普通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 <string.h>
- #include <stdlib.h>
- #include "delay.h"
- #include "sys.h"
- #include "timer.h"
- unsigned int Task_Delay[NumOfTask];
- BLTDev bltDevList;
- char sendData[1024];
- char linebuff[1024];
- /**
- * @brief 主函数
- * @param 无
- * @retval 无
- */
- int main(void)
- {
- char* redata;
- uint16_t len;
- static uint8_t hc05_role=0;
-
-
- delay_init();
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- //初始化systick
- SysTick_Init();
- SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk;
- USART_Config();
- HC05_INIT();
- TIM3_PWM_Init(1999,719);
-
- while(1)
- {
-
-
-
- //搜索蓝牙模块,并进行连接
- if(Task_Delay[2]==0 && !IS_HC05_CONNECTED() )
- {
- if(hc05_role == 1) //主模式
- {
- HC05_INFO("正在扫描蓝牙设备...");
-
- linkHC05();
-
- Task_Delay[2]=3000; //此值每1ms会减1,减到0才可以重新进来这里,所以执行的周期是3s
- }
- else //从模式
- {
- HC05_Send_CMD("AT+INQ\r\n",1);//模块在查询状态,才能容易被其它设备搜索到
- delay_ss(1000);
- HC05_Send_CMD("AT+INQC\r\n",1);//中断查询,防止查询的结果干扰串口透传的接收
- Task_Delay[2]=2000; //此值每1ms会减1,减到0才可以重新进来这里,所以执行的周期是2s
- }
-
- }
-
-
-
- //连接后每隔一段时间检查接收缓冲区
- if(Task_Delay[0]==0 && IS_HC05_CONNECTED())
- {
- uint16_t linelen;
- /*获取数据*/
- redata = get_rebuff(&len);
- // k=(int)redata;
- // printf ("k:\r\n%s",k);
- linelen = get_line(linebuff,redata,len);
-
- // /*检查数据是否有更新*/
- if(linelen<200 && linelen != 0)
- {
-
- if(strcmp(redata,"AT+LED1=ON")==0)
- {
-
- HC05_SendString("+LED1:ON\r\nOK\r\n");
- }
- else if(strcmp(redata,"AT+LED1=OFF")==0)
- {
-
- HC05_SendString("+LED1:OFF\r\nOK\r\n");
- }
- else
- {
- HC05_INFO("receive:\r\n%s",redata);
-
- if(strcmp(redata,"111")==0)
- {
-
- }
-
- if(strcmp(redata,"000")==0)
- {
-
- }
- if(strcmp(redata,"1")==0)
- {
- go_left_revolve1();
- }
- if(strcmp(redata,"2")==0)
- {
- go_straight1();
- }
- if(strcmp(redata,"3")==0)
- {
- go_right_revolve1();
- }
- if(strcmp(redata,"4")==0)
- {
- go_left1();
- }
- if(strcmp(redata,"5")==0)
- {
- go_stop();
- }
- if(strcmp(redata,"6")==0)
- {
- go_right1();
- }
- if(strcmp(redata,"8")==0)
- {
- go_back();
- }
-
- }
-
- /*处理数据后,清空接收蓝牙模块数据的缓冲区*/
-
- clean_rebuff();
- }
- Task_Delay[0]=500;//此值每1ms会减1,减到0才可以重新进来这里,所以执行的周期是500ms
- }
-
-
- }
- }
- /*********************************************END OF FILE**********************/
复制代码
所有资料51hei提供下载:
蓝牙小车.7z
(201.43 KB, 下载次数: 84)
|