STM32F030R8Tx HAL庫實現SYSTICK

  • 準備工程,在此鏈接的例程上增加SYSTICK功能

https://blog.csdn.net/mygod2008ok/article/details/106748615

  • 配置系統時鐘



/* Private function prototypes -----------------------------------------------*/
/**
  * @brief  System Clock Configuration
  *         The system Clock is configured as follow : 
  *            System Clock source            = PLL (HSI/2)
  *            SYSCLK(Hz)                     = 48000000
  *            HCLK(Hz)                       = 48000000
  *            AHB Prescaler                  = 1
  *            APB1 Prescaler                 = 1
  *            HSI Frequency(Hz)              = 8000000
  *            PREDIV                         = 1
  *            PLLMUL                         = 12
  *            Flash Latency(WS)              = 1
  * @param  None
  * @retval None
  */
static void SystemClock_Config(void)
{
  
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_OscInitTypeDef RCC_OscInitStruct;
  
  /* No HSE Oscillator on Nucleo, Activate PLL with HSI/2 as source */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
	RCC_OscInitStruct.LSIState = RCC_LSI_ON;
	RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
	HAL_StatusTypeDef err_code = HAL_RCC_OscConfig(&RCC_OscInitStruct);
	APP_ERROR_CHECK(err_code);
 
  /* Select PLL as system clock source and configure the HCLK, PCLK1 clocks dividers */
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  err_code = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
	APP_ERROR_CHECK(err_code);

  
}

  • 在sdk_config.h中加入SYS_TICK_COUNTER宏,這裏將SYSTICK配置成25Hz

#ifndef SDK_CONFIG_H
#define SDK_CONFIG_H

	
#define STM_MODULE_ENABLED(module) \
    ((defined(module ## _ENABLED) && (module ## _ENABLED)) ? 1 : 0)

// <<< Use Configuration Wizard in Context Menu >>>\n

//==================================================================================================

// <h> 系統嘀嗒配置
// <o> 系統嘀嗒時間
#ifndef SYS_TICK_COUNTER      
#define SYS_TICK_COUNTER 25
#endif
// </h>



//==========================================================


// <h> DEBUG配置
// <q> DEBUG使能
#ifndef DEBUG_ENABLED
#define DEBUG_ENABLED 1
#endif


// <q> ASSERT參數檢查使能
#ifndef IS_USE_FULL_ASSERT
#define IS_USE_FULL_ASSERT 	1
#endif
#if IS_USE_FULL_ASSERT
#define USE_FULL_ASSERT
#endif
// </h>

// <<< end of configuration section >>>
#endif //SDK_CONFIG_H

  • 重寫__weak弱編譯HAL_InitTick函數,這樣就將SYSTICK頻率配置成SYS_TICK_COUNTER,即25Hz,也就是每個tick時間爲40毫秒


/**
* @brief overload system tick
*
*/
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
  /*Configure the SysTick to have interrupt in 1ms time basis*/
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/SYS_TICK_COUNTER);

  /*Configure the SysTick IRQ priority */
  HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0U);

   /* Return function status */
  return HAL_OK;
}
  • 在main.C文件中定義變量

volatile uint16_t s_wakeup_flag;   // 喚醒事件標誌
  •  在main.h中聲明變量及定義事件標誌宏

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"

#define TICK_FOR_25HZ													0x02      //!< 40ms事件標誌


#define CLEAR_TICK_FOR_25HZ														(~TICK_FOR_25HZ)                 //!< 清除40ms事件標誌


extern volatile uint16_t s_wakeup_flag; 

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  • 在stm32f0xx_it.c中包含頭文件,並在SysTick_Handler中斷處理函數中加入以下代碼

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */

  s_wakeup_flag |= TICK_FOR_25HZ;
  /* USER CODE END SysTick_IRQn 1 */
}
  • 在main.c中實現25Hz處理函數

/**
* @brief 25Hz handler
*
*/
void tick_25hz_handler(void)
{
  if(s_wakeup_flag & TICK_FOR_25HZ)
  {
     s_wakeup_flag &= CLEAR_TICK_FOR_25HZ;
//####################################################################################
	//---------TODO this to add 25hz event handler-----------
     NRF_LOG_INFO("tick_25hz_and_1hz_handler");

  }
}
  • 在循環中調用tick_25hz_handler函數


/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
  RTT_INIT();
  HAL_Init();
  SystemClock_Config();
  NRF_LOG_INFO("program is reset!"); 
  BSP_wdt_init(5000);
   
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
     BSP_wdt_feed();
     tick_25hz_handler();
	
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
  • 運行結果如下:

例程下載鏈接:

https://download.csdn.net/download/mygod2008ok/12522570

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章