stb988 发表于 2024-11-18 17:04

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

#申请原创#            本文用一个简单的示例来体验蓝牙的广播功能,用STM32WB09KE使用内置的RTC时钟,通过蓝牙广播当前时间,并用手机连接后,显示当前时间。
       具体实现如下,先找到官方示例包,找到
      
通过修改些例程来实现。
      打开工程,第一部就是添加RTC时钟初始化程序,把这两个文件添加进来

然后自已添加RTC.C和RTC.H,这两个文件
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file    rtc.c
* @brief   This file provides code for the configuration
*          of the RTC instances.
******************************************************************************
* @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 */
/* Includes ------------------------------------------------------------------*/
#include "rtc.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

RTC_HandleTypeDef hrtc;

/* RTC init function */
void MX_RTC_Init(void)
{

/* USER CODE BEGIN RTC_Init 0 */

/* USER CODE END RTC_Init 0 */

RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};

/* USER CODE BEGIN RTC_Init 1 */

/* USER CODE END RTC_Init 1 */

/** Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
    Error_Handler();
}

/* USER CODE BEGIN Check_RTC_BKUP */

/* USER CODE END Check_RTC_BKUP */

/** Initialize RTC and set the Time and Date
*/
sTime.Hours = 12;
sTime.Minutes = 30;
sTime.Seconds = 50;
sTime.SubSeconds = 0;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
{
    Error_Handler();
}
sDate.WeekDay = RTC_WEEKDAY_THURSDAY;
sDate.Month = RTC_MONTH_OCTOBER;
sDate.Date = 30;
sDate.Year = 24;

if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
{
    Error_Handler();
}
/* USER CODE BEGIN RTC_Init 2 */

/* USER CODE END RTC_Init 2 */

}

void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
{

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

/* USER CODE END RTC_MspInit 0 */

/** Initializes the peripherals clock
*/
    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC_WDG_BLEWKUP;
    PeriphClkInitStruct.RTCWDGBLEWKUPClockSelection = RCC_RTC_WDG_BLEWKUP_CLKSOURCE_LSE;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
    {
      Error_Handler();
    }

    /* RTC clock enable */
    __HAL_RCC_RTC_CLK_ENABLE();
/* USER CODE BEGIN RTC_MspInit 1 */

/* USER CODE END RTC_MspInit 1 */
}
}

void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle)
{

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

/* USER CODE END RTC_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_RTC_CLK_DISABLE();
/* USER CODE BEGIN RTC_MspDeInit 1 */

/* USER CODE END RTC_MspDeInit 1 */
}
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
再来添加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;
          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;
      
    if(buffUartRxIndex < sizeof(a_buffUartRx))
    {
      a_buffUartRx = byte_received;
    }
    else
    {
      buffUartRxIndex = 0;
    }
   
    if( (byte_received == '\n') || (buffUartRxIndex >= sizeof(a_buffUartRx)))
    {
      memcpy(&a_buffSpTx, &a_buffUartRx, 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;

            if (buffUartRxIndex < sizeof(a_buffUartRx))
            {
                a_buffUartRx = byte_received;
            }
            else
            {
                buffUartRxIndex = 0; // 缓冲区溢出,重置索引
            }

            if ((byte_received == '\n') || (buffUartRxIndex >= sizeof(a_buffUartRx)))
            {
                memcpy(&a_buffSpTx, &a_buffUartRx, buffUartRxIndex);
                buffSpTxLen = buffUartRxIndex;

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

                break; // 数据处理完成,退出循环
            }
      }
}
return;
}最后编译下载,打开手机连接开发板




这时就可以看到当前时间信息了。
蓝牙体验到此,喜欢的朋友不如自已动手实现一下,也可以把开发板上的其实数据广播出来,比如电压,温度等信息。

稳稳の幸福 发表于 2024-11-18 18:53

奈斯,看起来不错。广播的用法是不是不需要连接?

stb988 发表于 2024-11-18 19:05

蓝牙都是无线的
页: [1]
查看完整版本: 【STM32WB09KE测评】用蓝牙广播时间并在手机上显示当前时间