Módulo STM32-Bluetooth HC06

Módulo STM32-Bluetooth HC06

El chip seleccionado es el chip STM32F407, un módulo Bluetooth HC06.

Usamos la conexión del puerto serie, por lo que encontramos el módulo del puerto serie en el diagrama esquemático del chip.

parámetro

Formato de transmisión de parámetro definido de comunicación asíncrona del puerto serie STM32

Bit de inicio, bit de
datos (8 o 9 bits),
bit de paridad (noveno bit),
bit de parada (1, 1,5, 2 bits)
configuración de velocidad en baudios

Inserte la descripción de la imagen aquí

Velocidad en baudios: la velocidad de transmisión de la comunicación en serie: en la comunicación en serie, los datos se transmiten en bits, por lo que la velocidad de transmisión se expresa por el número de bits por segundo en el formato de transmisión, llamado velocidad en baudios (velocidad de banda) . Es 1 baudios transmitir un bit de formato por segundo. 9600bps: 9600 bits por segundo transmisión transmisión de datos 1200 bytes efectivos 960 bytes

TXD: Transmitir (tx) Forma abreviada de datos (otra redacción: T TX TD)
RXD: Recibir (rx) Forma abreviada de datos (otra redacción: R RX RD)

Inserte la descripción de la imagen aquí
Entonces averigua los pines.
PA2 —— TX
PA3 —— RX
Inserte la descripción de la imagen aquí
luego Bluetooth VCC —— 5V (placa de desarrollo)
RX —— TX
TX —— RX
GND —— GND
conectar inmediatamente

Código

Pin de configuración

//蓝牙模块的初始化
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);                                 //开启串口工作
}

Supongo que te gusta

Origin blog.csdn.net/weixin_46026429/article/details/108716444
Recomendado
Clasificación