[技术问答] M451,PWM捕获外部脉冲,不用PDMA的有没有?

[复制链接]
1285|10
 楼主| newiot 发表于 2019-2-16 15:53 | 显示全部楼层 |阅读模式
M451,PWM捕获外部脉冲,不用PDMA的有没有?
另外,如果有2个PWM通道捕获外部脉冲,还能用PDMA吗?
643757107 发表于 2019-2-16 22:58 | 显示全部楼层
没看明白,PWM跟DMA是啥关系。
643757107 发表于 2019-2-16 23:00 | 显示全部楼层
  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: 15/09/02 10:04a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Capture the PWM1 Channel 0 waveform by PWM1 Channel 2.
  7. * @note
  8. * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved.
  9. *
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "M451Series.h"

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

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

  20. /**
  21. * @brief       PWM1 IRQ Handler
  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]     ISR to handle PWM1 interrupt event
  28. */
  29. void PWM1P1_IRQHandler(void)
  30. {
  31.     if(PWM_GetCaptureIntFlag(PWM1, 2) > 1)
  32.     {
  33.         PWM_ClearCaptureIntFlag(PWM1, 2, PWM_CAPTURE_INT_FALLING_LATCH);
  34.     }
  35. }

  36. /*--------------------------------------------------------------------------------------*/
  37. /* Capture function to calculate the input waveform information                         */
  38. /* u32Count[4] : Keep the internal counter value when input signal rising / falling     */
  39. /*               happens                                                                */
  40. /*                                                                                      */
  41. /* time    A    B     C     D                                                           */
  42. /*           ___   ___   ___   ___   ___   ___   ___   ___                              */
  43. /*      ____|   |_|   |_|   |_|   |_|   |_|   |_|   |_|   |_____                        */
  44. /* index              0 1   2 3                                                         */
  45. /*                                                                                      */
  46. /* The capture internal counter down count from 0x10000, and reload to 0x10000 after    */
  47. /* input signal falling happens (Time B/C/D)                                            */
  48. /*--------------------------------------------------------------------------------------*/
  49. void CalPeriodTime(PWM_T *PWM, uint32_t u32Ch)
  50. {
  51.     uint16_t u32Count[4];
  52.     uint32_t u32i;
  53.     uint16_t u16RisingTime, u16FallingTime, u16HighPeriod, u16LowPeriod, u16TotalPeriod;

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

  56.     /* Wait for Capture Falling Indicator  */
  57.     while((PWM1->CAPIF & PWM_CAPIF_CFLIF2_Msk) == 0);

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

  60.     u32i = 0;

  61.     while(u32i < 4)
  62.     {
  63.         /* Wait for Capture Falling Indicator */
  64.         while(PWM_GetCaptureIntFlag(PWM, u32Ch) < 2);

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

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

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

  71.         /* Clear Capture Rising Indicator */
  72.         PWM_ClearCaptureIntFlag(PWM, u32Ch, PWM_CAPTURE_INT_RISING_LATCH);

  73.         /* Get Capture Rising Latch Counter Data */
  74.         u32Count[u32i++] = PWM_GET_CAPTURE_RISING_DATA(PWM, u32Ch);
  75.     }

  76.     u16RisingTime = u32Count[1];

  77.     u16FallingTime = u32Count[0];

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

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

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

  81.     printf("\nPWM generate: \nHigh Period=17279 ~ 17281, Low Period=40319 ~ 40321, Total Period=57599 ~ 57601\n");
  82.     printf("\nCapture Result: Rising Time = %d, Falling Time = %d \nHigh Period = %d, Low Period = %d, Total Period = %d.\n\n",
  83.            u16RisingTime, u16FallingTime, u16HighPeriod, u16LowPeriod, u16TotalPeriod);
  84.     if((u16HighPeriod < 17279) || (u16HighPeriod > 17281) || (u16LowPeriod < 40319) || (u16LowPeriod > 40321) || (u16TotalPeriod < 57599) || (u16TotalPeriod > 57601))
  85.         printf("Capture Test Fail!!\n");
  86.     else
  87.         printf("Capture Test Pass!!\n");
  88. }

  89. void SYS_Init(void)
  90. {
  91.     /*---------------------------------------------------------------------------------------------------------*/
  92.     /* Init System Clock                                                                                       */
  93.     /*---------------------------------------------------------------------------------------------------------*/
  94.     /* Enable HIRC clock (Internal RC 22.1184MHz) */
  95.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  96.     /* Waiting for HIRC clock ready */
  97.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  98.     /* Select HCLK clock source as HIRC and and HCLK clock divider as 1 */
  99.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  100.     /* Enable HXT clock (external XTAL 12MHz) */
  101.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  102.     /* Waiting for HXT clock ready */
  103.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  104.     /* Set core clock as PLL_CLOCK from PLL */
  105.     CLK_SetCoreClock(PLL_CLOCK);

  106.     /* Waiting for PLL clock ready */
  107.     CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

  108.     /* Enable PWM1 module clock */
  109.     CLK_EnableModuleClock(PWM1_MODULE);

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

  115.     /* PWM clock frequency can be set equal or double to HCLK by choosing case 1 or case 2 */
  116.     /* case 1.PWM clock frequency is set equal to HCLK: select PWM module clock source as PCLK */
  117.     CLK_SetModuleClock(PWM1_MODULE, CLK_CLKSEL2_PWM1SEL_PCLK1, NULL);

  118.     /* case 2.PWM clock frequency is set double to HCLK: select PWM module clock source as PLL */
  119.     //CLK_SetModuleClock(PWM1_MODULE, CLK_CLKSEL2_PWM1SEL_PLL, NULL);
  120.     /*---------------------------------------------------------------------------------------------------------*/

  121.     /* Enable UART module clock */
  122.     CLK_EnableModuleClock(UART0_MODULE);

  123.     /* Select UART module clock source as HXT and UART module clock divider as 1 */
  124.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));

  125.     /* Reset PWM1 module */
  126.     SYS_ResetModule(PWM1_RST);

  127.     /* Update System Core Clock */
  128.     SystemCoreClockUpdate();

  129.     /*---------------------------------------------------------------------------------------------------------*/
  130.     /* Init I/O Multi-function                                                                                 */
  131.     /*---------------------------------------------------------------------------------------------------------*/
  132.     /* Set PD multi-function pins for UART0 RXD and TXD */
  133.     SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD0MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
  134.     SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);

  135.     /* Set PC multi-function pins for PWM1 Channel 0 and 2 */
  136.     SYS->GPC_MFPL = (SYS->GPC_MFPL & (~SYS_GPC_MFPL_PC6MFP_Msk));
  137.     SYS->GPC_MFPL |= SYS_GPC_MFPL_PC6MFP_PWM1_CH0;
  138.     SYS->GPC_MFPH = (SYS->GPC_MFPH & (~SYS_GPC_MFPH_PC11MFP_Msk));
  139.     SYS->GPC_MFPH |= SYS_GPC_MFPH_PC11MFP_PWM1_CH2;
  140. }

  141. void UART0_Init()
  142. {
  143.     /*---------------------------------------------------------------------------------------------------------*/
  144.     /* Init UART                                                                                               */
  145.     /*---------------------------------------------------------------------------------------------------------*/
  146.     /* Reset UART module */
  147.     SYS_ResetModule(UART0_RST);

  148.     /* Configure UART0 and set UART0 baud rate */
  149.     UART_Open(UART0, 115200);
  150. }


  151. /*---------------------------------------------------------------------------------------------------------*/
  152. /*  Main Function                                                                                          */
  153. /*---------------------------------------------------------------------------------------------------------*/
  154. int32_t main(void)
  155. {
  156.     /* Init System, IP clock and multi-function I/O
  157.        In the end of SYS_Init() will issue SYS_LockReg()
  158.        to lock protected register. If user want to write
  159.        protected register, please issue SYS_UnlockReg()
  160.        to unlock protected register if necessary */

  161.     /* Unlock protected registers */
  162.     SYS_UnlockReg();

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

  165.     /* Lock protected registers */
  166.     SYS_LockReg();

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

  169.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz(PLL@ %dHz)\n", SystemCoreClock, PllClock);
  170.     printf("PWM1 clock is from %s\n", (CLK->CLKSEL2 & CLK_CLKSEL2_PWM1SEL_Msk) ? "CPU" : "PLL");
  171.     printf("+------------------------------------------------------------------------+\n");
  172.     printf("|                          PWM Driver Sample Code                        |\n");
  173.     printf("|                                                                        |\n");
  174.     printf("+------------------------------------------------------------------------+\n");
  175.     printf("  This sample code will use PWM1 channel 2 to capture\n  the signal from PWM1 channel 0.\n");
  176.     printf("  I/O configuration:\n");
  177.     printf("    PWM1 channel 2(PC.11) <--> PWM1 channel 0(PC.6)\n\n");
  178.     printf("Use PWM1 Channel 2(PC.11) to capture the PWM1 Channel 0(PC.6) Waveform\n");

  179.     while(1)
  180.     {
  181.         printf("\n\nPress any key to start PWM Capture Test\n");
  182.         getchar();

  183.         /*--------------------------------------------------------------------------------------*/
  184.         /* Set the PWM1 Channel 0 as PWM output function.                                       */
  185.         /*--------------------------------------------------------------------------------------*/

  186.         /* Assume PWM output frequency is 250Hz and duty ratio is 30%, user can calculate PWM settings by follows.
  187.            duty ratio = (CMR+1)/(CNR+1)
  188.            cycle time = CNR+1
  189.            High level = CMR+1
  190.            PWM clock source frequency = PLL = 72000000
  191.            (CNR+1) = PWM clock source frequency/prescaler/PWM output frequency
  192.                    = 72000000/5/250 = 57600
  193.            (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.)
  194.            CNR = 57599
  195.            duty ratio = 30% ==> (CMR+1)/(CNR+1) = 30%
  196.            CMR = 17279
  197.            Prescale value is 4 : prescaler= 5
  198.         */

  199.         /* set PWM1 channel 0 output configuration */
  200.         PWM_ConfigOutputChannel(PWM1, 0, 250, 30);

  201.         /* Enable PWM Output path for PWM1 channel 0 */
  202.         PWM_EnableOutput(PWM1, PWM_CH_0_MASK);

  203.         /* Enable Timer for PWM1 channel 0 */
  204.         PWM_Start(PWM1, PWM_CH_0_MASK);

  205.         /*--------------------------------------------------------------------------------------*/
  206.         /* Set the PWM1 channel 2 for capture function                                          */
  207.         /*--------------------------------------------------------------------------------------*/

  208.         /* If input minimum frequency is 250Hz, user can calculate capture settings by follows.
  209.            Capture clock source frequency = PLL = 72000000 in the sample code.
  210.            (CNR+1) = Capture clock source frequency/prescaler/minimum input frequency
  211.                    = 72000000/5/250 = 57600
  212.            (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.)
  213.            CNR = 0xFFFF
  214.            (Note: In capture mode, user should set CNR to 0xFFFF to increase capture frequency range.)

  215.            Capture unit time = 1/Capture clock source frequency/prescaler
  216.            69.4ns = 1/72000000/5
  217.         */

  218.         /* set PWM1 channel 2 capture configuration */
  219.         PWM_ConfigCaptureChannel(PWM1, 2, 69, 0);

  220.         /* Enable capture falling edge interrupt for PWM1 channel 2 */
  221.         //PWM_EnableCaptureInt(PWM1, 2, PWM_CAPTURE_INT_FALLING_LATCH);

  222.         /* Enable PWM1 NVIC interrupt */
  223.         //NVIC_EnableIRQ(PWM1P1_IRQn);

  224.         /* Enable Timer for PWM1 channel 2 */
  225.         PWM_Start(PWM1, PWM_CH_2_MASK);

  226.         /* Enable Capture Function for PWM1 channel 2 */
  227.         PWM_EnableCapture(PWM1, PWM_CH_2_MASK);

  228.         /* Enable falling capture reload */
  229.         PWM1->CAPCTL |= PWM_CAPCTL_FCRLDEN2_Msk;

  230.         /* Wait until PWM1 channel 2 Timer start to count */
  231.         while((PWM1->CNT[2]) == 0);

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

  238.         /* Set PWM1 channel 0 loaded value as 0 */
  239.         PWM_Stop(PWM1, PWM_CH_0_MASK);

  240.         /* Wait until PWM1 channel 0 Timer Stop */
  241.         while((PWM1->CNT[0] & PWM_CNT_CNT_Msk) != 0);

  242.         /* Disable Timer for PWM1 channel 0 */
  243.         PWM_ForceStop(PWM1, PWM_CH_0_MASK);

  244.         /* Disable PWM Output path for PWM1 channel 0 */
  245.         PWM_DisableOutput(PWM1, PWM_CH_0_MASK);

  246.         /*---------------------------------------------------------------------------------------------------------*/
  247.         /* Stop PWM1 channel 2 (Recommended procedure method 1)                                                    */
  248.         /* Set PWM Timer loaded value(Period) as 0. When PWM internal counter(CNT) reaches to 0, disable PWM Timer */
  249.         /*---------------------------------------------------------------------------------------------------------*/

  250.         /* Disable PWM1 NVIC */
  251.         //NVIC_DisableIRQ(PWM1P1_IRQn);

  252.         /* Set loaded value as 0 for PWM1 channel 2 */
  253.         PWM_Stop(PWM1, PWM_CH_2_MASK);

  254.         /* Wait until PWM1 channel 2 current counter reach to 0 */
  255.         while((PWM1->CNT[2] & PWM_CNT_CNT_Msk) != 0);

  256.         /* Disable Timer for PWM1 channel 2 */
  257.         PWM_ForceStop(PWM1, PWM_CH_2_MASK);

  258.         /* Disable Capture Function and Capture Input path for  PWM1 channel 2*/
  259.         PWM_DisableCapture(PWM1, PWM_CH_2_MASK);

  260.         /* Clear Capture Interrupt flag for PWM1 channel 2 */
  261.         PWM_ClearCaptureIntFlag(PWM1, 2, PWM_CAPTURE_INT_FALLING_LATCH);
  262.     }
  263. }
643757107 发表于 2019-2-16 23:00 | 显示全部楼层
上面是官方的例子,是不用DMA的,另外还要一个用的。
643757107 发表于 2019-2-16 23:00 | 显示全部楼层
所以这个PWM真滴跟那个DMA没有太大关系吧。
稳稳の幸福 发表于 2019-2-17 14:20 | 显示全部楼层
楼上正解,官方的BPS里面有两个例子
zhuomuniao110 发表于 2019-2-17 20:33 | 显示全部楼层
可以那么做,DMA不是必须的
小明的同学 发表于 2019-2-19 10:42 | 显示全部楼层
解决了吧
小明的同学 发表于 2019-2-19 11:16 | 显示全部楼层
应该都支持PWM
小明的同学 发表于 2019-2-19 11:16 | 显示全部楼层
643757107 发表于 2019-2-19 13:29 | 显示全部楼层
PWM可以用DMA,也可以不用。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

59

主题

80

帖子

1

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