[DemoCode下载] 利用定时器捕捉双边沿

[复制链接]
933|1
 楼主| 小明的同学 发表于 2024-4-21 14:57 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]    main.c
  3. * @brief
  4. *          Demonstrate how to use the TIMER0 capture function to capture
  5. *          both edge of external capture signal and calculate the time
  6. *          of high level duration of capture signal.
  7. * @note
  8. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "M0518.h"

  12. /****************************************************************************/
  13. /* Define                                                                   */
  14. /****************************************************************************/

  15. #define HCLK_CLOCK           50000000

  16. /****************************************************************************/
  17. /* Global variables                                                         */
  18. /****************************************************************************/

  19. volatile uint32_t g_au32TMRCapCountRise, g_au32TMRCapCountFall, g_u32TimerCapFlag;

  20. /****************************************************************************/
  21. /* Functions                                                                */
  22. /****************************************************************************/

  23. /*------------------------------*/
  24. /* TIMER0 interrupt handler     */
  25. /*------------------------------*/
  26. void TMR0_IRQHandler(void)
  27. {
  28.     if (TIMER_GetCaptureIntFlag(TIMER0) == 1)
  29.     {
  30.         /* Clear TIMER0 capture interrupt flag */
  31.         TIMER_ClearCaptureIntFlag(TIMER0);

  32.         if (PB15)
  33.         {
  34.             /* Save the capture counter for PB15 rising edge */
  35.             g_au32TMRCapCountRise = TIMER_GetCaptureData(TIMER0);
  36.         }
  37.         else
  38.         {
  39.             /* Save the capture counter for PB15 falling edge */
  40.             g_au32TMRCapCountFall = TIMER_GetCaptureData(TIMER0);
  41.             g_u32TimerCapFlag = 1;
  42.         }
  43.     }
  44. }

  45. void SYS_Init(void)
  46. {
  47.     /*------------------------------*/
  48.     /* Init System Clock            */
  49.     /*------------------------------*/

  50.     /* Enable HIRC clock */
  51.     CLK_EnableXtalRC(CLK_PWRCON_IRC22M_EN_Msk);

  52.     /* Waiting for HIRC clock ready */
  53.     CLK_WaitClockReady(CLK_CLKSTATUS_IRC22M_STB_Msk);

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

  56.     /* Enable HXT */
  57.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  58.     /* Waiting for clock ready */
  59.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  60.     /* Set core clock as HCLK_CLOCK from PLL and SysTick source to HCLK/2*/
  61.     CLK_SetCoreClock(HCLK_CLOCK);

  62.     /* Enable peripheral clock */
  63.     CLK_EnableModuleClock(UART0_MODULE);
  64.     CLK_EnableModuleClock(TMR0_MODULE);

  65.     /* Peripheral clock source */
  66.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));
  67.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0_S_HCLK, 0);

  68.     /*------------------------------*/
  69.     /* Init I/O Multi-function      */
  70.     /*------------------------------*/
  71.     /* Set PB multi-function pins for UART0 RXD, TXD */
  72.     SYS->GPB_MFP = SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;

  73.     /* Set PB multi-function pins for TM0_EXT on PB.15 */
  74.     SYS->GPB_MFP |= (SYS_GPB_MFP_PB15_TM0_EXT);
  75.     SYS->ALT_MFP  = SYS_ALT_MFP_PB15_TM0_EXT;
  76. }


  77. void UART0_Init(void)
  78. {
  79.     /*------------------------------*/
  80.     /* Init UART                    */
  81.     /*------------------------------*/
  82.     /* Reset UART IP */
  83.     SYS_ResetModule(UART0_RST);

  84.     /* Configure UART0 and set UART0 Baudrate */
  85.     UART_Open(UART0, 115200);
  86. }


  87. /*------------------------------*/
  88. /* Main Function                */
  89. /*------------------------------*/
  90. int main(void)
  91. {
  92.     uint32_t u32CAPValue = 0;
  93.     uint32_t u32delayCounter;

  94.     /* Unlock protected registers */
  95.     SYS_UnlockReg();

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

  98.     /* Lock protected registers */
  99.     SYS_LockReg();

  100.     /* Init UART0 for printf */
  101.     UART0_Init();

  102.     printf("CPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  103.     printf("+------------------------------------------+\n");
  104.     printf("|    TIMER0 Capture Counter Sample Code    |\n");
  105.     printf("+------------------------------------------+\n\n");
  106.     printf(" * Please connect PB14 and PB15 by wire.\n");

  107.     /* Config PB14 to GPIO output mode to generate a pulse as external capture signal */
  108.     PB14 = 0;
  109.     GPIO_SetMode(PB, BIT14, GPIO_PMD_OUTPUT);

  110.     /*----- Initial TIMER0 -----*/

  111.     SYS_ResetModule(TMR0_RST);
  112.     TIMER_Open(TIMER0, TIMER_CONTINUOUS_MODE, 1);

  113.     /* Configure TIMER0 setting for external counter input and capture function */
  114.     /* Set TIMER0 clock to HCLK/(1+1) */
  115.     TIMER_SET_PRESCALE_VALUE(TIMER0, 1);
  116.     printf("TIMER0 clock @ %d Hz\n", SystemCoreClock / 2);
  117.     /* Reset TIMER0 counter value */
  118.     TIMER_SET_CMP_VALUE(TIMER0, 0xFFFFFF);
  119.     /* Set TIMER0 to capture both edges */
  120.     TIMER_EnableCapture(TIMER0, TIMER_CAPTURE_FREE_COUNTING_MODE, TIMER_CAPTURE_FALLING_AND_RISING_EDGE);
  121.     /* Enable TIMER0 capture interrupt */
  122.     TIMER_EnableCaptureInt(TIMER0);
  123.     NVIC_EnableIRQ(TMR0_IRQn);

  124.     /*---- Begin to test -----*/

  125.     g_au32TMRCapCountRise = g_au32TMRCapCountFall = g_u32TimerCapFlag = 0;

  126.     /* Start TIMER0 counting */
  127.     TIMER_Start(TIMER0);

  128.     /* Generate a pulse with both rising edge and falling edge to PB14 */
  129.     PB14 = 1;   /* Rising edge */

  130.     /* Delay to keep high level of pulse. */
  131.     /* Change the delay counter will get different capture counter value */
  132.     for (u32delayCounter = 0; u32delayCounter < 10000; u32delayCounter++);

  133.     PB14 = 0;   /* Falling edge */

  134.     /* Wait a rising edge and a falling edge from PB15 */
  135.     /* TIMER0 interrupt handler will set g_u32TimerCapFlag to 1 when received */
  136.     /*      external capture signal from PB15 */
  137.     while (g_u32TimerCapFlag == 0);

  138.     /* Got the counter value for external capture signal */
  139.     if (g_au32TMRCapCountFall > g_au32TMRCapCountRise)
  140.         u32CAPValue = g_au32TMRCapCountFall - g_au32TMRCapCountRise;
  141.     else
  142.         /* In the case that TIMER0 counter overflowed */
  143.         u32CAPValue = 0xffffff - g_au32TMRCapCountRise + g_au32TMRCapCountFall;

  144.     printf("Capture counter for rising edge: 0x%X (%d)\n", g_au32TMRCapCountRise, g_au32TMRCapCountRise);
  145.     printf("Capture counter for falling edge: 0x%X (%d)\n", g_au32TMRCapCountFall, g_au32TMRCapCountFall);
  146.     printf("\n");
  147.     printf("Capture counter for high level duration: 0x%X (%d)\n", u32CAPValue, u32CAPValue);
  148.     printf("The high level duration time should be about (TIMER0 clock) * %d\n", u32CAPValue);
  149.     printf("                                             = %d us\n", u32CAPValue / (SystemCoreClock / 2 / 1000000));

  150.     /* Stop TIMER0 counting */
  151.     TIMER_Close(TIMER0);

  152.     while (1);
  153. }

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


21mengnan 发表于 2024-4-21 17:48 | 显示全部楼层
定时器功能比较强大,捕捉功能,可以高精度捕捉脉宽。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

159

主题

1640

帖子

2

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