STM32-Bluetooth module HC06
The selected chip is STM32F407 chip, a HC06 Bluetooth module.
We use serial port connection, so we find the serial port module in the chip schematic diagram.
parameter
STM32 serial port asynchronous communication defined parameter transmission format
Start bit,
data bit (8 or 9 bits),
parity bit (9th bit),
stop bit (1, 1.5, 2 bit)
baud rate setting
Baud rate: The transmission rate of serial communication: In serial communication, data is transmitted in bits, so the transmission rate is expressed by the number of bits per second in the transmission format, called the baud rate (band rate) . It is 1 baud to transmit one format bit per second. 9600bps: 9600 bits per second transmission data transmission 1200 bytes effective 960 bytes
TXD: Transmit(tx) Data abbreviated form (other wording: T TX TD)
RXD: Receive(rx) Data abbreviated form (other wording: R RX RD)
Then find out the pins.
PA2——TX
PA3——RX
then Bluetooth VCC——5V (development board)
RX——TX
TX——RX
GND——GND
connect immediately
Code
Configuration pin
//蓝牙模块的初始化
void HC06_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); //引脚进行映射
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Fast_Speed;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStruct); //初始化引脚A2,A3为复用功能
USART_InitStruct.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_InitStruct.USART_BaudRate = 9600; //蓝牙模块的波特率一般为9600
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_Init(USART2, &USART_InitStruct); //初始化串口2的接收端和发送端
NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x00;
NVIC_Init(&NVIC_InitStruct); //初始化中断向量表
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //开启接收端的中断
USART_Cmd(USART2, ENABLE); //开启串口工作
}