无线手持二维码识别项目是中山大学电子与信息工程学院(微电子学院)的工程应用训练课程的设计要求。项目是基于STM32F103开发的,开源代码:stm32f103zet6-qrcode-detect,bilibili:基于STM32F1的无线手持二维码识别项目。CSDN总目录:基于STM32F103的二维码识别项目。项目移植了Zbar库进行二维码识别,而非使用二维码识别模块。
此篇文章讲述HC-05蓝牙模块相关的设计。
HC-05蓝牙模块通过串口与STM32通讯,之后通过蓝牙将识别到的二维码的信息发送到手机。
初始化UART的配置,之后在main函数里面调用UART4_Init(void)函数即可实现该串口的激活。
//蓝牙
void UART4_Init(void) {
// 1. Enable the clock for USART4 and GPIOC
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
// 2. Configure PC10 as USART2 TX (Alternate Function Push Pull)
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOC, &GPIO_InitStruct);
// 3. Configure PC11 as USART4 RX (Input, Floating)
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStruct);
// 4. Configure USART2 settings
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(UART4, &USART_InitStructure);
// 5. Enable USART2
USART_Cmd(UART4, ENABLE);
}
实现发送功能的函数如下:
void Serial_SendByte4(uint8_t Byte)
{
USART_SendData(UART4, Byte);
while (USART_GetFlagStatus(UART4, USART_FLAG_TXE) == RESET);
}
void Serial_SendArray4(uint8_t *Array, uint16_t Length)
{
uint16_t i;
for (i = 0; i < Length; i ++)
{
Serial_SendByte4(Array[i]);
}
}
void Serial_SendString4(char *String)
{
uint8_t i;
for (i = 0; String[i] != '\0'; i ++)
{
Serial_SendByte4(String[i]);
}
}
之后在主函数里面通过下面函数实现了发送信息的功能,其中data就是将图像矩阵送入Zbar检测函数后得到的结果。
Serial_SendString4((char* )data);
蓝牙模块的这些串口的设置很基础,这里不详细展开。重点介绍手机端使用的软件。
直接浏览器搜索就行了,蓝色的图标,使用方法也很简单,手机蓝牙配对后直接点击连接就行了。
HC-05接线的话只需要四根就行了:RXD、TXD、GND、VCC,注意了VCC要用STM32的5V驱动,3.3V电压不足。蓝牙模块的HC-05的RXD接STM32的TX,TXD接STM32的RX。