[DemoCode下载] 求MNIN51 用PWM点LED渐明渐暗示例程序

[复制链接]
2042|7
 楼主| wuchengyong 发表于 2017-3-14 14:46 | 显示全部楼层 |阅读模式
请那位大哥提供一个,MNIN51  用PWM点LED渐明渐暗示例程序 。开始学习新塘MCU

huangcunxiake 发表于 2017-3-14 18:44 | 显示全部楼层
  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/09/24 5:28p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Demonstrate the PWM double buffer feature.
  7. *
  8. * @note
  9. * Copyright (C) 2015 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #include <stdio.h>
  12. #include "Mini51Series.h"

  13. uint32_t duty30, duty60;
  14. void PWM_IRQHandler(void)
  15. {
  16.     static int toggle = 0;  // First two already fill into PWM, so start from 30%

  17.     // Update PWM channel 0 duty
  18.     if(toggle == 0) {
  19.         PWM_SET_CMR(PWM, 0, duty30);
  20.     } else {
  21.         PWM_SET_CMR(PWM, 0, duty60);
  22.     }
  23.     toggle ^= 1;
  24.     // Clear channel 0 period interrupt flag
  25.     PWM_ClearPeriodIntFlag(PWM, 0);
  26. }



  27. void SYS_Init(void)
  28. {
  29.     /*---------------------------------------------------------------------------------------------------------*/
  30.     /* Init System Clock                                                                                       */
  31.     /*---------------------------------------------------------------------------------------------------------*/

  32.     /* Unlock protected registers */
  33.     SYS_UnlockReg();

  34.     /* Set P5 multi-function pins for XTAL1 and XTAL2 */
  35.     SYS->P5_MFP = SYS_MFP_P50_XTAL1 | SYS_MFP_P51_XTAL2;

  36.     /* Enable external 12MHz XTAL (UART), HIRC */
  37.     CLK->PWRCON = CLK_PWRCON_OSC22M_EN_Msk | CLK_PWRCON_HXT;

  38.     /* Waiting for clock ready */
  39.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk | CLK_CLKSTATUS_XTL_STB_Msk);

  40.     /* Switch HCLK clock source to XTL */
  41.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_XTAL,CLK_CLKDIV_HCLK(1));

  42.     /* Enable IP clock */
  43.     CLK->APBCLK = CLK_APBCLK_UART_EN_Msk | CLK_APBCLK_PWM01_EN_Msk;

  44.     /* Select UART clock source from external crystal*/
  45.     CLK->CLKSEL1 = (CLK->CLKSEL1 & ~CLK_CLKSEL1_UART_S_Msk) | CLK_CLKSEL1_UART_S_XTAL;

  46.     /* Update System Core Clock */
  47.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CycylesPerUs automatically. */
  48.     SystemCoreClockUpdate();


  49.     /*---------------------------------------------------------------------------------------------------------*/
  50.     /* Init I/O Multi-function                                                                                 */
  51.     /*---------------------------------------------------------------------------------------------------------*/
  52.     /* Set P0 multi-function pins for UART RXD, TXD */
  53.     SYS->P0_MFP = SYS_MFP_P00_TXD | SYS_MFP_P01_RXD;


  54.     /* Set P2 multi-function pin for PWM Channel 0  */
  55.     SYS->P2_MFP = SYS_MFP_P22_PWM0;


  56.     /* Lock protected registers */
  57.     SYS_LockReg();
  58. }

  59. int32_t main (void)
  60. {
  61.     /* Init System, IP clock and multi-function I/O
  62.        In the end of SYS_Init() will issue SYS_LockReg()
  63.        to lock protected register. If user want to write
  64.        protected register, please issue SYS_UnlockReg()
  65.        to unlock protected register if necessary */
  66.     SYS_Init();

  67.     /* Init UART to 115200-8n1 for print message */
  68.     UART_Open(UART0, 115200);

  69.     printf("This sample changed PWM0 output between 30%% and 60%% duty ratio\n");
  70.     /*
  71.         PWM channel 0 wave form of this sample changed between 30% and 60% duty ratio
  72.     */
  73.     PWM_ConfigOutputChannel(PWM, 0, 1000, 30);

  74.     // Save 30% duty setting
  75.     duty30 = PWM->CMR[0];
  76.     // Calculate 60% duty setting. CMR store the actual value minus 1.
  77.     duty60 = (duty30 + 1) * 2 - 1;

  78.     // Enable output of all PWM channel 0
  79.     PWM_EnableOutput(PWM, 1);

  80.     // Enable PWM channel 0 period interrupt
  81.     PWM_EnablePeriodInt(PWM, 0, PWM_PERIOD_INT_UNDERFLOW);
  82.     NVIC_EnableIRQ(PWM_IRQn);

  83.     // Start
  84.     PWM_Start(PWM, 0x1);

  85.     // Fill second duty setting immediately after PWM start
  86.     PWM_SET_CMR(PWM, 0, duty60);

  87.     while(1);

  88. }

  89. /*** (C) COPYRIGHT 2015 Nuvoton Technology Corp. ***/

huangcunxiake 发表于 2017-3-14 18:45 | 显示全部楼层
这个是官方提供的例程序,你可以修改一下就能用了。一会儿再给你个。
huangcunxiake 发表于 2017-3-14 18:47 | 显示全部楼层

  1. /******************************************************************************
  2. * @file     main.c
  3. * @version  V1.00
  4. * $Revision: 7 $
  5. * $Date: 15/10/06 1:21p $
  6. * @brief    Demonstrate the dead-zone feature with PWM.
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #include <stdio.h>
  12. #include "Mini51Series.h"


  13. void PWM_IRQHandler(void)
  14. {
  15.     static uint32_t cnt;
  16.     static uint32_t out;

  17.     // Channel 0 frequency is 100Hz, every 1 second enter this IRQ handler 100 times.
  18.     if(++cnt == 100) {
  19.         if(out)
  20.             PWM_EnableOutput(PWM, 0x3F);
  21.         else
  22.             PWM_DisableOutput(PWM, 0x3F);
  23.         out ^= 1;
  24.         cnt = 0;
  25.     }
  26.     // Clear channel 0 period interrupt flag
  27.     PWM_ClearPeriodIntFlag(PWM, 0);
  28. }


  29. void SYS_Init(void)
  30. {
  31.     /*---------------------------------------------------------------------------------------------------------*/
  32.     /* Init System Clock                                                                                       */
  33.     /*---------------------------------------------------------------------------------------------------------*/

  34.     /* Unlock protected registers */
  35.     SYS_UnlockReg();

  36.     /* Set P5 multi-function pins for XTAL1 and XTAL2 */
  37.     SYS->P5_MFP &= ~(SYS_MFP_P50_Msk | SYS_MFP_P51_Msk);
  38.     SYS->P5_MFP |= (SYS_MFP_P50_XTAL1 | SYS_MFP_P51_XTAL2);

  39.     /* Enable external 12MHz XTAL, internal 22.1184MHz */
  40.     CLK->PWRCON = CLK_PWRCON_XTL12M | CLK_PWRCON_IRC22M_EN_Msk;

  41.     /* Waiting for clock ready */
  42.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL_STB_Msk | CLK_CLKSTATUS_IRC22M_STB_Msk);

  43.     /* Enable IP clock */
  44.     CLK->APBCLK = CLK_APBCLK_UART_EN_Msk | CLK_APBCLK_PWM01_EN_Msk | CLK_APBCLK_PWM23_EN_Msk | CLK_APBCLK_PWM45_EN_Msk;

  45.     /* Select UART clock source from external crystal*/
  46.     CLK->CLKSEL1 = (CLK->CLKSEL1 & ~CLK_CLKSEL1_UART_S_Msk) | CLK_CLKSEL1_UART_S_XTAL;

  47.     /* Update System Core Clock */
  48.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CycylesPerUs automatically. */
  49.     SystemCoreClockUpdate();


  50.     /*---------------------------------------------------------------------------------------------------------*/
  51.     /* Init I/O Multi-function                                                                                 */
  52.     /*---------------------------------------------------------------------------------------------------------*/
  53.     /* Set P1 multi-function pins for UART RXD, TXD */
  54.     SYS->P0_MFP = SYS_MFP_P00_TXD | SYS_MFP_P01_RXD;

  55.     /* Set P0 multi-function pins for PWM Channel 5  */
  56.     SYS->P0_MFP = SYS_MFP_P04_PWM5;
  57.     /* Set P2 multi-function pins for PWM Channel 0~4  */
  58.     SYS->P2_MFP = SYS_MFP_P22_PWM0 | SYS_MFP_P23_PWM1 | SYS_MFP_P24_PWM2 | SYS_MFP_P25_PWM3 | SYS_MFP_P26_PWM4;


  59.     /* Lock protected registers */
  60.     SYS_LockReg();
  61. }


  62. int32_t main (void)
  63. {
  64.     /* Init System, IP clock and multi-function I/O
  65.        In the end of SYS_Init() will issue SYS_LockReg()
  66.        to lock protected register. If user want to write
  67.        protected register, please issue SYS_UnlockReg()
  68.        to unlock protected register if necessary */
  69.     SYS_Init();

  70.     /* Init UART to 115200-8n1 for print message */
  71.     UART_Open(UART, 115200);

  72.     printf("\nThis sample code will output PWM channel 0 to with different\n");
  73.     printf("frequency and duty, enable dead zone function of all PWM pairs.\n");
  74.     printf("And also enable/disable PWM output every 1 second.\n");
  75.     // PWM0 frequency is 100Hz, duty 30%,
  76.     PWM_ConfigOutputChannel(PWM, 0, 100, 30);
  77.     PWM_EnableDeadZone(PWM, 0, 400);

  78.     // PWM2 frequency is 300Hz, duty 50%
  79.     PWM_ConfigOutputChannel(PWM, 2, 300, 50);
  80.     PWM_EnableDeadZone(PWM, 2, 200);

  81.     // PWM4 frequency is 500Hz, duty 70%
  82.     PWM_ConfigOutputChannel(PWM, 4, 600, 70);
  83.     PWM_EnableDeadZone(PWM, 4, 100);

  84.     // Enable output of all PWM channels
  85.     PWM_EnableOutput(PWM, 0x3F);

  86.     // Enable PWM channel 0 period interrupt, use channel 0 to measure time.
  87.     PWM_EnablePeriodInt(PWM, 0, 0);
  88.     NVIC_EnableIRQ(PWM_IRQn);

  89.     // Start
  90.     PWM_Start(PWM, 0x3F);

  91.     while(1);

  92. }

  93. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/

 楼主| wuchengyong 发表于 2017-3-14 18:52 | 显示全部楼层
好的。先了解一下,谢谢!!!
598330983 发表于 2017-3-14 20:39 | 显示全部楼层
http://www.nuvoton.com/resource- ... CMSIS_v3.01.001.zip
下载这个开发包啊,然后例子和库函数都有,参考一下。
mintspring 发表于 2017-3-14 20:52 | 显示全部楼层
弄个循环操作PWM。
wahahaheihei 发表于 2017-3-17 19:17 | 显示全部楼层
也可以用定时器模拟这个PWM。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

6

帖子

0

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