[DemoCode下载] NUC123定时器捕获

[复制链接]
903|8
 楼主| heisexingqisi 发表于 2019-9-24 20:39 | 显示全部楼层 |阅读模式
  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: 5 $
  5. * $Date: 15/07/02 11:18a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Demonstrate how to use timer2 capture event to capture timer2 counter value.
  7. * @note
  8. * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "NUC123.h"

  12. #define HCLK_CLOCK           72000000


  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_NUC123.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_NUC123.s.
  40. */
  41. void TMR1_IRQHandler(void)
  42. {
  43.     /* Clear Timer1 time-out interrupt flag */
  44.     TIMER_GetIntFlag(TIMER1);

  45.     g_au32TMRINTCount[1]++;
  46. }

  47. /**
  48. * @brief       Timer2 IRQ
  49. *
  50. * @param       None
  51. *
  52. * @return      None
  53. *
  54. * @details     The Timer2 default IRQ, declared in startup_NUC123.s.
  55. */
  56. void TMR2_IRQHandler(void)
  57. {
  58.     if(TIMER_GetCaptureIntFlag(TIMER2) == 1)
  59.     {
  60.         /* Clear Timer2 capture interrupt flag */
  61.         TIMER_ClearCaptureIntFlag(TIMER2);

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

  65. void SYS_Init(void)
  66. {
  67.     /*---------------------------------------------------------------------------------------------------------*/
  68.     /* Init System Clock                                                                                       */
  69.     /*---------------------------------------------------------------------------------------------------------*/
  70.     /* Enable IRC22M clock */
  71.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  72.     /* Waiting for IRC22M clock ready */
  73.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  74.     /* Switch HCLK clock source to HIRC */
  75.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  76.     /* Enable XT1_OUT(PF.0) and XT1_IN(PF.1) */
  77.     SYS->GPF_MFP |= SYS_GPF_MFP_PF0_XT1_OUT | SYS_GPF_MFP_PF1_XT1_IN;

  78.     /* Enable external 12 MHz XTAL */
  79.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  80.     /* Waiting for clock ready */
  81.     while(!CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk));

  82.     /* Set core clock as HCLK_CLOCK */
  83.     CLK_SetCoreClock(HCLK_CLOCK);

  84.     /* Enable peripheral clock */
  85.     CLK_EnableModuleClock(UART0_MODULE);
  86.     CLK_EnableModuleClock(TMR0_MODULE);
  87.     CLK_EnableModuleClock(TMR1_MODULE);
  88.     CLK_EnableModuleClock(TMR2_MODULE);

  89.     /* Peripheral clock source */
  90.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));
  91.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0_S_HXT, 0);
  92.     CLK_SetModuleClock(TMR1_MODULE, CLK_CLKSEL1_TMR1_S_HXT, 0);
  93.     CLK_SetModuleClock(TMR2_MODULE, CLK_CLKSEL1_TMR2_S_HCLK, 0);

  94.     /*---------------------------------------------------------------------------------------------------------*/
  95.     /* Init I/O Multi-function                                                                                 */
  96.     /*---------------------------------------------------------------------------------------------------------*/
  97.     /* Set PB multi-function pins for UART0 RXD, TXD, TM0, TM1, TM2 and TM2_EXT */
  98.     SYS->GPB_MFP = SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD |
  99.                    SYS_GPB_MFP_PB8_TM0 | SYS_GPB_MFP_PB9_TM1 | SYS_GPB_MFP_PB10_TM2 |
  100.                    SYS_GPB_MFP_PB2_TM2_EXT;

  101.     SYS->ALT_MFP  = SYS_ALT_MFP_PB2_TM2_EXT;
  102. }

  103. void UART0_Init(void)
  104. {
  105.     /*---------------------------------------------------------------------------------------------------------*/
  106.     /* Init UART                                                                                               */
  107.     /*---------------------------------------------------------------------------------------------------------*/
  108.     /* Reset IP */
  109.     SYS_ResetModule(UART0_RST);

  110.     /* Configure UART0 and set UART0 Baudrate */
  111.     UART_Open(UART0, 115200);
  112. }

  113. /*---------------------------------------------------------------------------------------------------------*/
  114. /*  MAIN function                                                                                          */
  115. /*---------------------------------------------------------------------------------------------------------*/
  116. int main(void)
  117. {
  118.     volatile uint32_t u32InitCount, u32TimeoutCount;
  119.     uint32_t au32CAPValus[10];;

  120.     /* Unlock protected registers */
  121.     SYS_UnlockReg();

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

  124.     /* Lock protected registers */
  125.     SYS_LockReg();

  126.     /* Init UART0 for printf */
  127.     UART0_Init();

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

  132.     printf("# Timer Settings:\n");
  133.     printf("  Timer0: Clock source is 12 MHz; Toggle-output mode and frequency is 500 Hz.\n");
  134.     printf("  Timer1: Clock source is 12 MHz; Toggle-output mode and frequency is 1 Hz.\n");
  135.     printf("  Timer2: Clock source is HCLK(72 MHz); Continuous counting mode; TCMP is 0xFFFFFF;\n");
  136.     printf("          Counter pin enable; Capture pin and capture interrupt enable;\n");
  137.     printf("# Generate 500 Hz frequency from TM0(PB.8) and connect TM0 pin to Timer2 counter pin(PB.10).\n");
  138.     printf("# Generate 1 Hz frequency from TM1(PB.9) and connect TM1 pin to TM2_EXT(PB.2) capture pin.\n");
  139.     printf("# Get 500 event counts from Timer2 counter pin when each TM2_EXT pin interrupt occurred.\n\n");

  140.     /* Initial Timer0 and Timer1 default setting */
  141.     TIMER_Open(TIMER0, TIMER_TOGGLE_MODE, 1000);
  142.     TIMER_Open(TIMER1, TIMER_TOGGLE_MODE, 2);

  143.     /* Initial Timer2 default setting */
  144.     TIMER_Open(TIMER2, TIMER_CONTINUOUS_MODE, 1);

  145.     /* Configure Timer2 setting for external counter input and capture function */
  146.     TIMER_SET_PRESCALE_VALUE(TIMER2, 0);
  147.     TIMER_SET_CMP_VALUE(TIMER2, 0xFFFFFF);
  148.     TIMER_EnableEventCounter(TIMER2, TIMER_COUNTER_FALLING_EDGE);
  149.     TIMER_EnableCapture(TIMER2, TIMER_CAPTURE_FREE_COUNTING_MODE, TIMER_CAPTURE_FALLING_EDGE);
  150.     TIMER_EnableCaptureInt(TIMER2);

  151.     /* Enable Timer2 NVIC */
  152.     NVIC_EnableIRQ(TMR2_IRQn);

  153.     /* Clear Timer2 interrupt counts to 0 */
  154.     u32InitCount = g_au32TMRINTCount[2] = 0;

  155.     /* Start Timer0, Timer1 and Timer2 counting */
  156.     TIMER_Start(TIMER0);
  157.     TIMER_Start(TIMER1);
  158.     TIMER_Start(TIMER2);

  159.     /* Check TM2_EXT interrupt counts */
  160.     u32TimeoutCount = 0;
  161.     while(g_au32TMRINTCount[2] <= 10)
  162.     {
  163.         if(g_au32TMRINTCount[2] != u32InitCount)
  164.         {
  165.             u32TimeoutCount = 0;
  166.             au32CAPValus[u32InitCount] = TIMER_GetCaptureData(TIMER2);
  167.             printf("[%2d] - %4d\n", g_au32TMRINTCount[2], au32CAPValus[u32InitCount]);
  168.             if(u32InitCount > 1)
  169.             {
  170.                 if((au32CAPValus[u32InitCount] - au32CAPValus[u32InitCount - 1]) != 500)
  171.                 {
  172.                     printf("*** FAIL ***\n");
  173.                     while(1);
  174.                 }
  175.             }
  176.             u32InitCount = g_au32TMRINTCount[2];
  177.         }

  178.         if(u32TimeoutCount++ > SystemCoreClock / 2)
  179.         {
  180.             printf("Timer2 capture event time-out.\n");
  181.             while(1);
  182.         }
  183.     }

  184.     /* Stop Timer0, Timer1 and Timer2 counting */
  185.     TIMER_Close(TIMER0);
  186.     TIMER_Close(TIMER1);
  187.     TIMER_Close(TIMER2);

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

  189.     while(1);
  190. }

  191. /*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/


 楼主| heisexingqisi 发表于 2019-9-24 20:40 | 显示全部楼层
是不是发现,新唐的各种系列的库函数都很相似。
734774645 发表于 2019-9-24 20:46 | 显示全部楼层
一种中断形式。
幸福小强 发表于 2019-9-24 21:03 | 显示全部楼层
理不辩不明,有疑问的,讨论讨论。
天灵灵地灵灵 发表于 2019-9-24 21:15 | 显示全部楼层
这些基本配置都是一样的硬件,所以软件上很容易简单。
天灵灵地灵灵 发表于 2019-9-24 21:15 | 显示全部楼层
很容易实现软件兼容的。
zhuotuzi 发表于 2019-9-24 21:51 | 显示全部楼层
    TIMER_Close(TIMER0);
    TIMER_Close(TIMER1);
    TIMER_Close(TIMER2);
zhuotuzi 发表于 2019-9-24 21:51 | 显示全部楼层
关闭后就结束测试?
yiy 发表于 2019-9-24 22:38 | 显示全部楼层
所以学会一个就行了啊。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

157

主题

2778

帖子

2

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