[DemoCode下载] 定时器捕获应用

[复制链接]
1373|2
 楼主| xinxianshi 发表于 2025-1-25 13:08 | 显示全部楼层 |阅读模式
  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_Msk | SYS_MFP_P31_Msk | SYS_MFP_P34_Msk | SYS_MFP_P35_Msk | SYS_MFP_P33_Msk);
  112.                 SYS->P3_MFP |= (SYS_MFP_P30_RXD | SYS_MFP_P31_TXD | SYS_MFP_P34_T0 | SYS_MFP_P35_T1 | SYS_MFP_P33_T1EX);
  113.     /* Set P1 multi-function pins for T3 */
  114.     SYS->P1_MFP &= ~SYS_MFP_P11_Msk;
  115.     SYS->P1_MFP |= SYS_MFP_P11_T3;
  116. }

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

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

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

  133.     /* Unlock protected registers */
  134.     SYS_UnlockReg();

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

  137.     /* Init UART0 for printf */
  138.     UART0_Init();

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

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

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

  154.     /* Initial Timer1 default setting */
  155.     TIMER_Open(TIMER1, TIMER_CONTINUOUS_MODE, 1);

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

  162.     /* Enable Timer1 NVIC */
  163.     NVIC_EnableIRQ(TMR1_IRQn);

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

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

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

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

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

  184.     while(1);
  185. }

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


 楼主| xinxianshi 发表于 2025-1-25 13:08 | 显示全部楼层
你用过定时器捕获这个功能吗?
 楼主| xinxianshi 发表于 2025-1-25 13:12 | 显示全部楼层
 通过TCAP (定时器捕捉数据寄存器)可读取 24 位捕捉计数器的值
 支持外部管脚捕捉(T0EX~T3EX),可用于脉宽测量
 支持外部引脚捕捉(T0EX~T3EX),可用于复位24位向上定时器
您需要登录后才可以回帖 登录 | 注册

本版积分规则

102

主题

1019

帖子

1

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