[STM32WB] 【STM32WB09KE测评】用蓝牙广播时间并在手机上显示当前时间

[复制链接]
1299|2
 楼主| stb988 发表于 2024-11-18 17:04 | 显示全部楼层 |阅读模式
#申请原创#              本文用一个简单的示例来体验蓝牙的广播功能,用STM32WB09KE使用内置的RTC时钟,通过蓝牙广播当前时间,并用手机连接后,显示当前时间。
       具体实现如下,先找到官方示例包,找到
       屏幕截图 2024-11-18 165411.png
通过修改些例程来实现。
      打开工程,第一部就是添加RTC时钟初始化程序,把这两个文件添加进来
屏幕截图 2024-11-18 165614.png
然后自已添加RTC.C和RTC.H,这两个文件
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * [url=home.php?mod=space&uid=288409]@file[/url]    rtc.c
  5.   * [url=home.php?mod=space&uid=247401]@brief[/url]   This file provides code for the configuration
  6.   *          of the RTC instances.
  7.   ******************************************************************************
  8.   * @attention
  9.   *
  10.   * Copyright (c) 2024 STMicroelectronics.
  11.   * All rights reserved.
  12.   *
  13.   * This software is licensed under terms that can be found in the LICENSE file
  14.   * in the root directory of this software component.
  15.   * If no LICENSE file comes with this software, it is provided AS-IS.
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "rtc.h"

  22. /* USER CODE BEGIN 0 */

  23. /* USER CODE END 0 */

  24. RTC_HandleTypeDef hrtc;

  25. /* RTC init function */
  26. void MX_RTC_Init(void)
  27. {

  28.   /* USER CODE BEGIN RTC_Init 0 */

  29.   /* USER CODE END RTC_Init 0 */

  30.   RTC_TimeTypeDef sTime = {0};
  31.   RTC_DateTypeDef sDate = {0};

  32.   /* USER CODE BEGIN RTC_Init 1 */

  33.   /* USER CODE END RTC_Init 1 */

  34.   /** Initialize RTC Only
  35.   */
  36.   hrtc.Instance = RTC;
  37.   hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  38.   hrtc.Init.AsynchPrediv = 127;
  39.   hrtc.Init.SynchPrediv = 255;
  40.   hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  41.   hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  42.   if (HAL_RTC_Init(&hrtc) != HAL_OK)
  43.   {
  44.     Error_Handler();
  45.   }

  46.   /* USER CODE BEGIN Check_RTC_BKUP */

  47.   /* USER CODE END Check_RTC_BKUP */

  48.   /** Initialize RTC and set the Time and Date
  49.   */
  50.   sTime.Hours = 12;
  51.   sTime.Minutes = 30;
  52.   sTime.Seconds = 50;
  53.   sTime.SubSeconds = 0;
  54.   sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  55.   sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  56.   if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  57.   {
  58.     Error_Handler();
  59.   }
  60.   sDate.WeekDay = RTC_WEEKDAY_THURSDAY;
  61.   sDate.Month = RTC_MONTH_OCTOBER;
  62.   sDate.Date = 30;
  63.   sDate.Year = 24;

  64.   if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  65.   {
  66.     Error_Handler();
  67.   }
  68.   /* USER CODE BEGIN RTC_Init 2 */

  69.   /* USER CODE END RTC_Init 2 */

  70. }

  71. void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
  72. {

  73.   RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  74.   if(rtcHandle->Instance==RTC)
  75.   {
  76.   /* USER CODE BEGIN RTC_MspInit 0 */

  77.   /* USER CODE END RTC_MspInit 0 */

  78.   /** Initializes the peripherals clock
  79.   */
  80.     PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC_WDG_BLEWKUP;
  81.     PeriphClkInitStruct.RTCWDGBLEWKUPClockSelection = RCC_RTC_WDG_BLEWKUP_CLKSOURCE_LSE;
  82.     if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  83.     {
  84.       Error_Handler();
  85.     }

  86.     /* RTC clock enable */
  87.     __HAL_RCC_RTC_CLK_ENABLE();
  88.   /* USER CODE BEGIN RTC_MspInit 1 */

  89.   /* USER CODE END RTC_MspInit 1 */
  90.   }
  91. }

  92. void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle)
  93. {

  94.   if(rtcHandle->Instance==RTC)
  95.   {
  96.   /* USER CODE BEGIN RTC_MspDeInit 0 */

  97.   /* USER CODE END RTC_MspDeInit 0 */
  98.     /* Peripheral clock disable */
  99.     __HAL_RCC_RTC_CLK_DISABLE();
  100.   /* USER CODE BEGIN RTC_MspDeInit 1 */

  101.   /* USER CODE END RTC_MspDeInit 1 */
  102.   }
  103. }

  104. /* USER CODE BEGIN 1 */

  105. /* USER CODE END 1 */
再来添加rtc.h
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file    rtc.h
  5.   * @brief   This file contains all the function prototypes for
  6.   *          the rtc.c file
  7.   ******************************************************************************
  8.   * @attention
  9.   *
  10.   * Copyright (c) 2024 STMicroelectronics.
  11.   * All rights reserved.
  12.   *
  13.   * This software is licensed under terms that can be found in the LICENSE file
  14.   * in the root directory of this software component.
  15.   * If no LICENSE file comes with this software, it is provided AS-IS.
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Define to prevent recursive inclusion -------------------------------------*/
  21. #ifndef __RTC_H__
  22. #define __RTC_H__

  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif

  26. /* Includes ------------------------------------------------------------------*/
  27. #include "main.h"

  28. /* USER CODE BEGIN Includes */

  29. /* USER CODE END Includes */

  30. extern RTC_HandleTypeDef hrtc;

  31. /* USER CODE BEGIN Private defines */

  32. /* USER CODE END Private defines */

  33. void MX_RTC_Init(void);

  34. /* USER CODE BEGIN Prototypes */

  35. /* USER CODE END Prototypes */

  36. #ifdef __cplusplus
  37. }
  38. #endif

  39. #endif /* __RTC_H__ */

现在主函数中添加初始化函数
  1.     RTC_TimeTypeDef sTime;
  2.     RTC_DateTypeDef sDate;
  3. int main(void)
  4. {

  5.   /* USER CODE BEGIN 1 */
  6.                 uint8_t hours = sTime.Hours;
  7.                 uint8_t minutes = sTime.Minutes;
  8.                 uint8_t seconds = sTime.Seconds;
  9.                 uint8_t day = sDate.Date;
  10.                 uint8_t month = sDate.Month;
  11.                 uint8_t year = sDate.Year;
  12.   /* USER CODE END 1 */

  13.   /* MCU Configuration--------------------------------------------------------*/

  14.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  15.   HAL_Init();

  16.   /* USER CODE BEGIN Init */

  17.   /* USER CODE END Init */

  18.   /* Configure the system clock */
  19.   SystemClock_Config();

  20.   /* Configure the peripherals common clocks */
  21.   PeriphCommonClock_Config();

  22.   /* USER CODE BEGIN SysInit */

  23.   /* USER CODE END SysInit */

  24.   /* Initialize all configured peripherals */
  25.   MX_GPIO_Init();
  26.   MX_RADIO_Init();
  27.   MX_RADIO_TIMER_Init();
  28.   MX_PKA_Init();
  29.   /* USER CODE BEGIN 2 */
  30.         MX_RTC_Init();
  31.   /* USER CODE END 2 */

  32.   /* Init code for STM32_BLE */
  33.   MX_APPE_Init(NULL);

  34.   /* Infinite loop */
  35.   /* USER CODE BEGIN WHILE */
  36.   while (1)
  37.   {

  38.     /* USER CODE END WHILE */
  39.     MX_APPE_Process();

  40.     /* USER CODE BEGIN 3 */
  41.                
  42.   }
  43.   /* USER CODE END 3 */
  44. }
要发送广播,到app_ertry.c中修改
  1. void MX_APPE_Process(void)
  2. {
  3.        
  4.   /* USER CODE BEGIN MX_APPE_Process_1 */
  5.     char time_str[16];
  6.           uint8_t i =0;
  7.     HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
  8.     HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);

  9.     sprintf(time_str, "%02d:%02d:%02d", sTime.Hours, sTime.Minutes, sTime.Seconds);

  10.           
  11.     UartRxCpltCallback((uint8_t *)time_str, strlen(time_str));
  12.   
  13.         //        printf("Time: %02d:%02d:%02d\n",sTime.Hours, sTime.Minutes, sTime.Seconds);
  14.   /* USER CODE END MX_APPE_Process_1 */
  15.   UTIL_SEQ_Run(UTIL_SEQ_DEFAULT);
  16.   /* USER CODE BEGIN MX_APPE_Process_2 */

  17.   /* USER CODE END MX_APPE_Process_2 */
  18. }
最后还要修改UartRxCpltCallback函数,因为这个函数只实现了单个字符发送,
  1. void UartRxCpltCallback(uint8_t * pdata, uint16_t size)
  2. {

  3.   uint8_t byte_received;
  4.    
  5.   if(size == 1)
  6.   {
  7.     byte_received = pdata[0];
  8.         
  9.     if(buffUartRxIndex < sizeof(a_buffUartRx))
  10.     {
  11.       a_buffUartRx[buffUartRxIndex++] = byte_received;
  12.     }
  13.     else
  14.     {
  15.       buffUartRxIndex = 0;
  16.     }
  17.    
  18.     if( (byte_received == '\n') || (buffUartRxIndex >= sizeof(a_buffUartRx)))
  19.     {
  20.       memcpy(&a_buffSpTx[0], &a_buffUartRx[0], buffUartRxIndex);
  21.       buffSpTxLen = buffUartRxIndex;
  22.       
  23.       buffUartRxIndex = 0;
  24.       UTIL_SEQ_SetTask(1 << CFG_TASK_SERIALPORT_TX_REQ_ID, CFG_SEQ_PRIO_0);
  25.     }   
  26.   }
  27.   else
  28.   {
  29.         for (uint16_t i = 0; i < size; i++)
  30.         {
  31.             byte_received = pdata[i];

  32.             if (buffUartRxIndex < sizeof(a_buffUartRx))
  33.             {
  34.                 a_buffUartRx[buffUartRxIndex++] = byte_received;
  35.             }
  36.             else
  37.             {
  38.                 buffUartRxIndex = 0; // 缓冲区溢出,重置索引
  39.             }

  40.             if ((byte_received == '\n') || (buffUartRxIndex >= sizeof(a_buffUartRx)))
  41.             {
  42.                 memcpy(&a_buffSpTx[0], &a_buffUartRx[0], buffUartRxIndex);
  43.                 buffSpTxLen = buffUartRxIndex;

  44.                 buffUartRxIndex = 0;
  45.                 UTIL_SEQ_SetTask(1 << CFG_TASK_SERIALPORT_TX_REQ_ID, CFG_SEQ_PRIO_0);

  46.                 break; // 数据处理完成,退出循环
  47.             }
  48.         }  
  49.   }
  50.   return;
  51. }
最后编译下载,打开手机连接开发板
Screenshot_20241118-164332.jpg

Screenshot_20241118-164314.jpg
Screenshot_20241118-164321.jpg
这时就可以看到当前时间信息了。
蓝牙体验到此,喜欢的朋友不如自已动手实现一下,也可以把开发板上的其实数据广播出来,比如电压,温度等信息。
稳稳の幸福 发表于 2024-11-18 18:53 | 显示全部楼层
奈斯,看起来不错。广播的用法是不是不需要连接?
 楼主| stb988 发表于 2024-11-18 19:05 | 显示全部楼层
蓝牙都是无线的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

54

主题

402

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部