1. Explanation: The purpose of writing this article is to abandon some more professional descriptions, and start from the perspective of some newcomers to help such friends solve some problems, so the content is written in relatively popular vernacular. There are already many If you want some more professional descriptions, I suggest you read other friends' tutorials.
2. Introduction: The content of this article mainly introduces how to add and use the Bluetooth module on STM32, and how to connect Bluetooth with STM32. The article finally introduces an example.
3. Hardware connection
1. Connection diagram
Physical connection diagram (see the color of the line)
As shown in the figure above, the STM32 has three sets of serial port pins, choose one set by yourself (see the circuit diagram of your own STM32 board for details, I only use the RX1/TX1 set as an example in the following content, and the following code configuration will be different for different sets ), as for the VCC pins and GND pins, find them yourself, there are signs on the board.
First of all, let’s talk about the essence of the function implemented by the STM32 board here - "serial port communication", and the role played by the Bluetooth module here - "serial port client"
How to understand this? To put it simply, the STM board and the Bluetooth module carry out "WeChat chat", that is, both have the function of sending and receiving.
3. Serial port content:
After connecting the pins, the next step is the code part. The serial port function has a total of five functions (recommended to copy and paste)
(1) Communication initialization
void USART_Config(void)
{
GPIO_InitTypeDef x;
USART_InitTypeDef y;
//打开时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
x.GPIO_Pin=GPIO_Pin_9;
x.GPIO_Mode=GPIO_Mode_AF_PP;
x.GPIO_Speed=GPIO_Speed_50MHz;
//初始化发送管脚
GPIO_Init(GPIOA,&x);
x.GPIO_Pin=GPIO_Pin_10;
x.GPIO_Mode=GPIO_Mode_IN_FLOATING;
//初始化接收管脚
GPIO_Init(GPIOA,&x);
//注意波特率,否则会出现乱码
y.USART_BaudRate=9600;
y.USART_WordLength=USART_WordLength_8b;
y.USART_Parity=USART_Parity_No;
y.USART_StopBits=USART_StopBits_1;
y.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
y.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
//串口配置(波特率等)
USART_Init(USART1,&y);
//打开串口接收中断
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,ENABLE);
//配置串口中断的优先级
NVIC_Configuration();
}
(2) Configure serial port interrupt priority
void NVIC_Configuration()
{
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
}
(3) Write an interrupt handler function
void USART1_IRQHandler(void)
{
uint8_t ucTemp;
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
{
ucTemp=USART_ReceiveData(USART1);
USART_SendData(USART1,ucTemp);
}
}
(4) Rewrite the output and input functions (remember to add #include "stdio.h")
#include "stdio.h"
int fputc(int ch,FILE *f)
{
USART_SendData(USART1,(uint8_t)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return (ch);
}
int fgetc(FILE *f)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return (int)USART_ReceiveData(USART1);
}
(5) Finally, of course, the main function (remember that there are two header files including the header file above)
#include "stm32f10x.h"
int main(void)
{
USART_Config();
while(1);
}
4. Function example (control the STM32 board light on the mobile phone via Bluetooth)
(1) Configure the serial port (that is, the code above)
(2) Initialize the small light (the pin of my STM32 to control the LED light is PC13)
void LED_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStruct);
}
(3) Small light control function
//LED_G(0): 灯亮 | LED_G(1):灯灭
void LED_G(uint8_t n)
{
if(n)
GPIO_SetBits(GPIOC, GPIO_Pin_13);
else
GPIO_ResetBits(GPIOC, GPIO_Pin_13);
}
(4) Modify the main function
#include "stm32f10x.h"
int main(void)
{
USART_Config();
LED_GPIO_Config();
while(1);
}
(5) Modify the serial interrupt function
//如果手机蓝牙发送‘0’则灯亮,(若其他数据)否则灯灭
void USART1_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
{
res= USART_ReceiveData(USART1);
USART_SendData(USART1,res);
if(res=='0')
{
LED_G(0);
}
else
{
LED_G(1);
}
}
}
5. Effect
6. Resources
Complete project link: https://pan.baidu.com/s/18EuSGRANwR2XbEAwpQLwbg Extraction code: x6v4
Test software link: https://pan.baidu.com/s/1rX6GbTvHRU3Mb18tgwEjHg Extraction code: td1d
Seven, later