[APM32E0] APM32E030的定时器驱动(捕获)

[复制链接]
53|0
口天土立口 发表于 2025-10-16 09:05 | 显示全部楼层 |阅读模式
7074668f0442613df8.png

APM32E030的定时器有三类,不同定时器支持的功能互有差异,部分定时器支持输入捕获功能;

驱动代码如下:
  1. #define TIM_INS     (TMR3)

  2. /* 定时器捕获数据获取函数 */
  3. static tim_capture_data_func_f tim_capture_data_func_1;
  4. static tim_capture_data_func_f tim_capture_data_func_2;

  5. /* 捕获精度us */
  6. #define CAPTURE_PRECISION   (100)
  7. /* 最大周期 */
  8. static uint32_t period_max;
  1. /*
  2. * [url=/u/brief]@brief[/url]       捕获引脚初始化
  3. *
  4. * @param       None
  5. *
  6. * @retval      None
  7. *
  8. */
  9. static void bsp_tim_capture_gpio_init(void)
  10. {
  11.     GPIO_Config_T gpioConfig;
  12.    
  13.     /*  PC6 -> TMR3_CH1
  14.      *  PC8 -> TMR3_CH3
  15.      */
  16.     RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOC);
  17.     GPIO_ConfigStructInit(&gpioConfig);
  18.     gpioConfig.pin     = GPIO_PIN_6 | GPIO_PIN_8;
  19.     gpioConfig.mode    = GPIO_MODE_AF;
  20.     gpioConfig.outtype = GPIO_OUT_TYPE_PP;
  21.     gpioConfig.speed   = GPIO_SPEED_50MHz;
  22.     gpioConfig.pupd    = GPIO_PUPD_NO;
  23.     GPIO_Config(GPIOC, &gpioConfig);
  24.    
  25.     GPIO_ConfigPinAF(GPIOC, GPIO_PIN_SOURCE_6, GPIO_AF_PIN0);
  26.     GPIO_ConfigPinAF(GPIOC, GPIO_PIN_SOURCE_8, GPIO_AF_PIN0);
  27. }
  1. /*
  2. * @brief       定时器初始化
  3. *
  4. * @param       None
  5. *
  6. * @retval      None
  7. *
  8. */
  9. void bsp_tim_capture_init(void)
  10. {
  11.     TMR_TimeBase_T timeBaseConfig;
  12.     TMR_ICConfig_T ICconfigstruct;
  13.    
  14.     bsp_tim_capture_gpio_init();
  15.    
  16.     RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR3);
  17.     TMR_Reset(TIM_INS);
  18.     TMR_ConfigTimeBaseStruct(&timeBaseConfig);
  19.     /* CK_CNT=fCK_PSC/(PSC+1) */
  20.     /* 10KHz,捕获精度是100us */
  21.     timeBaseConfig.div = 7200 - 1;     
  22.     /* 最大捕获PWM周期为6s
  23.      * 因AUTORLD寄存器为16位,所以period最大为65535
  24.      * 同时AUTORLD寄存器限制,提高捕获精度,需同时缩短最大捕获PWM周期
  25.      */        
  26.     timeBaseConfig.period = 60000 - 1;         
  27.     timeBaseConfig.clockDivision = TMR_CKD_DIV1;
  28.     timeBaseConfig.counterMode = TMR_COUNTER_MODE_UP;
  29.     timeBaseConfig.repetitionCounter = 0;
  30.     TMR_ConfigTimeBase(TIM_INS, &timeBaseConfig);
  31.     period_max = timeBaseConfig.period + 1;
  32.     /* 捕获PWM1:
  33.      * 捕获通道1: 输入来源为TMR3_CH1
  34.      * 捕获通道2: 输入来源为TMR3_CH1
  35.      */
  36.     TMR_ICConfigStructInit(&ICconfigstruct);
  37.     ICconfigstruct.channel = TMR_CHANNEL_1;
  38.     /* 捕获上升沿 */
  39.     ICconfigstruct.ICpolarity = TMR_IC_POLARITY_RISING;
  40.     ICconfigstruct.ICselection = TMR_IC_SELECTION_DIRECT_TI;    /* TMR3_CH1 */
  41.     ICconfigstruct.ICprescaler = TMR_ICPSC_DIV1;
  42.     ICconfigstruct.ICfilter = 0x00;
  43.     TMR_ICConfig(TIM_INS, &ICconfigstruct);
  44.     ICconfigstruct.channel = TMR_CHANNEL_2;
  45.     /* 捕获下降沿 */
  46.     ICconfigstruct.ICpolarity = TMR_IC_POLARITY_FALLING;
  47.     ICconfigstruct.ICselection = TMR_IC_SELECTION_INDIRECT_TI;  /* TMR3_CH1 */
  48.     TMR_ICConfig(TIM_INS, &ICconfigstruct);
  49.     /* 捕获PWM2:
  50.      * 捕获通道3: 输入来源为TMR3_CH3
  51.      * 捕获通道4: 输入来源为TMR3_CH3
  52.      */
  53.     TMR_ICConfigStructInit(&ICconfigstruct);
  54.     ICconfigstruct.channel = TMR_CHANNEL_3;
  55.     /* 捕获上升沿 */
  56.     ICconfigstruct.ICpolarity = TMR_IC_POLARITY_RISING;
  57.     ICconfigstruct.ICselection = TMR_IC_SELECTION_DIRECT_TI;    /* TMR3_CH3 */
  58.     ICconfigstruct.ICprescaler = TMR_ICPSC_DIV1;
  59.     ICconfigstruct.ICfilter = 0x00;
  60.     TMR_ICConfig(TIM_INS, &ICconfigstruct);
  61.     ICconfigstruct.channel = TMR_CHANNEL_4;
  62.     /* 捕获下降沿 */
  63.     ICconfigstruct.ICpolarity = TMR_IC_POLARITY_FALLING;
  64.     ICconfigstruct.ICselection = TMR_IC_SELECTION_INDIRECT_TI;  /* TMR3_CH3 */
  65.     TMR_ICConfig(TIM_INS, &ICconfigstruct);
  66.    
  67.     /* 使能捕获中断 */
  68.     TMR_EnableInterrupt(TIM_INS, TMR_INT_CH1);
  69.     TMR_EnableInterrupt(TIM_INS, TMR_INT_CH2);
  70.     TMR_EnableInterrupt(TIM_INS, TMR_INT_CH3);
  71.     TMR_EnableInterrupt(TIM_INS, TMR_INT_CH4);
  72.     /* 使能中断 */
  73.     NVIC_SetPriority(TMR3_IRQn, 0);
  74.     NVIC_EnableIRQ(TMR3_IRQn);   
  75.     /* 使能定时器 */
  76.     TMR_Enable(TIM_INS);
  77. }
  1. /*
  2. * @brief       定时器捕获数据获取函数注册
  3. *
  4. * @param       func: 定时器捕获数据获取函数
  5. *
  6. * @retval      None
  7. *
  8. */
  9. void bsp_tim_capture_capture_data_func_register(uint8_t index, tim_capture_data_func_f func)
  10. {
  11.     if (index == 1) {
  12.         tim_capture_data_func_1 = func;
  13.     } if (index == 2) {
  14.         tim_capture_data_func_2 = func;
  15.     }
  16. }


测试代码如下:
  1. /* PWM输出 */
  2. uint8_t pwm1_output_duty = 20;
  3. uint8_t pwm2_output_duty = 35;

  4. /* PWM捕获 */
  5. uint16_t pwm1_capture_duty;
  6. uint32_t pwm1_capture_hz;
  7. uint16_t pwm2_capture_duty;
  8. uint32_t pwm2_capture_hz;
  1. void test_gpio_init(void)
  2. {
  3.     GPIO_Config_T gpioConfig;
  4.     RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOA);
  5.     GPIO_ConfigStructInit(&gpioConfig);
  6.     gpioConfig.pin     = GPIO_PIN_5;
  7.     gpioConfig.mode    = GPIO_MODE_OUT;
  8.     gpioConfig.outtype = GPIO_OUT_TYPE_PP;
  9.     gpioConfig.speed   = GPIO_SPEED_50MHz;
  10.     gpioConfig.pupd    = GPIO_PUPD_NO;
  11.     GPIO_Config(GPIOA, &gpioConfig);
  12. }
  1. void test_gpio_toggle(void)
  2. {
  3.     (GPIO_ReadOutputBit(GPIOA, GPIO_PIN_5) == BIT_SET) ? \
  4.         GPIO_ClearBit(GPIOA, GPIO_PIN_5) : \
  5.         GPIO_SetBit(GPIOA, GPIO_PIN_5);
  6. }

  7. void pwm1_capture_data(uint16_t duty, uint32_t pwm_hz)
  8. {
  9.     pwm1_capture_duty = duty;
  10.     pwm1_capture_hz = pwm_hz;
  11. }

  12. void pwm2_capture_data(uint16_t duty, uint32_t pwm_hz)
  13. {
  14.     pwm2_capture_duty = duty;
  15.     pwm2_capture_hz = pwm_hz;
  16. }
  1. // 应用初始化
  2. void app_init(void)
  3. {   
  4.     test_gpio_init();
  5.     /* PWM输出 */
  6.     bsp_tim_pwm_update_deal_func_register(test_gpio_toggle);
  7.     bsp_tim_pwm_init(1);
  8.     /* PWM捕获 */
  9.     bsp_tim_capture_capture_data_func_register(1, pwm1_capture_data);
  10.     bsp_tim_capture_capture_data_func_register(2, pwm2_capture_data);
  11.     bsp_tim_capture_init();
  12. }

  13. // 应用任务
  14. void app_task(void)
  15. {
  16.     /* PWM输出 */
  17.     bsp_tim_pwm_duty_set(pwm1_output_duty, pwm2_output_duty);
  18. }


详细代码,请查看附件!
Capture.zip (2.28 MB, 下载次数: 0)




您需要登录后才可以回帖 登录 | 注册

本版积分规则

26

主题

54

帖子

0

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