[DemoCode下载] 使用PWM做BLINK演示

[复制链接]
615|6
 楼主| 稳稳の幸福 发表于 2020-7-26 23:00 | 显示全部楼层 |阅读模式
link, PWM, ck, se, TE
  1. /**************************************************************************/
  2. ******************************************************************************/
  3. #include <stdio.h>
  4. #include "NUC029xGE.h"

  5. /*---------------------------------------------------------------------------------------------------------*/
  6. /* Macro, type and constant definitions                                                                    */
  7. /*---------------------------------------------------------------------------------------------------------*/
  8. #define PLL_CLOCK       144000000

  9. /*---------------------------------------------------------------------------------------------------------*/
  10. /* Global variables                                                                                        */
  11. /*---------------------------------------------------------------------------------------------------------*/



  12. void SYS_Init(void)
  13. {
  14.     /*---------------------------------------------------------------------------------------------------------*/
  15.     /* Init System Clock                                                                                       */
  16.     /*---------------------------------------------------------------------------------------------------------*/
  17.     /* Enable HIRC clock (Internal RC 22.1184MHz) */
  18.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  19.     /* Waiting for HIRC clock ready */
  20.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

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

  23.     /* Enable HXT clock (external XTAL 12MHz) */
  24.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  25.     /* Waiting for HXT clock ready */
  26.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  27.     /* Set core clock as PLL_CLOCK from PLL */
  28.     CLK_SetCoreClock(PLL_CLOCK);

  29.     /* Waiting for PLL clock ready */
  30.     CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

  31.     /* Enable PWM0 module clock */
  32.     CLK_EnableModuleClock(PWM0_MODULE);

  33.     /*---------------------------------------------------------------------------------------------------------*/
  34.     /* PWM clock frequency configuration                                                                       */
  35.     /*---------------------------------------------------------------------------------------------------------*/
  36.     /* Select HCLK clock source as PLL and and HCLK clock divider as 2 */
  37.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));

  38.     /* PWM clock frequency can be set equal or double to HCLK by choosing case 1 or case 2 */
  39.     /* case 1.PWM clock frequency is set equal to HCLK: select PWM module clock source as PCLK */
  40.     CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL1_PWM0SEL_PCLK0, NULL);

  41.     /* case 2.PWM clock frequency is set double to HCLK: select PWM module clock source as PLL */
  42.     //CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL1_PWM0SEL_PLL, NULL);
  43.     /*---------------------------------------------------------------------------------------------------------*/

  44.     /* Enable UART module clock */
  45.     CLK_EnableModuleClock(UART0_MODULE);

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

  48.     /* Reset PWM0 module */
  49.     SYS_ResetModule(PWM0_RST);

  50.     /* Update System Core Clock */
  51.     SystemCoreClockUpdate();

  52.     /*---------------------------------------------------------------------------------------------------------*/
  53.     /* Init I/O Multi-function                                                                                 */
  54.     /*---------------------------------------------------------------------------------------------------------*/
  55.     /* Set multi-function pins for UART0 RXD and TXD */
  56.     SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA2MFP_Msk | SYS_GPA_MFPL_PA3MFP_Msk);
  57.     SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA3MFP_UART0_RXD | SYS_GPA_MFPL_PA2MFP_UART0_TXD);

  58.     /* Set PC multi-function pins for PWM0 Channel 5 */
  59.                 SYS->GPC_MFPL &= ~SYS_GPC_MFPL_PC5MFP_Msk;
  60.                 SYS->GPC_MFPL |=SYS_GPC_MFPL_PC5MFP_PWM0_CH5;

  61. }

  62. void UART0_Init()
  63. {
  64.     /*---------------------------------------------------------------------------------------------------------*/
  65.     /* Init UART                                                                                               */
  66.     /*---------------------------------------------------------------------------------------------------------*/
  67.     /* Reset UART module */
  68.     SYS_ResetModule(UART0_RST);

  69.     /* Configure UART0 and set UART0 baud rate */
  70.     UART_Open(UART0, 115200);
  71. }


  72. /*---------------------------------------------------------------------------------------------------------*/
  73. /*  Main Function                                                                                          */
  74. /*---------------------------------------------------------------------------------------------------------*/
  75. int32_t main(void)
  76. {
  77.     /* Init System, IP clock and multi-function I/O
  78.        In the end of SYS_Init() will issue SYS_LockReg()
  79.        to lock protected register. If user want to write
  80.        protected register, please issue SYS_UnlockReg()
  81.        to unlock protected register if necessary */

  82.     /* Unlock protected registers */
  83.     SYS_UnlockReg();

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

  86.     /* Lock protected registers */
  87.     SYS_LockReg();

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

  90.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz(PLL@ %dHz)\n", SystemCoreClock, PllClock);
  91.     printf("PWM0 clock is from %s\n", (CLK->CLKSEL1 & CLK_CLKSEL1_PWM0SEL_Msk) ? "CPU" : "PLL");
  92.     printf("+------------------------------------------------------------------------+\n");
  93.     printf("|                          LED Sample Code                        |\n");
  94.     printf("|                                                                        |\n");


  95.     /* Set Pwm mode as complementary mode */
  96. //    PWM_ENABLE_COMPLEMENTARY_MODE(PWM0);

  97.     /* PWM0 channel 5 frequency is 200Hz, duty 50% */
  98.     PWM_ConfigOutputChannel(PWM0, 5, 2, 50);


  99.     /* Enable output of PWM0 channel 5 */
  100.     PWM_EnableOutput(PWM0, BIT5);



  101.     /* Start */
  102.     PWM_Start(PWM0, BIT5);

  103.     while(1);
  104. }


 楼主| 稳稳の幸福 发表于 2020-7-26 23:02 | 显示全部楼层
  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: 2 $
  5. * $Date: 16/10/25 4:29p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    演示如何使用PWM0 CH5 生成2HZ方波驱动LED
  7. * @note
  8. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  9. *
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "NUC029xGE.h"

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

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



  20. void SYS_Init(void)
  21. {
  22.     /*---------------------------------------------------------------------------------------------------------*/
  23.     /* Init System Clock                                                                                       */
  24.     /*---------------------------------------------------------------------------------------------------------*/
  25.     /* Enable HIRC clock (Internal RC 22.1184MHz) */
  26.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  27.     /* Waiting for HIRC clock ready */
  28.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

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

  31.     /* Enable HXT clock (external XTAL 12MHz) */
  32.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  33.     /* Waiting for HXT clock ready */
  34.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  35.     /* Set core clock as PLL_CLOCK from PLL */
  36.     CLK_SetCoreClock(PLL_CLOCK);

  37.     /* Waiting for PLL clock ready */
  38.     CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

  39.     /* Enable PWM0 module clock */
  40.     CLK_EnableModuleClock(PWM0_MODULE);

  41.     /*---------------------------------------------------------------------------------------------------------*/
  42.     /* PWM clock frequency configuration                                                                       */
  43.     /*---------------------------------------------------------------------------------------------------------*/
  44.     /* Select HCLK clock source as PLL and and HCLK clock divider as 2 */
  45.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));

  46.     /* PWM clock frequency can be set equal or double to HCLK by choosing case 1 or case 2 */
  47.     /* case 1.PWM clock frequency is set equal to HCLK: select PWM module clock source as PCLK */
  48.     CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL1_PWM0SEL_PCLK0, NULL);

  49.     /* case 2.PWM clock frequency is set double to HCLK: select PWM module clock source as PLL */
  50.     //CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL1_PWM0SEL_PLL, NULL);
  51.     /*---------------------------------------------------------------------------------------------------------*/

  52.     /* Enable UART module clock */
  53.     CLK_EnableModuleClock(UART0_MODULE);

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

  56.     /* Reset PWM0 module */
  57.     SYS_ResetModule(PWM0_RST);

  58.     /* Update System Core Clock */
  59.     SystemCoreClockUpdate();

  60.     /*---------------------------------------------------------------------------------------------------------*/
  61.     /* Init I/O Multi-function                                                                                 */
  62.     /*---------------------------------------------------------------------------------------------------------*/
  63.     /* Set multi-function pins for UART0 RXD and TXD */
  64.     SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA2MFP_Msk | SYS_GPA_MFPL_PA3MFP_Msk);
  65.     SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA3MFP_UART0_RXD | SYS_GPA_MFPL_PA2MFP_UART0_TXD);

  66.     /* Set PC multi-function pins for PWM0 Channel 5 */
  67.                 SYS->GPC_MFPL &= ~SYS_GPC_MFPL_PC5MFP_Msk;
  68.                 SYS->GPC_MFPL |=SYS_GPC_MFPL_PC5MFP_PWM0_CH5;

  69. }

  70. void UART0_Init()
  71. {
  72.     /*---------------------------------------------------------------------------------------------------------*/
  73.     /* Init UART                                                                                               */
  74.     /*---------------------------------------------------------------------------------------------------------*/
  75.     /* Reset UART module */
  76.     SYS_ResetModule(UART0_RST);

  77.     /* Configure UART0 and set UART0 baud rate */
  78.     UART_Open(UART0, 115200);
  79. }


  80. /*---------------------------------------------------------------------------------------------------------*/
  81. /*  Main Function                                                                                          */
  82. /*---------------------------------------------------------------------------------------------------------*/
  83. int32_t main(void)
  84. {
  85.     /* Init System, IP clock and multi-function I/O
  86.        In the end of SYS_Init() will issue SYS_LockReg()
  87.        to lock protected register. If user want to write
  88.        protected register, please issue SYS_UnlockReg()
  89.        to unlock protected register if necessary */

  90.     /* Unlock protected registers */
  91.     SYS_UnlockReg();

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

  94.     /* Lock protected registers */
  95.     SYS_LockReg();

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

  98.     printf("\n\nCPU @ %dHz(PLL@ %dHz)\n", SystemCoreClock, PllClock);
  99.     printf("PWM0 clock is from %s\n", (CLK->CLKSEL1 & CLK_CLKSEL1_PWM0SEL_Msk) ? "CPU" : "PLL");
  100.     printf("+------------------------------------------------------------------------+\n");
  101.     printf("|                          LED Sample Code                        |\n");
  102.     printf("|                                                                        |\n");

  103.     /* PWM0 channel 5 frequency is 2Hz, duty 50% */
  104.     PWM_ConfigOutputChannel(PWM0, 5, 2, 50);

  105.     /* Enable output of PWM0 channel 5 */
  106.     PWM_EnableOutput(PWM0, BIT5);

  107.     /* Start */
  108.     PWM_Start(PWM0, BIT5);

  109.     while(1);
  110. }
 楼主| 稳稳の幸福 发表于 2020-7-26 23:02 | 显示全部楼层
效果非常好。
 楼主| 稳稳の幸福 发表于 2020-7-26 23:11 | 显示全部楼层
总结:设置基本的时钟,然后使能PWM0时钟模块,设置模块的时钟源与分频信息。复位模块。
接下来就是将PWM功能的引脚打开,通过多功能选择寄存器将IO口上对应的PWM打开。
然后设置输出通道,频率,占空比
然后使能输出,然后启动对应通道的PWM功能。
 楼主| 稳稳の幸福 发表于 2020-7-26 23:11 | 显示全部楼层
总结:设置基本的时钟,然后使能PWM0时钟模块,设置模块的时钟源与分频信息。复位模块。
接下来就是将PWM功能的引脚打开,通过多功能选择寄存器将IO口上对应的PWM打开。
然后设置输出通道,频率,占空比
然后使能输出,然后启动对应通道的PWM功能。
gaoyang9992006 发表于 2020-7-27 00:22 | 显示全部楼层
643757107 发表于 2020-7-27 11:51 | 显示全部楼层
多谢分享经验。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

207

主题

3457

帖子

8

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