[牛人杂谈] PWM外设不仅可以输出PWM波,还可以输入信号实现捕获测量

[复制链接]
2996|11
 楼主| gejigeji521 发表于 2024-9-9 21:58 | 显示全部楼层 |阅读模式
       捕获输入通道和PWM输出通道共用引脚和计数器。计数器可以是上或下计数方式。如果输入通道有上升沿或下降沿跳变时,捕获功能将PWM计数器值分别锁存到RCAPDATn (PWM_RCAPDATn[15:0]) 或FCAPDATn (PWM_FCAPDATn[15:0]) 寄存器。如果上升沿或下降沿锁存发生并且相应通道n的上升沿
或下降沿中断使能位被设置,即CAPRIENn (PWM_CAPIEN[5:0])设置上升沿中断使能或CAPFIENn(PWM_CAPIEN[13:8])设置下降沿中断使能,捕获功能将产生一个中断CAP_INT (使用PWM_INT向量)。当上升沿或下降沿锁存发生,相应的PWM计数器是否重载PWM_PERIODn值,取决于RCRLDENn或 FCRLDENn 的设置, RCRLDENn 和 FCRLDENn 分别位于 PWM_CAPCTL[21:16] 和PWM_CAPCTL[29:24]。
注:相应的GPIO引脚必须通过使能相应的捕获通道n来配置它的捕获功能,捕获通道使能设置位为CAPINENn (PWM_CAPINEN[5:0])。
8239066defe8611c52.png

在这个例子中,捕获计数器被设置成PWM下计数器类型,而且周期PERIOD被设置成8,所以这个计数器计数的方向是从上到下的,从8到0。当输入捕获引脚检测到一个下降沿时,捕获功能把计数器数值锁存到PWM_FCAPDATn中。当输入捕获引脚检测到一个上升沿,它锁存计数器数值到PWM_RCAPDATn中。在这个时序框图中,最开始检测到的是一个下降沿信号,因为使能了FCRLDENn,捕获器就会重新加载计数器的数值,数值为周期值(PERIOD)。但是在第二次下降沿时,计数器就不会重新被加载,这是因为关闭了FCRLDENn。在这个例子中,计数器在捕获到上升沿时也被重新加载了,原因是RCRLDENn也被使能了。
另外,这种情况如果是设为向上计数方式,计数器将重载零并向上计数到PERIOD值。
当上升沿在通道n被检测到时,相应位CRLIFn
(PWM_CAPIF[5:0])将被硬件置位。同样,通道n检测到下降沿时,相应位CFLIFn (PWM_CAPIF[13:8])被硬件置位。CRLIFn和CFLIFn可以通过软件写1清除。如果CRLIFn被置位并且CAPRIENn被使能,捕获功能将产生一个中断。如果CFLIFn被置位并且CAPFIENn被使能,捕获功能也将产生一个中断。
在本图没有描述的一个情况是:当CRLIF已经被置位了,如果上升锁存再次发生了,运行状态寄存器CRLIFOVn (PWM_CAPSTS[5:0])将通过硬件被置起,来指示CAPRIF超载。同理,当下降锁存再次发生,对于中断标志CFLIF和超载状态CFLIFOVn (BPWM_CAPSTS[13:8]),也会发生相同的硬件操作。
363166deff1fe6d01.png


 楼主| gejigeji521 发表于 2024-9-9 21:59 | 显示全部楼层
 楼主| gejigeji521 发表于 2024-9-9 21:59 | 显示全部楼层
另外捕获也可以支持DMA功能,这样就可以自动完成捕获数据转移了。
 楼主| gejigeji521 发表于 2024-9-9 22:03 | 显示全部楼层
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Revision: 9 $
  5. * $Date: 18/07/19 2:15p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Capture the PWM0 Channel 0 waveform by PWM0 Channel 2.
  7. *
  8. * SPDX-License-Identifier: Apache-2.0
  9. * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2018 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "NuMicro.h"

  13. /*---------------------------------------------------------------------------------------------------------*/
  14. /* Macro, type and constant definitions                                                                    */
  15. /*---------------------------------------------------------------------------------------------------------*/
  16. #define PLL_CLOCK           96000000

  17. /*---------------------------------------------------------------------------------------------------------*/
  18. /* Global variables                                                                                        */
  19. /*---------------------------------------------------------------------------------------------------------*/


  20. /*--------------------------------------------------------------------------------------*/
  21. /* Capture function to calculate the input waveform information                         */
  22. /* u32Count[4] : Keep the internal counter value when input signal rising / falling     */
  23. /*               happens                                                                */
  24. /*                                                                                      */
  25. /* time    A    B     C     D                                                           */
  26. /*           ___   ___   ___   ___   ___   ___   ___   ___                              */
  27. /*      ____|   |_|   |_|   |_|   |_|   |_|   |_|   |_|   |_____                        */
  28. /* index              0 1   2 3                                                         */
  29. /*                                                                                      */
  30. /* The capture internal counter down count from 0x10000, and reload to 0x10000 after    */
  31. /* input signal falling happens (Time B/C/D)                                            */
  32. /*--------------------------------------------------------------------------------------*/
  33. void CalPeriodTime(PWM_T *PWM, uint32_t u32Ch)
  34. {
  35.     uint16_t u32Count[4];
  36.     uint32_t u32i;
  37.     uint16_t u16RisingTime, u16FallingTime, u16HighPeriod, u16LowPeriod, u16TotalPeriod;
  38.     uint32_t u32TimeOutCount;

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

  41.     /* setup timeout */
  42.     u32TimeOutCount = SystemCoreClock;

  43.     /* Wait for Capture Falling Indicator  */
  44.     while((PWM0->CAPIF & PWM_CAPIF_CFLIF2_Msk) == 0)
  45.     {
  46.         if(u32TimeOutCount == 0)
  47.         {
  48.             printf("\nSomething is wrong, please check if pin connection is correct. \n");
  49.             while(1);
  50.         }
  51.         u32TimeOutCount--;
  52.     }

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

  55.     u32i = 0;

  56.     while(u32i < 4)
  57.     {
  58.         /* Wait for Capture Falling Indicator */
  59.         while(PWM_GetCaptureIntFlag(PWM, u32Ch) < 2);

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

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

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

  66.         /* Clear Capture Rising Indicator */
  67.         PWM_ClearCaptureIntFlag(PWM, u32Ch, PWM_CAPTURE_INT_RISING_LATCH);

  68.         /* Get Capture Rising Latch Counter Data */
  69.         u32Count[u32i++] = PWM_GET_CAPTURE_RISING_DATA(PWM, u32Ch);
  70.     }

  71.     u16RisingTime = u32Count[1];

  72.     u16FallingTime = u32Count[0];

  73.     u16HighPeriod = u32Count[1] - u32Count[2];

  74.     u16LowPeriod = 0x10000 - u32Count[1];

  75.     u16TotalPeriod = 0x10000 - u32Count[2];

  76.     printf("\nPWM generate: \nHigh Period=19199 ~ 19201, Low Period=44799 ~ 44801, Total Period=63999 ~ 64001\n");
  77.     printf("\nCapture Result: Rising Time = %d, Falling Time = %d \nHigh Period = %d, Low Period = %d, Total Period = %d.\n\n",
  78.            u16RisingTime, u16FallingTime, u16HighPeriod, u16LowPeriod, u16TotalPeriod);
  79.     if((u16HighPeriod < 19199) || (u16HighPeriod > 19201)
  80.             || (u16LowPeriod < 44799) || (u16LowPeriod > 44801)
  81.             || (u16TotalPeriod < 63999) || (u16TotalPeriod > 64001))
  82.         printf("Capture Test Fail!!\n");
  83.     else
  84.         printf("Capture Test Pass!!\n");
  85. }

  86. void SYS_Init(void)
  87. {
  88.     /*---------------------------------------------------------------------------------------------------------*/
  89.     /* Init System Clock                                                                                       */
  90.     /*---------------------------------------------------------------------------------------------------------*/
  91.     /* Enable HIRC clock */
  92.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  93.     /* Waiting for HIRC clock ready */
  94.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  95.     /* Switch HCLK clock source to HIRC and HCLK source divide 1 */
  96.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  97.     /* Select HIRC as the clock source of UART0 */
  98.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

  99.     /* Set core clock as PLL_CLOCK from PLL (no PLL in rev. B & C) */
  100. //    CLK_SetCoreClock(PLL_CLOCK);

  101.     /* Waiting for PLL clock ready */
  102. //    CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

  103.     /* Enable UART peripheral clock */
  104.     CLK_EnableModuleClock(UART0_MODULE);

  105.     /* Enable PWM0 module clock */
  106.     CLK_EnableModuleClock(PWM0_MODULE);

  107.     /*---------------------------------------------------------------------------------------------------------*/
  108.     /* PWM clock frequency configuration                                                                       */
  109.     /*---------------------------------------------------------------------------------------------------------*/
  110.     /* Select HCLK clock source as PLL and and HCLK clock divider as 2 */
  111. //    CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));

  112.     /* PWM clock frequency can be set equal or double to HCLK by choosing case 1 or case 2 */
  113.     /* case 1.PWM clock frequency is set equal to HCLK: select PWM module clock source as PCLK */
  114.     CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PCLK0, 0);

  115.     /* case 2.PWM clock frequency is set double to HCLK: select PWM module clock source as PLL */
  116. //    CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PLL, 0);
  117.     /*---------------------------------------------------------------------------------------------------------*/

  118.     /* Reset PWM0 module */
  119.     SYS_ResetModule(PWM0_RST);

  120.     /* Update System Core Clock */
  121.     SystemCoreClockUpdate();

  122.     /*---------------------------------------------------------------------------------------------------------*/
  123.     /* Init I/O Multi-function                                                                                 */
  124.     /*---------------------------------------------------------------------------------------------------------*/
  125.     /* Set PB multi-function pins for UART0 RXD=PB.12 and TXD=PB.13 */
  126.     SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk)) |
  127.                     (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);

  128.     /* Set PB multi-function pins for PWM0 Channel 0 and 2 */
  129.     SYS->GPB_MFPL = (SYS->GPB_MFPL & ~(SYS_GPB_MFPL_PB5MFP_Msk | SYS_GPB_MFPL_PB3MFP_Msk)) |
  130.                     (SYS_GPB_MFPL_PB5MFP_PWM0_CH0 | SYS_GPB_MFPL_PB3MFP_PWM0_CH2);
  131. }

  132. void UART0_Init()
  133. {
  134.     /*---------------------------------------------------------------------------------------------------------*/
  135.     /* Init UART                                                                                               */
  136.     /*---------------------------------------------------------------------------------------------------------*/
  137.     /* Reset UART module */
  138.     SYS_ResetModule(UART0_RST);

  139.     /* Configure UART0 and set UART0 baud rate */
  140.     UART_Open(UART0, 115200);
  141. }

  142. /*---------------------------------------------------------------------------------------------------------*/
  143. /*  Main Function                                                                                          */
  144. /*---------------------------------------------------------------------------------------------------------*/
  145. int32_t main(void)
  146. {
  147.     uint32_t u32TimeOutCount;

  148.     /* Init System, IP clock and multi-function I/O
  149.        In the end of SYS_Init() will issue SYS_LockReg()
  150.        to lock protected register. If user want to write
  151.        protected register, please issue SYS_UnlockReg()
  152.        to unlock protected register if necessary */

  153.     /* Unlock protected registers */
  154.     SYS_UnlockReg();

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

  157.     /* Lock protected registers */
  158.     SYS_LockReg();

  159.     /* Init UART to 115200-8n1 for print message */
  160.     UART0_Init();

  161.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz(PLL@ %dHz)\n", SystemCoreClock, PllClock);
  162.     printf("PWM0 clock is from %s\n", (CLK->CLKSEL2 & CLK_CLKSEL2_PWM0SEL_Msk) ? "PCLK" : "PLL");
  163.     printf("+------------------------------------------------------------------------+\n");
  164.     printf("|                          PWM Driver Sample Code                        |\n");
  165.     printf("|                                                                        |\n");
  166.     printf("+------------------------------------------------------------------------+\n");
  167.     printf("  This sample code will use PWM0 channel 2 to capture\n  the signal from PWM0 channel 0.\n");
  168.     printf("  I/O configuration:\n");
  169.     printf("    PWM0 channel 2(PB.3) <--> PWM0 channel 0(PB.5)\n\n");
  170.     printf("Use PWM0 Channel 2(PB.3) to capture the PWM0 Channel 0(PB.5) Waveform\n");

  171.     while(1)
  172.     {
  173.         printf("\n\nPress any key to start PWM Capture Test\n");
  174.         getchar();

  175.         /*--------------------------------------------------------------------------------------*/
  176.         /* Set the PWM0 Channel 0 as PWM output function.                                       */
  177.         /*--------------------------------------------------------------------------------------*/

  178.         /* Assume PWM output frequency is 250Hz and duty ratio is 30%, user can calculate PWM settings by follows.
  179.            duty ratio = (CMR+1)/(CNR+1)
  180.            cycle time = CNR+1
  181.            High level = CMR+1
  182.            PWM clock source frequency from PLL is 48,000,000
  183.            (CNR+1) = PWM clock source frequency/prescaler/PWM output frequency
  184.                    = 48,000,000/3/250 = 64,000
  185.            (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.)
  186.            CNR = 64,000
  187.            duty ratio = 30% ==> (CMR+1)/(CNR+1) = 30%
  188.            CMR = 19,200
  189.            Prescale value is 4 : prescaler= 5
  190.         */

  191.         /* set PWM0 channel 0 output configuration */
  192.         PWM_ConfigOutputChannel(PWM0, 0, 250, 30);

  193.         /* Enable PWM Output path for PWM0 channel 0 */
  194.         PWM_EnableOutput(PWM0, PWM_CH_0_MASK);

  195.         /* Enable Timer for PWM0 channel 0 */
  196.         PWM_Start(PWM0, PWM_CH_0_MASK);

  197.         /*--------------------------------------------------------------------------------------*/
  198.         /* Set the PWM0 channel 2 for capture function                                          */
  199.         /*--------------------------------------------------------------------------------------*/
  200.         /* If input minimum frequency is 250Hz, user can calculate capture settings by follows.
  201.            Capture clock source frequency = PLL = 48,000,000 in the sample code.
  202.            (CNR+1) = Capture clock source frequency/prescaler/minimum input frequency
  203.                    = 48,000,000/3/250 = 64,000
  204.            (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.)
  205.            CNR = 0xFFFF
  206.            (Note: In capture mode, user should set CNR to 0xFFFF to increase capture frequency range.)

  207.            Capture unit time = 1/Capture clock source frequency/prescaler
  208.            62.5ns = 1/48,000,000/3
  209.         */

  210.         /* set PWM0 channel 2 capture configuration */
  211.         PWM_ConfigCaptureChannel(PWM0, 2, 62, 0);

  212.         /* Enable Timer for PWM0 channel 2 */
  213.         PWM_Start(PWM0, PWM_CH_2_MASK);

  214.         /* Enable Capture Function for PWM0 channel 2 */
  215.         PWM_EnableCapture(PWM0, PWM_CH_2_MASK);

  216.         /* Enable falling capture reload */
  217.         PWM0->CAPCTL |= PWM_CAPCTL_FCRLDEN2_Msk;

  218.         /* setup timeout */
  219.         u32TimeOutCount = SystemCoreClock;

  220.         /* Wait until PWM0 channel 2 Timer start to count */
  221.         while((PWM0->CNT[2]) == 0)
  222.         {
  223.             if(u32TimeOutCount == 0)
  224.             {
  225.                 printf("PWM encounters some errors, please check it. \n");
  226.                 while(1);
  227.             }
  228.             u32TimeOutCount--;
  229.         }

  230.         /* Capture the Input Waveform Data */
  231.         CalPeriodTime(PWM0, 2);
  232.         /*---------------------------------------------------------------------------------------------------------*/
  233.         /* Stop PWM0 channel 0 (Recommended procedure method 1)                                                    */
  234.         /* Set PWM Timer loaded value(Period) as 0. When PWM internal counter(CNT) reaches to 0, disable PWM Timer */
  235.         /*---------------------------------------------------------------------------------------------------------*/
  236.         /* Set PWM0 channel 0 loaded value as 0 */
  237.         PWM_Stop(PWM0, PWM_CH_0_MASK);

  238.         /* Wait until PWM0 channel 0 Timer Stop */
  239.         while((PWM0->CNT[0] & PWM_CNT_CNT_Msk) != 0)
  240.         {
  241.             if(u32TimeOutCount == 0)
  242.             {
  243.                 printf("PWM encounters some errors, please check it. \n");
  244.                 while(1);
  245.             }
  246.             u32TimeOutCount--;
  247.         }

  248.         /* Disable Timer for PWM0 channel 0 */
  249.         PWM_ForceStop(PWM0, PWM_CH_0_MASK);

  250.         /* Disable PWM Output path for PWM0 channel 0 */
  251.         PWM_DisableOutput(PWM0, PWM_CH_0_MASK);

  252.         /*---------------------------------------------------------------------------------------------------------*/
  253.         /* Stop PWM0 channel 2 (Recommended procedure method 1)                                                    */
  254.         /* Set PWM Timer loaded value(Period) as 0. When PWM internal counter(CNT) reaches to 0, disable PWM Timer */
  255.         /*---------------------------------------------------------------------------------------------------------*/

  256.         /* Set loaded value as 0 for PWM0 channel 2 */
  257.         PWM_Stop(PWM0, PWM_CH_2_MASK);

  258.         /* setup timeout */
  259.         u32TimeOutCount = SystemCoreClock;

  260.         /* Wait until PWM0 channel 2 current counter reach to 0 */
  261.         while((PWM0->CNT[2] & PWM_CNT_CNT_Msk) != 0)
  262.         {
  263.             if(u32TimeOutCount == 0)
  264.             {
  265.                 printf("PWM encounters some errors, please check it. \n");
  266.                 while(1);
  267.             }
  268.             u32TimeOutCount--;
  269.         }

  270.         /* Disable Timer for PWM0 channel 2 */
  271.         PWM_ForceStop(PWM0, PWM_CH_2_MASK);

  272.         /* Disable Capture Function and Capture Input path for  PWM0 channel 2*/
  273.         PWM_DisableCapture(PWM0, PWM_CH_2_MASK);

  274.         /* Clear Capture Interrupt flag for PWM0 channel 2 */
  275.         PWM_ClearCaptureIntFlag(PWM0, 2, PWM_CAPTURE_INT_FALLING_LATCH);
  276.     }
  277. }
 楼主| gejigeji521 发表于 2024-9-9 22:04 | 显示全部楼层
接下来再展示一下捕获结果通过PDMA传输
  1. /**************************************************************************//**
  2. * @file     main.c
  3. * @version  V1.00
  4. * $Revision: 11 $
  5. * $Date: 18/07/19 2:18p $
  6. * @brief    Capture the PWM0 Channel 0 waveform by PWM0 Channel 2, and use PDMA to transfer captured data.
  7. *
  8. * SPDX-License-Identifier: Apache-2.0
  9. * @copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "NuMicro.h"

  13. /*---------------------------------------------------------------------------------------------------------*/
  14. /* Macro, type and constant definitions                                                                    */
  15. /*---------------------------------------------------------------------------------------------------------*/
  16. #define PLL_CLOCK       96000000

  17. /*---------------------------------------------------------------------------------------------------------*/
  18. /* Global variables                                                                                        */
  19. /*---------------------------------------------------------------------------------------------------------*/
  20. uint16_t g_au16Count[4];
  21. volatile uint32_t g_u32IsTestOver = 0;


  22. /**
  23. * @brief       PDMA IRQ Handler
  24. *
  25. * @param       None
  26. *
  27. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  28. *
  29. * [url=home.php?mod=space&uid=1543424]@Details[/url]     ISR to handle PDMA interrupt event
  30. */
  31. void PDMA_IRQHandler(void)
  32. {
  33.     uint32_t status = PDMA_GET_INT_STATUS(PDMA);

  34.     if(status & 0x1)    /* abort */
  35.     {
  36.         if(PDMA_GET_ABORT_STS(PDMA) & 0x1)
  37.             g_u32IsTestOver = 2;
  38.         PDMA_CLR_ABORT_FLAG(PDMA, PDMA_ABTSTS_ABTIF0_Msk);
  39.     }
  40.     else if(status & 0x2)      /* done */
  41.     {
  42.         if(PDMA_GET_TD_STS(PDMA) & 0x1)
  43.             g_u32IsTestOver = 1;
  44.         PDMA_CLR_TD_FLAG(PDMA, PDMA_TDSTS_TDIF0_Msk);
  45.     }
  46.     else
  47.         printf("unknown interrupt !!\n");
  48. }

  49. /*--------------------------------------------------------------------------------------*/
  50. /* Capture function to calculate the input waveform information                         */
  51. /* g_au16Count[4] : Keep the internal counter value when input signal rising / falling     */
  52. /*               happens                                                                */
  53. /*                                                                                      */
  54. /* time    A    B     C     D                                                           */
  55. /*           ___   ___   ___   ___   ___   ___   ___   ___                              */
  56. /*      ____|   |_|   |_|   |_|   |_|   |_|   |_|   |_|   |_____                        */
  57. /* index              0 1   2 3                                                         */
  58. /*                                                                                      */
  59. /* The capture internal counter down count from 0x10000, and reload to 0x10000 after    */
  60. /* input signal falling happens (Time B/C/D)                                            */
  61. /*--------------------------------------------------------------------------------------*/
  62. void CalPeriodTime(PWM_T *PWM, uint32_t u32Ch)
  63. {
  64.     uint16_t u16RisingTime, u16FallingTime, u16HighPeriod, u16LowPeriod, u16TotalPeriod;
  65.     uint32_t u32TimeOutCount;

  66.     g_u32IsTestOver = 0;

  67.     /* setup timeout */
  68.     u32TimeOutCount = SystemCoreClock;

  69.     /* Wait PDMA interrupt (g_u32IsTestOver will be set at IRQ_Handler function) */
  70.     while(g_u32IsTestOver == 0)
  71.     {
  72.         if(u32TimeOutCount == 0)
  73.         {
  74.             printf("\nSomething is wrong, please check if pin connection is correct. \n");
  75.             while(1);
  76.         }
  77.         u32TimeOutCount--;
  78.     }

  79.     u16RisingTime = g_au16Count[1];

  80.     u16FallingTime = g_au16Count[0];

  81.     u16HighPeriod = g_au16Count[1] - g_au16Count[2];

  82.     u16LowPeriod = 0x10000 - g_au16Count[1];

  83.     u16TotalPeriod = 0x10000 - g_au16Count[2];

  84.     printf("\nPWM generate: \nHigh Period=19199 ~ 19201, Low Period=44799 ~ 44801, Total Period=63999 ~ 64001\n");
  85.     printf("\nCapture Result: Rising Time = %d, Falling Time = %d \nHigh Period = %d, Low Period = %d, Total Period = %d.\n\n",
  86.            u16RisingTime, u16FallingTime, u16HighPeriod, u16LowPeriod, u16TotalPeriod);
  87.     if((u16HighPeriod < 19199) || (u16HighPeriod > 19201)
  88.             || (u16LowPeriod < 44799) || (u16LowPeriod > 44801)
  89.             || (u16TotalPeriod < 63999) || (u16TotalPeriod > 64001))
  90.         printf("Capture Test Fail!!\n");
  91.     else
  92.         printf("Capture Test Pass!!\n");
  93. }

  94. void SYS_Init(void)
  95. {
  96.     /*---------------------------------------------------------------------------------------------------------*/
  97.     /* Init System Clock                                                                                       */
  98.     /*---------------------------------------------------------------------------------------------------------*/
  99.     /* Enable HIRC clock */
  100.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  101.     /* Waiting for HIRC clock ready */
  102.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  103.     /* Switch HCLK clock source to HIRC and HCLK source divide 1 */
  104.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  105.     /* Select HIRC as the clock source of UART0 */
  106.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

  107.     /* Set core clock as PLL_CLOCK from PLL (no PLL in rev. B & C) */
  108. //    CLK_SetCoreClock(PLL_CLOCK);

  109.     /* Waiting for PLL clock ready */
  110. //    CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

  111.     /* Enable UART peripheral clock */
  112.     CLK_EnableModuleClock(UART0_MODULE);

  113.     /*---------------------------------------------------------------------------------------------------------*/
  114.     /* PWM clock frequency configuration                                                                       */
  115.     /*---------------------------------------------------------------------------------------------------------*/
  116.     /* Select HCLK clock source as PLL and and HCLK clock divider as 2 */
  117. //    CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));

  118.     /* PWM clock frequency can be set equal or double to HCLK by choosing case 1 or case 2 */
  119.     /* case 1.PWM clock frequency is set equal to HCLK: select PWM module clock source as PCLK */
  120.     CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PCLK0, 0);

  121.     /* case 2.PWM clock frequency is set double to HCLK: select PWM module clock source as PLL */
  122. //    CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PLL, 0);
  123.     /*---------------------------------------------------------------------------------------------------------*/

  124.     /* Enable PDMA module clock */
  125.     CLK_EnableModuleClock(PDMA_MODULE);

  126.     /* Enable PWM0 module clock */
  127.     CLK_EnableModuleClock(PWM0_MODULE);

  128.     /* Reset PWM0 module */
  129.     SYS_ResetModule(PWM0_RST);

  130.     /* Reset PDMA module */
  131.     SYS_ResetModule(PDMA_RST);

  132.     /* Update System Core Clock */
  133.     SystemCoreClockUpdate();

  134.     /*---------------------------------------------------------------------------------------------------------*/
  135.     /* Init I/O Multi-function                                                                                 */
  136.     /*---------------------------------------------------------------------------------------------------------*/
  137.     /* Set PB multi-function pins for UART0 RXD=PB.12 and TXD=PB.13 */
  138.     SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk)) |
  139.                     (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);

  140.     /* Set PB multi-function pins for PWM0 Channel 0 and 2 */
  141.     SYS->GPB_MFPL = (SYS->GPB_MFPL & ~(SYS_GPB_MFPL_PB5MFP_Msk | SYS_GPB_MFPL_PB3MFP_Msk)) |
  142.                     (SYS_GPB_MFPL_PB5MFP_PWM0_CH0 | SYS_GPB_MFPL_PB3MFP_PWM0_CH2);
  143. }

  144. void UART0_Init()
  145. {
  146.     /*---------------------------------------------------------------------------------------------------------*/
  147.     /* Init UART                                                                                               */
  148.     /*---------------------------------------------------------------------------------------------------------*/
  149.     /* Reset UART module */
  150.     SYS_ResetModule(UART0_RST);

  151.     /* Configure UART0 and set UART0 baud rate */
  152.     UART_Open(UART0, 115200);
  153. }

  154. /*---------------------------------------------------------------------------------------------------------*/
  155. /*  Main Function                                                                                          */
  156. /*---------------------------------------------------------------------------------------------------------*/
  157. int32_t main(void)
  158. {
  159.     uint32_t u32TimeOutCount;

  160.     /* Init System, IP clock and multi-function I/O
  161.        In the end of SYS_Init() will issue SYS_LockReg()
  162.        to lock protected register. If user want to write
  163.        protected register, please issue SYS_UnlockReg()
  164.        to unlock protected register if necessary */

  165.     /* Unlock protected registers */
  166.     SYS_UnlockReg();

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

  169.     /* Lock protected registers */
  170.     SYS_LockReg();

  171.     /* Init UART to 115200-8n1 for print message */
  172.     UART0_Init();

  173.     printf("\n\nCPU @ %dHz(PLL@ %dHz)\n", SystemCoreClock, PllClock);
  174.     printf("PWM0 clock is from %s\n", (CLK->CLKSEL2 & CLK_CLKSEL2_PWM0SEL_Msk) ? "PCLK" : "PLL");
  175.     printf("+------------------------------------------------------------------------+\n");
  176.     printf("|                          PWM Driver Sample Code                        |\n");
  177.     printf("|                                                                        |\n");
  178.     printf("+------------------------------------------------------------------------+\n");
  179.     printf("  This sample code will use PWM0 channel 2 to capture the signal from PWM0 channel 0.\n");
  180.     printf("  And the captured data is transferred by PDMA channel 0.\n");
  181.     printf("  I/O configuration:\n");
  182.     printf("    PWM0 channel 2(PB.3) <--> PWM0 channel 0(PB.5)\n\n");
  183.     printf("Use PWM0 Channel 2(PB.3) to capture the PWM0 Channel 0(PB.5) Waveform\n");

  184.     while(1)
  185.     {
  186.         printf("\n\nPress any key to start PWM Capture Test\n");
  187.         getchar();

  188.         /*--------------------------------------------------------------------------------------*/
  189.         /* Set the PWM0 Channel 0 as PWM output function.                                       */
  190.         /*--------------------------------------------------------------------------------------*/

  191.         /* Assume PWM output frequency is 250Hz and duty ratio is 30%, user can calculate PWM settings by follows.
  192.            duty ratio = (CMR+1)/(CNR+1)
  193.            cycle time = CNR+1
  194.            High level = CMR+1
  195.            PWM clock source frequency from PLL is 48,000,000
  196.            (CNR+1) = PWM clock source frequency/prescaler/PWM output frequency
  197.                    = 48,000,000/3/250 = 64,000
  198.            (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.)
  199.            CNR = 64,000
  200.            duty ratio = 30% ==> (CMR+1)/(CNR+1) = 30%
  201.            CMR = 19,200
  202.            Prescale value is 4 : prescaler= 5
  203.         */

  204.         /* Set PWM0 channel 0 output configuration */
  205.         PWM_ConfigOutputChannel(PWM0, 0, 250, 30);

  206.         /* Enable PWM Output path for PWM0 channel 0 */
  207.         PWM_EnableOutput(PWM0, PWM_CH_0_MASK);

  208.         /* Enable Timer for PWM0 channel 0 */
  209.         PWM_Start(PWM0, PWM_CH_0_MASK);

  210.         /*--------------------------------------------------------------------------------------*/
  211.         /* Configure PDMA peripheral mode form PWM to memory                                    */
  212.         /*--------------------------------------------------------------------------------------*/
  213.         /* Open Channel 0 */
  214.         PDMA_Open(PDMA, 0x1);

  215.         /* Transfer width is half word(16 bit) and transfer count is 4 */
  216.         PDMA_SetTransferCnt(PDMA, 0, PDMA_WIDTH_16, 4);

  217.         /* Set source address as PWM capture channel PDMA register(no increment) and destination address as g_au16Count array(increment) */
  218.         PDMA_SetTransferAddr(PDMA, 0, (uint32_t)&PWM0->PDMACAP2_3, PDMA_SAR_FIX, (uint32_t)&g_au16Count[0], PDMA_DAR_INC);

  219.         /* Select PDMA request source as PWM RX(PWM0 channel 2 should be PWM0 pair 2) */
  220.         PDMA_SetTransferMode(PDMA, 0, PDMA_PWM0_P2_RX, FALSE, 0);

  221.         /* Set PDMA as single request type for PWM */
  222.         PDMA_SetBurstType(PDMA, 0, PDMA_REQ_SINGLE, PDMA_BURST_4);

  223.         PDMA_EnableInt(PDMA, 0, PDMA_INT_TRANS_DONE);
  224.         NVIC_EnableIRQ(PDMA_IRQn);

  225.         /* Enable PDMA for PWM0 channel 2 capture function, and set capture order as falling first, */
  226.         /* And select capture mode as both rising and falling to do PDMA transfer. */
  227.         PWM_EnablePDMA(PWM0, 2, FALSE, PWM_CAPTURE_PDMA_RISING_FALLING_LATCH);

  228.         /*--------------------------------------------------------------------------------------*/
  229.         /* Set the PWM0 channel 2 for capture function                                          */
  230.         /*--------------------------------------------------------------------------------------*/
  231.         /* If input minimum frequency is 250Hz, user can calculate capture settings by follows.
  232.            Capture clock source frequency = PLL = 48,000,000 in the sample code.
  233.            (CNR+1) = Capture clock source frequency/prescaler/minimum input frequency
  234.                    = 48,000,000/3/250 = 64,000
  235.            (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.)
  236.            CNR = 0xFFFF
  237.            (Note: In capture mode, user should set CNR to 0xFFFF to increase capture frequency range.)

  238.            Capture unit time = 1/Capture clock source frequency/prescaler
  239.            62.5ns = 1/48,000,000/3
  240.         */

  241.         /* Set PWM0 channel 2 capture configuration */
  242.         PWM_ConfigCaptureChannel(PWM0, 2, 62, 0);

  243.         /* Enable Timer for PWM0 channel 2 */
  244.         PWM_Start(PWM0, PWM_CH_2_MASK);

  245.         /* Enable Capture Function for PWM0 channel 2 */
  246.         PWM_EnableCapture(PWM0, PWM_CH_2_MASK);

  247.         /* Enable falling capture reload */
  248.         PWM0->CAPCTL |= PWM_CAPCTL_FCRLDEN2_Msk;

  249.         /* setup timeout */
  250.         u32TimeOutCount = SystemCoreClock;

  251.         /* Wait until PWM0 channel 2 Timer start to count */
  252.         while((PWM0->CNT[2]) == 0)
  253.         {
  254.             if(u32TimeOutCount == 0)
  255.             {
  256.                 printf("PWM encounters some errors, please check it. \n");
  257.                 while(1);
  258.             }
  259.             u32TimeOutCount--;
  260.         }

  261.         /* Capture the Input Waveform Data */
  262.         CalPeriodTime(PWM0, 2);
  263.         /*---------------------------------------------------------------------------------------------------------*/
  264.         /* Stop PWM0 channel 0 (Recommended procedure method 1)                                                    */
  265.         /* Set PWM Timer loaded value(Period) as 0. When PWM internal counter(CNT) reaches to 0, disable PWM Timer */
  266.         /*---------------------------------------------------------------------------------------------------------*/

  267.         /* Set PWM0 channel 0 loaded value as 0 */
  268.         PWM_Stop(PWM0, PWM_CH_0_MASK);

  269.         /* Wait until PWM0 channel 0 Timer Stop */
  270.         while((PWM0->CNT[0] & PWM_CNT_CNT_Msk) != 0)
  271.         {
  272.             if(u32TimeOutCount == 0)
  273.             {
  274.                 printf("PWM encounters some errors, please check it. \n");
  275.                 while(1);
  276.             }
  277.             u32TimeOutCount--;
  278.         }

  279.         /* Disable Timer for PWM0 channel 0 */
  280.         PWM_ForceStop(PWM0, PWM_CH_0_MASK);

  281.         /* Disable PWM Output path for PWM0 channel 0 */
  282.         PWM_DisableOutput(PWM0, PWM_CH_0_MASK);

  283.         /*---------------------------------------------------------------------------------------------------------*/
  284.         /* Stop PWM0 channel 2 (Recommended procedure method 1)                                                    */
  285.         /* Set PWM Timer loaded value(Period) as 0. When PWM internal counter(CNT) reaches to 0, disable PWM Timer */
  286.         /*---------------------------------------------------------------------------------------------------------*/

  287.         /* Set loaded value as 0 for PWM0 channel 2 */
  288.         PWM_Stop(PWM0, PWM_CH_2_MASK);

  289.         /* Wait until PWM0 channel 2 current counter reach to 0 */
  290.         while((PWM0->CNT[2] & PWM_CNT_CNT_Msk) != 0)
  291.         {
  292.             if(u32TimeOutCount == 0)
  293.             {
  294.                 printf("PWM encounters some errors, please check it. \n");
  295.                 while(1);
  296.             }
  297.             u32TimeOutCount--;
  298.         }

  299.         /* Disable Timer for PWM0 channel 2 */
  300.         PWM_ForceStop(PWM0, PWM_CH_2_MASK);

  301.         /* Disable Capture Function and Capture Input path for  PWM0 channel 2*/
  302.         PWM_DisableCapture(PWM0, PWM_CH_2_MASK);

  303.         /* Clear Capture Interrupt flag for PWM0 channel 2 */
  304.         PWM_ClearCaptureIntFlag(PWM0, 2, PWM_CAPTURE_INT_FALLING_LATCH);

  305.         /* Disable PDMA NVIC */
  306.         NVIC_DisableIRQ(PDMA_IRQn);

  307.         PDMA_Close(PDMA);
  308.     }
  309. }
AloneKaven 发表于 2024-9-11 21:30 | 显示全部楼层
之前使用外部计数模式做过,测量范围很高的
花开了相爱吧 发表于 2025-8-30 13:38 | 显示全部楼层
PWM 外设功能多样,除输出 PWM 波用于电机调速、灯光调光等,还能通过输入捕获功能,测量外部信号的频率、占空比等参数,拓展应用场景。
今夜限定月光 发表于 2025-9-23 18:35 | 显示全部楼层
PWM 外设的捕获功能能测量输入信号参数,先配置对应 IO 为捕获模式并选触发边沿。当检测到信号边沿时,会记录定时器当前值,通过两次捕获值差计算周期(频率),单周期内高低电平捕获值差算占空比。可用于测量电机转速、传感器脉冲信号等,无需额外硬件,简化电路设计。
我趴在云边 发表于 2025-9-26 13:52 | 显示全部楼层
PWM 外设可配置为输入捕获模式,通过检测外部信号的跳变沿(上升 / 下降)触发捕获。此时计数器值会被锁存,通过计算两次捕获的时间差,可测得信号周期、频率及脉冲宽度。该功能扩展了 PWM 应用,适用于转速测量、传感器信号解析等场景,无需额外硬件。


灵犀幻影 发表于 2025-9-29 13:53 | 显示全部楼层
我注意到了,当捕获功能检测到上升沿或下降沿时,可以产生中断,这对于实时控制应用来说非常有用。
暗夜幽灵骑士 发表于 2025-9-29 17:24 | 显示全部楼层
学习了,原来PWM外设不仅可以输出PWM波,还可以通过捕获功能来测量输入信号
野玫瑰 发表于 2025-10-2 11:43 | 显示全部楼层
PWM 外设通过输入捕获功能可测量外部信号。配置为输入模式时,能捕获信号边沿(上升 / 下降沿),记录对应时刻计时器值,计算周期(两同边沿间隔)和占空比(高低电平时间比)。适用于转速测量、脉冲计数等场景,无需额外硬件,简化电路设计。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

196

主题

2465

帖子

8

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