[STM32N6] 5.运行STM32N6 的TIMER并测试

[复制链接]
677|1
 楼主| 一路向北lm 发表于 2025-3-13 14:50 | 显示全部楼层 |阅读模式
本帖最后由 一路向北lm 于 2025-3-13 14:52 编辑

添加bsp外设进行管理,新建bsp_timer.c 和.h文件,具体内容如下:bsp_timer.c
  1. #include "bsp_timer.h"

  2. #include "bsp_led.h"
  3. #include "bsp_uart.h"

  4. TIM_HandleTypeDef htim2;

  5. void bsp_timer_init(void)
  6. {
  7.   TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  8.   TIM_MasterConfigTypeDef sMasterConfig = {0};


  9.   htim2.Instance = TIM2;
  10.   htim2.Init.Prescaler = PRESCALER_VALUE;
  11.   htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  12.   htim2.Init.Period = PERIOD_VALUE;
  13.   htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  14.   htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  15.   HAL_TIM_Base_Init(&htim2);
  16.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  17.   HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig);
  18.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  19.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  20.   HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
  21.         HAL_TIM_Base_Start_IT(&htim2);
  22. }
  23. void TIM2_IRQHandler(void)
  24. {
  25.   /* USER CODE BEGIN TIM2_IRQn 0 */

  26.   /* USER CODE END TIM2_IRQn 0 */
  27.   HAL_TIM_IRQHandler(&htim2);
  28.   /* USER CODE BEGIN TIM2_IRQn 1 */

  29.   /* USER CODE END TIM2_IRQn 1 */
  30. }

  31. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  32. {
  33.            LED1_TOGGLE();
  34.                  LED2_TOGGLE();
  35.                  printf("led running ...\n\r");
  36.     //BSP_LED_Toggle(LED1);
  37. }
bsp_timer.h

  1. #ifndef __BSP_TIMER_H__
  2. #define __BSP_TIMER_H__

  3. #include "stm32n6xx_hal.h"


  4. /* Compute the prescaler value to have TIMx counter clock equal to 10000 Hz */
  5. #define PRESCALER_VALUE (uint32_t)(((400000000) / (10000)) - 1)

  6.   /* Initialize TIMx peripheral as follows:
  7.        + Period = 10000 - 1
  8.        + Prescaler = (400000000/10000) - 1
  9.        + ClockDivision = 0
  10.        + Counter direction = Up
  11.   */

  12. #define PERIOD_VALUE (10000 - 1);


  13. void bsp_timer_init(void);



  14. #endif

conf中打开TIMER模块
6325767d28081a830c.png
添加timerhal库文件
2437267d28092a018f.png
增加msp初始化代码中的timer部分
2171567d280a877c23.png
测试运行后,定时器中断回调函数执行,led闪烁,串口打印信息
3756367d280b96210a.png

yangjiaxu 发表于 2025-4-9 14:53 | 显示全部楼层
好强好强,这种芯片也真的很强,不过不知道N6一般可以应用什么场合啊?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

293

主题

3837

帖子

81

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