[DemoCode下载] 029的PWM测试

[复制链接]
560|3
 楼主| 稳稳の幸福 发表于 2020-7-26 22:22 | 显示全部楼层 |阅读模式



  1. CPU [url=home.php?mod=space&uid=72445]@[/url] 72000000 Hz

  2. +-----------------------------------------------+

  3. |    Timer PWM Change Duty Cycle Sample Code    |

  4. +-----------------------------------------------+


  5. # Timer0 PWM_CH0 frequency of first period is 18 kHz and duty is 50%.

  6. # Timer0 PWM_CH0 frequency of second period is 36 kHz and duty is 40%.

  7. # I/O configuration:

  8.     - Timer0 PWM_CH0 on PD.4


  9.         Timer0 PWM_CH0 waveform of this sample shown below:


  10.         |<-        PERIOD0+1        ->|                     

  11.                        |<-   CMP0   ->|                     

  12.                                       |<-  PERIOD1+1 ->|   

  13.                                                |<CMP1 >|   

  14.       __                ______________          _______                             

  15.         |______50%_____|     50%      |___60%__|  40%  |___ PWM_CH0 outut duty  


  16. *** Check Timer0 PWM_CH0 output waveform by oscilloscope ***



 楼主| 稳稳の幸福 发表于 2020-7-26 22:23 | 显示全部楼层
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 3 $
  5. * $Date: 17/05/04 1:55p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Change duty cycle and period of output waveform in PWM down count type.
  7. * @note
  8. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "NUC029xGE.h"


  12. #define PLL_CLOCK       72000000

  13. volatile uint32_t gu32Period;

  14. /**
  15. * @brief       Timer0 IRQ
  16. *
  17. * @param       None
  18. *
  19. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  20. *
  21. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The Timer0 default IRQ, declared in startup_NUC029xGE.s.
  22. */
  23. void TMR0_IRQHandler(void)
  24. {
  25.     static uint32_t u32Toggle = 0;

  26.     if(TPWM_GET_PERIOD_INT_FLAG(TIMER0))
  27.     {
  28.         if(u32Toggle == 0)
  29.         {
  30.             /* Set PWM period to generate output frequency 36000 Hz */
  31.             TPWM_SET_PERIOD(TIMER0, ((gu32Period / 2) - 1));

  32.             /* Set PWM duty, 40% */
  33.             TPWM_SET_CMPDAT(TIMER0, (((gu32Period / 2) * 4) / 10));
  34.         }
  35.         else
  36.         {
  37.             /* Set PWM period to generate output frequency 18000 Hz */
  38.             TPWM_SET_PERIOD(TIMER0, (gu32Period - 1));

  39.             /* Set PWM duty, 50% */
  40.             TPWM_SET_CMPDAT(TIMER0, (gu32Period / 2));
  41.         }
  42.         u32Toggle ^= 1;
  43.         TPWM_CLEAR_PERIOD_INT_FLAG(TIMER0);
  44.     }
  45. }

  46. void SYS_Init(void)
  47. {
  48.     /*---------------------------------------------------------------------------------------------------------*/
  49.     /* Init System Clock                                                                                       */
  50.     /*---------------------------------------------------------------------------------------------------------*/
  51.     /* Enable HIRC clock */
  52.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  53.     /* Waiting for HIRC clock ready */
  54.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  55.     /* Switch HCLK clock source to HIRC */
  56.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  57.     /* Enable HXT */
  58.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  59.     /* Waiting for clock ready */
  60.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  61.     /* Set core clock as PLL_CLOCK from PLL and SysTick source to HCLK/2*/
  62.     CLK_SetCoreClock(PLL_CLOCK);
  63.     CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2);

  64.     /* Enable peripheral clock */
  65.     CLK_EnableModuleClock(UART0_MODULE);
  66.     CLK_EnableModuleClock(TMR0_MODULE);

  67.     /* Peripheral clock source */
  68.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_PLL, CLK_CLKDIV0_UART(1));
  69.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_PCLK0, 0);

  70.     /*---------------------------------------------------------------------------------------------------------*/
  71.     /* Init I/O Multi-function                                                                                 */
  72.     /*---------------------------------------------------------------------------------------------------------*/
  73.     /* Set multi-function pins for UART0 RXD and TXD */
  74.     SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA2MFP_Msk | SYS_GPA_MFPL_PA3MFP_Msk);
  75.     SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA3MFP_UART0_RXD | SYS_GPA_MFPL_PA2MFP_UART0_TXD);

  76.     /* Set Timer0 PWM CH0(TM0) pin */
  77.     SYS->GPD_MFPL &= ~SYS_GPD_MFPL_PD4MFP_Msk;
  78.     SYS->GPD_MFPL |= SYS_GPD_MFPL_PD4MFP_TM0;
  79. }

  80. void UART0_Init(void)
  81. {
  82.     /*---------------------------------------------------------------------------------------------------------*/
  83.     /* Init UART                                                                                               */
  84.     /*---------------------------------------------------------------------------------------------------------*/
  85.     /* Reset UART module */
  86.     SYS_ResetModule(UART0_RST);

  87.     /* Configure UART0 and set UART0 Baudrate */
  88.     UART_Open(UART0, 115200);
  89. }

  90. /*---------------------------------------------------------------------------------------------------------*/
  91. /*  MAIN function                                                                                          */
  92. /*---------------------------------------------------------------------------------------------------------*/
  93. int main(void)
  94. {
  95.     /* Unlock protected registers */
  96.     SYS_UnlockReg();

  97.     /* Init System, peripheral clock and multi-function I/O */
  98.     SYS_Init();

  99.     /* Lock protected registers */
  100.     SYS_LockReg();

  101.     /* Init UART0 for printf */
  102.     UART0_Init();

  103.     printf("\n\nCPU @ %d Hz\n", SystemCoreClock);
  104.     printf("+-----------------------------------------------+\n");
  105.     printf("|    Timer PWM Change Duty Cycle Sample Code    |\n");
  106.     printf("+-----------------------------------------------+\n\n");

  107.     printf("# Timer0 PWM_CH0 frequency of first period is 18 kHz and duty is 50%%.\n");
  108.     printf("# Timer0 PWM_CH0 frequency of second period is 36 kHz and duty is 40%%.\n");
  109.     printf("# I/O configuration:\n");
  110.     printf("    - Timer0 PWM_CH0 on PD.4\n\n");
  111.     printf("        Timer0 PWM_CH0 waveform of this sample shown below: \n");
  112.     printf("\n");
  113.     printf("        |<-        PERIOD0+1        ->|                     \n");
  114.     printf("                       |<-   CMP0   ->|                     \n");
  115.     printf("                                      |<-  PERIOD1+1 ->|    \n");
  116.     printf("                                               |<CMP1 >|    \n");
  117.     printf("      __                ______________          _______                             \n");
  118.     printf("        |______50%%_____|     50%%      |___60%%__|  40%%  |___ PWM_CH0 outut duty  \n");
  119.     printf("\n");

  120.     /* Change Timer to PWM counter mode */
  121.     TPWM_ENABLE_PWM_MODE(TIMER0);

  122.     /* Set PWM mode as independent mode*/
  123.     TPWM_ENABLE_INDEPENDENT_MODE(TIMER0);

  124.     /* Set Timer0 PWM output frequency is 18000 Hz, duty 50% in up count type */
  125.     TPWM_ConfigOutputFreqAndDuty(TIMER0, 18000, 50);

  126.     /* Get initial period and comparator value */
  127.     gu32Period = TPWM_GET_PERIOD(TIMER0) + 1;

  128.     /* Set PWM down count type */
  129.     TPWM_SET_COUNTER_TYPE(TIMER0, TPWM_DOWN_COUNT);

  130.     /* Enable output of PWM_CH0 */
  131.     TPWM_ENABLE_OUTPUT(TIMER0, TPWM_CH0);

  132.     /* Enable period event interrupt */
  133.     TPWM_ENABLE_PERIOD_INT(TIMER0);
  134.     NVIC_EnableIRQ(TMR0_IRQn);

  135.     /* Start Timer PWM counter */
  136.     TPWM_START_COUNTER(TIMER0);

  137.     printf("*** Check Timer0 PWM_CH0 output waveform by oscilloscope ***\n");

  138.     while(1);
  139. }

  140. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
 楼主| 稳稳の幸福 发表于 2020-7-26 22:23 | 显示全部楼层
效果非常棒。
643757107 发表于 2020-7-27 11:52 | 显示全部楼层
讨厌中断的演示,没法看到本质。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

207

主题

3457

帖子

8

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