[DemoCode下载] 029的PWM捕捉应用

[复制链接]
 楼主| 捉虫天师 发表于 2020-1-18 21:27 | 显示全部楼层 |阅读模式
PWM, TI, AN, TE, mb
  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: 1 $
  5. * $Date: 14/10/01 10:35a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    NUC029 Series PWM Generator and Capture Timer Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "NUC029xAN.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. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  25. *
  26. * [url=home.php?mod=space&uid=1543424]@Details[/url]     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_Msk | SYS_MFP_P31_Msk);
  190.     SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0);

  191.     /* Set P2 multi-function pins for PWMB Channel0~3  */
  192.     SYS->P2_MFP &= ~(SYS_MFP_P25_Msk | SYS_MFP_P26_Msk);
  193.     SYS->P2_MFP |= (SYS_MFP_P25_PWM5 | SYS_MFP_P26_PWM6);
  194. }


  195. void UART0_Init(void)
  196. {

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



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

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

  212.     /* Lock protected registers */
  213.     SYS_LockReg();

  214.     /* Init UART0 for printf */
  215.     UART0_Init();

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

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

  228.         /*--------------------------------------------------------------------------------------*/
  229.         /* Set the PWMB Channel 1 as PWM output function.                                               */
  230.         /*--------------------------------------------------------------------------------------*/

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

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

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

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

  251.         /*--------------------------------------------------------------------------------------*/
  252.         /* Set the PWMB channel 2  for capture function                                         */
  253.         /*--------------------------------------------------------------------------------------*/

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

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

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

  266.         /* Enable PWMB NVIC interrupt */
  267.         NVIC_EnableIRQ((IRQn_Type)(PWMB_IRQn));

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

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

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

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

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

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

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

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

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

  292.         /* Disable PWMB NVIC */
  293.         NVIC_DisableIRQ((IRQn_Type)(PWMB_IRQn));

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

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

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

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

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






 楼主| 捉虫天师 发表于 2020-1-18 21:28 | 显示全部楼层
这么多的厂家,我就发现新唐的PWM最容易上手。
 楼主| 捉虫天师 发表于 2020-1-18 21:28 | 显示全部楼层
PLL也是异常的强大。
wanduzi 发表于 2020-1-18 21:53 | 显示全部楼层
全是在中断里实现
天灵灵地灵灵 发表于 2020-1-18 22:43 | 显示全部楼层
搞清楚原理很容易
幸福小强 发表于 2020-1-18 23:58 | 显示全部楼层
PWM定时器的另一个功能是数字输入捕捉功能。当捕捉功能使能时,PWM输出引脚被切为输入捕捉模式。捕捉器0和PWM0 共享PWM0 定时器,捕捉器1和PWM1 共享PWM1 定时器,以此类推。因此,在使能捕捉功能之前,用户必须先设置PMW定时器。捕捉功能使能后,当输入端口有上升沿时转变时,PWM 计数器的值会被锁存入CRLR ,输入端口有下降沿转变时,PWM 计数器的值会被锁存入CFLR 。通过软件设定CRL_IE0(CCR0[1])( 使能上升沿锁存中断)和CFL_IE0(CCR0[2]) ( 使能下降沿锁存中断),可以设置捕捉器通道0产生中断的条件。同样设定CRL_IE1(CCR0[17])和CFL_IE1(CCR0[18]),可以设定捕捉器通道1产生中断的条件。通过设定CCR2的相应的数据位,捕捉通道2和3也有同样的功能。对每组而言,每当捕捉器触发中断0/1/2/3 时,PWM计数器0/1/2/3的值也会同时被重新加载。
幸福小强 发表于 2020-1-18 23:58 | 显示全部楼层
PWM可以支持的最大的捕捉频率由捕捉中断延迟决定.捕捉中断发生时,软件至少执行以下三步: 第一步,读PIIR 获取中断源,第二步,读CRLRx/CFLRx(x=0~3) 获取捕捉的值,第三步写1清PIIR 为0.如果中断到完成的延时时间为TO,捕捉信号在(T0)间隔内一定不能改变. 此条件下,最大捕捉频率为1/T0.
wahahaheihei 发表于 2020-1-23 19:10 | 显示全部楼层
测量频率就要用到这个功能
antusheng 发表于 2020-1-23 20:09 | 显示全部楼层
定时器捕捉貌似没这个效果好
xixi2017 发表于 2020-1-24 14:03 | 显示全部楼层
这种带简单示意图的,非常棒。
antusheng 发表于 2020-1-24 15:11 | 显示全部楼层
时钟配置工具一起用非常方便。
gejigeji521 发表于 2020-1-25 15:45 | 显示全部楼层
准备系统的研究一下时钟系统。
yiy 发表于 2020-1-26 13:40 | 显示全部楼层
还是有不少要寄存器操作的
zhuomuniao110 发表于 2020-1-31 22:05 | 显示全部楼层
捕捉功能比较好。
zhuomuniao110 发表于 2020-1-31 22:05 | 显示全部楼层
多谢分享。
643757107 发表于 2020-1-31 22:17 | 显示全部楼层
用好了这个可以实现超多功能了。
jiekou001 发表于 2020-2-2 21:04 | 显示全部楼层
都要一个一个练习练习。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

212

主题

3272

帖子

7

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