[DemoCode下载] N76E003通过PWM触发ADC采样

[复制链接]
3138|5
 楼主| yiy 发表于 2017-11-9 21:27 | 显示全部楼层 |阅读模式
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2016 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/2016
  11. //***********************************************************************************************************

  12. //***********************************************************************************************************
  13. //  File Function: N76E003 ADC demo code
  14. //***********************************************************************************************************

  15. #include "N76E003.h"
  16. #include "SFR_Macro.h"
  17. #include "Function_define.h"
  18. #include "Common.h"
  19. #include "Delay.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. //#define Enable_ADC_AIN0                        ADCCON0&=0xF0;P17_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT0;ADCCON1|=SET_BIT0                                                                        //P17
  25. //#define Enable_ADC_AIN1                        ADCCON0&=0xF0;ADCCON0|=0x01;P30_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT1;ADCCON1|=SET_BIT0                //P30
  26. //#define Enable_ADC_AIN2                        ADCCON0&=0xF0;ADCCON0|=0x02;P07_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT2;ADCCON1|=SET_BIT0                //P07
  27. //#define Enable_ADC_AIN3                        ADCCON0&=0xF0;ADCCON0|=0x03;P06_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT3;ADCCON1|=SET_BIT0                //P06
  28. //#define Enable_ADC_AIN4                        ADCCON0&=0xF0;ADCCON0|=0x04;P05_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT4;ADCCON1|=SET_BIT0                //P05
  29. //#define Enable_ADC_AIN5                        ADCCON0&=0xF0;ADCCON0|=0x05;P04_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT5;ADCCON1|=SET_BIT0                //P04
  30. //#define Enable_ADC_AIN6                        ADCCON0&=0xF0;ADCCON0|=0x06;P03_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT6;ADCCON1|=SET_BIT0                //P03
  31. //#define Enable_ADC_AIN7                        ADCCON0&=0xF0;ADCCON0|=0x07;P11_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT7;ADCCON1|=SET_BIT0                //P11
  32. //#define Enable_ADC_BandGap        ADCCON0|=0x0F;ADCCON1|=SET_BIT0                                                                                                                                                                                                                                                                //Band-gap 1.22V

  33. //#define PWM0_FALLINGEDGE_TRIG_ADC                ADCCON0&=~SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
  34. //#define PWM2_FALLINGEDGE_TRIG_ADC                ADCCON0&=~SET_BIT5;ADCCON0|=SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
  35. //#define PWM4_FALLINGEDGE_TRIG_ADC                ADCCON0|=SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
  36. //#define PWM0_RISINGEDGE_TRIG_ADC                ADCCON0&=~SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1
  37. //#define PWM2_RISINGEDGE_TRIG_ADC                ADCCON0&=~SET_BIT5;ADCCON0|=SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1
  38. //#define PWM4_RISINGEDGE_TRIG_ADC                ADCCON0|=SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1
  39. //#define PWM0_CENTRAL_TRIG_ADC                                ADCCON0&=~SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1|=SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
  40. //#define PWM2_CENTRAL_TRIG_ADC                                ADCCON0&=~SET_BIT5;ADCCON0|=SET_BIT4;ADCCON1|=SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
  41. //#define PWM4_CENTRAL_TRIG_ADC                                ADCCON0|=SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1|=SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
  42. //#define PWM0_END_TRIG_ADC                                                ADCCON0&=~SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1|=SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1
  43. //#define PWM2_END_TRIG_ADC                                                ADCCON0&=~SET_BIT5;ADCCON0|=SET_BIT4;ADCCON1|=SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1
  44. //#define PWM4_END_TRIG_ADC                                                ADCCON0|=SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1|=SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1

  45. //#define P04_FALLINGEDGE_TRIG_ADC                ADCCON0|=0x30;ADCCON1&=0xF3;ADCCON1|=SET_BIT1;ADCCON1&=~SET_BIT6
  46. //#define P13_FALLINGEDGE_TRIG_ADC                ADCCON0|=0x30;ADCCON1&=0xF3;ADCCON1|=SET_BIT1;ADCCON1|=SET_BIT6
  47. //#define P04_RISINGEDGE_TRIG_ADC                        ADCCON0|=0x30;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1;ADCCON1&=~SET_BIT6
  48. //#define P13_RISINGEDGE_TRIG_ADC                        ADCCON0|=0x30;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1;ADCCON1|=SET_BIT6
  49. #endif

  50. /******************************************************************************
  51. * FUNCTION_PURPOSE: ADC interrupt Service Routine
  52. ******************************************************************************/
  53. void ADC_ISR (void) interrupt 11
  54. {
  55.     clr_ADCF;                               // Clear ADC interrupt flag
  56.                 printf ("\n Value = 0x%bx",ADCRH);                // printf display will cause delay in ADC interrupt
  57.                 P30 ^= 1;                                                                                                                                // Check the P3.0 toggle at falling edge of PWM
  58. }

  59. /******************************************************************************
  60. The main C function.  Program execution starts
  61. here after stack initialization.
  62. ******************************************************************************/
  63. void main (void)
  64. {
  65.     Set_All_GPIO_Quasi_Mode;
  66.                 InitialUART0_Timer1(9600);
  67.        
  68. /*-------------------------------------------------
  69.         ADC trig initial setting
  70.         Please modify #if value to open diffent type
  71. --------------------------------------------------*/
  72. #if 1
  73. // By PWM falling edge
  74.                 PWM0_P12_OUTPUT_ENABLE;                               
  75.                 Enable_ADC_AIN0;                                                                                                                // Enable AIN0 P1.7 as ADC input
  76.                 PWM0_FALLINGEDGE_TRIG_ADC;       
  77. #endif               
  78. #if 0
  79. // By PWM rising edge
  80.                 PWM0_P12_OUTPUT_ENABLE;                               
  81.                 Enable_ADC_AIN0;                                                                                                                // Enable AIN0 P1.7 as ADC input
  82.                 PWM0_RISINGEDGE_TRIG_ADC;
  83. #endif               
  84. #if 0
  85. // By PWM central point
  86.                 PWM0_P12_OUTPUT_ENABLE;                               
  87.                 Enable_ADC_AIN0;                                                                                                                // Enable AIN0 P1.7 as ADC input
  88.                 PWM_CENTER_TYPE;
  89.                 PWM0_CENTRAL_TRIG_ADC;       
  90. #endif       
  91. #if 0
  92. // By PWM end point
  93.                 PWM0_P12_OUTPUT_ENABLE;                               
  94.                 Enable_ADC_AIN0;                                                                                                                // Enable AIN0 P1.7 as ADC input
  95.                 PWM_CENTER_TYPE;
  96.                 PWM0_END_TRIG_ADC;
  97. #endif       
  98.        
  99. // Setting PWM value
  100.                 PWMPH = 0x07;                                                                                                                                //Setting PWM value         
  101.     PWMPL = 0xFF;
  102.     PWM0H = 0x02;
  103.     PWM0L = 0xFF;
  104.           set_LOAD;                                                                                                                                                // PWM run
  105.     set_PWMRUN;
  106. // Setting ADC
  107.           set_EADC;                                                                                                                                                // Enable ADC interrupt (if use interrupt)
  108.                 EA = 1;       
  109.                 while(1);

  110. }




 楼主| yiy 发表于 2017-11-9 21:27 | 显示全部楼层
之前是不是有朋友要这个例子的?现在有了,拿去不谢。
jiekou001 发表于 2017-11-9 22:27 | 显示全部楼层
是不是也可以颠倒一下,通过ADC的值设置PWM的占空比
jiekou001 发表于 2017-11-9 22:27 | 显示全部楼层
这样可以通过一个电位器旋钮调整PWM。
zl1211 发表于 2018-1-24 14:00 | 显示全部楼层
请问如果我想用串口将采集到的电压值输出该怎样改
smilefenfen 发表于 2018-4-18 10:14 | 显示全部楼层
ADCCON1&=~SET_BIT6;
这是什么意思,谁能给解答一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则

yiy

114

主题

1966

帖子

4

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