[技术问答] HC32F460 TimerA 定时PWM问题

[复制链接]
7412|8
 楼主| 茂xiang 发表于 2021-8-6 14:04 | 显示全部楼层 |阅读模式
主系统时钟HCLK 40M, PCLK0 40M,通过时钟输出测试okay;
但是PWM参考例程"timera_triangular_wave_compare_output"没有PWM输出,也没有进入TimA3定时中断,大家帮看下是什么问题。
  1. #include "hc32_ddl.h"
  2. #include "hal_CLK.h"
  3. #include "hc32f46x_timera.h"


  4. #define LED0_PORT                       (PortC)
  5. #define LED0_PIN                        (Pin09)
  6. #define LED0_ON()                       (PORT_SetBits(LED0_PORT, LED0_PIN))
  7. #define LED0_OFF()                      (PORT_ResetBits(LED0_PORT, LED0_PIN))
  8. #define LED0_TOGGLE()                   (PORT_Toggle(LED0_PORT, LED0_PIN))


  9. /* TIMERA unit and clock definition */
  10. #define TIMERA_UNIT3                    (M4_TMRA3)                                // TimerA1--TimerA6,硬件TMRA3
  11. #define TIMERA_UNIT3_CLOCK              (PWC_FCG2_PERIPH_TIMA3)        // TimerA3时钟
  12. #define TIMERA_UNIT3_OVERFLOW_INT       (INT_TMRA3_OVF)                        // 每次溢出产生中断

  13. /* TIMERA channel 7 Port/Pin definition */
  14. #define TIMERA_UNIT3_CH7                (TimeraCh7)
  15. #define TIMERA_UNIT3_CH7_PORT           (PortC)
  16. #define TIMERA_UNIT3_CH7_PIN            (Pin04)
  17. #define TIMERA_UNIT3_CH7_FUNC           (Func_Tima0)

  18. /* TIMERA channel 8 Port/Pin definition */
  19. #define TIMERA_UNIT3_CH8                (TimeraCh8)
  20. #define TIMERA_UNIT3_CH8_PORT           (PortC)
  21. #define TIMERA_UNIT3_CH8_PIN            (Pin05)
  22. #define TIMERA_UNIT3_CH8_FUNC           (Func_Tima0)

  23. static uint8_t u8ExIntFlag = 0u, u8TmraUnit1Cnt = 0u;

  24. /*******************************************************************************
  25. ** \brief Timera unit 3 callback function
  26. **
  27. ** \param [in]  None
  28. **
  29. ** \retval None
  30. **
  31. ******************************************************************************/
  32. static void TimeraUnit3_IrqCallback(void)
  33. {
  34.     u8TmraUnit1Cnt++;
  35.     if (u8TmraUnit1Cnt >= 100u)      //1s
  36.     {
  37.         u8TmraUnit1Cnt = 0u;
  38.         LED0_TOGGLE();
  39.     }
  40.     TIMERA_ClearFlag(TIMERA_UNIT3, TimeraFlagOverflow);
  41. }

  42. /*******************************************************************************
  43. ** \brief Configure Timera peripheral function
  44. **
  45. ** \param [in] None
  46. **
  47. ** \retval None
  48. **
  49. ******************************************************************************/
  50. static void Timera_Config(void){
  51.     stc_timera_base_init_t stcTimeraInit;
  52.     stc_timera_compare_init_t stcTimerCompareInit;
  53.     stc_irq_regi_conf_t stcIrqRegiConf;
  54.     stc_timera_hw_startup_config_t stcTimeraHwConfig;
  55.     stc_port_init_t stcPortInit;

  56.     /* configuration structure initialization */
  57.     MEM_ZERO_STRUCT(stcTimeraInit);
  58.     MEM_ZERO_STRUCT(stcIrqRegiConf);
  59.     MEM_ZERO_STRUCT(stcTimerCompareInit);
  60.     MEM_ZERO_STRUCT(stcTimeraHwConfig);
  61.     MEM_ZERO_STRUCT(stcPortInit);

  62.     /* Configuration peripheral clock */
  63.     PWC_Fcg2PeriphClockCmd(TIMERA_UNIT3_CLOCK, Enable);
  64.     PWC_Fcg0PeriphClockCmd(PWC_FCG0_PERIPH_AOS, Enable);

  65.     /* Configuration TIMERA compare pin */
  66.     PORT_SetFunc(TIMERA_UNIT3_CH7_PORT, TIMERA_UNIT3_CH7_PIN, TIMERA_UNIT3_CH7_FUNC, Disable);
  67.     PORT_SetFunc(TIMERA_UNIT3_CH8_PORT, TIMERA_UNIT3_CH8_PIN, TIMERA_UNIT3_CH8_FUNC, Disable);

  68.     /* Configuration timera unit 3 base structure */
  69.     stcTimeraInit.enClkDiv = TimeraPclkDiv128;        //PCLK0-PCLK4具体是哪个呢? PCLK1吧,20M/128=156.25Khz
  70.     stcTimeraInit.enCntMode = TimeraCountModeTriangularWave;
  71.     stcTimeraInit.enCntDir = TimeraCountDirUp;
  72.     stcTimeraInit.enSyncStartupEn = Disable;
  73.     stcTimeraInit.u16PeriodVal = 781;                //freq: 100Hz -> 20M/128/200Hz=781
  74.     TIMERA_BaseInit(TIMERA_UNIT3, &stcTimeraInit);

  75.     /* Configuration timera unit 3 compare structure */
  76.     stcTimerCompareInit.u16CompareVal = stcTimeraInit.u16PeriodVal * 4u / 5u;
  77.     stcTimerCompareInit.enStartCountOutput = TimeraCountStartOutputLow;
  78.     stcTimerCompareInit.enStopCountOutput = TimeraCountStopOutputLow;
  79.     stcTimerCompareInit.enCompareMatchOutput = TimeraCompareMatchOutputReverse;
  80.     stcTimerCompareInit.enPeriodMatchOutput = TimeraPeriodMatchOutputKeep;
  81.     stcTimerCompareInit.enSpecifyOutput = TimeraSpecifyOutputInvalid;
  82.     stcTimerCompareInit.enCacheEn = Enable;
  83.     stcTimerCompareInit.enTriangularTroughTransEn = Enable;
  84.     stcTimerCompareInit.enTriangularCrestTransEn = Disable;
  85.     stcTimerCompareInit.u16CompareCacheVal = stcTimerCompareInit.u16CompareVal;
  86.     /* Configure Channel 7 */
  87.     TIMERA_CompareInit(TIMERA_UNIT3, TIMERA_UNIT3_CH7, &stcTimerCompareInit);
  88.     TIMERA_CompareCmd(TIMERA_UNIT3, TIMERA_UNIT3_CH7, Enable);

  89.     /* Configure channel 8 */
  90.     stcTimerCompareInit.enStartCountOutput = TimeraCountStartOutputHigh;
  91.     stcTimerCompareInit.enStopCountOutput = TimeraCountStopOutputHigh;
  92.     TIMERA_CompareInit(TIMERA_UNIT3, TIMERA_UNIT3_CH8, &stcTimerCompareInit);
  93.     TIMERA_CompareCmd(TIMERA_UNIT3, TIMERA_UNIT3_CH8, Enable);

  94.     /* Enable period count interrupt */
  95.     TIMERA_IrqCmd(TIMERA_UNIT3, TimeraIrqOverflow, Enable);
  96.     /* Interrupt of timera unit 3 */
  97.     stcIrqRegiConf.enIntSrc = TIMERA_UNIT3_OVERFLOW_INT;
  98.     stcIrqRegiConf.enIRQn = Int006_IRQn;
  99.     stcIrqRegiConf.pfnCallback = &TimeraUnit3_IrqCallback;
  100.     enIrqRegistration(&stcIrqRegiConf);
  101.     NVIC_ClearPendingIRQ(stcIrqRegiConf.enIRQn);
  102.     NVIC_SetPriority(stcIrqRegiConf.enIRQn, DDL_IRQ_PRIORITY_15);
  103.     NVIC_EnableIRQ(stcIrqRegiConf.enIRQn);

  104.     /* Configure timera unit 3 startup */
  105.     stcTimeraHwConfig.enSpecifyEventStartupEn = Enable;
  106.     stcTimeraHwConfig.enTrigFallingStartupEn = Disable;
  107.     stcTimeraHwConfig.enTrigRisingStartupEn = Disable;
  108.     TIMERA_HwStartupConfig(TIMERA_UNIT3, &stcTimeraHwConfig);

  109. //    /* Set external Int Ch.4 trigger timera startup */
  110. //    stcPortInit.enExInt = Enable;
  111. //    PORT_Init(KEY1_PORT, KEY1_PIN, &stcPortInit);
  112. //    TIMERA_SetCountTriggerSrc(KEY1_TRIGGER_EVENT);
  113. }


  114. void LED_Init(){
  115.         stc_port_init_t stcPortInit;

  116.     MEM_ZERO_STRUCT(stcPortInit);                /* configure structure initialization */
  117.         LED0_OFF();
  118.         stcPortInit.enPinMode = Pin_Mode_Out;
  119.         PORT_Init(LED0_PORT, LED0_PIN, &stcPortInit);
  120. }



  121. void main(){
  122.         uint16_t u16TimerPeriod = 0u, u16DutyCycle = 0u;
  123.        
  124.     sysCLK_Init();        //HCLK 40M, PCLK0 40M
  125.         LED_Init();
  126.        
  127.         /* Configure Timera */
  128.     Timera_Config();
  129.     u16DutyCycle = TIMERA_GetCompareValue(TIMERA_UNIT3, TIMERA_UNIT3_CH7);
  130.     u16TimerPeriod = TIMERA_GetPeriodValue(TIMERA_UNIT3);
  131.        
  132.         while(1);
  133. }



martinhu 发表于 2021-8-6 15:24 | 显示全部楼层
你用的是硬件触发启动模式,需要这个KEY按下才会启动,
如果不需要的话删掉这些,之前调用库函数start TimerA就好。

28113610ce37de2c40.png
 楼主| 茂xiang 发表于 2021-8-6 20:19 | 显示全部楼层
感谢解答。
但是使能定时器也没有进入中断 stcTimeraInit.enSyncStartupEn = Enable;
另外下面的外部触发也全部屏蔽了
  1.     /* Configure timera unit 3 startup */
  2. //    stcTimeraHwConfig.enSpecifyEventStartupEn = Disable;
  3. //    stcTimeraHwConfig.enTrigFallingStartupEn = Disable;
  4. //    stcTimeraHwConfig.enTrigRisingStartupEn = Disable;
  5. //    TIMERA_HwStartupConfig(TIMERA_UNIT3, &stcTimeraHwConfig);

  6. //        TIMERA_Cmd(TIMERA_UNIT3, Enable);
  7. //    /*<div class="blockcode"><blockquote>#include "hc32_ddl.h"
  8. #include "hal_CLK.h"
  9. #include "hc32f46x_timera.h"


  10. #define LED0_PORT                       (PortC)
  11. #define LED0_PIN                        (Pin09)
  12. #define LED0_ON()                       (PORT_SetBits(LED0_PORT, LED0_PIN))
  13. #define LED0_OFF()                      (PORT_ResetBits(LED0_PORT, LED0_PIN))
  14. #define LED0_TOGGLE()                   (PORT_Toggle(LED0_PORT, LED0_PIN))


  15. /* TIMERA unit and clock definition */
  16. #define TIMERA_UNIT3                    (M4_TMRA3)                                // TimerA1--TimerA6,硬件TMRA3
  17. #define TIMERA_UNIT3_CLOCK              (PWC_FCG2_PERIPH_TIMA3)        // TimerA3时钟
  18. #define TIMERA_UNIT3_OVERFLOW_INT       (INT_TMRA3_OVF)                        // 每次溢出产生中断

  19. /* TIMERA channel 7 Port/Pin definition */
  20. #define TIMERA_UNIT3_CH7                (TimeraCh7)
  21. #define TIMERA_UNIT3_CH7_PORT           (PortC)
  22. #define TIMERA_UNIT3_CH7_PIN            (Pin04)
  23. #define TIMERA_UNIT3_CH7_FUNC           (Func_Tima0)

  24. /* TIMERA channel 8 Port/Pin definition */
  25. #define TIMERA_UNIT3_CH8                (TimeraCh8)
  26. #define TIMERA_UNIT3_CH8_PORT           (PortC)
  27. #define TIMERA_UNIT3_CH8_PIN            (Pin05)
  28. #define TIMERA_UNIT3_CH8_FUNC           (Func_Tima0)

  29. static uint8_t u8ExIntFlag = 0u, u8TmraUnit1Cnt = 0u;

  30. /*******************************************************************************
  31. ** \brief Timera unit 3 callback function
  32. **
  33. ** \param [in]  None
  34. **
  35. ** \retval None
  36. **
  37. ******************************************************************************/
  38. static void TimeraUnit3_IrqCallback(void)
  39. {
  40.     u8TmraUnit1Cnt++;
  41.     if (u8TmraUnit1Cnt >= 100u)      //1s
  42.     {
  43.         u8TmraUnit1Cnt = 0u;
  44.         LED0_TOGGLE();
  45.     }
  46.     TIMERA_ClearFlag(TIMERA_UNIT3, TimeraFlagOverflow);
  47. }

  48. /*******************************************************************************
  49. ** \brief Configure Timera peripheral function
  50. **
  51. ** \param [in] None
  52. **
  53. ** \retval None
  54. **
  55. ******************************************************************************/
  56. static void Timera_Config(void){
  57.     stc_timera_base_init_t stcTimeraInit;
  58.     stc_timera_compare_init_t stcTimerCompareInit;
  59.     stc_irq_regi_conf_t stcIrqRegiConf;
  60.     stc_timera_hw_startup_config_t stcTimeraHwConfig;
  61.     stc_port_init_t stcPortInit;

  62.     /* configuration structure initialization */
  63.     MEM_ZERO_STRUCT(stcTimeraInit);
  64.     MEM_ZERO_STRUCT(stcIrqRegiConf);
  65.     MEM_ZERO_STRUCT(stcTimerCompareInit);
  66.     MEM_ZERO_STRUCT(stcTimeraHwConfig);
  67.     MEM_ZERO_STRUCT(stcPortInit);

  68.     /* Configuration peripheral clock */
  69.     PWC_Fcg2PeriphClockCmd(TIMERA_UNIT3_CLOCK, Enable);
  70.     PWC_Fcg0PeriphClockCmd(PWC_FCG0_PERIPH_AOS, Enable);

  71.     /* Configuration TIMERA compare pin */
  72.     PORT_SetFunc(TIMERA_UNIT3_CH7_PORT, TIMERA_UNIT3_CH7_PIN, TIMERA_UNIT3_CH7_FUNC, Disable);
  73.     PORT_SetFunc(TIMERA_UNIT3_CH8_PORT, TIMERA_UNIT3_CH8_PIN, TIMERA_UNIT3_CH8_FUNC, Disable);

  74.     /* Configuration timera unit 3 base structure */
  75.     stcTimeraInit.enClkDiv = TimeraPclkDiv128;        //PCLK0-PCLK4具体是哪个呢? PCLK1吧,20M/128=156.25Khz
  76.     stcTimeraInit.enCntMode = TimeraCountModeTriangularWave;
  77.     stcTimeraInit.enCntDir = TimeraCountDirUp;
  78.     stcTimeraInit.enSyncStartupEn = Enable;                //启动定时器
  79.     stcTimeraInit.u16PeriodVal = 781;                //freq: 100Hz -> 20M/128/200Hz=781
  80.     TIMERA_BaseInit(TIMERA_UNIT3, &stcTimeraInit);

  81.     /* Configuration timera unit 3 compare structure */
  82.     stcTimerCompareInit.u16CompareVal = stcTimeraInit.u16PeriodVal * 4u / 5u;
  83.     stcTimerCompareInit.enStartCountOutput = TimeraCountStartOutputLow;
  84.     stcTimerCompareInit.enStopCountOutput = TimeraCountStopOutputLow;
  85.     stcTimerCompareInit.enCompareMatchOutput = TimeraCompareMatchOutputReverse;
  86.     stcTimerCompareInit.enPeriodMatchOutput = TimeraPeriodMatchOutputKeep;
  87.     stcTimerCompareInit.enSpecifyOutput = TimeraSpecifyOutputInvalid;
  88.     stcTimerCompareInit.enCacheEn = Enable;
  89.     stcTimerCompareInit.enTriangularTroughTransEn = Enable;
  90.     stcTimerCompareInit.enTriangularCrestTransEn = Disable;
  91.     stcTimerCompareInit.u16CompareCacheVal = stcTimerCompareInit.u16CompareVal;
  92.     /* Configure Channel 7 */
  93.     TIMERA_CompareInit(TIMERA_UNIT3, TIMERA_UNIT3_CH7, &stcTimerCompareInit);
  94.     TIMERA_CompareCmd(TIMERA_UNIT3, TIMERA_UNIT3_CH7, Enable);

  95.     /* Configure channel 8 */
  96.     stcTimerCompareInit.enStartCountOutput = TimeraCountStartOutputHigh;
  97.     stcTimerCompareInit.enStopCountOutput = TimeraCountStopOutputHigh;
  98.     TIMERA_CompareInit(TIMERA_UNIT3, TIMERA_UNIT3_CH8, &stcTimerCompareInit);
  99.     TIMERA_CompareCmd(TIMERA_UNIT3, TIMERA_UNIT3_CH8, Enable);

  100.     /* Enable period count interrupt */
  101.     TIMERA_IrqCmd(TIMERA_UNIT3, TimeraIrqOverflow, Enable);
  102.     /* Interrupt of timera unit 3 */
  103.     stcIrqRegiConf.enIntSrc = TIMERA_UNIT3_OVERFLOW_INT;
  104.     stcIrqRegiConf.enIRQn = Int006_IRQn;
  105.     stcIrqRegiConf.pfnCallback = &TimeraUnit3_IrqCallback;
  106.     enIrqRegistration(&stcIrqRegiConf);
  107.     NVIC_ClearPendingIRQ(stcIrqRegiConf.enIRQn);
  108.     NVIC_SetPriority(stcIrqRegiConf.enIRQn, DDL_IRQ_PRIORITY_15);
  109.     NVIC_EnableIRQ(stcIrqRegiConf.enIRQn);
  110. }


  111. void LED_Init(){
  112.         stc_port_init_t stcPortInit;

  113.     MEM_ZERO_STRUCT(stcPortInit);                /* configure structure initialization */
  114.         LED0_OFF();
  115.         stcPortInit.enPinMode = Pin_Mode_Out;
  116.         PORT_Init(LED0_PORT, LED0_PIN, &stcPortInit);
  117. }



  118. void main(){
  119.         uint16_t u16TimerPeriod = 0u, u16DutyCycle = 0u;
  120.        
  121.     sysCLK_Init();        //HCLK 40M, PCLK0 40M
  122.         LED_Init();
  123.        
  124.         /* Configure Timera */
  125.     Timera_Config();
  126.     u16DutyCycle = TIMERA_GetCompareValue(TIMERA_UNIT3, TIMERA_UNIT3_CH7);
  127.     u16TimerPeriod = TIMERA_GetPeriodValue(TIMERA_UNIT3);
  128.        
  129.         while(1);
  130. }


trigger timera startup */
//    stcPortInit.enExInt = Enable;
//    PORT_Init(KEY1_PORT, KEY1_PIN, &stcPortInit);
//    TIMERA_SetCountTriggerSrc(KEY1_TRIGGER_EVENT);
现在的代码,在定时器中断中设置断点,一直没有进入
 楼主| 茂xiang 发表于 2021-8-7 11:30 | 显示全部楼层
TIMERA_Cmd(TIMERA_UNIT3, Enable);
这个调用了也是无效的,没有进入定时器中断、没有PWM输出
我复制粘贴贼快 发表于 2022-1-10 18:36 | 显示全部楼层
茂xiang 发表于 2021-8-7 11:30
TIMERA_Cmd(TIMERA_UNIT3, Enable);
这个调用了也是无效的,没有进入定时器中断、没有PWM输出 ...

我这是用的这个例程  没有PWM输出
我复制粘贴贼快 发表于 2022-1-10 18:37 | 显示全部楼层
茂xiang 发表于 2021-8-7 11:30
TIMERA_Cmd(TIMERA_UNIT3, Enable);
这个调用了也是无效的,没有进入定时器中断、没有PWM输出 ...

楼主解决了吗
单片小菜 发表于 2022-1-11 11:21 | 显示全部楼层
楼主现在解决了吗?
foxsbig 发表于 2022-2-9 13:06 | 显示全部楼层
哪个例程,跑跑看
一刀一级 发表于 2022-2-11 10:19 来自手机 | 显示全部楼层
问题就这样无疾而终了吗
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

13

帖子

1

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