[DemoCode下载] M0518的PWM_DoubleBuffer

[复制链接]
 楼主| 734774645 发表于 2018-10-18 19:48 | 显示全部楼层 |阅读模式
  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: 14/12/25 10:24a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Change duty cycle and period of output waveform by PWM Double Buffer function.
  7. * @note
  8. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  9. *
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "M0518.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       PWM0 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 PWM0 interrupt event
  29. */
  30. void PWM0_IRQHandler(void)
  31. {
  32.     static int toggle = 0;

  33.     // Update PWM0 channel 0 period and duty
  34.     if(toggle == 0)
  35.     {
  36.         PWM_SET_CNR(PWM0, 0, 99);
  37.         PWM_SET_CMR(PWM0, 0, 39);
  38.     }
  39.     else
  40.     {
  41.         PWM_SET_CNR(PWM0, 0, 399);
  42.         PWM_SET_CMR(PWM0, 0, 199);
  43.     }
  44.     toggle ^= 1;
  45.     // Clear channel 0 period interrupt flag
  46.     PWM_ClearPeriodIntFlag(PWM0, 0);
  47. }

  48. void SYS_Init(void)
  49. {
  50.     /*---------------------------------------------------------------------------------------------------------*/
  51.     /* Init System Clock                                                                                       */
  52.     /*---------------------------------------------------------------------------------------------------------*/

  53.     /* Enable Internal RC clock */
  54.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  55.     /* Waiting for IRC22M clock ready */
  56.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

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

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

  61.     /* Enable PLL and Set PLL frequency */
  62.     CLK_SetCoreClock(PLL_CLOCK);

  63.     /* Waiting for clock ready */
  64.     CLK_WaitClockReady(CLK_CLKSTATUS_PLL_STB_Msk | CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_OSC22M_STB_Msk);

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

  67.     /* Enable UART module clock */
  68.     CLK_EnableModuleClock(UART0_MODULE);

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

  71.     /* Enable PWM0 clock source */
  72.     CLK_EnableModuleClock(PWM0_MODULE);

  73.     /* Select PWM module clock source */
  74.     //CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL3_PWM0_S_HClK, 0);
  75.     CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL3_PWM0_S_PLL, 0);

  76.     /* Reset PWM0 */
  77.     SYS_ResetModule(PWM0_RST);

  78.     /* Update System Core Clock */
  79.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  80.     //SystemCoreClockUpdate();
  81.     PllClock        = PLL_CLOCK;            // PLL
  82.     SystemCoreClock = PLL_CLOCK / 1;        // HCLK
  83.     CyclesPerUs     = PLL_CLOCK / 1000000;  // For SYS_SysTickDelay()

  84.     /*---------------------------------------------------------------------------------------------------------*/
  85.     /* Init I/O Multi-function                                                                                 */
  86.     /*---------------------------------------------------------------------------------------------------------*/
  87.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  88.     SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
  89.     SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD);
  90.     /* Set GPA multi-function pins for PWM0 Channel 0 */
  91.     SYS->GPA_MFP &= ~(SYS_GPA_MFP_PA12_Msk);
  92.     SYS->GPA_MFP |= (SYS_GPA_MFP_PA12_PWM0_CH0);
  93.     SYS->ALT_MFP4 &= ~(SYS_ALT_MFP4_PA12_Msk);
  94. }

  95. void UART0_Init()
  96. {
  97.     /*---------------------------------------------------------------------------------------------------------*/
  98.     /* Init UART                                                                                               */
  99.     /*---------------------------------------------------------------------------------------------------------*/
  100.     /* Reset IP */
  101.     SYS_ResetModule(UART0_RST);

  102.     /* Configure UART0 and set UART0 Baudrate */
  103.     UART_Open(UART0, 115200);
  104. }

  105. /*---------------------------------------------------------------------------------------------------------*/
  106. /*  Main Function                                                                                          */
  107. /*---------------------------------------------------------------------------------------------------------*/
  108. int32_t main(void)
  109. {
  110.     /* Unlock protected registers */
  111.     SYS_UnlockReg();

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

  114.     /* Lock protected registers */
  115.     SYS_LockReg();

  116.     /* Init UART to 115200-8n1 for print message */
  117.     UART0_Init();
  118.     printf("+------------------------------------------------------------------------+\n");
  119.     printf("|                          PWM Driver Sample Code                        |\n");
  120.     printf("|                                                                        |\n");
  121.     printf("+------------------------------------------------------------------------+\n");
  122.     printf("  This sample code will use PWM0 channel 0 to output waveform\n");
  123.     printf("  I/O configuration:\n");
  124.     printf("    waveform output pin: PWM0 channel 0(PA.12)\n");
  125.     printf("\nUse double buffer feature.\n");

  126.     /*
  127.         PWM0 channel 0 waveform of this sample shown below:

  128.         |<-        CNR + 1  clk     ->|  CNR + 1 = 399 + 1 CLKs
  129.                        |<-CMR+1 clk ->|  CMR + 1 = 199 + 1 CLKs
  130.                                       |<-   CNR + 1  ->|  CNR + 1 = 99 + 1 CLKs
  131.                                                |<CMR+1>|  CMR + 1 = 39 + 1 CLKs
  132.       __                ______________          _______
  133.         |______200_____|     200      |____60__|   40  |_____PWM waveform

  134.     */


  135.     /*
  136.       Configure PWM0 channel 0 init period and duty.
  137.       Period is __HXT / (prescaler * clock divider * (CNR + 1))
  138.       Duty ratio = (CMR + 1) / (CNR + 1)
  139.       Period = 12 MHz / (2 * 1 * (199 + 1)) =  30000 Hz
  140.       Duty ratio = (99 + 1) / (199 + 1) = 50%
  141.     */
  142.     // PWM0 channel 0 frequency is 100Hz, duty 30%,
  143.     PWM_ConfigOutputChannel(PWM0, 0, 30000, 30);

  144.     // Enable output of PWM0 channel 0
  145.     PWM_EnableOutput(PWM0, PWM_CH_0_MASK);

  146.     // Enable PWM0 channel 0 period interrupt, use channel 0 to measure time.
  147.     PWM_EnablePeriodInt(PWM0, 0, 0);
  148.     NVIC_EnableIRQ(PWM0_IRQn);

  149.     // Start
  150.     PWM_Start(PWM0, PWM_CH_0_MASK);

  151.     while(1);

  152. }




复制代码


 楼主| 734774645 发表于 2018-10-18 19:49 | 显示全部楼层
主要研究学习每个外设的配置方法。
 楼主| 734774645 发表于 2018-10-18 20:03 | 显示全部楼层
    /*
      Configure PWM0 channel 0 init period and duty.
      Period is __HXT / (prescaler * clock divider * (CNR + 1))
      Duty ratio = (CMR + 1) / (CNR + 1)
      Period = 12 MHz / (2 * 1 * (199 + 1)) =  30000 Hz
      Duty ratio = (99 + 1) / (199 + 1) = 50%
    */
    // PWM0 channel 0 frequency is 100Hz, duty 30%,
    PWM_ConfigOutputChannel(PWM0, 0, 30000, 30);

大家也许注意到这句了,是注释错了,还是咋的?上面写的100HZ,但是下面写的3000,有人懂吗?

dongnanxibei 发表于 2018-10-19 11:39 | 显示全部楼层
734774645 发表于 2018-10-18 20:03
/*
      Configure PWM0 channel 0 init period and duty.
      Period is __HXT / (prescaler * clo ...

    // PWM2 frequency is 300Hz, duty 50%
    PWM_ConfigOutputChannel(PWMA, PWM_CH2, 300, 50);
参考这个例子,可以看出来,你贴的那个注释人家写错了,这个程序员坑爹。
xuanhuanzi 发表于 2018-10-19 15:06 | 显示全部楼层
双缓冲是啥意思。
xuanhuanzi 发表于 2018-10-19 15:21 | 显示全部楼层
就是两个频率两个占空比交替进行,看PWM中断,在这里进行了占空比和频率修改。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

211

主题

3588

帖子

15

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