#申请原创# 本文用一个简单的示例来体验蓝牙的广播功能,用STM32WB09KE使用内置的RTC时钟,通过蓝牙广播当前时间,并用手机连接后,显示当前时间。
具体实现如下,先找到官方示例包,找到
通过修改些例程来实现。
打开工程,第一部就是添加RTC时钟初始化程序,把这两个文件添加进来
然后自已添加RTC.C和RTC.H,这两个文件
再来添加rtc.h
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file rtc.h
- * @brief This file contains all the function prototypes for
- * the rtc.c file
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2024 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Define to prevent recursive inclusion -------------------------------------*/
- #ifndef __RTC_H__
- #define __RTC_H__
- #ifdef __cplusplus
- extern "C" {
- #endif
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- /* USER CODE BEGIN Includes */
- /* USER CODE END Includes */
- extern RTC_HandleTypeDef hrtc;
- /* USER CODE BEGIN Private defines */
- /* USER CODE END Private defines */
- void MX_RTC_Init(void);
- /* USER CODE BEGIN Prototypes */
- /* USER CODE END Prototypes */
- #ifdef __cplusplus
- }
- #endif
- #endif /* __RTC_H__ */
现在主函数中添加初始化函数
- RTC_TimeTypeDef sTime;
- RTC_DateTypeDef sDate;
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- uint8_t hours = sTime.Hours;
- uint8_t minutes = sTime.Minutes;
- uint8_t seconds = sTime.Seconds;
- uint8_t day = sDate.Date;
- uint8_t month = sDate.Month;
- uint8_t year = sDate.Year;
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* Configure the peripherals common clocks */
- PeriphCommonClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_RADIO_Init();
- MX_RADIO_TIMER_Init();
- MX_PKA_Init();
- /* USER CODE BEGIN 2 */
- MX_RTC_Init();
- /* USER CODE END 2 */
- /* Init code for STM32_BLE */
- MX_APPE_Init(NULL);
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- MX_APPE_Process();
- /* USER CODE BEGIN 3 */
-
- }
- /* USER CODE END 3 */
- }
要发送广播,到app_ertry.c中修改
- void MX_APPE_Process(void)
- {
-
- /* USER CODE BEGIN MX_APPE_Process_1 */
- char time_str[16];
- uint8_t i =0;
- HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
- HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
- sprintf(time_str, "%02d:%02d:%02d", sTime.Hours, sTime.Minutes, sTime.Seconds);
-
- UartRxCpltCallback((uint8_t *)time_str, strlen(time_str));
-
- // printf("Time: %02d:%02d:%02d\n",sTime.Hours, sTime.Minutes, sTime.Seconds);
- /* USER CODE END MX_APPE_Process_1 */
- UTIL_SEQ_Run(UTIL_SEQ_DEFAULT);
- /* USER CODE BEGIN MX_APPE_Process_2 */
- /* USER CODE END MX_APPE_Process_2 */
- }
最后还要修改UartRxCpltCallback函数,因为这个函数只实现了单个字符发送,
- void UartRxCpltCallback(uint8_t * pdata, uint16_t size)
- {
-
- uint8_t byte_received;
-
- if(size == 1)
- {
- byte_received = pdata[0];
-
- if(buffUartRxIndex < sizeof(a_buffUartRx))
- {
- a_buffUartRx[buffUartRxIndex++] = byte_received;
- }
- else
- {
- buffUartRxIndex = 0;
- }
-
- if( (byte_received == '\n') || (buffUartRxIndex >= sizeof(a_buffUartRx)))
- {
- memcpy(&a_buffSpTx[0], &a_buffUartRx[0], buffUartRxIndex);
- buffSpTxLen = buffUartRxIndex;
-
- buffUartRxIndex = 0;
- UTIL_SEQ_SetTask(1 << CFG_TASK_SERIALPORT_TX_REQ_ID, CFG_SEQ_PRIO_0);
- }
- }
- else
- {
- for (uint16_t i = 0; i < size; i++)
- {
- byte_received = pdata[i];
- if (buffUartRxIndex < sizeof(a_buffUartRx))
- {
- a_buffUartRx[buffUartRxIndex++] = byte_received;
- }
- else
- {
- buffUartRxIndex = 0; // 缓冲区溢出,重置索引
- }
- if ((byte_received == '\n') || (buffUartRxIndex >= sizeof(a_buffUartRx)))
- {
- memcpy(&a_buffSpTx[0], &a_buffUartRx[0], buffUartRxIndex);
- buffSpTxLen = buffUartRxIndex;
- buffUartRxIndex = 0;
- UTIL_SEQ_SetTask(1 << CFG_TASK_SERIALPORT_TX_REQ_ID, CFG_SEQ_PRIO_0);
- break; // 数据处理完成,退出循环
- }
- }
- }
- return;
- }
最后编译下载,打开手机连接开发板
这时就可以看到当前时间信息了。
蓝牙体验到此,喜欢的朋友不如自已动手实现一下,也可以把开发板上的其实数据广播出来,比如电压,温度等信息。
|