本帖最后由 一路向北lm 于 2025-3-13 14:52 编辑
添加bsp外设进行管理,新建bsp_timer.c 和.h文件,具体内容如下:bsp_timer.c
- #include "bsp_timer.h"
- #include "bsp_led.h"
- #include "bsp_uart.h"
- TIM_HandleTypeDef htim2;
- void bsp_timer_init(void)
- {
- TIM_ClockConfigTypeDef sClockSourceConfig = {0};
- TIM_MasterConfigTypeDef sMasterConfig = {0};
-
- htim2.Instance = TIM2;
- htim2.Init.Prescaler = PRESCALER_VALUE;
- htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim2.Init.Period = PERIOD_VALUE;
- htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
- HAL_TIM_Base_Init(&htim2);
- sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig);
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
- HAL_TIM_Base_Start_IT(&htim2);
- }
- void TIM2_IRQHandler(void)
- {
- /* USER CODE BEGIN TIM2_IRQn 0 */
- /* USER CODE END TIM2_IRQn 0 */
- HAL_TIM_IRQHandler(&htim2);
- /* USER CODE BEGIN TIM2_IRQn 1 */
- /* USER CODE END TIM2_IRQn 1 */
- }
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- LED1_TOGGLE();
- LED2_TOGGLE();
- printf("led running ...\n\r");
- //BSP_LED_Toggle(LED1);
- }
bsp_timer.h
- #ifndef __BSP_TIMER_H__
- #define __BSP_TIMER_H__
- #include "stm32n6xx_hal.h"
- /* Compute the prescaler value to have TIMx counter clock equal to 10000 Hz */
- #define PRESCALER_VALUE (uint32_t)(((400000000) / (10000)) - 1)
- /* Initialize TIMx peripheral as follows:
- + Period = 10000 - 1
- + Prescaler = (400000000/10000) - 1
- + ClockDivision = 0
- + Counter direction = Up
- */
- #define PERIOD_VALUE (10000 - 1);
- void bsp_timer_init(void);
- #endif
在conf中打开TIMER模块 添加timer的hal库文件 增加msp初始化代码中的timer部分 测试运行后,定时器中断回调函数执行,led闪烁,串口打印信息
|