[DemoCode下载] M031使用PWM控制LED

[复制链接]
1349|7
 楼主| wahahaheihei 发表于 2024-2-19 20:35 | 显示全部楼层 |阅读模式
  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: 10 $
  5. * $Date: 18/07/17 6:05p $
  6. * @brief
  7. *           Change duty cycle of output waveform to show different brightness of
  8.                          Red LED.
  9. *
  10. * SPDX-License-Identifier: Apache-2.0
  11. * Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  12. *
  13. ******************************************************************************/
  14. #include <stdio.h>
  15. #include "NuMicro.h"

  16. /*---------------------------------------------------------------------------------------------------------*/
  17. /* Macro, type and constant definitions                                                                    */
  18. /*---------------------------------------------------------------------------------------------------------*/

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

  22. void SYS_Init(void)
  23. {
  24.     /*---------------------------------------------------------------------------------------------------------*/
  25.     /* Init System Clock                                                                                       */
  26.     /*---------------------------------------------------------------------------------------------------------*/
  27.     /* Enable HIRC clock */
  28.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  29.     /* Waiting for HIRC clock ready */
  30.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  31.     /* Switch HCLK clock source to HIRC and HCLK source divide 1 */
  32.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  33.     /* Enable PWM1 module clock */
  34.     CLK_EnableModuleClock(PWM1_MODULE);

  35.     /*---------------------------------------------------------------------------------------------------------*/
  36.     /* PWM clock frequency configuration                                                                       */
  37.     /*---------------------------------------------------------------------------------------------------------*/
  38.     /* Set PLL clock as 96 MHz from HIRC/4 */
  39.     CLK_EnablePLL(CLK_PLLCTL_PLLSRC_HIRC_DIV4, 96000000);

  40.     /* Waiting for PLL clock ready */
  41.     CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

  42.     /* Select HCLK clock source as PLL and and HCLK clock divider as 2 */
  43.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));

  44.     /* PWM clock frequency can be set equal or double to HCLK by choosing case 1 or case 2 */
  45.     /* case 1.PWM clock frequency is set equal to HCLK: select PWM module clock source as PCLK */
  46. //    CLK_SetModuleClock(PWM1_MODULE, CLK_CLKSEL2_PWM1SEL_PCLK0, 0);

  47.     /* case 2.PWM clock frequency is set double to HCLK: select PWM module clock source as PLL */
  48.     CLK_SetModuleClock(PWM1_MODULE, CLK_CLKSEL2_PWM1SEL_PLL, NULL);
  49.     /*---------------------------------------------------------------------------------------------------------*/

  50.     /* Reset PWM1 module */
  51.     SYS_ResetModule(PWM1_RST);

  52.     /* Update System Core Clock */
  53.     SystemCoreClockUpdate();

  54.     /*---------------------------------------------------------------------------------------------------------*/
  55.     /* Init I/O Multi-function                                                                                 */
  56.     /*---------------------------------------------------------------------------------------------------------*/
  57.     /* Set PC.4 multi-function pins for PWM1 Channel 1 for Red of LED */
  58.     SYS->GPC_MFPL = (SYS->GPC_MFPL & (~SYS_GPC_MFPL_PC4MFP_Msk)) |
  59.                     SYS_GPC_MFPL_PC4MFP_PWM1_CH1;
  60. }

  61. /*---------------------------------------------------------------------------------------------------------*/
  62. /*  Main Function                                                                                          */
  63. /*---------------------------------------------------------------------------------------------------------*/
  64. int32_t main(void)
  65. {
  66.     /* Unlock protected registers */
  67.     SYS_UnlockReg();
  68.     /* Init System, IP clock and multi-function I/O */
  69.     SYS_Init();
  70.     /* Lock protected registers */
  71.     SYS_LockReg();

  72.     /* Set frequency and duty of PWM1 Channel 1 to control Red LED */
  73.     PWM_ConfigOutputChannel(PWM1, 1, 1, 50);
  74.     /* Enable PWM1 Output path for channel 1 */
  75.     PWM_EnableOutput(PWM1, BIT1);
  76.     /* Start PWM1 Counter */
  77.     PWM_Start(PWM1, BIT1);

  78.     while(1);
  79. }


 楼主| wahahaheihei 发表于 2024-2-19 20:36 | 显示全部楼层
官方的BSP库写的非常棒,配置通道,频率,占空比,然后使能输出。
真好用。
 楼主| wahahaheihei 发表于 2024-2-19 20:37 | 显示全部楼层
PWM_ConfigOutputChannel(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Frequency, uint32_t u32DutyCycle);
yangxiaor520 发表于 2024-2-19 20:42 来自手机 | 显示全部楼层
PWM可以实现呼吸灯效果
 楼主| wahahaheihei 发表于 2024-2-19 20:55 | 显示全部楼层
yangxiaor520 发表于 2024-2-19 20:42
PWM可以实现呼吸灯效果

这是实现跑马灯,周期只有1Hz
捉虫天师 发表于 2024-2-27 22:20 | 显示全部楼层
另类的闪灯方法。
xixi2017 发表于 2024-2-28 11:03 | 显示全部楼层
PWM太重要了,驱动很多东西可以用。
夜晚有三年 发表于 2025-9-11 14:11 | 显示全部楼层
配置 M031 的定时器为 PWM 模式,设定频率和占空比,将 PWM 输出引脚接 LED,通过改变占空比调节 LED 亮度。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

232

主题

3223

帖子

12

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