[DemoCode下载] N79E715的PWM操作方法

[复制链接]
1520|4
 楼主| huangcunxiake 发表于 2017-1-10 22:55 | 显示全部楼层 |阅读模式
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technology Corp.
  8. //  E-mail: MicroC-8bit@nuvoton.com
  9. //***********************************************************************************************************
  10. //  Application: PWM Function
  11. //  Set PWM output channel, PWM frequency and PWM duty before PWM running
  12. //  PWM will be braked to a stop when P0.2 is low level
  13. //
  14. //  Output : PWM channel 0 => P0.1 (default)
  15. //           PWM channel 1 => P1.6
  16. //           PWM channel 2 => P1.7
  17. //           PWM channel 3 => P0.0
  18. //***********************************************************************************************************

  19. //========================================= How to set PWM register =========================================
  20. //
  21. //  1.set load = 1; PWMRUN=1; ==> PWMCON0=0xC0;
  22. //
  23. //  2.Frequency == Fpwm/(PWMP+1)
  24. //
  25. //  3.Duty = PWMn/PWMP+1
  26. //
  27. //===========================================================================================================

  28. //------------------------- <<< Use Configuration Wizard in Context Menu >>> --------------------------------
  29. //      <o0.0..9> PWM Period<0-1023>
  30. //      <o1.0..9> PWM0(P01) Duty<0-1023>
  31. //      <o2.0..9> PWM1(P16) Duty<0-1023>
  32. //      <o3.0..9> PWM2(P17) Duty<0-1023>
  33. //      <o4.0..9> PWM3(P00) Duty<0-1023>
  34. //     <o5.6> UART pin Select
  35. //          <0=> Select P1.0, P1.1 as UART pin(default)
  36. //          <1=> Select P2.6, P2.7 as UART pin(28 pin only)
  37. //-------------------------------- <<< end of configuration section >>> -------------------------------------


  38. #define PWMP            0x3FF
  39. #define PWM0_Duty       0x200
  40. #define PWM1_Duty       0x100
  41. #define PWM2_Duty       0x080
  42. #define PWM3_Duty       0x040
  43. #define Uart_Port_Sel   0x00

  44. typedef enum
  45. {
  46.     E_CHANNEL0,
  47.     E_CHANNEL1,
  48.     E_CHANNEL2,
  49.     E_CHANNEL3,
  50. } E_PWMCNL_SEL;

  51. #include <stdio.h>
  52. #include "N79E715.h"
  53. #include "Typedef.h"
  54. #include "Define.h"
  55. #include "Common.h"
  56. #include "Version.h"
  57. #include <intrins.h>

  58. UINT16 PWM_shadow;
  59. //-----------------------------------------------------------------------------------------------------------
  60. void Init_PWM(void)
  61. {
  62.     AUXR1 |= Uart_Port_Sel;             // Select P10/P11 as UART pin(default)
  63.     InitialUART0_Timer1(9600);          // 9600 Baud Rate [url=home.php?mod=space&uid=72445]@[/url] 11.0592MHz
  64.     printf ("\n*===================================================================");
  65.     printf ("\n*  Name: N79E715 Series PWM Sample Code.");
  66.     printf ("\n*===================================================================");
  67.     printf ("\nPWM Demo Start.\n");


  68.     PWM_shadow = PWMP;
  69.     PWMPH = HIBYTE(PWM_shadow);         // PWM Period
  70.     PWMPL = LOBYTE(PWM_shadow);
  71.     PWMCON0 |= 0xD0;                    // Enable PWM and select auto reload mode
  72. }
  73. //------------------------------------------------------------------------------
  74. void PWM_Pin_Recover_to_High(void)
  75. {
  76.     PWMCON0 = 0x00;
  77.     PWMCON1 = 0x00;
  78.     PWMCON2 = 0x00;
  79.     PWMPH   = 0x00;
  80.     PWMPL   = 0x01;
  81.     PWM0H   = 0x00;
  82.     PWM0L   = 0x00;
  83.     PWM1H   = 0x00;
  84.     PWM1L   = 0x00;
  85.     PWM2H   = 0x00;
  86.     PWM2L   = 0x00;
  87.     PWM3H   = 0x00;
  88.     PWM3L   = 0x00;
  89.     PWMCON0 |= 0xD0;// PWM always non-active(high).
  90.     _nop_ ();       // NOP
  91.     PWMCON0 &= 0x0F;// PWM Stop
  92.     PWMPL   = 0x00; // Recover to reset value
  93. }
  94. //------------------------------------------------------------------------------
  95. void PWM_Channel(E_PWMCNL_SEL Channel)
  96. {
  97.     switch(Channel)
  98.     {
  99.         case E_CHANNEL0:
  100.             PWM_shadow=PWM0_Duty;
  101.             PWM0H = HIBYTE(PWM_shadow);// PWM0 Duty (P0.1)
  102.             PWM0L = LOBYTE(PWM_shadow);
  103.             break;
  104.         case E_CHANNEL1:
  105.             PWM_shadow=PWM1_Duty;
  106.             PWM1H = HIBYTE(PWM_shadow);// PWM1 Duty (P1.6)
  107.             PWM1L = LOBYTE(PWM_shadow);
  108.             break;
  109.         case E_CHANNEL2:
  110.             PWM_shadow=PWM2_Duty;
  111.             PWM2H = HIBYTE(PWM_shadow);// PWM2 Duty (P1.7)
  112.             PWM2L = LOBYTE(PWM_shadow);
  113.             break;
  114.         case E_CHANNEL3:
  115.             PWM_shadow=PWM3_Duty;
  116.             PWM3H = HIBYTE(PWM_shadow);// PWM3 Duty (P0.0)
  117.             PWM3L = LOBYTE(PWM_shadow);
  118.             break;
  119.     }
  120. }
  121. //------------------------------------------------------------------------------
  122. void Enabled_Brake_Function(void)
  123. {
  124.     PWMCON1 = 0x30;                    // PWM is running. PWM will be stopped when P0.2 is low level.
  125. }
  126. //-----------------------------------------------------------------------------------------------------------
  127. void main(void)
  128. {
  129.     PWM_Channel(E_CHANNEL0);           // Select PWM channel.
  130.     PWM_Channel(E_CHANNEL1);           // Select PWM channel.
  131.     PWM_Channel(E_CHANNEL2);           // Select PWM channel.
  132.     PWM_Channel(E_CHANNEL3);           // Select PWM channel.
  133.     Init_PWM();                        // Enable PWM function and set PWM period.
  134.     Enabled_Brake_Function();          // Enable Brake function.
  135.     while(1);
  136. }
  137. //-----------------------------------------------------------------------------------------------------------


 楼主| huangcunxiake 发表于 2017-1-10 22:56 | 显示全部楼层
通过这例程可以充分体验到这个单片机PWM的强大的功能。
kk5004 发表于 2017-1-11 09:26 | 显示全部楼层
很好啊
gejigeji521 发表于 2017-1-11 19:23 | 显示全部楼层
switch case用的果然出神入化。
643757107 发表于 2017-1-11 23:56 | 显示全部楼层
一共4个通道。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

223

主题

3760

帖子

11

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