[STM32]STM32双机蓝牙串口通信
期末考完力,虽然GPA--,但也终于有空搓一搓32了
蓝牙模块配置
我们先配置蓝牙模块,需要主从兼容,配置过程可以参考这个博客:https://blog.csdn.net/m0_59113542/article/details/122028037?spm=1001.2014.3001.5506
cubeMX配置
然后就是MX里的配置。PS: 两块单片机烧的是同一个程序
尝龟配置省略~
蓝牙串口模块部分:
开启USART1,Baud Rate设置成9600,并且使能全局中断
为了方便测试,我们需要配置OLED:
开启SPI1,设置为全双工模式,让PC15为推挽输出,取名为OLED_RES;PA4也为推挽输出,取名为SPI1_NSS;PB10同上,取名为OLED_DC。
然后还可以配置一下按键方便测试:
设置PB14,PB13,PB12为外部中断,下降沿触发(根据自己PCB个性化设置),同时别忘记使能EXTI line中断
然后尝龟配置,生成代码~
代码部分
先来到usart.h,加入如下代码:
/* USER CODE BEGIN 0 */
#include "string.h"
#include "main.h"
uint8_t aRxBuffer;
uint8_t Uart_RxBuff[256];
uint8_t Uart_Rx_Cnt;
uint8_t Uart_RxFlag;
uint8_t cAlmStr[] = "Warning!\r\n";
int rxUpdateFlag = 0;
int startReadFlag = 0;
/* USER CODE END 0 */
/* USER CODE BEGIN 1 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(huart);
if(huart == &huart1 && rxUpdateFlag == 0)
{
if(Uart_Rx_Cnt >= 255) // 溢出判断
{
Uart_Rx_Cnt = 0;
memset(Uart_RxBuff,0x00,sizeof(Uart_RxBuff));
HAL_UART_Transmit(&huart1, (uint8_t *)&cAlmStr, sizeof(cAlmStr),0xFFFF);
}
else // 没有溢出
{
if(aRxBuffer == '@')
{
startReadFlag = 1;
}
if(startReadFlag == 1)
{
Uart_RxBuff[Uart_Rx_Cnt++] = aRxBuffer; // 接收数据转存
if((Uart_RxBuff[Uart_Rx_Cnt-1] == 0x0A)&&(Uart_RxBuff[Uart_Rx_Cnt-2] == 0x0D)) //判断结束
{
startReadFlag = 0;
rxUpdateFlag = 1; // 数据更新
}
}
}
}
HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1); //再开启接收中???
}
/* USER CODE END 1 */
这部分代码主要是按一定格式(后面会说明)接收不定长数据
然后就是usart.h:
/* USER CODE BEGIN Prototypes */
extern uint8_t aRxBuffer;
extern uint8_t Uart_RxBuff[256];
extern uint8_t Uart_Rx_Cnt;
extern uint8_t Uart_RxFlag;
extern int rxUpdateFlag;
/* USER CODE END Prototypes */
这个就是声明外部变量,方便main.c里面调用。
然后就是main.c:
/* USER CODE BEGIN Includes */
#include "string.h"
#include "stdio.h"
#include "u8g2.h"
#include "screen.h"
/* USER CODE END Includes */
/* USER CODE BEGIN PV */
char rxStr[256];
char drawBuffer[256];
int32_t tick;
/* USER CODE END PV */
/* USER CODE BEGIN 2 */
tick = HAL_GetTick();
HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);
oledInit();
/* USER CODE END 2 */
/* USER CODE BEGIN 3 */
if(rxUpdateFlag == 1) // 收到新数据,开始处理数据
{
strcpy(rxStr, (char *)Uart_RxBuff);
memset(Uart_RxBuff,0x00,sizeof(Uart_RxBuff));
// flag置位
rxUpdateFlag = 0;
Uart_Rx_Cnt = 0;
}
u8g2_FirstPage(&u8g2);
do
{
u8g2_DrawStr(&u8g2, 0, 15, rxStr);
u8g2_DrawStr(&u8g2, 0, 30, drawBuffer);
}while(u8g2_NextPage(&u8g2));
/* USER CODE END 3 */
/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == GPIO_PIN_14) // KEY_1
{
if(HAL_GetTick() - tick > 200) // 消抖是个好习惯
{
HAL_UART_Transmit(&huart1, (uint8_t *)"@1\r\n", sizeof("@1\r\n"), HAL_MAX_DELAY);
strcpy(drawBuffer, "KEY1 send ok!");
tick=HAL_GetTick();
}
}
if(GPIO_Pin == GPIO_PIN_13) // KEY_2
{
if(HAL_GetTick() - tick > 200)
{
HAL_UART_Transmit(&huart1, (uint8_t *)"@2\r\n", sizeof("@1\r\n"), HAL_MAX_DELAY);
strcpy(drawBuffer, "KEY2 send ok!");
tick=HAL_GetTick();
}
}
if(GPIO_Pin == GPIO_PIN_12) // KEY_3
{
if(HAL_GetTick() - tick > 200)
{
HAL_UART_Transmit(&huart1, (uint8_t *)"@3\r\n", sizeof("@1\r\n"), HAL_MAX_DELAY);
strcpy(drawBuffer, "KEY3 send ok!");
tick=HAL_GetTick();
}
}
}
/* USER CODE END 4 */
之后,我们只要按下按键就可以啦~
注:消息发送格式:"@XXXXXX\r\n"
标签:CODE,HAL,Uart,uint8,STM32,RxBuff,USER,串口,双机 From: https://www.cnblogs.com/Asaka-QianXiang/p/17537731.html