[牛人杂谈] M058定时器捕获

[复制链接]
818|18
 楼主| xinxianshi 发表于 2018-12-23 23:25 | 显示全部楼层 |阅读模式
484695c1fa8b75b1a3.png
    SYS->P3_MFP = SYS_MFP_P30_RXD | SYS_MFP_P31_TXD | SYS_MFP_P34_T0 | SYS_MFP_P35_T1 | SYS_MFP_P33_T1EX;
是不是指的这个标红。
 楼主| xinxianshi 发表于 2018-12-23 23:26 | 显示全部楼层
所以按照说明时钟要这么配置
    CLK_SetModuleClock(TMR1_MODULE, CLK_CLKSEL1_TMR1_S_HCLK, 0);
 楼主| xinxianshi 发表于 2018-12-23 23:27 | 显示全部楼层
    /* Initial Timer1 default setting */
    TIMER_Open(TIMER1, TIMER_CONTINUOUS_MODE, 1);
而定时器也要用这种连续模式,就是获取一个中断值后,继续计数,而不重置到0.
 楼主| xinxianshi 发表于 2018-12-23 23:28 | 显示全部楼层
    /* Configure Timer1 setting for external counter input and capture function */
    TIMER_SET_PRESCALE_VALUE(TIMER1, 0);
    TIMER_SET_CMP_VALUE(TIMER1, 0xFFFFFF);
    TIMER_EnableEventCounter(TIMER1, TIMER_COUNTER_FALLING_EDGE);
    TIMER_EnableCapture(TIMER1, TIMER_CAPTURE_FREE_COUNTING_MODE, TIMER_CAPTURE_FALLING_EDGE);
    TIMER_EnableCaptureInt(TIMER1);

    /* Enable Timer1 NVIC */
    NVIC_EnableIRQ(TMR1_IRQn);
 楼主| xinxianshi 发表于 2018-12-23 23:38 | 显示全部楼层
xinxianshi 发表于 2018-12-23 23:28
/* Configure Timer1 setting for external counter input and capture function */
    TIMER_SET_PRE ...

这里预分频0,就是不分频
然后设置比较的值(连续模式下有说明,TCMP)
而这里刚好是设置的满量程
使能时间中断,下降沿。
接着启用带有指定检测边缘的计时器计数器函数
 楼主| xinxianshi 发表于 2018-12-23 23:40 | 显示全部楼层
void TMR1_IRQHandler(void)
{
    if(TIMER_GetCaptureIntFlag(TIMER1) == 1)
    {
        /* Clear Timer1 capture interrupt flag */
        TIMER_ClearCaptureIntFlag(TIMER1);

        g_au32TMRINTCount[1]++;
    }
}
这是中断统计次数呢。
 楼主| xinxianshi 发表于 2018-12-23 23:43 | 显示全部楼层
其实手册写的挺抽象的。
 楼主| xinxianshi 发表于 2018-12-23 23:44 | 显示全部楼层
* @Details     The Timer3 default IRQ, declared in startup_M058S.s.
例子中这个细节注释,说明这个中断函数名都是在启动文件里设置的。
 楼主| xinxianshi 发表于 2018-12-23 23:47 | 显示全部楼层
  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. * @details     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. ***/
 楼主| xinxianshi 发表于 2018-12-23 23:48 | 显示全部楼层
T0和T3的中断生成的方波发送给T1捕获检测。
598330983 发表于 2018-12-23 23:51 | 显示全部楼层
    TIMER_SET_CMP_VALUE(TIMER1, 0xFFFFFF);
这个是啥意思,比较值?
是通过这个发生中断的周期?
dongnanxibei 发表于 2018-12-24 00:00 | 显示全部楼层

  1.     while(g_au32TMRINTCount[1] <= 10)
  2.     {
  3.         if(g_au32TMRINTCount[1] != u32InitCount)
  4.         {
  5.             printf("[%2d] - %4d\n", g_au32TMRINTCount[1], TIMER_GetCaptureData(TIMER1));
  6.             u32InitCount = g_au32TMRINTCount[1];
  7.         }
  8.     }
好多人看不懂这里,是因为担心连续发生多计数周期后,才捕获到,那么就不知道前面经历了多少次的完整计数周期。
这里就是通过w


dongnanxibei 发表于 2018-12-24 00:00 | 显示全部楼层
dongnanxibei 发表于 2018-12-24 00:00
好多人看不懂这里,是因为担心连续发生多计数周期后,才捕获到,那么就不知道前面经历了多少次的完整计数 ...

通过多次完整的周期+最后捕获的值,准确反映出真实的值。
dongnanxibei 发表于 2018-12-24 00:01 | 显示全部楼层
这里的TIMER_SET_CMP_VALUE(TIMER1, 0xFFFFFF);
就是完整的一个计数周期。
但是不一定会发生,要看触发信号是否长于这个值。
dongnanxibei 发表于 2018-12-24 00:02 | 显示全部楼层
我第一次看这个例子我也很纳闷,怎么还进行比较中断啊,干啥。
想到之前有个问题就是循环计数,我怎么知道这是第几次循环时候才得到的结果呢。比如延时函数。
dongnanxibei 发表于 2018-12-24 00:02 | 显示全部楼层
计数器满了,要回头从0计数,这里满的时候就要中断,进行统计。
dongnanxibei 发表于 2018-12-24 00:03 | 显示全部楼层
希望我的这个学习发现可以帮到更多入门的人。
zhuomuniao110 发表于 2018-12-24 11:35 | 显示全部楼层
终于看懂这个例子了。谢谢。我之前很纳闷,搞满量程的比较器中断干啥。
598330983 发表于 2018-12-25 21:50 来自手机 | 显示全部楼层
好久没有玩板子了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

102

主题

1019

帖子

1

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