[DemoCode下载] M058S定时器捕获

[复制链接]
862|5
 楼主| mintspring 发表于 2017-8-26 21:13 | 显示全部楼层 |阅读模式
  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]    Show how to use the timer1 capture function to capture timer1 counter value.
  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.     /* Clear Timer0 time-out interrupt flag */
  29.     TIMER_ClearIntFlag(TIMER0);

  30.     g_au32TMRINTCount[0]++;
  31. }

  32. /**
  33. * @brief       Timer1 IRQ
  34. *
  35. * @param       None
  36. *
  37. * @return      None
  38. *
  39. * @details     The Timer1 default IRQ, declared in startup_M058S.s.
  40. */
  41. void TMR1_IRQHandler(void)
  42. {
  43.     if(TIMER_GetCaptureIntFlag(TIMER1) == 1)
  44.     {
  45.         /* Clear Timer1 capture interrupt flag */
  46.         TIMER_ClearCaptureIntFlag(TIMER1);

  47.         g_au32TMRINTCount[1]++;
  48.     }
  49. }

  50. /**
  51. * @brief       Timer2 IRQ
  52. *
  53. * @param       None
  54. *
  55. * @return      None
  56. *
  57. * @details     The Timer2 default IRQ, declared in startup_M058S.s.
  58. */
  59. void TMR2_IRQHandler(void)
  60. {
  61.     /* Clear Timer2 time-out interrupt flag */
  62.     TIMER_GetIntFlag(TIMER2);

  63.     g_au32TMRINTCount[2]++;
  64. }

  65. /**
  66. * @brief       Timer3 IRQ
  67. *
  68. * @param       None
  69. *
  70. * @return      None
  71. *
  72. * @details     The Timer3 default IRQ, declared in startup_M058S.s.
  73. */
  74. void TMR3_IRQHandler(void)
  75. {
  76.     /* Clear Timer3 time-out interrupt flag */
  77.     TIMER_GetIntFlag(TIMER3);

  78.     g_au32TMRINTCount[3]++;
  79. }

  80. void SYS_Init(void)
  81. {
  82.     /*---------------------------------------------------------------------------------------------------------*/
  83.     /* Init System Clock                                                                                       */
  84.     /*---------------------------------------------------------------------------------------------------------*/
  85.     /* Enable HIRC 22.1184MHz clock */
  86.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  87.     /* Waiting for HIRC clock ready */
  88.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

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

  91.     /* Enable external XTAL 12MHz clock */
  92.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  93.     /* Waiting for external XTAL clock ready */
  94.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  95.     /* Set core clock as PLL_CLOCK from PLL */
  96.     CLK_SetCoreClock(PLL_CLOCK);

  97.     /* Enable peripheral clock */
  98.     CLK_EnableModuleClock(UART0_MODULE);
  99.     CLK_EnableModuleClock(TMR0_MODULE);
  100.     CLK_EnableModuleClock(TMR1_MODULE);
  101.     CLK_EnableModuleClock(TMR3_MODULE);

  102.     /* Peripheral clock source */
  103.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));
  104.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0_S_HXT, 0);
  105.     CLK_SetModuleClock(TMR1_MODULE, CLK_CLKSEL1_TMR1_S_HCLK, 0);
  106.     CLK_SetModuleClock(TMR3_MODULE, CLK_CLKSEL1_TMR3_S_HXT, 0);

  107.     /*---------------------------------------------------------------------------------------------------------*/
  108.     /* Init I/O Multi-function                                                                                 */
  109.     /*---------------------------------------------------------------------------------------------------------*/
  110.     /* Set P3 multi-function pins for UART0 RXD, TXD, T0, T1 and T1EX */
  111.     SYS->P3_MFP = SYS_MFP_P30_RXD | SYS_MFP_P31_TXD | SYS_MFP_P34_T0 | SYS_MFP_P35_T1 | SYS_MFP_P33_T1EX;

  112.     /* Set P1 multi-function pins for T3 */
  113.     SYS->P1_MFP = SYS_MFP_P11_T3;
  114. }

  115. void UART0_Init(void)
  116. {
  117.     /*---------------------------------------------------------------------------------------------------------*/
  118.     /* Init UART                                                                                               */
  119.     /*---------------------------------------------------------------------------------------------------------*/
  120.     /* Reset IP */
  121.     SYS_ResetModule(UART0_RST);

  122.     /* Configure UART0 and set UART0 Baudrate */
  123.     UART_Open(UART0, 115200);
  124. }

  125. /*---------------------------------------------------------------------------------------------------------*/
  126. /*  MAIN function                                                                                          */
  127. /*---------------------------------------------------------------------------------------------------------*/
  128. int main(void)
  129. {
  130.     volatile uint32_t u32InitCount;

  131.     /* Unlock protected registers */
  132.     SYS_UnlockReg();

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

  135.     /* Init UART0 for printf */
  136.     UART0_Init();

  137.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  138.     printf("+---------------------------------------------------+\n");
  139.     printf("|    Timer External Capture Function Sample Code    |\n");
  140.     printf("+---------------------------------------------------+\n\n");

  141.     printf("# Timer Settings:\n");
  142.     printf("  Timer0: Clock source is 12 MHz; Toggle-output mode and frequency is 500 Hz.\n");
  143.     printf("  Timer3: Clock source is 12 MHz; Toggle-output mode and frequency is 1 Hz.\n");
  144.     printf("  Timer1: Clock source is HCLK(50 MHz); Continuous counting mode; TCMP is 0xFFFFFF;\n");
  145.     printf("          Counter pin enable; Capture pin and capture interrupt enable;\n");
  146.     printf("# Generate 500 Hz frequency from T0 and connect T0 pin to Timer1 counter pin.\n");
  147.     printf("# Generate 1 Hz frequency from T3 and connect T3 pin to T1EX capture pin.\n");
  148.     printf("# Get 500 event counts from Timer1 counter pin when each T1EX pin interrupt occurred.\n\n");

  149.     /* Initial Timer0 and Timer3 default setting */
  150.     TIMER_Open(TIMER0, TIMER_TOGGLE_MODE, 1000);
  151.     TIMER_Open(TIMER3, TIMER_TOGGLE_MODE, 2);

  152.     /* Initial Timer1 default setting */
  153.     TIMER_Open(TIMER1, TIMER_CONTINUOUS_MODE, 1);

  154.     /* Configure Timer1 setting for external counter input and capture function */
  155.     TIMER_SET_PRESCALE_VALUE(TIMER1, 0);
  156.     TIMER_SET_CMP_VALUE(TIMER1, 0xFFFFFF);
  157.     TIMER_EnableEventCounter(TIMER1, TIMER_COUNTER_FALLING_EDGE);
  158.     TIMER_EnableCapture(TIMER1, TIMER_CAPTURE_FREE_COUNTING_MODE, TIMER_CAPTURE_FALLING_EDGE);
  159.     TIMER_EnableCaptureInt(TIMER1);

  160.     /* Enable Timer1 NVIC */
  161.     NVIC_EnableIRQ(TMR1_IRQn);

  162.     /* Clear Timer1 interrupt counts to 0 */
  163.     u32InitCount = g_au32TMRINTCount[1] = 0;

  164.     /* Start Timer0, Timer1 and Timer3 counting */
  165.     TIMER_Start(TIMER0);
  166.     TIMER_Start(TIMER1);
  167.     TIMER_Start(TIMER3);

  168.     /* Check T1EX interrupt counts */
  169.     while(g_au32TMRINTCount[1] <= 10)
  170.     {
  171.         if(g_au32TMRINTCount[1] != u32InitCount)
  172.         {
  173.             printf("[%2d] - %4d\n", g_au32TMRINTCount[1], TIMER_GetCaptureData(TIMER1));
  174.             u32InitCount = g_au32TMRINTCount[1];
  175.         }
  176.     }

  177.     /* Stop Timer0, Timer1 and Timer3 counting */
  178.     TIMER_Close(TIMER0);
  179.     TIMER_Close(TIMER1);
  180.     TIMER_Close(TIMER3);

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

  182.     while(1);
  183. }

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


wahahaheihei 发表于 2017-8-26 22:59 | 显示全部楼层
定时器是捕获数量用的吧
huahuagg 发表于 2017-8-27 18:41 | 显示全部楼层
不来点文字讲下原理吗
dongnanxibei 发表于 2017-8-27 21:17 | 显示全部楼层
蒙圈了,找找资料看看咋回事。
jiekou001 发表于 2017-8-27 21:22 | 显示全部楼层
定时器可以计数,也可以捕获脉冲。
jiekou001 发表于 2017-8-27 21:24 | 显示全部楼层
当使用到定时器时,会涉及到使用到的时钟源,芯片的外置晶振为12MHz,因此选择时钟源时使用12MHz,通过时钟源选择控制寄存器1(CLKSEL1)进行设置,有一点不同的是,ARM面向低功耗设备的,因此,所有内部单元涉及到时钟源都可独立地自行关闭和开启。当前实验用到定时器0,必需使能定时器0时钟,通过APB设备时钟使能控制寄存器(APBCLK)进行设置
您需要登录后才可以回帖 登录 | 注册

本版积分规则

303

主题

4972

帖子

24

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