[技术问答] 谁有新塘NUC120的PWM捕获的例子?

[复制链接]
1138|10
 楼主| rocHP 发表于 2019-5-15 11:47 | 显示全部楼层 |阅读模式
MUC1XX系列的例子即可,讲解下捕获边缘开发也行,主要是用在曼彻斯特编码的解析,要是直接有这个解码的例程更好啦!麻烦各位大神了!
734774645 发表于 2019-5-19 11:54 | 显示全部楼层
还没找到吗,BSP里面有啊。
734774645 发表于 2019-5-19 11:55 | 显示全部楼层
  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: 2 $
  5. * $Date: 15/04/10 2:04p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Capture the PWMB Channel 1 waveform by PWMB Channel 2.
  7. * @note
  8. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  9. *
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "NUC100Series.h"

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

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


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

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

  34.     /* Handle PWMB Capture function */
  35.     u32CapIntFlag1 = PWMB->CCR2;

  36.     /* PWMB channel 2 Capture interrupt */
  37.     if(u32CapIntFlag1 & PWM_CCR2_CAPIF2_Msk)
  38.     {
  39.         PWMB->CCR2 &= (PWM_CCR_MASK | PWM_CCR2_CAPIF2_Msk);
  40.     }
  41. }

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

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

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

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

  66.     u32i = 0;

  67.     while(u32i < 4)
  68.     {
  69.         /* Wait for Capture Falling Indicator */
  70.         while(PWM_GetCaptureIntFlag(PWM, u32Ch) < 2);

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

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

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

  77.         /* Clear Capture Rising Indicator */
  78.         PWM_ClearCaptureIntFlag(PWM, u32Ch, PWM_CAPTURE_INT_RISING_LATCH);

  79.         /* Get Capture Rising Latch Counter Data */
  80.         u32Count[u32i++] = PWM_GET_CAPTURE_RISING_DATA(PWM, u32Ch);
  81.     }

  82.     u16RisingTime = u32Count[1];

  83.     u16FallingTime = u32Count[0];

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

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

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

  87.     printf("\nPWM generate: \nHigh Period=7199 ~ 7201, Low Period=16799 ~ 16801, Total Period=23999 ~ 24001\n");
  88.     printf("\nCapture Result: Rising Time = %d, Falling Time = %d \nHigh Period = %d, Low Period = %d, Total Period = %d.\n\n",
  89.            u16RisingTime, u16FallingTime, u16HighPeroid, u16LowPeroid, u16TotalPeroid);
  90.     if((u16HighPeroid < 7199) || (u16HighPeroid > 7201) || (u16LowPeroid < 16799) || (u16LowPeroid > 16801) || (u16TotalPeroid < 23999) || (u16TotalPeroid > 24001))
  91.         printf("Capture Test Fail!!\n");
  92.     else
  93.         printf("Capture Test Pass!!\n");
  94. }

  95. void SYS_Init(void)
  96. {
  97.     /*---------------------------------------------------------------------------------------------------------*/
  98.     /* Init System Clock                                                                                       */
  99.     /*---------------------------------------------------------------------------------------------------------*/

  100.     /* Enable Internal RC clock */
  101.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  102.     /* Waiting for IRC22M clock ready */
  103.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  104.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  105.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  106.     /* Enable external 12MHz XTAL, internal 22.1184MHz */
  107.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_OSC22M_EN_Msk);

  108.     /* Enable PLL and Set PLL frequency */
  109.     CLK_SetCoreClock(PLL_CLOCK);

  110.     /* Waiting for clock ready */
  111.     CLK_WaitClockReady(CLK_CLKSTATUS_PLL_STB_Msk | CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_OSC22M_STB_Msk);

  112.     /* Switch HCLK clock source to PLL, STCLK to HCLK/2 */
  113.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_PLL, CLK_CLKDIV_HCLK(2));

  114.     /* Enable UART module clock */
  115.     CLK_EnableModuleClock(UART0_MODULE);

  116.     /* Enable PWM module clock */
  117.     CLK_EnableModuleClock(PWM45_MODULE);
  118.     CLK_EnableModuleClock(PWM67_MODULE);

  119.     /* Select UART module clock source */
  120.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  121.     /* Select PWM module clock source */
  122.     CLK_SetModuleClock(PWM45_MODULE, CLK_CLKSEL2_PWM45_S_HXT, 0);
  123.     CLK_SetModuleClock(PWM67_MODULE, CLK_CLKSEL2_PWM67_S_HXT, 0);

  124.     /* Reset PWMB channel0~channel3 */
  125.     SYS_ResetModule(PWM47_RST);

  126.     /* Update System Core Clock */
  127.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  128.     //SystemCoreClockUpdate();
  129.     PllClock        = PLL_CLOCK;            // PLL
  130.     SystemCoreClock = PLL_CLOCK / 1;        // HCLK
  131.     CyclesPerUs     = PLL_CLOCK / 1000000;  // For SYS_SysTickDelay()

  132.     /*---------------------------------------------------------------------------------------------------------*/
  133.     /* Init I/O Multi-function                                                                                 */
  134.     /*---------------------------------------------------------------------------------------------------------*/
  135.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  136.     SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
  137.     SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD);

  138.     /* Set GPE multi-function pins for PWMB Channel1 and channel2 */
  139.     SYS->GPE_MFP = SYS_GPE_MFP_PE5_PWM5 | SYS_GPE_MFP_PE0_PWM6;
  140. }

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

  148.     /* Configure UART0 and set UART0 Baudrate */
  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 UART0 for printf */
  168.     UART0_Init();

  169.     printf("\nSystem clock rate: %d Hz\n", SystemCoreClock);

  170.     printf("+------------------------------------------------------------------------+\n");
  171.     printf("|                          PWM Driver Sample Code                        |\n");
  172.     printf("|                                                                        |\n");
  173.     printf("+------------------------------------------------------------------------+\n");
  174.     printf("  This sample code will use PWMB channel 2 to capture\n  the signal from PWMB channel 1.\n");
  175.     printf("  I/O configuration:\n");
  176.     printf("    PWM5(PE.5 PWMB channel 1) <--> PWM6(PE.0 PWMB channel 2)\n\n");
  177.     printf("Use PWMB Channel 2(PE.0) to capture the PWMB Channel 1(PE.5) Waveform\n");

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

  182.         /*--------------------------------------------------------------------------------------*/
  183.         /* Set the PWMB Channel 1 as PWM output function.                                               */
  184.         /*--------------------------------------------------------------------------------------*/

  185.         /* Assume PWM output frequency is 250Hz and duty ratio is 30%, user can calculate PWM settings by follows.
  186.            duty ratio = (CMR+1)/(CNR+1)
  187.            cycle time = CNR+1
  188.            High level = CMR+1
  189.            PWM clock source frequency = __HXT = 12000000
  190.            (CNR+1) = PWM clock source frequency/prescaler/clock source divider/PWM output frequency
  191.                    = 12000000/2/1/250 = 24000
  192.            (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.)
  193.            CNR = 23999
  194.            duty ratio = 30% ==> (CMR+1)/(CNR+1) = 30%
  195.            CMR = 7199
  196.            Prescale value is 1 : prescaler= 2
  197.            Clock divider is PWM_CSR_DIV1 : clock divider =1
  198.         */

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

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

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

  205.         /*--------------------------------------------------------------------------------------*/
  206.         /* Set the PWMB 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 = __HXT = 12000000 in the sample code.
  210.            (CNR+1) = Capture clock source frequency/prescaler/clock source divider/minimum input frequency
  211.                    = 12000000/2/1/250 = 24000
  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.         */

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

  218.         /* Enable Backward Compatible: write 1 to clear CFLRI0~3 and CRLRI0~3 */
  219.         PWMB->PBCR = 1;

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

  222.         /* Enable PWMB NVIC interrupt */
  223.         NVIC_EnableIRQ((IRQn_Type)(PWMB_IRQn));

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

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

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

  230.         /* Capture the Input Waveform Data */
  231.         CalPeriodTime(PWMB, PWM_CH2);
  232.         /*------------------------------------------------------------------------------------------------------*/
  233.         /* Stop PWMB channel 1 (Recommended procedure method 1)                                                 */
  234.         /* Set PWM Timer loaded value(CNR) as 0. When PWM internal counter(PDR) reaches to 0, disable PWM Timer */
  235.         /*------------------------------------------------------------------------------------------------------*/

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

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

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

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

  244.         /*------------------------------------------------------------------------------------------------------*/
  245.         /* Stop PWMB channel 2 (Recommended procedure method 1)                                                 */
  246.         /* Set PWM Timer loaded value(CNR) as 0. When PWM internal counter(PDR) reaches to 0, disable PWM Timer */
  247.         /*------------------------------------------------------------------------------------------------------*/

  248.         /* Disable PWMB NVIC */
  249.         NVIC_DisableIRQ((IRQn_Type)(PWMB_IRQn));

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

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

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

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

  258.         /* Clear Capture Interrupt flag for PWMB channel 2*/
  259.         PWM_ClearCaptureIntFlag(PWMB, PWM_CH2, PWM_CAPTURE_INT_FALLING_LATCH);
  260.     }
  261. }




734774645 发表于 2019-5-19 11:56 | 显示全部楼层
楼主试试这个,120系列的BSP开发包提供的官方例子。
捉虫天师 发表于 2019-5-19 12:16 | 显示全部楼层
学习一下这个技术。PWM还可以做捕获?厉害了。
捉虫天师 发表于 2019-5-19 12:16 | 显示全部楼层
这比PIC单片机厉害多了。
huahuagg 发表于 2019-5-19 19:36 | 显示全部楼层
官方的例子还是很容易上手懂的。

评论

您好,可以把NUC230视频教程给我发一份吗,谢谢  发表于 2019-5-20 11:20
huahuagg 发表于 2019-5-19 19:36 | 显示全部楼层
注释的很全面。
xinpian101 发表于 2019-5-21 23:40 | 显示全部楼层
啥时候有视频教程了?
玛尼玛尼哄 发表于 2019-5-23 23:17 | 显示全部楼层
有开发板?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

9

主题

36

帖子

1

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