[DemoCode下载] M051的定时器捕获和PWM捕获

[复制链接]
1437|12
 楼主| dongnanxibei 发表于 2019-5-25 22:59 | 显示全部楼层 |阅读模式
先说定时器捕获
  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: 4 $
  5. * $Date: 15/05/22 2:31p $
  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 "M051Series.h"

  12. #define PLLCON_SETTING      CLK_PLLCON_50MHz_HXT
  13. #define PLL_CLOCK           50000000


  14. /*---------------------------------------------------------------------------------------------------------*/
  15. /* Global Interface Variables Declarations                                                                 */
  16. /*---------------------------------------------------------------------------------------------------------*/
  17. extern int IsDebugFifoEmpty(void);
  18. volatile uint8_t  g_u8IsWDTTimeoutINT = 0;
  19. volatile uint32_t g_au32TMRINTCount[4] = {0};


  20. /**
  21. * @brief       Timer0 IRQ
  22. *
  23. * @param       None
  24. *
  25. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  26. *
  27. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The Timer0 default IRQ, declared in startup_M051Series.s.
  28. */
  29. void TMR0_IRQHandler(void)
  30. {
  31.     /* Clear Timer0 time-out interrupt flag */
  32.     TIMER_ClearIntFlag(TIMER0);

  33.     g_au32TMRINTCount[0]++;
  34. }

  35. /**
  36. * @brief       Timer1 IRQ
  37. *
  38. * @param       None
  39. *
  40. * @return      None
  41. *
  42. * @details     The Timer1 default IRQ, declared in startup_M051Series.s.
  43. */
  44. void TMR1_IRQHandler(void)
  45. {
  46.     if(TIMER_GetCaptureIntFlag(TIMER1) == 1)
  47.     {
  48.         /* Clear Timer1 capture interrupt flag */
  49.         TIMER_ClearCaptureIntFlag(TIMER1);

  50.         g_au32TMRINTCount[1]++;
  51.     }
  52. }

  53. /**
  54. * @brief       Timer2 IRQ
  55. *
  56. * @param       None
  57. *
  58. * @return      None
  59. *
  60. * @details     The Timer2 default IRQ, declared in startup_M051Series.s.
  61. */
  62. void TMR2_IRQHandler(void)
  63. {
  64.     /* Clear Timer2 time-out interrupt flag */
  65.     TIMER_GetIntFlag(TIMER2);

  66.     g_au32TMRINTCount[2]++;
  67. }

  68. /**
  69. * @brief       Timer3 IRQ
  70. *
  71. * @param       None
  72. *
  73. * @return      None
  74. *
  75. * @details     The Timer3 default IRQ, declared in startup_M051Series.s.
  76. */
  77. void TMR3_IRQHandler(void)
  78. {
  79.     /* Clear Timer3 time-out interrupt flag */
  80.     TIMER_GetIntFlag(TIMER3);

  81.     g_au32TMRINTCount[3]++;
  82. }

  83. void SYS_Init(void)
  84. {
  85.     /*---------------------------------------------------------------------------------------------------------*/
  86.     /* Init System Clock                                                                                       */
  87.     /*---------------------------------------------------------------------------------------------------------*/
  88.     /* Enable IRC22M clock */
  89.     CLK->PWRCON |= CLK_PWRCON_IRC22M_EN_Msk;

  90.     /* Waiting for IRC22M clock ready */
  91.     CLK_WaitClockReady(CLK_CLKSTATUS_IRC22M_STB_Msk);

  92.     /* Switch HCLK clock source to HIRC */
  93.     CLK->CLKSEL0 = CLK_CLKSEL0_HCLK_S_HIRC;

  94.     /* Set PLL to Power-down mode and PLL_STB bit in CLKSTATUS register will be cleared by hardware.*/
  95.     CLK->PLLCON |= CLK_PLLCON_PD_Msk;

  96.     /* Enable external 12 MHz XTAL, IRC10K */
  97.     CLK->PWRCON |= CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_OSC10K_EN_Msk;

  98.     /* Enable PLL and Set PLL frequency */
  99.     CLK->PLLCON = PLLCON_SETTING;

  100.     /* Waiting for clock ready */
  101.     CLK_WaitClockReady(CLK_CLKSTATUS_PLL_STB_Msk | CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_IRC10K_STB_Msk);

  102.     /* Switch HCLK clock source to PLL, STCLK to HCLK/2 */
  103.     CLK->CLKSEL0 = CLK_CLKSEL0_STCLK_S_HCLK_DIV2 | CLK_CLKSEL0_HCLK_S_PLL;

  104.     /* Enable peripheral clock */
  105.     CLK->APBCLK = CLK_APBCLK_UART0_EN_Msk |
  106.                   CLK_APBCLK_TMR0_EN_Msk | CLK_APBCLK_TMR1_EN_Msk | CLK_APBCLK_TMR2_EN_Msk | CLK_APBCLK_TMR3_EN_Msk;

  107.     /* Peripheral clock source */
  108.     CLK->CLKSEL1 = CLK_CLKSEL1_UART_S_PLL |
  109.                    CLK_CLKSEL1_TMR0_S_HXT | CLK_CLKSEL1_TMR1_S_HCLK | CLK_CLKSEL1_TMR1_S_HIRC | CLK_CLKSEL1_TMR3_S_HXT;

  110.     /* Update System Core Clock */
  111.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  112.     SystemCoreClockUpdate();

  113.     /*---------------------------------------------------------------------------------------------------------*/
  114.     /* Init I/O Multi-function                                                                                 */
  115.     /*---------------------------------------------------------------------------------------------------------*/
  116.     /* Set P3 multi-function pins for UART0 RXD, TXD, T0, T1 and T1EX */
  117.     SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0 | SYS_MFP_P34_T0 | SYS_MFP_P35_T1 | SYS_MFP_P33_T1EX;

  118.     /* Set P1 multi-function pins for T3 */
  119.     SYS->P1_MFP = SYS_MFP_P11_T3;
  120. }

  121. void UART0_Init(void)
  122. {
  123.     /*---------------------------------------------------------------------------------------------------------*/
  124.     /* Init UART                                                                                               */
  125.     /*---------------------------------------------------------------------------------------------------------*/
  126.     /* Reset IP */
  127.     SYS_ResetModule(UART0_RST);

  128.     /* Configure UART0 and set UART0 Baudrate */
  129.     UART_Open(UART0, 115200);
  130. }

  131. /*---------------------------------------------------------------------------------------------------------*/
  132. /*  MAIN function                                                                                          */
  133. /*---------------------------------------------------------------------------------------------------------*/
  134. int main(void)
  135. {
  136.     volatile uint32_t u32InitCount;

  137.     /* Unlock protected registers */
  138.     SYS_UnlockReg();

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

  141.     /* Lock protected registers */
  142.     SYS_LockReg();

  143.     /* Init UART0 for printf */
  144.     UART0_Init();

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

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

  157.     /* Initial Timer0 and Timer3 default setting */
  158.     TIMER_Open(TIMER0, TIMER_TOGGLE_MODE, 1000);
  159.     TIMER_Open(TIMER3, TIMER_TOGGLE_MODE, 2);

  160.     /* Initial Timer1 default setting */
  161.     TIMER_Open(TIMER1, TIMER_CONTINUOUS_MODE, 1);

  162.     /* Configure Timer1 setting for external counter input and capture function */
  163.     TIMER_SET_PRESCALE_VALUE(TIMER1, 0);
  164.     TIMER_SET_CMP_VALUE(TIMER1, 0xFFFFFF);
  165.     TIMER_EnableEventCounter(TIMER1, TIMER_COUNTER_FALLING_EDGE);
  166.     TIMER_EnableCapture(TIMER1, TIMER_CAPTURE_FREE_COUNTING_MODE, TIMER_CAPTURE_FALLING_EDGE);
  167.     TIMER_EnableCaptureInt(TIMER1);

  168.     /* Enable Timer1 NVIC */
  169.     NVIC_EnableIRQ(TMR1_IRQn);

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

  172.     /* Start Timer0, Timer1 and Timer3 counting */
  173.     TIMER_Start(TIMER0);
  174.     TIMER_Start(TIMER1);
  175.     TIMER_Start(TIMER3);

  176.     /* Check T1EX interrupt counts */
  177.     while(g_au32TMRINTCount[1] <= 10)
  178.     {
  179.         if(g_au32TMRINTCount[1] != u32InitCount)
  180.         {
  181.             printf("[%2d] - %4d\n", g_au32TMRINTCount[1], TIMER_GetCaptureData(TIMER1));
  182.             u32InitCount = g_au32TMRINTCount[1];
  183.         }
  184.     }

  185.     /* Stop Timer0, Timer1 and Timer3 counting */
  186.     TIMER_Close(TIMER0);
  187.     TIMER_Close(TIMER1);
  188.     TIMER_Close(TIMER3);

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

  190.     while(1);
  191. }

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


 楼主| dongnanxibei 发表于 2019-5-25 22:59 | 显示全部楼层
有多少人知道这两种捕获的区别的吗
 楼主| dongnanxibei 发表于 2019-5-25 23:00 | 显示全部楼层
PWM捕获
  1. /**************************************************************************//**
  2. * @file     main.c
  3. * @version  V1.00
  4. * $Revision: 4 $
  5. * $Date: 15/05/22 2:06p $
  6. * @brief    Capture the PWMB Channel 1 waveform by PWMB Channel 2.
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "M051Series.h"

  14. /*---------------------------------------------------------------------------------------------------------*/
  15. /* Macro, type and constant definitions                                                                    */
  16. /*---------------------------------------------------------------------------------------------------------*/

  17. #define PLLCON_SETTING      CLK_PLLCON_50MHz_HXT
  18. #define PLL_CLOCK           50000000

  19. /**
  20. * @brief       PWMA IRQ Handler
  21. *
  22. * @param       None
  23. *
  24. * @return      None
  25. *
  26. * @details     ISR to handle PWMA interrupt event
  27. */
  28. void PWMA_IRQHandler(void)
  29. {
  30.     /*In this sample code, user will not use PWMA channel 0~3 PIIR interrupt and CAPIF capture interrupt.
  31.       Defined following code as 0 for reducing interrupt execution time and code size. */
  32. #if 0
  33.     uint32_t u32PwmIntFlag, u32CapIntFlag0, u32CapIntFlag1;

  34.     /* Handle PWMA Capture function */
  35.     u32CapIntFlag0 = PWMA->CCR0;
  36.     u32CapIntFlag1 = PWMA->CCR2;

  37.     if(u32CapIntFlag0 & PWM_CCR0_CAPIF0_Msk)
  38.     {
  39.         PWMA->CCR0 &= (PWM_CCR_MASK | PWM_CCR0_CAPIF0_Msk);
  40.     }

  41.     else if(u32CapIntFlag0 & PWM_CCR0_CAPIF1_Msk)
  42.     {
  43.         PWMA->CCR0 &= (PWM_CCR_MASK | PWM_CCR0_CAPIF1_Msk);
  44.     }

  45.     else if(u32CapIntFlag1 & PWM_CCR2_CAPIF2_Msk)
  46.     {
  47.         PWMA->CCR2 &= (PWM_CCR_MASK | PWM_CCR2_CAPIF2_Msk);
  48.     }

  49.     else if(u32CapIntFlag1 & PWM_CCR2_CAPIF3_Msk)
  50.     {
  51.         PWMA->CCR2 &= (PWM_CCR_MASK | PWM_CCR2_CAPIF3_Msk);
  52.     }

  53.     /* Handle PWMA Timer function */
  54.     u32PwmIntFlag = PWMA->PIIR;

  55.     if(u32PwmIntFlag & PWM_PIIR_PWMIF0_Msk)
  56.     {
  57.         PWMA->PIIR = PWM_PIIR_PWMIF0_Msk;
  58.     }

  59.     else if(u32PwmIntFlag & PWM_PIIR_PWMIF1_Msk)
  60.     {
  61.         PWMA->PIIR = PWM_PIIR_PWMIF1_Msk;
  62.     }

  63.     else if(u32PwmIntFlag & PWM_PIIR_PWMIF2_Msk)
  64.     {
  65.         PWMA->PIIR = PWM_PIIR_PWMIF2_Msk;
  66.     }

  67.     else if(u32PwmIntFlag & PWM_PIIR_PWMIF3_Msk)
  68.     {
  69.         PWMA->PIIR = PWM_PIIR_PWMIF3_Msk;
  70.     }
  71. #endif
  72. }

  73. /**
  74. * @brief       PWMB IRQ Handler
  75. *
  76. * @param       None
  77. *
  78. * @return      None
  79. *
  80. * @details     ISR to handle PWMB interrupt event
  81. */
  82. void PWMB_IRQHandler(void)
  83. {
  84. //    uint32_t u32PwmIntFlag;
  85.     uint32_t u32CapIntFlag1;

  86.     /* Handle PWMB Capture function */
  87.     u32CapIntFlag1 = PWMB->CCR2;

  88.     /* PWMB channel 2 Capture interrupt */
  89.     if(u32CapIntFlag1 & PWM_CCR2_CAPIF2_Msk)
  90.     {
  91.         PWMB->CCR2 &= (PWM_CCR_MASK | PWM_CCR2_CAPIF2_Msk);
  92.     }
  93. }


  94. /*--------------------------------------------------------------------------------------*/
  95. /* Capture function to calculate the input waveform information                         */
  96. /* u32Count[4] : Keep the internal counter value when input signal rising / falling     */
  97. /*               happens                                                                */
  98. /*                                                                                      */
  99. /* time    A    B     C     D                                                           */
  100. /*           ___   ___   ___   ___   ___   ___   ___   ___                              */
  101. /*      ____|   |_|   |_|   |_|   |_|   |_|   |_|   |_|   |_____                        */
  102. /* index              0 1   2 3                                                         */
  103. /*                                                                                      */
  104. /* The capture internal counter down count from 0x10000, and reload to 0x10000 after    */
  105. /* input signal falling happens (Time B/C/D)                                            */
  106. /*--------------------------------------------------------------------------------------*/
  107. void CalPeriodTime(PWM_T *PWM, uint32_t u32Ch)
  108. {
  109.     uint16_t u32Count[4];
  110.     uint32_t u32i;
  111.     uint16_t u16RisingTime, u16FallingTime, u16HighPeroid, u16LowPeroid, u16TotalPeroid;

  112.     /* Clear Capture Falling Indicator (Time A) */
  113.     PWM_ClearCaptureIntFlag(PWM, u32Ch, PWM_CAPTURE_INT_FALLING_LATCH);

  114.     /* Wait for Capture Falling Indicator  */
  115.     while(PWM_GetCaptureIntFlag(PWM, u32Ch) < 2);

  116.     /* Clear Capture Falling Indicator (Time B)*/
  117.     PWM_ClearCaptureIntFlag(PWM, u32Ch, PWM_CAPTURE_INT_FALLING_LATCH);

  118.     u32i = 0;

  119.     while(u32i < 4)
  120.     {
  121.         /* Wait for Capture Falling Indicator */
  122.         while(PWM_GetCaptureIntFlag(PWM, u32Ch) < 2);

  123.         /* Clear Capture Falling and Rising Indicator */
  124.         PWM_ClearCaptureIntFlag(PWM, u32Ch, PWM_CAPTURE_INT_FALLING_LATCH | PWM_CAPTURE_INT_RISING_LATCH);

  125.         /* Get Capture Falling Latch Counter Data */
  126.         u32Count[u32i++] = PWM_GET_CAPTURE_FALLING_DATA(PWM, u32Ch);

  127.         /* Wait for Capture Rising Indicator */
  128.         while(PWM_GetCaptureIntFlag(PWM, u32Ch) < 2);

  129.         /* Clear Capture Rising Indicator */
  130.         PWM_ClearCaptureIntFlag(PWM, u32Ch, PWM_CAPTURE_INT_RISING_LATCH);

  131.         /* Get Capture Rising Latch Counter Data */
  132.         u32Count[u32i++] = PWM_GET_CAPTURE_RISING_DATA(PWM, u32Ch);
  133.     }

  134.     u16RisingTime = u32Count[1];

  135.     u16FallingTime = u32Count[0];

  136.     u16HighPeroid = u32Count[1] - u32Count[2];

  137.     u16LowPeroid = 0x10000 - u32Count[1];

  138.     u16TotalPeroid = 0x10000 - u32Count[2];

  139.     printf("\nPWM generate: \nHigh Period=7199 ~ 7201, Low Period=16799 ~ 16801, Total Period=23999 ~ 24001\n");
  140.     printf("\nCapture Result: Rising Time = %d, Falling Time = %d \nHigh Period = %d, Low Period = %d, Total Period = %d.\n\n",
  141.            u16RisingTime, u16FallingTime, u16HighPeroid, u16LowPeroid, u16TotalPeroid);
  142.     if((u16HighPeroid < 7199) || (u16HighPeroid > 7201) || (u16LowPeroid < 16799) || (u16LowPeroid > 16801) || (u16TotalPeroid < 23999) || (u16TotalPeroid > 24001))
  143.         printf("Capture Test Fail!!\n");
  144.     else
  145.         printf("Capture Test Pass!!\n");
  146. }


  147. void SYS_Init(void)
  148. {
  149.     /*---------------------------------------------------------------------------------------------------------*/
  150.     /* Init System Clock                                                                                       */
  151.     /*---------------------------------------------------------------------------------------------------------*/

  152.     /* Enable Internal RC clock */
  153.     CLK->PWRCON |= CLK_PWRCON_OSC22M_EN_Msk;

  154.     /* Waiting for IRC22M clock ready */
  155.     while(!(CLK->CLKSTATUS & CLK_CLKSTATUS_OSC22M_STB_Msk));

  156.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  157.     CLK->CLKSEL0 &= ~CLK_CLKSEL0_HCLK_S_Msk;
  158.     CLK->CLKSEL0 |= CLK_CLKSEL0_HCLK_S_HIRC;
  159.     CLK->CLKDIV &= ~CLK_CLKDIV_HCLK_N_Msk;

  160.     /* Set PLL to power down mode and PLL_STB bit in CLKSTATUS register will be cleared by hardware.*/
  161.     CLK->PLLCON |= CLK_PLLCON_PD_Msk;

  162.     /* Enable external 12MHz XTAL, internal 22.1184MHz */
  163.     CLK->PWRCON |= CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_OSC22M_EN_Msk;

  164.     /* Enable PLL and Set PLL frequency */
  165.     CLK->PLLCON = PLLCON_SETTING;

  166.     /* Waiting for clock ready */
  167.     while(!(CLK->CLKSTATUS & (CLK_CLKSTATUS_PLL_STB_Msk | CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_OSC22M_STB_Msk)));

  168.     /* Switch HCLK clock source to PLL, STCLK to HCLK/2 */
  169.     CLK->CLKSEL0 = CLK_CLKSEL0_STCLK_S_HCLK_DIV2 | CLK_CLKSEL0_HCLK_S_PLL;

  170.     /* Enable IP clock */
  171.     CLK->APBCLK = CLK_APBCLK_UART0_EN_Msk | CLK_APBCLK_PWM45_EN_Msk | CLK_APBCLK_PWM67_EN_Msk;

  172.     /* IP clock source */
  173.     CLK->CLKSEL1 = CLK_CLKSEL1_UART_S_PLL;

  174.     /* IP clock source */
  175.     CLK->CLKSEL2 = CLK_CLKSEL2_PWM45_S_HXT | CLK_CLKSEL2_PWM67_S_HXT;

  176.     /* Reset PWMB channel0~channel3 */
  177.     SYS->IPRSTC2 = SYS_IPRSTC2_PWM47_RST_Msk;
  178.     SYS->IPRSTC2 = 0;

  179.     /* Update System Core Clock */
  180.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  181.     //SystemCoreClockUpdate();
  182.     PllClock        = PLL_CLOCK;            // PLL
  183.     SystemCoreClock = PLL_CLOCK / 1;        // HCLK
  184.     CyclesPerUs     = PLL_CLOCK / 1000000;  // For SYS_SysTickDelay()

  185.     /*---------------------------------------------------------------------------------------------------------*/
  186.     /* Init I/O Multi-function                                                                                 */
  187.     /*---------------------------------------------------------------------------------------------------------*/
  188.     /* Set P3 multi-function pins for UART0 RXD and TXD  */
  189.     SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;
  190.     /* Set P2 multi-function pins for PWMB Channel0~3  */
  191.     SYS->P2_MFP = SYS_MFP_P25_PWM5 | SYS_MFP_P26_PWM6;
  192. }


  193. void UART0_Init(void)
  194. {

  195.     /*---------------------------------------------------------------------------------------------------------*/
  196.     /* Init UART                                                                                               */
  197.     /*---------------------------------------------------------------------------------------------------------*/
  198.     UART0->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(PLL_CLOCK, 115200);
  199.     UART0->LCR = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
  200. }



  201. /*---------------------------------------------------------------------------------------------------------*/
  202. /*  Main Function                                                                                          */
  203. /*---------------------------------------------------------------------------------------------------------*/
  204. int32_t main(void)
  205. {
  206.     /* Unlock protected registers */
  207.     SYS_UnlockReg();

  208.     /* Init System, IP clock and multi-function I/O */
  209.     SYS_Init();

  210.     /* Lock protected registers */
  211.     SYS_LockReg();

  212.     /* Init UART0 for printf */
  213.     UART0_Init();

  214.     printf("+------------------------------------------------------------------------+\n");
  215.     printf("|                          PWM Driver Sample Code                        |\n");
  216.     printf("|                                                                        |\n");
  217.     printf("+------------------------------------------------------------------------+\n");
  218.     printf("  This sample code will use PWMB channel 2 to capture\n  the signal from PWMB channel 1.\n");
  219.     printf("  I/O configuration:\n");
  220.     printf("    PWM5(P2.5 PWMB channel 1) <--> PWM6(P2.6 PWMB channel 2)\n\n");
  221.     printf("Use PWMB Channel 2(P2.6) to capture the PWMB Channel 1(P2.5) Waveform\n");

  222.     while(1)
  223.     {
  224.         printf("Press any key to start PWM Capture Test\n");
  225.         getchar();

  226.         /*--------------------------------------------------------------------------------------*/
  227.         /* Set the PWMB Channel 1 as PWM output function.                                               */
  228.         /*--------------------------------------------------------------------------------------*/

  229.         /* Assume PWM output frequency is 250Hz and duty ratio is 30%, user can calculate PWM settings by follows.
  230.            duty ratio = (CMR+1)/(CNR+1)
  231.            cycle time = CNR+1
  232.            High level = CMR+1
  233.            PWM clock source frequency = __HXT = 12000000
  234.            (CNR+1) = PWM clock source frequency/prescaler/clock source divider/PWM output frequency
  235.                    = 12000000/2/1/250 = 24000
  236.            (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.)
  237.            CNR = 23999
  238.            duty ratio = 30% ==> (CMR+1)/(CNR+1) = 30%
  239.            CMR = 7199
  240.            Prescale value is 1 : prescaler= 2
  241.            Clock divider is PWM_CSR_DIV1 : clock divider =1
  242.         */

  243.         /* set PWMB channel 1 output configuration */
  244.         PWM_ConfigOutputChannel(PWMB, PWM_CH1, 250, 30);

  245.         /* Enable PWM Output path for PWMB channel 1 */
  246.         PWM_EnableOutput(PWMB, 0x2);

  247.         /* Enable Timer for PWMB channel 1 */
  248.         PWM_Start(PWMB, 0x2);

  249.         /*--------------------------------------------------------------------------------------*/
  250.         /* Set the PWMB channel 2  for capture function                                         */
  251.         /*--------------------------------------------------------------------------------------*/

  252.         /* If input minimum frequency is 250Hz, user can calculate capture settings by follows.
  253.            Capture clock source frequency = __HXT = 12000000 in the sample code.
  254.            (CNR+1) = Capture clock source frequency/prescaler/clock source divider/minimum input frequency
  255.                    = 12000000/2/1/250 = 24000
  256.            (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.)
  257.            CNR = 0xFFFF
  258.            (Note: In capture mode, user should set CNR to 0xFFFF to increase capture frequency range.)
  259.         */

  260.         /* set PWMB channel 2 capture configuration */
  261.         PWM_ConfigCaptureChannel(PWMB, PWM_CH2, 166, 0);

  262.         /* Enable capture falling edge interrupt for PWMB channel 2 */
  263.         PWM_EnableCaptureInt(PWMB, PWM_CH2, PWM_CAPTURE_INT_FALLING_LATCH);

  264.         /* Enable PWMB NVIC interrupt */
  265.         NVIC_EnableIRQ((IRQn_Type)(PWMB_IRQn));

  266.         /* Enable Timer for PWMB channel 2  */
  267.         PWM_Start(PWMB, 0x4);

  268.         /* Enable Capture Function for PWMB channel 2 */
  269.         PWM_EnableCapture(PWMB, 0x4);

  270.         /* Wait until PWMB channel 2 Timer start to count */
  271.         while(PWMB->PDR2 == 0);

  272.         /* Capture the Input Waveform Data */
  273.         CalPeriodTime(PWMB, PWM_CH2);
  274.         /*------------------------------------------------------------------------------------------------------*/
  275.         /* Stop PWMB channel 1 (Recommended procedure method 1)                                                 */
  276.         /* Set PWM Timer loaded value(CNR) as 0. When PWM internal counter(PDR) reaches to 0, disable PWM Timer */
  277.         /*------------------------------------------------------------------------------------------------------*/

  278.         /* Set PWMB channel 1 loaded value as 0 */
  279.         PWM_Stop(PWMB, 0x2);

  280.         /* Wait until PWMB channel 1 Timer Stop */
  281.         while(PWMB->PDR1 != 0);

  282.         /* Disable Timer for PWMB channel 1 */
  283.         PWM_ForceStop(PWMB, 0x2);

  284.         /* Disable PWM Output path for PWMB channel 1 */
  285.         PWM_DisableOutput(PWMB, 0x2);

  286.         /*------------------------------------------------------------------------------------------------------*/
  287.         /* Stop PWMB channel 2 (Recommended procedure method 1)                                                 */
  288.         /* Set PWM Timer loaded value(CNR) as 0. When PWM internal counter(PDR) reaches to 0, disable PWM Timer */
  289.         /*------------------------------------------------------------------------------------------------------*/

  290.         /* Disable PWMB NVIC */
  291.         NVIC_DisableIRQ((IRQn_Type)(PWMB_IRQn));

  292.         /* Set loaded value as 0 for PWMB channel 2 */
  293.         PWM_Stop(PWMB, 0x4);

  294.         /* Wait until PWMB channel 2 current counter reach to 0 */
  295.         while(PWMB->PDR2 != 0);

  296.         /* Disable Timer for PWMB channel 2 */
  297.         PWM_ForceStop(PWMB, 0x4);

  298.         /* Disable Capture Function and Capture Input path for  PWMB channel 2*/
  299.         PWM_DisableCapture(PWMB, 0x4);

  300.         /* Clear Capture Interrupt flag for PWMB channel 2*/
  301.         PWM_ClearCaptureIntFlag(PWMB, PWM_CH2, PWM_CAPTURE_INT_FALLING_LATCH);
  302.     }
  303. }




 楼主| dongnanxibei 发表于 2019-5-25 23:00 | 显示全部楼层
有大神能出来讲讲吗
734774645 发表于 2019-5-25 23:41 | 显示全部楼层
PWM定时器可复用为数字输入捕捉功能。如果捕捉功能使能,PWM的输出引脚将被切换至捕捉输入模
式。捕捉器0和PWM0使用同一个定时器,捕捉器1和PWM1使用另一组定时器,以此类推。因此在使用
捕捉功能之前,用户必须预先配置PMW定时器。捕捉功能使能后,捕捉器在输入通道有上升沿跳变
时,将PWM计数器的值锁存至捕捉上升沿锁存寄存器(CRLR),在输入通道有下降沿跳变时将PWM计数
器值锁存至捕捉下降沿锁存寄存器(CFLR)。捕捉通道0的中断是可编程的,通过设定.CRL_IE0 (CCR0
[1]) (上升沿锁存中断使能)和CFL_IE0(CCR0[2]) (下降沿锁存中断使能) 来决定中断发生的条件。 通过设
置.CRL_IE1(CCR0 [17])和.CRL_IE1(CCR0[18]),捕捉通道1有同样的特性。通过设置CCR2中的相应
的控制位,每组的通道2到通道3有同样的特性。 对于每一组,不管捕捉何时产生中断0/1/2/3,PWM计
数器0/1/2/3都将在该时刻重载。
最大的捕捉频率受捕捉中断延迟限制。捕捉中断发生时,软件至少要执行三个步骤:读PIIRx 以得到中
断源,读CRLRx/CFLRx(x=0~3) 以得到捕捉值,最后写1清PIIRx为0。 如果中断延迟要花时间T0完成,
在这段时间内(T0),捕捉信号一定不能翻转。在这种情况下,最大的捕捉频率将是1/T0。例如:
HCLK = 50 MHz, PWM_CLK = 25 MHz, 中断延迟时间 900 ns
因此最大的捕捉频率将是1/900ns ≈ 1000 kHz
734774645 发表于 2019-5-25 23:42 | 显示全部楼层
事件捕获功能
计数器提供输入捕获功能,当TxEx引脚(x= 0~3)上的信号发生改变时,可以捕获TDR的值存到
TCAP寄存器中。为了将TxEX引脚用作事件捕获,RSTCAPSEL (TEXCON[4])应该设为0并且定时
器时钟源应该选择HCLK。
软件可以通过TEXDB(TEXCON[6])位使能/关闭TxEx引脚的防弹跳功能。如果禁止防弹跳功能,输
入事件的频率应该小于HCLK 频率的1/3;如果使能防弹跳功能,输入事件的频率应该小于HCLK 频
率的1/8 。否则,捕获功能可能工作的不正常。软件通过TEX_EDGE (TEXCON[2:1])位可以选择检
测的TxEX引脚的边沿相位。
事件计数模式下,软件不必考虑计数器工作在哪种模式下,只要TxEX引脚上的信号发生变化(符合
用户设定),捕获事件就会发生。
QFGL 发表于 2019-11-13 12:40 | 显示全部楼层
看到PWM定时器捕获功能,为啥输入的频率是250,捕获频率是160,还有就是printf("\nPWM generate: \nHigh Period=7199 ~ 7201, Low Period=16799 ~ 16801, Total Period=23999 ~ 24001\n");
    printf("\nCapture Result: Rising Time = %d, Falling Time = %d \nHigh Period = %d, Low Period = %d, Total Period = %d.\n\n",
           u16RisingTime, u16FallingTime, u16HighPeroid, u16LowPeroid, u16TotalPeroid);
    if((u16HighPeroid < 7199) || (u16HighPeroid > 7201) || (u16LowPeroid < 16799) || (u16LowPeroid > 16801) || (u16TotalPeroid < 23999) || (u16TotalPeroid > 24001))
        printf("Capture Test Fail!!\n");
    else
        printf("Capture Test Pass!!\n");
,这样的脉宽是如何计算的,这个结果和示波器捕获天壤之别,完全不对呀,有懂的大神说一下
monitoring 发表于 2019-11-17 18:17 | 显示全部楼层
捕获也是需要响应时间的吧
zhuotuzi 发表于 2019-11-18 20:38 | 显示全部楼层
QFGL 发表于 2019-11-13 12:40
看到PWM定时器捕获功能,为啥输入的频率是250,捕获频率是160,还有就是printf("\nPWM generate: \nHigh Pe ...

有人给你那贴回复了。
zhuotuzi 发表于 2019-11-18 20:38 | 显示全部楼层
QFGL 发表于 2019-11-13 12:40
看到PWM定时器捕获功能,为啥输入的频率是250,捕获频率是160,还有就是printf("\nPWM generate: \nHigh Pe ...

测试结果打印什么信息了,测试失败?
jasontu 发表于 2019-11-19 08:38 | 显示全部楼层
補充一下,timer是24bit, pwm capture是16bit,能補抓的長度時間不同
其它就跟5,6樓的描述相同
 楼主| dongnanxibei 发表于 2019-11-20 09:51 | 显示全部楼层
QFGL 发表于 2019-11-13 12:40
看到PWM定时器捕获功能,为啥输入的频率是250,捕获频率是160,还有就是printf("\nPWM generate: \nHigh Pe ...

就是你捕获的跟实际的不一致?
yiy 发表于 2019-11-20 15:09 | 显示全部楼层
那个例子看的头疼,特别计算那,不仔细看,真难懂。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

225

主题

3848

帖子

18

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