[DemoCode下载] PWM示例M051

[复制链接]
3986|14
 楼主| gaoyang9992006 发表于 2014-12-21 16:21 | 显示全部楼层 |阅读模式
PWM, TE, TI, se, ck
  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: 3 $
  5. * $Date: 14/01/28 11:44a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    M051 Series PWM Generator and Capture Timer Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "M051Series.h"

  14. /*---------------------------------------------------------------------------------------------------------*/
  15. /* Macro, type and constant definitions                                                                    */
  16. /*---------------------------------------------------------------------------------------------------------*/

  17. #define PLLCON_SETTING      CLK_PLLCON_50MHz_HXT
  18. #define PLL_CLOCK           50000000

  19. // Scale frequency and unit is Hz
  20. #define TENOR_C 523
  21. #define TENOR_D 587
  22. #define TENOR_E 659
  23. #define TENOR_F 698
  24. #define TENOR_G 784
  25. #define TENOR_A 880
  26. #define TENOR_B 988

  27. void PWM_PwmIRQHandler(void);
  28. /*---------------------------------------------------------------------------------------------------------*/
  29. /* Global variables                                                                                        */
  30. /*---------------------------------------------------------------------------------------------------------*/
  31. volatile uint8_t g_u8PWMCount = 1;
  32. volatile uint16_t g_u16Frequency;
  33. volatile uint32_t g_u32Pulse = 0;

  34. /* Assume PWM output frequency is 523Hz and duty ratio is 60%, user can calculate PWM settings by follows.
  35.    PWM clock source frequency = __HXT = 12000000 in the sample code.
  36.    (CNR+1) = PWM clock source frequency/prescaler/clock source divider/PWM output frequency
  37.            = 12000000/2/1/523 = 11472 < 65536  (Note: If calculated value is larger than 65536, user should increase prescale value.)
  38.    CNR = 11471 =>g_au16ScaleCnr[0] = 11471
  39.    duty ratio = 60% = (CMR+1)/(CNR+1) ==> CMR = (CNR+1)*60/100-1 = 11472*60/100-1
  40.    CMR = 6882 =>g_au16ScaleCmr[0] = 6882
  41. */
  42. static const uint16_t g_au16ScaleFreq[7] = {TENOR_C, TENOR_D, TENOR_E, TENOR_F, TENOR_G, TENOR_A, TENOR_B};
  43. // static const uint16_t g_au16ScaleCnr[7] =  {11471,10220,9103,8594,7652,6817,6071};
  44. // static const uint16_t g_au16ScaleCmr[7] =  {6882 ,6131 ,5461,5156,4590,4089,3642};

  45. /**
  46. * @brief       PWMA IRQ Handler
  47. *
  48. * @param       None
  49. *
  50. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  51. *
  52. * @details     ISR to handle PWMA interrupt event
  53. */
  54. void PWMA_IRQHandler(void)
  55. {
  56.     uint32_t u32PwmIntFlag;

  57.     /* Handle PWMA Timer function */
  58.     u32PwmIntFlag = PWMA->PIIR;

  59.     /* PWMB channel 0 PWM timer interrupt */
  60.     if(u32PwmIntFlag & PWM_PIIR_PWMIF0_Msk)
  61.     {
  62.         PWMA->PIIR = PWM_PIIR_PWMIF0_Msk;
  63.         PWM_PwmIRQHandler();
  64.     }
  65. }


  66. /*---------------------------------------------------------------------------------------------------------*/
  67. /* PWM Timer function                                                                                      */
  68. /*---------------------------------------------------------------------------------------------------------*/
  69. void PWM_PwmIRQHandler(void)
  70. {
  71.     if(g_u32Pulse == 1 * g_u16Frequency / 10)
  72.     {
  73.         /*--------------------------------------------------------------------------------------*/
  74.         /* Stop PWMA channel 0 Timer (Recommended procedure method 2)                           */
  75.         /* Set PWM Timer counter as 0, When interrupt request happen, disable PWM Timer         */
  76.         /*--------------------------------------------------------------------------------------*/
  77.         PWMA->CNR0 = 0;
  78.     }

  79.     if(g_u32Pulse == 1 * g_u16Frequency / 10 + 1)
  80.         g_u8PWMCount = 0;
  81.     g_u32Pulse++;
  82. }


  83. void SYS_Init(void)
  84. {
  85.     /*---------------------------------------------------------------------------------------------------------*/
  86.     /* Init System Clock                                                                                       */
  87.     /*---------------------------------------------------------------------------------------------------------*/

  88.     /* Enable Internal RC clock */
  89.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  90.     /* Waiting for IRC22M clock ready */
  91.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

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

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

  96.     /* Enable PLL and Set PLL frequency */
  97.     CLK_SetCoreClock(PLLCON_SETTING);

  98.     /* Waiting for clock ready */
  99.     CLK_WaitClockReady(CLK_CLKSTATUS_PLL_STB_Msk | CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_OSC22M_STB_Msk);

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

  102.     /* Enable UART module clock */
  103.     CLK_EnableModuleClock(UART0_MODULE);

  104.     /* Enable PWM module clock */
  105.     CLK_EnableModuleClock(PWM01_MODULE);
  106.     CLK_EnableModuleClock(PWM23_MODULE);

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

  109.     /* Select PWM module clock source */
  110.     CLK_SetModuleClock(PWM01_MODULE, CLK_CLKSEL1_PWM01_S_HXT, 0);
  111.     CLK_SetModuleClock(PWM23_MODULE, CLK_CLKSEL1_PWM23_S_HXT, 0);

  112.     /* Reset PWMA channel0~channel3 */
  113.     SYS_ResetModule(PWM03_RST);

  114.     /* Update System Core Clock */
  115.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  116.     //SystemCoreClockUpdate();
  117.     PllClock        = PLL_CLOCK;            // PLL
  118.     SystemCoreClock = PLL_CLOCK / 1;        // HCLK
  119.     CyclesPerUs     = PLL_CLOCK / 1000000;  // For SYS_SysTickDelay()

  120.     /*---------------------------------------------------------------------------------------------------------*/
  121.     /* Init I/O Multi-function                                                                                 */
  122.     /*---------------------------------------------------------------------------------------------------------*/
  123.     /* Set P3 multi-function pins for UART0 RXD and TXD  */
  124.     SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;
  125.     /* Set P4 multi-function pins for PWMA Channel0 */
  126.     SYS->P4_MFP = SYS_MFP_P40_PWM0;
  127. }


  128. void UART0_Init(void)
  129. {

  130.     /*---------------------------------------------------------------------------------------------------------*/
  131.     /* Init UART                                                                                               */
  132.     /*---------------------------------------------------------------------------------------------------------*/
  133.     /* Configure UART0 and set UART0 Baudrate */
  134.     UART_Open(UART0, 115200);
  135. }



  136. /*---------------------------------------------------------------------------------------------------------*/
  137. /*  Main Function                                                                                          */
  138. /*---------------------------------------------------------------------------------------------------------*/
  139. int32_t main(void)
  140. {
  141.     uint8_t u8Item, u8ItemOK;

  142.     /* Unlock protected registers */
  143.     SYS_UnlockReg();

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

  146.     /* Lock protected registers */
  147.     SYS_LockReg();

  148.     /* Init UART0 for printf */
  149.     UART0_Init();

  150.     printf("+------------------------------------------------------------------------+\n");
  151.     printf("|                          PWM Driver Sample Code                        |\n");
  152.     printf("|                                                                        |\n");
  153.     printf("+------------------------------------------------------------------------+\n");
  154.     printf("  This sample code will use PWMA channel 0 to drive Buzzer\n");
  155.     printf("  I/O configuration:\n");
  156.     printf("    PWM0(P4.0 PWMA channel 0) <--> Buzzer\n");
  157.     printf("\nPWM Timer Waveform Test. Waveform output(P4.0 PWMA channel 0) to Buzzer\n");
  158.     /* P4.0 PWMA channel 0 generates PWM frequency Do - Si */

  159.     printf("Select Test Item\n");
  160.     printf(" 1: Do (523Hz)Tenor C\n");
  161.     printf(" 2: Re (587Hz)\n");
  162.     printf(" 3: Mi (659Hz)\n");
  163.     printf(" 4: Fa (698Hz)\n");
  164.     printf(" 5: Sol(784Hz)\n");
  165.     printf(" 6: La (880Hz)\n");
  166.     printf(" 7: Si (988Hz)\n");

  167.     while(1)
  168.     {
  169.         u8ItemOK = 1;
  170.         u8Item = getchar();
  171.         printf("\t");

  172.         switch(u8Item)
  173.         {
  174.         case '1':
  175.         case '2':
  176.         case '3':
  177.         case '4':
  178.         case '5':
  179.         case '6':
  180.         case '7':
  181.             g_u16Frequency = g_au16ScaleFreq[(u8Item - '1')];
  182.             break;
  183.         default:
  184.             u8ItemOK = 0;
  185.             break;
  186.         }

  187.         if(u8ItemOK)
  188.         {
  189.             g_u32Pulse = 0;
  190.             g_u8PWMCount = 1;

  191.             /* Assume PWM output frequency is 523Hz and duty ratio is 60%, user can calculate PWM settings by follows.
  192.                duty ratio = (CMR+1)/(CNR+1)
  193.                cycle time = CNR+1
  194.                High level = CMR+1
  195.                PWM clock source frequency = __HXT = 12000000
  196.                (CNR+1) = PWM clock source frequency/prescaler/clock source divider/PWM output frequency
  197.                        = 12000000/2/1/523 = 11472
  198.                (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.)
  199.                CNR = 11471
  200.                duty ratio = 60% ==> (CMR+1)/(CNR+1) = 60% ==> CMR = (CNR+1)*0.6-1 = 11472*60/100-1
  201.                CMR = 6882
  202.                Prescale value is 1 : prescaler = 2
  203.                Clock divider is PWM_CSR_DIV1 : clock divider = 1
  204.             */

  205.             /* Enable PWM Output pin */
  206.             PWM_EnableOutput(PWMA, 0x1);

  207.             PWM_ConfigOutputChannel(PWMA, PWM_CH0, g_u16Frequency, 60);

  208.             /* Enable Timer period Interrupt */
  209.             PWM_EnablePeriodInt(PWMA, PWM_CH0, PWM_PERIOD_INT_UNDERFLOW);

  210.             /* Enable PWMB NVIC */
  211.             NVIC_EnableIRQ((IRQn_Type)(PWMA_IRQn));

  212.             /* Enable PWM Timer */
  213.             PWM_Start(PWMA, 0x1);

  214.             while(g_u8PWMCount);

  215.             /*--------------------------------------------------------------------------------------*/
  216.             /* Stop PWM Timer (Recommended procedure method 2)                                      */
  217.             /* Set PWM Timer counter as 0, When interrupt request happen, disable PWM Timer         */
  218.             /* Set PWM Timer counter as 0 in Call back function                                     */
  219.             /*--------------------------------------------------------------------------------------*/

  220.             /* Disable PWMB NVIC */
  221.             NVIC_DisableIRQ((IRQn_Type)(PWMA_IRQn));

  222.             /* Wait until PWMB channel 0 Timer Stop */
  223.             while(PWMA->PDR0 != 0);

  224.             /* Disable the PWM Timer */
  225.             PWM_Stop(PWMA, 0x1);

  226.             /* Disable PWM Output pin */
  227.             PWM_DisableOutput(PWMA, 0x1);
  228.         }
  229.     }
  230. }





 楼主| gaoyang9992006 发表于 2014-12-21 16:22 | 显示全部楼层
代码之所以长,就是要写的标准,方便移植。
 楼主| gaoyang9992006 发表于 2014-12-21 16:23 | 显示全部楼层
通过PWM可以控制电机,示例中控制了一枚扬声器发生,发出都如艾米法搜拉下的音调。
598330983 发表于 2014-12-22 16:48 | 显示全部楼层
有点意思,嘿嘿,这个PWM还可以控制喇叭。
mintspring 发表于 2014-12-22 17:10 | 显示全部楼层
开头的宏定义很重要啊。
RAYINGPX 发表于 2017-7-4 15:53 | 显示全部楼层
这个例程的PWM的占空比一直是60%,只是改变了频率而已,频率变了声音也变了。如果要改变占空比,是要在中断中改变CMR的数值吧
RAYINGPX 发表于 2017-7-4 16:01 | 显示全部楼层
  /* Enable Timer period Interrupt */
    PWM_EnablePeriodInt(PWMA, PWM_CH0, PWM_PERIOD_INT_UNDERFLOW);

楼主,请问这句意思是说:设置定时器为周期中断吗,是CNR减小到0时触发中断吗?
RAYINGPX 发表于 2017-7-4 16:05 | 显示全部楼层
/*---------------------------------------------------------------------------------------------------------*/
/* PWM Timer function                                                                                      */
/*---------------------------------------------------------------------------------------------------------*/
void PWM_PwmIRQHandler(void)
{
    if(g_u32Pulse == 1 * g_u16Frequency / 10)
    {
        /*--------------------------------------------------------------------------------------*/
        /* Stop PWMA channel 0 Timer (Recommended procedure method 2)                           */
        /* Set PWM Timer counter as 0, When interrupt request happen, disable PWM Timer         */
        /*--------------------------------------------------------------------------------------*/
        PWMA->CNR0 = 0;
    }

    if(g_u32Pulse == 1 * g_u16Frequency / 10 + 1)
        g_u8PWMCount = 0;
    g_u32Pulse++;
}
这个PWM的定时器中断就是由
/* Enable Timer period Interrupt */
  PWM_EnablePeriodInt(PWMA, PWM_CH0, PWM_PERIOD_INT_UNDERFLOW);
这个使能开启的吧?

不好意思,新人刚接触,问题多
玛尼玛尼哄 发表于 2017-7-5 18:43 | 显示全部楼层
新唐单片机是很强大的,有时候不能拿别的单片机来揣度。
yyglucky 发表于 2017-7-5 20:21 | 显示全部楼层
dongnanxibei 发表于 2017-7-5 21:19 | 显示全部楼层
PWM可以作为波形发生器使用,也是很方便,配置好,自动工作。
gejigeji521 发表于 2017-7-6 11:02 | 显示全部楼层
拿到板子了。学习中。
wahahaheihei 发表于 2017-7-6 18:44 | 显示全部楼层
是否可以理解为,各种终端处理函数的名字是定下来的,不能修改。
xixi2017 发表于 2017-7-6 19:04 | 显示全部楼层
如果程序中执行设置,是不是都要进行解锁。。
yiy 发表于 2017-7-6 22:55 | 显示全部楼层
看起来很有通用性。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:如果你觉得我的分享或者答复还可以,请给我点赞,谢谢。

2058

主题

16431

帖子

222

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