[技术问答] NUC029定时器问题

[复制链接]
218|5
 楼主 | 2019-10-4 16:33 | 显示全部楼层 |阅读模式
这个代码,我调用过来,发现定时器0可以进入中断,但是定时器1却不能进入中断。使用的是内部时钟,外部没有加晶振。
  1. void TimerInit(void)
  2. {
  3.           // Timer0选择HXT做时钟源,并且不除频 /
  4.    // CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0_S_HXT, CLK_TMR0_CLK_DIVIDER(1));
  5.    
  6.           //使能Timer0的时钟 //
  7.     CLK_EnableModuleClock(TMR0_MODULE);
  8.        
  9.     //初始化Timer0,周期模式,每秒发生一次中断  
  10.     TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1);
  11.        
  12.     //如果要修改预分频和比较寄存器的值可以用下面两个宏  
  13.     TIMER_SET_CMP_VALUE(TIMER0, 23789);//修改比较寄存器的值
  14.     TIMER_SET_PRESCALE_VALUE(TIMER0, TIMER_COUNTER_FALLING_EDGE);//修改预分频的值

  15.     //使能Timer0中断
  16.     TIMER_EnableInt(TIMER0);
  17.     NVIC_EnableIRQ(TMR0_IRQn);

  18.     // 启动 Timer 0
  19.     TIMER_Start(TIMER0);
  20.        
  21.        
  22.           // 使能Timer1的时钟//
  23.     CLK_EnableModuleClock(TMR1_MODULE);
  24.                
  25.            //Initial Timer1 default setting //
  26.     TIMER_Open(TIMER1, TIMER_PERIODIC_MODE, 1);

  27.     // Configure Timer1 setting for external counter input function
  28.     TIMER_SET_CMP_VALUE(TIMER1, 239);
  29.     TIMER_EnableEventCounter(TIMER1, TIMER_COUNTER_FALLING_EDGE);
  30.                
  31.     TIMER_EnableInt(TIMER1);
  32.     NVIC_EnableIRQ(TMR1_IRQn);
  33.                
  34.                 // 启动 Timer 1
  35.     TIMER_Start(TIMER1);
  36. }
复制代码

使用特权

评论回复
| 2019-10-6 08:25 | 显示全部楼层
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 4 $
  5. * $Date: 14/01/28 11:45a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    NUC029 Series Timer Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "NUC029xAN.h"

  13. #define PLLCON_SETTING      CLK_PLLCON_50MHz_HXT
  14. #define PLL_CLOCK           50000000


  15. /*---------------------------------------------------------------------------------------------------------*/
  16. /* Global Interface Variables Declarations                                                                 */
  17. /*---------------------------------------------------------------------------------------------------------*/
  18. volatile uint32_t g_au32TMRINTCount[4] = {0};


  19. /**
  20. * @brief       Timer0 IRQ
  21. *
  22. * @param       None
  23. *
  24. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  25. *
  26. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The Timer0 default IRQ, declared in startup_NUC029xAN.s.
  27. */
  28. void TMR0_IRQHandler(void)
  29. {
  30.     if(TIMER_GetIntFlag(TIMER0) == 1)
  31.     {
  32.         /* Clear Timer0 time-out interrupt flag */
  33.         TIMER_ClearIntFlag(TIMER0);

  34.         g_au32TMRINTCount[0]++;
  35.     }
  36. }

  37. /**
  38. * @brief       Timer1 IRQ
  39. *
  40. * @param       None
  41. *
  42. * @return      None
  43. *
  44. * @details     The Timer1 default IRQ, declared in startup_NUC029xAN.s.
  45. */
  46. void TMR1_IRQHandler(void)
  47. {
  48.     if(TIMER_GetIntFlag(TIMER1) == 1)
  49.     {
  50.         /* Clear Timer1 time-out interrupt flag */
  51.         TIMER_ClearIntFlag(TIMER1);

  52.         g_au32TMRINTCount[1]++;
  53.     }
  54. }

  55. /**
  56. * @brief       Timer2 IRQ
  57. *
  58. * @param       None
  59. *
  60. * @return      None
  61. *
  62. * @details     The Timer2 default IRQ, declared in startup_NUC029xAN.s.
  63. */
  64. void TMR2_IRQHandler(void)
  65. {
  66.     if(TIMER_GetIntFlag(TIMER2) == 1)
  67.     {
  68.         /* Clear Timer2 time-out interrupt flag */
  69.         TIMER_ClearIntFlag(TIMER2);

  70.         g_au32TMRINTCount[2]++;
  71.     }
  72. }

  73. /**
  74. * @brief       Timer3 IRQ
  75. *
  76. * @param       None
  77. *
  78. * @return      None
  79. *
  80. * @details     The Timer3 default IRQ, declared in startup_NUC029xAN.s.
  81. */
  82. void TMR3_IRQHandler(void)
  83. {
  84.     if(TIMER_GetIntFlag(TIMER3) == 1)
  85.     {
  86.         /* Clear Timer3 time-out interrupt flag */
  87.         TIMER_ClearIntFlag(TIMER3);

  88.         g_au32TMRINTCount[3]++;
  89.     }
  90. }

  91. void SYS_Init(void)
  92. {
  93.     /*---------------------------------------------------------------------------------------------------------*/
  94.     /* Init System Clock                                                                                       */
  95.     /*---------------------------------------------------------------------------------------------------------*/
  96.     /* Enable IRC22M clock */
  97.     CLK->PWRCON |= CLK_PWRCON_IRC22M_EN_Msk;

  98.     /* Waiting for IRC22M clock ready */
  99.     CLK_WaitClockReady(CLK_CLKSTATUS_IRC22M_STB_Msk);

  100.     /* Switch HCLK clock source to HIRC */
  101.     CLK->CLKSEL0 = CLK_CLKSEL0_HCLK_S_HIRC;

  102.     /* Set PLL to Power-down mode and PLL_STB bit in CLKSTATUS register will be cleared by hardware.*/
  103.     CLK->PLLCON |= CLK_PLLCON_PD_Msk;

  104.     /* Enable external 12 MHz XTAL, IRC10K */
  105.     CLK->PWRCON |= CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_OSC10K_EN_Msk;

  106.     /* Enable PLL and Set PLL frequency */
  107.     CLK->PLLCON = PLLCON_SETTING;

  108.     /* Waiting for clock ready */
  109.     CLK_WaitClockReady(CLK_CLKSTATUS_PLL_STB_Msk | CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_IRC10K_STB_Msk);

  110.     /* Switch HCLK clock source to PLL, STCLK to HCLK/2 */
  111.     CLK->CLKSEL0 = CLK_CLKSEL0_STCLK_S_HCLK_DIV2 | CLK_CLKSEL0_HCLK_S_PLL;

  112.     /* Enable peripheral clock */
  113.     CLK->APBCLK = CLK_APBCLK_UART0_EN_Msk |
  114.                   CLK_APBCLK_TMR0_EN_Msk | CLK_APBCLK_TMR1_EN_Msk | CLK_APBCLK_TMR2_EN_Msk | CLK_APBCLK_TMR3_EN_Msk;

  115.     /* Peripheral clock source */
  116.     CLK->CLKSEL1 = CLK_CLKSEL1_UART_S_PLL |
  117.                    CLK_CLKSEL1_TMR0_S_HXT | CLK_CLKSEL1_TMR1_S_HCLK | CLK_CLKSEL1_TMR2_S_HIRC | CLK_CLKSEL1_TMR3_S_HXT;

  118.     /* Update System Core Clock */
  119.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  120.     SystemCoreClockUpdate();

  121.     /*---------------------------------------------------------------------------------------------------------*/
  122.     /* Init I/O Multi-function                                                                                 */
  123.     /*---------------------------------------------------------------------------------------------------------*/
  124.     /* Set P3 multi-function pins for UART0 RXD, TXD */
  125.     SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;
  126. }

  127. void UART0_Init(void)
  128. {
  129.     /*---------------------------------------------------------------------------------------------------------*/
  130.     /* Init UART                                                                                               */
  131.     /*---------------------------------------------------------------------------------------------------------*/
  132.     /* Reset IP */
  133.     SYS_ResetModule(UART0_RST);

  134.     /* Configure UART0 and set UART0 Baudrate */
  135.     UART_Open(UART0, 115200);
  136. }

  137. /*---------------------------------------------------------------------------------------------------------*/
  138. /*  MAIN function                                                                                          */
  139. /*---------------------------------------------------------------------------------------------------------*/
  140. int main(void)
  141. {
  142.     volatile uint32_t u32InitCount;

  143.     /* Unlock protected registers */
  144.     SYS_UnlockReg();

  145.     /* Init System, peripheral clock and multi-function I/O */
  146.     SYS_Init();

  147.     /* Lock protected registers */
  148.     SYS_LockReg();

  149.     /* Init UART0 for printf */
  150.     UART0_Init();

  151.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  152.     printf("+------------------------------------------------+\n");
  153.     printf("|    Timer Periodic and Interrupt Sample Code    |\n");
  154.     printf("+------------------------------------------------+\n\n");

  155.     printf("# Timer Settings:\n");
  156.     printf("  Timer0: Clock source 12 MHz;       Periodic mode; Enable interrupt; 1 interrupt tick/sec.\n");
  157.     printf("  Timer1: Clock source HCLK(50 MHz); Periodic mode; Enable interrupt; 2 interrupt ticks/sec.\n");
  158.     printf("  Timer2: Clock source HIRC(22 MHz); Periodic mode; Enable interrupt; 4 interrupt ticks/sec.\n");
  159.     printf("  Timer3: Clock source 12 MHz;       Periodic mode; Enable interrupt; 8 interrupt ticks/sec.\n");
  160.     printf("# Check Timer0 ~ Timer3 interrupt counts are reasonable or not.\n\n");

  161.     /* Open Timer0 frequency to 0.5 Hz in periodic mode, and enable interrupt */
  162.     TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1);
  163.     TIMER_EnableInt(TIMER0);

  164.     /* Open Timer1 frequency to 1 Hz in periodic mode, and enable interrupt */
  165.     TIMER_Open(TIMER1, TIMER_PERIODIC_MODE, 2);
  166.     TIMER_EnableInt(TIMER1);

  167.     /* Open Timer2 frequency to 2 Hz in periodic mode, and enable interrupt */
  168.     TIMER_Open(TIMER2, TIMER_PERIODIC_MODE, 4);
  169.     TIMER_EnableInt(TIMER2);

  170.     /* Open Timer3 frequency to 4 Hz in periodic mode, and enable interrupt */
  171.     TIMER_Open(TIMER3, TIMER_PERIODIC_MODE, 8);
  172.     TIMER_EnableInt(TIMER3);

  173.     /* Enable Timer0 ~ Timer3 NVIC */
  174.     NVIC_EnableIRQ(TMR0_IRQn);
  175.     NVIC_EnableIRQ(TMR1_IRQn);
  176.     NVIC_EnableIRQ(TMR2_IRQn);
  177.     NVIC_EnableIRQ(TMR3_IRQn);

  178.     /* Clear Timer0 ~ Timer3 interrupt counts to 0 */
  179.     g_au32TMRINTCount[0] = g_au32TMRINTCount[1] = g_au32TMRINTCount[2] = g_au32TMRINTCount[3] = 0;
  180.     u32InitCount = g_au32TMRINTCount[0];

  181.     /* Start Timer0 ~ Timer3 counting */
  182.     TIMER_Start(TIMER0);
  183.     TIMER_Start(TIMER1);
  184.     TIMER_Start(TIMER2);
  185.     TIMER_Start(TIMER3);

  186.     /* Check Timer0 ~ Timer3 interrupt counts */
  187.     printf("# Timer interrupt counts :\n");
  188.     while(u32InitCount < 20)
  189.     {
  190.         if(g_au32TMRINTCount[0] != u32InitCount)
  191.         {
  192.             printf("TMR0:%3d    TMR1:%3d    TMR2:%3d    TMR3:%3d\n",
  193.                    g_au32TMRINTCount[0], g_au32TMRINTCount[1], g_au32TMRINTCount[2], g_au32TMRINTCount[3]);
  194.             u32InitCount = g_au32TMRINTCount[0];
  195.         }
  196.     }

  197.     printf("*** PASS ***\n");

  198.     while(1);
  199. }

  200. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
复制代码

使用特权

评论回复
| 2019-10-6 08:26 | 显示全部楼层
官方提供了各个定时器的中断实例

使用特权

评论回复
| 2019-10-6 08:26 | 显示全部楼层
楼主对比一下,或者下载最新的BSP看看。

使用特权

评论回复
| 2019-10-7 14:47 | 显示全部楼层
把TIMER 1的CLOCK SOURCE也改成HXT, 試試
CLK->CLKSEL1 = CLK_CLKSEL1_UART_S_PLL | CLK_CLKSEL1_TMR0_S_HXT | CLK_CLKSEL1_TMR1_S_HXT | CLK_CLKSEL1_TMR2_S_HIRC | CLK_CLKSEL1_TMR3_S_HXT;

使用特权

评论回复
| 2019-10-8 21:06 | 显示全部楼层
使用例子里的配置,好像也是用的内部的时钟吧

使用特权

评论回复
扫描二维码,随时随地手机跟帖
*滑动验证:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

我要发帖 投诉建议 创建版块 申请版主

快速回复

您需要登录后才可以回帖
登录 | 注册
高级模式

论坛热帖

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