[DemoCode下载] M058S的定时器周期中断

[复制链接]
777|9
 楼主| zhuomuniao110 发表于 2019-12-22 23:36 | 显示全部楼层 |阅读模式
  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: 3 $
  5. * $Date: 15/02/06 10:22a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Implement timer counting in periodic mode.
  7. * @note
  8. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "M058S.h"

  12. #define PLL_CLOCK           50000000


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


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

  32.         g_au32TMRINTCount[0]++;
  33.     }
  34. }

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

  50.         g_au32TMRINTCount[1]++;
  51.     }
  52. }

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

  68.         g_au32TMRINTCount[2]++;
  69.     }
  70. }

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

  86.         g_au32TMRINTCount[3]++;
  87.     }
  88. }

  89. void SYS_Init(void)
  90. {
  91.     /*---------------------------------------------------------------------------------------------------------*/
  92.     /* Init System Clock                                                                                       */
  93.     /*---------------------------------------------------------------------------------------------------------*/
  94.     /* Enable HIRC 22.1184MHz clock */
  95.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  96.     /* Waiting for HIRC clock ready */
  97.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  98.     /* Switch HCLK clock source to HIRC and HCLK source divide 1 */
  99.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  100.     /* Enable external XTAL 12MHz clock */
  101.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  102.     /* Waiting for external XTAL clock ready */
  103.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  104.     /* Set core clock as PLL_CLOCK from PLL */
  105.     CLK_SetCoreClock(PLL_CLOCK);

  106.     /* Enable peripheral clock */
  107.     CLK_EnableModuleClock(UART0_MODULE);
  108.     CLK_EnableModuleClock(TMR0_MODULE);
  109.     CLK_EnableModuleClock(TMR1_MODULE);
  110.     CLK_EnableModuleClock(TMR2_MODULE);
  111.     CLK_EnableModuleClock(TMR3_MODULE);

  112.     /* Peripheral clock source */
  113.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));
  114.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0_S_HXT, 0);
  115.     CLK_SetModuleClock(TMR1_MODULE, CLK_CLKSEL1_TMR1_S_HCLK, 0);
  116.     CLK_SetModuleClock(TMR2_MODULE, CLK_CLKSEL1_TMR2_S_HIRC, 0);
  117.     CLK_SetModuleClock(TMR3_MODULE, CLK_CLKSEL1_TMR3_S_HXT, 0);
  118.     /*---------------------------------------------------------------------------------------------------------*/
  119.     /* Init I/O Multi-function                                                                                 */
  120.     /*---------------------------------------------------------------------------------------------------------*/
  121.     /* Set P3 multi-function pins for UART0 RXD, TXD */
  122.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  123.     SYS->P3_MFP |= (SYS_MFP_P30_RXD | SYS_MFP_P31_TXD);
  124. }

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

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

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

  141.     /* Unlock protected registers */
  142.     SYS_UnlockReg();

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

  145.     /* Init UART0 for printf */
  146.     UART0_Init();

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

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

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

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

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

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

  169.     /* Enable Timer0 ~ Timer3 NVIC */
  170.     NVIC_EnableIRQ(TMR0_IRQn);
  171.     NVIC_EnableIRQ(TMR1_IRQn);
  172.     NVIC_EnableIRQ(TMR2_IRQn);
  173.     NVIC_EnableIRQ(TMR3_IRQn);

  174.     /* Clear Timer0 ~ Timer3 interrupt counts to 0 */
  175.     g_au32TMRINTCount[0] = g_au32TMRINTCount[1] = g_au32TMRINTCount[2] = g_au32TMRINTCount[3] = 0;
  176.     u32InitCount = g_au32TMRINTCount[0];

  177.     /* Start Timer0 ~ Timer3 counting */
  178.     TIMER_Start(TIMER0);
  179.     TIMER_Start(TIMER1);
  180.     TIMER_Start(TIMER2);
  181.     TIMER_Start(TIMER3);

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

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

  194.     while(1);
  195. }

  196. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/


 楼主| zhuomuniao110 发表于 2019-12-22 23:37 | 显示全部楼层
4个定时器的中断都有了l
 楼主| zhuomuniao110 发表于 2019-12-22 23:42 | 显示全部楼层
这个例子非常不错。只有带MSK的一般才可以通过逻辑操作,一次赋值。
 楼主| zhuomuniao110 发表于 2019-12-22 23:43 | 显示全部楼层
我们也可以通过这个例子看到,定时器可以使用不同的时钟源。
 楼主| zhuomuniao110 发表于 2019-12-22 23:43 | 显示全部楼层
也可以使用相同的时钟源。
antusheng 发表于 2019-12-22 23:54 | 显示全部楼层
中断测试只20次?
yiy 发表于 2019-12-23 23:25 | 显示全部楼层
学习一下
小灵通2018 发表于 2019-12-24 23:56 | 显示全部楼层
还没搞清楚怎么通过定时器中断做PWM模拟。
dongnanxibei 发表于 2019-12-24 23:59 | 显示全部楼层
貌似几个定时器的使用方法是一样的。
天灵灵地灵灵 发表于 2019-12-25 13:33 | 显示全部楼层
准备做个定时器的应用。好像不能在中断里操作 太多。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

233

主题

3529

帖子

11

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