HC-05實現電腦與stm32通訊
阿新 • • 發佈:2019-01-30
藍芽在通訊中代替的是串列埠通訊時的一根線,所以在串列埠通訊改藍芽通訊時無需更改程式碼,只需將藍芽連到微控制器上,注意若使用usart1,注意連線位置,之前我用的正點mini的板子,PA9、10用跳線帽和USB串列埠R和T連在一起,導致我把藍芽的R和Tlian連在了USB的T和R上,藍芽根本沒和微控制器連在一起,從而無法通訊,正確連線方式應為藍芽R——PA10,T——PA9
初始化設定上電過程中長按藍芽上的小按鈕,直到紅色由長亮變為隔1s亮2s,由此進入AT模式,串列埠助手配置即可。
由於考慮到要下載程式,我把藍芽改成了從微控制器usart2接收發資料,程式如下:
#include "sys.h" #include "bluetooth.h" #if SYSTEM_SUPPORT_UCOS #include "includes.h" #endif #if 1 #pragma import(__use_no_semihosting) //±ê×¼¿âÐèÒªµÄÖ§³Öº¯Êý struct __FILE { int handle; }; FILE __stdout; _sys_exit(int x) { x = x; } //printf函式,重心 int fputc(int ch, FILE *f) { while((USART2->SR&0X40)==0); USART2->DR = (u8) ch; return ch; } #endif u8 USART_RX_BUF2[USART_REC_LEN2]; u16 USART_RX_STA2=0; void uart2_init(u32 bound){ //GPIO¶Ë¿ÚÉèÖà GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //usart2在APB1上 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); USART_DeInit(USART2); //USART1_TX PA.9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö GPIO_Init(GPIOA, &GPIO_InitStructure); //³õʼ»¯PA9 //USART1_RX PA.10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë GPIO_Init(GPIOA, &GPIO_InitStructure); //³õʼ»¯PA10 //Usart1 NVIC ÅäÖà NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//ÇÀÕ¼ÓÅÏȼ¶3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×ÓÓÅÏȼ¶3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷ //USART ³õʼ»¯ÉèÖà USART_InitStructure.USART_BaudRate = bound; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART2, &USART_InitStructure); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_Cmd(USART2, ENABLE); } #if EN_USART2_RX void USART2_IRQHandler(void) { u8 Res; #ifdef OS_TICKS_PER_SEC OSIntEnter(); #endif if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) ±ØÐëÊÇ0x0d 0x0a½áβ) { Res =USART_ReceiveData(USART2); if((USART_RX_STA2&0x8000)==0) { if(USART_RX_STA2&0x4000) { if(Res!=0x0a)USART_RX_STA2=0; else USART_RX_STA2|=0x8000; } else //»¹Ã»ÊÕµ½0X0D { if(Res==0x0d)USART_RX_STA2|=0x4000; else { USART_RX_BUF2[USART_RX_STA2&0X3FFF]=Res ; USART_RX_STA2++; if(USART_RX_STA2>(USART_REC_LEN2-1))USART_RX_STA2=0; } } } } #ifdef OS_TICKS_PER_SEC OSIntExit(); #endif } #endif
#ifndef __BLUETOOTH_H #define __BLUETOOTH_H #include "stdio.h" #include "sys.h" #define USART_REC_LEN2 200 //¶¨Òå×î´ó½ÓÊÕ×Ö½ÚÊý 200 #define EN_USART2_RX 1 //ʹÄÜ£¨1£©/½ûÖ¹£¨0£©´®¿Ú1½ÓÊÕ extern u8 USART_RX_BUF2[USART_REC_LEN2]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.Ä©×Ö½ÚΪ»»Ðзû extern u16 USART_RX_STA2; //½ÓÊÕ״̬±ê¼Ç //Èç¹ûÏë´®¿ÚÖжϽÓÊÕ£¬Ç벻ҪעÊÍÒÔϺ궨Òå void uart2_init(u32 bound); #endif
由此,在主函式中新增“Bluetooth.h”,主函式內設定好波特率,記得線查對,就可以用了。