[DemoCode下载] NUC200基于定时器的捕获

[复制链接]
791|6
 楼主| heisexingqisi 发表于 2019-11-24 20:45 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V2.00
  4. * $Revision: 8 $
  5. * $Date: 15/04/13 11:49a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show how to use the timer2 capture function to capture timer2 counter value.
  7. * @note
  8. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "NUC200Series.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_NUC200Series.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_NUC200Series.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_NUC200Series.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. /**
  66. * @brief       Timer3 IRQ
  67. *
  68. * @param       None
  69. *
  70. * @return      None
  71. *
  72. * @details     The Timer3 default IRQ, declared in startup_NUC200Series.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 Internal RC 22.1184MHz clock */
  86.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

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

  89.     /* Switch HCLK clock source to Internal RC 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 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.    
  98.     /* Enable UART module clock */
  99.     CLK_EnableModuleClock(UART0_MODULE);   
  100.    
  101.     /* Select UART module clock source */
  102.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  103.     /* Enable Timer 0~3 module clock */
  104.     CLK_EnableModuleClock(TMR0_MODULE);   
  105.     CLK_EnableModuleClock(TMR2_MODULE);   
  106.     CLK_EnableModuleClock(TMR3_MODULE);   

  107.     /* Select Timer 0~3 module clock source */
  108.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0_S_HXT, NULL);
  109.     CLK_SetModuleClock(TMR2_MODULE, CLK_CLKSEL1_TMR2_S_HCLK, NULL);
  110.     CLK_SetModuleClock(TMR3_MODULE, CLK_CLKSEL1_TMR3_S_HXT, NULL);

  111.     /*---------------------------------------------------------------------------------------------------------*/
  112.     /* Init I/O Multi-function                                                                                 */
  113.     /*---------------------------------------------------------------------------------------------------------*/
  114.     /* Set PB multi-function pins for UART0 RXD, TXD, TM0, TM2, TM3 and TM2_EXT */
  115.     SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
  116.     SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD);
  117.    
  118.     SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk |
  119.                    SYS_GPB_MFP_PB8_Msk | SYS_GPB_MFP_PB10_Msk | SYS_GPB_MFP_PB11_Msk |
  120.                    SYS_GPB_MFP_PB2_Msk);
  121.     SYS->GPB_MFP |= SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD |
  122.                    SYS_GPB_MFP_PB8_TM0 | SYS_GPB_MFP_PB10_TM2 | SYS_GPB_MFP_PB11_TM3 |
  123.                    SYS_GPB_MFP_PB2_TM2_EXT;

  124.     SYS->ALT_MFP &= ~SYS_ALT_MFP_PB2_Msk;
  125.     SYS->ALT_MFP |= SYS_ALT_MFP_PB2_TM2_EXT;
  126.    
  127.     SYS->ALT_MFP2 = SYS_ALT_MFP2_PB2_TM2_EXT;
  128. }

  129. void UART0_Init(void)
  130. {
  131.     /*---------------------------------------------------------------------------------------------------------*/
  132.     /* Init UART                                                                                               */
  133.     /*---------------------------------------------------------------------------------------------------------*/
  134.     /* Reset UART0 */
  135.     SYS_ResetModule(UART0_RST);

  136.     /* Configure UART0 and set UART0 Baudrate */
  137.     UART_Open(UART0, 115200);
  138. }

  139. /*---------------------------------------------------------------------------------------------------------*/
  140. /*  MAIN function                                                                                          */
  141. /*---------------------------------------------------------------------------------------------------------*/
  142. int main(void)
  143. {
  144.     volatile uint32_t u32InitCount;
  145.     uint32_t au32CAPValus[10];

  146.     /* Unlock protected registers */
  147.     SYS_UnlockReg();

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

  150.     /* Lock protected registers */
  151.     SYS_LockReg();

  152.     /* Init UART0 for printf */
  153.     UART0_Init();

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

  158.     printf("# Timer Settings:\n");
  159.     printf("  Timer0: Clock source is 12 MHz; Toggle-output mode and frequency is 500 Hz.\n");
  160.     printf("  Timer3: Clock source is 12 MHz; Toggle-output mode and frequency is 1 Hz.\n");
  161.     printf("  Timer2: Clock source is HCLK(50 MHz); Continuous counting mode; TCMP is 0xFFFFFF;\n");
  162.     printf("          Counter pin enable; Capture pin and capture interrupt enable;\n");
  163.     printf("# Generate 500 Hz frequency from TM0 and connect TM0 pin to Timer2 counter pin.\n");
  164.     printf("# Generate 1 Hz frequency from TM3 and connect TM3 pin to TM2_EXT capture pin.\n");
  165.     printf("# Get 500 event counts from Timer2 counter pin when each TM2_EXT pin interrupt occurred.\n\n");

  166.     /* Initial Timer0 and Timer3 default setting */
  167.     TIMER_Open(TIMER0, TIMER_TOGGLE_MODE, 1000);
  168.     TIMER_Open(TIMER3, TIMER_TOGGLE_MODE, 2);

  169.     /* Initial Timer2 default setting */
  170.     TIMER_Open(TIMER2, TIMER_CONTINUOUS_MODE, 1);

  171.     /* Configure Timer2 setting for external counter input and capture function */
  172.     TIMER_SET_PRESCALE_VALUE(TIMER2, 0);
  173.     TIMER_SET_CMP_VALUE(TIMER2, 0xFFFFFF);
  174.     TIMER_EnableEventCounter(TIMER2, TIMER_COUNTER_FALLING_EDGE);
  175.     TIMER_EnableCapture(TIMER2, TIMER_CAPTURE_FREE_COUNTING_MODE, TIMER_CAPTURE_FALLING_EDGE);
  176.     TIMER_EnableCaptureInt(TIMER2);

  177.     /* Enable Timer2 NVIC */
  178.     NVIC_EnableIRQ(TMR2_IRQn);

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

  181.     /* Start Timer0, Timer2 and Timer3 counting */
  182.     TIMER_Start(TIMER0);
  183.     TIMER_Start(TIMER2);
  184.     TIMER_Start(TIMER3);

  185.     /* Check TM2_EXT interrupt counts */
  186.     while(g_au32TMRINTCount[2] <= 10)
  187.     {
  188.         if(g_au32TMRINTCount[2] != u32InitCount)
  189.         {
  190.             au32CAPValus[u32InitCount] = TIMER_GetCaptureData(TIMER2);
  191.             printf("[%2d] - %4d\n", g_au32TMRINTCount[2], au32CAPValus[u32InitCount]);
  192.             if(u32InitCount > 1)
  193.             {
  194.                 if((au32CAPValus[u32InitCount] - au32CAPValus[u32InitCount - 1]) != 500)
  195.                 {
  196.                     printf("*** FAIL ***\n");
  197.                     while(1);
  198.                 }
  199.             }
  200.             u32InitCount = g_au32TMRINTCount[2];
  201.         }
  202.     }

  203.     /* Stop Timer0, Timer2 and Timer3 counting */
  204.     TIMER_Close(TIMER0);
  205.     TIMER_Close(TIMER2);
  206.     TIMER_Close(TIMER3);

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

  208.     while(1);
  209. }

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


 楼主| heisexingqisi 发表于 2019-11-24 20:54 | 显示全部楼层
是不是不好理解,有没有大神讲讲
幸福小强 发表于 2019-11-24 21:28 | 显示全部楼层
看半天没看懂。
manufact 发表于 2019-11-25 16:46 | 显示全部楼层
需要一些注释或者说明文字之类的
TomLinTon 发表于 2019-11-26 11:32 | 显示全部楼层
请问是否有NUC121/125的SPI例程,我按照官网那个去跑,跑不起来
21mengnan 发表于 2019-11-26 16:27 | 显示全部楼层
TomLinTon 发表于 2019-11-26 11:32
请问是否有NUC121/125的SPI例程,我按照官网那个去跑,跑不起来

啥情况。官方提供的有寄存器版本和库函数版本,你都试试看。
21mengnan 发表于 2019-11-26 16:28 | 显示全部楼层
这个没有PWM那个好懂。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

157

主题

2778

帖子

2

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