[牛人杂谈] 最新版N76E003的BSP1.06版本修复了PWM中断例程

[复制链接]
1016|1
 楼主| xuanhuanzi 发表于 2018-7-31 17:27 | 显示全部楼层 |阅读模式
之前的版本大家都说没法用,其实真滴没法用,新版的修复了这个问题
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technoledge Corp.
  8. //  Website: http://www.nuvoton.com
  9. //  E-Mail : MicroC-8bit@nuvoton.com
  10. //  Date   : Apr/21/2017
  11. //***********************************************************************************************************

  12. //***********************************************************************************************************
  13. //  File Function: N76E003 PWM Output demo code
  14. //***********************************************************************************************************
  15. #include "N76E003.h"
  16. #include "Common.h"
  17. #include "Delay.h"
  18. #include "SFR_Macro.h"
  19. #include "Function_define.h"


  20. //*****************  The Following is in define in Fucntion_define.h  ***************************
  21. //****** Always include Function_define.h call the define you want, detail see main(void) *******
  22. //***********************************************************************************************
  23. #if 0
  24. //--------- PMW clock source select define ---------------------
  25. //#define                PWM_CLOCK_FSYS                                        CKCON&=0xBF
  26. //#define                PWM_CLOCK_TIMER1                                CKCON|=0x40
  27. ////--------- PWM clock devide define ----------------------------
  28. //#define                PWM_CLOCK_DIV_2                                        PWMCON1|=0x01;PWMCON1&=0xF9
  29. //#define                PWM_CLOCK_DIV_4                                        PWMCON1|=0x02;PWMCON1&=0xFA
  30. //#define                PWM_CLOCK_DIV_8                                        PWMCON1|=0x03;PWMCON1&=0xFB
  31. //#define                PWM_CLOCK_DIV_16                                PWMCON1|=0x04;PWMCON1&=0xFC
  32. //#define                PWM_CLOCK_DIV_32                                PWMCON1|=0x05;PWMCON1&=0xFD
  33. //#define                PWM_CLOCK_DIV_64                                PWMCON1|=0x06;PWMCON1&=0xFE
  34. //#define                PWM_CLOCK_DIV_128                                PWMCON1|=0x07
  35. ////--------- PWM I/O select define ------------------------------
  36. //#define                PWM5_P15_OUTPUT_ENABLE                BIT_TMP=EA;EA=0;TA=0xAA;TA=0x55;SFRS|=0x01;PIOCON1|=0x20;TA=0xAA;TA=0x55;SFRS&=0xFE;EA=BIT_TMP                                //P1.5 as PWM5 output enable
  37. //#define                PWM5_P03_OUTPUT_ENABLE                PIOCON0|=0x20                                                                                                                                                                                                                //P0.3 as PWM5
  38. //#define                PWM4_P01_OUTPUT_ENABLE                PIOCON0|=0x10                                                                                                                                                                                                                //P0.1 as PWM4 output enable
  39. //#define                PWM3_P04_OUTPUT_ENABLE                BIT_TMP=EA;EA=0;TA=0xAA;TA=0x55;SFRS|=0x01;PIOCON1|=0x08;TA=0xAA;TA=0x55;SFRS&=0xFE;EA=BIT_TMP                                //P0.4 as PWM3 output enable
  40. //#define                PWM3_P00_OUTPUT_ENABLE                PIOCON0|=0x08                                                                                                                                                                                                                //P0.0 as PWM3
  41. //#define                PWM2_P05_OUTPUT_ENABLE                BIT_TMP=EA;EA=0;TA=0xAA;TA=0x55;SFRS|=0x01;PIOCON1|=0x04;TA=0xAA;TA=0x55;SFRS&=0xFE;EA=BIT_TMP                                //P1.0 as PWM2 output enable
  42. //#define                PWM2_P10_OUTPUT_ENABLE                PIOCON0|=0x04                                                                                                                                                                                                                //P1.0 as PWM2
  43. //#define                PWM1_P14_OUTPUT_ENABLE                BIT_TMP=EA;EA=0;TA=0xAA;TA=0x55;SFRS|=0x01;PIOCON1|=0x02;TA=0xAA;TA=0x55;SFRS&=0xFE;EA=BIT_TMP                                //P1.4 as PWM1 output enable
  44. //#define                PWM1_P11_OUTPUT_ENABLE                PIOCON0|=0x02                                                                                                                                                                                                                //P1.1 as PWM1
  45. //#define                PWM0_P12_OUTPUT_ENABLE                PIOCON0|=0x01                                                                                                                                                                                                                //P1.2 as PWM0 output enable
  46. //#define         ALL_PWM_OUTPUT_ENABLE                        PIOCON0=0xFF;PIOCON1=0xFF
  47. //#define                PWM5_P15_OUTPUT_DISABLE                BIT_TMP=EA;EA=0;TA=0xAA;TA=0x55;SFRS|=0x01;PIOCON1&=0xDF;TA=0xAA;TA=0x55;SFRS&=0xFE;EA=BIT_TMP                                //P1.5 as PWM5 output disable
  48. //#define                PWM5_P03_OUTPUT_DISABLE                PIOCON0&=0xDF                                                                                                                                                                                                                //P0.3 as PWM5
  49. //#define                PWM4_P01_OUTPUT_DISABLE                PIOCON0&=0xEF                                                                                                                                                                                                                //P0.1 as PWM4 output disable
  50. //#define                PWM3_P04_OUTPUT_DISABLE                BIT_TMP=EA;EA=0;TA=0xAA;TA=0x55;SFRS|=0x01;PIOCON1&=0xF7;TA=0xAA;TA=0x55;SFRS&=0xFE;EA=BIT_TMP                                //P0.4 as PWM3 output disable
  51. //#define                PWM3_P00_OUTPUT_DISABLE                PIOCON0&=0xF7                                                                                                                                                                                                                //P0.0 as PWM3
  52. //#define                PWM2_P05_OUTPUT_DISABLE                BIT_TMP=EA;EA=0;TA=0xAA;TA=0x55;SFRS|=0x01;PIOCON1&=0xFB;TA=0xAA;TA=0x55;SFRS&=0xFE;EA=BIT_TMP                                //P1.0 as PWM2 output disable
  53. //#define                PWM2_P10_OUTPUT_DISABLE                PIOCON0&=0xFB                                                                                                                                                                                                                //P1.0 as PWM2
  54. //#define                PWM1_P14_OUTPUT_DISABLE                BIT_TMP=EA;EA=0;TA=0xAA;TA=0x55;SFRS|=0x01;PIOCON1&=0xFD;TA=0xAA;TA=0x55;SFRS&=0xFE;EA=BIT_TMP                                //P1.4 as PWM1 output disable
  55. //#define                PWM1_P11_OUTPUT_DISABLE                PIOCON0&=0xFD                                                                                                                                                                                                                //P1.1 as PWM1
  56. //#define                PWM0_P12_OUTPUT_DISABLE                PIOCON0&=0xFE                                                                                                                                                                                                                //P1.2 as PWM0 output disable
  57. //#define         ALL_PWM_OUTPUT_DISABLE                PIOCON0=0x00;PIOCON1=0x00
  58. ////--------- PWM I/O Polarity Control ---------------------------
  59. //#define                PWM5_OUTPUT_INVERSE                        PNP|=0x20                               
  60. //#define                PWM4_OUTPUT_INVERSE                        PNP|=0x10                               
  61. //#define                PWM3_OUTPUT_INVERSE                        PNP|=0x08                               
  62. //#define                PWM2_OUTPUT_INVERSE                        PNP|=0x04                               
  63. //#define                PWM1_OUTPUT_INVERSE                        PNP|=0x02                               
  64. //#define                PWM0_OUTPUT_INVERSE                        PNP|=0x01                               
  65. //#define                PWM_OUTPUT_ALL_INVERSE        PNP=0xFF
  66. //#define                PWM5_OUTPUT_NORMAL                        PNP&=0xDF                               
  67. //#define                PWM4_OUTPUT_NORMAL                        PNP&=0xEF                               
  68. //#define                PWM3_OUTPUT_NORMAL                        PNP&=0xF7                               
  69. //#define                PWM2_OUTPUT_NORMAL                        PNP&=0xFB                               
  70. //#define                PWM1_OUTPUT_NORMAL                        PNP&=0xFD                               
  71. //#define                PWM0_OUTPUT_NORMAL                        PNP&=0xFE                               
  72. //#define                PWM_OUTPUT_ALL_NORMAL                PNP=0x00
  73. ////--------- PWM type define ------------------------------------
  74. //#define                PWM_EDGE_TYPE                                                PWMCON1&=~SET_BIT4
  75. //#define                PWM_CENTER_TYPE                                        PWMCON1|=SET_BIT4
  76. ////--------- PWM mode define ------------------------------------
  77. //#define                PWM_IMDEPENDENT_MODE                PWMCON1&=0x3F
  78. //#define                PWM_COMPLEMENTARY_MODE        PWMCON1|=0x40;PWMCON1&=0x7F
  79. //#define                PWM_SYNCHRONIZED_MODE                PWMCON1|=0x80;PWMCON1&=0xBF
  80. //#define         PWM_GP_MODE_ENABLE                        PWMCON1|=0x20
  81. //#define                PWM_GP_MODE_DISABLE                        PWMCON1&=0xDF
  82. ////--------- PMW interrupt setting ------------------------------
  83. //#define                PWM_FALLING_INT                                        PWMCON0&=0xF3
  84. //#define                PWM_RISING_INT                                        PWMCON0|=0x04;PWMCON0&=0xF7
  85. //#define                PWM_CENTRAL_POINT_INT                PWMCON0|=0x08;PWMCON0&=0xFB
  86. //#define                PWM_PERIOD_END_INT                        PWMCON0|=0x0C
  87. ////--------- PWM interrupt pin select ---------------------------
  88. //#define                PWM_INT_PWM0                                                PWMCON0&=0xFC
  89. //#define                PWM_INT_PWM2                                                PWMCON0|=0x01;PWMCON0&=0xFD
  90. //#define                PWM_INT_PWM4                                                PWMCON0|=0x02;PWMCON0&=0xFE
  91. //#define                PWM_INT_PMW6                                                PWMCON0|=0x03
  92. ////--------- PWM Dead time setting ------------------------------
  93. //#define         PWM45_DEADTIME_ENABLE                        BIT_TMP=EA;EA=0;TA=0xAA;TA=0x55;PDTEN|=0x04;EA=BIT_TMP
  94. //#define         PWM34_DEADTIME_ENABLE                        BIT_TMP=EA;EA=0;TA=0xAA;TA=0x55;PDTEN|=0x02;EA=BIT_TMP
  95. //#define         PWM01_DEADTIME_ENABLE                        BIT_TMP=EA;EA=0;TA=0xAA;TA=0x55;PDTEN|=0x01;EA=BIT_TMP
  96. #endif


  97. /************************************************************************************************************
  98. *   PWM interrupt subroutine
  99. ************************************************************************************************************/
  100. void PWM_ISR (void) interrupt 13
  101. {
  102.     clr_PWMF;               // clear PWM interrupt flag
  103.     P12 = ~P12;                                                        // GPIO1 show PWM INT
  104. }

  105. /************************************************************************************************************
  106. *    Main function
  107. ************************************************************************************************************/
  108. void main(void)
  109. {
  110.     Set_All_GPIO_Quasi_Mode;
  111. /**********************************************************************
  112.         PWM frequency = Fpwm/((PWMPH,PWMPL) + 1) <Fpwm = Fsys/PWM_CLOCK_DIV>
  113.                                                                 = (22.1184MHz/8)/(0x3FF + 1)
  114.                                                                 = 2.7KHz
  115. ***********************************************************************/
  116.                 PWM2_P10_OUTPUT_ENABLE;
  117.                 PWM_INT_PWM2;       
  118.                 PWM_FALLING_INT;                                        //Setting Interrupt happen when PWM0 falling signal
  119.        
  120.                 PWM_CLOCK_DIV_8;
  121.                 PWMPH = 0x03;                                                        //Setting PWM period
  122.                 PWMPL = 0xFF;
  123.                 PWM2H = 0x01;                                                        //PWM0 high duty = 1/2 PWM period
  124.                 PWM2L = 0xFF;       

  125.                 set_EPWM;                                                                        //Enable PWM interrupt
  126.                 set_EA;                                                                       
  127.     set_LOAD;
  128.     set_PWMRUN;
  129.                 while(1);
  130. }


mintspring 发表于 2018-7-31 17:35 | 显示全部楼层
终于知道我之前怎么测试不对了,希望官方出的例子每个都要测试一下。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

183

主题

2331

帖子

3

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