[DemoCode下载] N76E003的ADC16次采样均值滤波方式最优化

[复制链接]
2284|2
 楼主| yiy 发表于 2017-11-9 21:16 | 显示全部楼层 |阅读模式
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Website: http://www.nuvoton.com
  8. //  E-Mail : MicroC-8bit@nuvoton.com
  9. //  Date   : Jan/21/2017
  10. //***********************************************************************************************************

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

  19. //*****************  The Following is in define in Fucntion_define.h  ***************************
  20. //****** Always include Function_define.h call the define you want, detail see main(void) *******
  21. //***********************************************************************************************
  22. #if 0
  23. //#define Enable_ADC_AIN0                        ADCCON0&=0xF0;P17_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT0;ADCCON1|=SET_BIT0                                                                        //P17
  24. //#define Enable_ADC_AIN1                        ADCCON0&=0xF0;ADCCON0|=0x01;P30_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT1;ADCCON1|=SET_BIT0                //P30
  25. //#define Enable_ADC_AIN2                        ADCCON0&=0xF0;ADCCON0|=0x02;P07_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT2;ADCCON1|=SET_BIT0                //P07
  26. //#define Enable_ADC_AIN3                        ADCCON0&=0xF0;ADCCON0|=0x03;P06_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT3;ADCCON1|=SET_BIT0                //P06
  27. //#define Enable_ADC_AIN4                        ADCCON0&=0xF0;ADCCON0|=0x04;P05_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT4;ADCCON1|=SET_BIT0                //P05
  28. //#define Enable_ADC_AIN5                        ADCCON0&=0xF0;ADCCON0|=0x05;P04_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT5;ADCCON1|=SET_BIT0                //P04
  29. //#define Enable_ADC_AIN6                        ADCCON0&=0xF0;ADCCON0|=0x06;P03_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT6;ADCCON1|=SET_BIT0                //P03
  30. //#define Enable_ADC_AIN7                        ADCCON0&=0xF0;ADCCON0|=0x07;P11_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT7;ADCCON1|=SET_BIT0                //P11
  31. //#define Enable_ADC_BandGap        ADCCON0|=SET_BIT3;ADCCON0&=0xF8;                                                                                                                                                                                                                                                        //Band-gap 1.22V

  32. //#define PWM0_FALLINGEDGE_TRIG_ADC                ADCCON0&=~SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
  33. //#define PWM2_FALLINGEDGE_TRIG_ADC                ADCCON0&=~SET_BIT5;ADCCON0|=SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
  34. //#define PWM4_FALLINGEDGE_TRIG_ADC                ADCCON0|=SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
  35. //#define PWM0_RISINGEDGE_TRIG_ADC                ADCCON0&=~SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1
  36. //#define PWM2_RISINGEDGE_TRIG_ADC                ADCCON0&=~SET_BIT5;ADCCON0|=SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1
  37. //#define PWM4_RISINGEDGE_TRIG_ADC                ADCCON0|=SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1

  38. //#define P04_FALLINGEDGE_TRIG_ADC                ADCCON0|=0x30;ADCCON1&=0xF3;ADCCON1|=SET_BIT1;ADCCON1&=~SET_BIT6
  39. //#define P13_FALLINGEDGE_TRIG_ADC                ADCCON0|=0x30;ADCCON1&=0xF3;ADCCON1|=SET_BIT1;ADCCON1|=SET_BIT6
  40. //#define P04_RISINGEDGE_TRIG_ADC                        ADCCON0|=0x30;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1;ADCCON1&=~SET_BIT6
  41. //#define P13_RISINGEDGE_TRIG_ADC                        ADCCON0|=0x30;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1;ADCCON1|=SET_BIT6
  42. #endif

  43.                 int ADCdataH[16], ADCdataL[16], ADCsumH=0, ADCsumL=0;
  44.                 unsigned char ADCavgH, ADCavgL;
  45.                                
  46. /******************************************************************************
  47. The main C function.  Program execution starts
  48. here after stack initialization.
  49. ******************************************************************************/
  50. void main (void)
  51. {
  52.                 int i;                       
  53.                 P05_PushPull_Mode;
  54.                 P05 = 1;
  55.                 Timer0_Delay1ms(300);
  56.        
  57.                 P05 = 0;       
  58.                 Enable_ADC_AIN0;                                                // Enable AIN0 P1.7 as ADC input, Find in "Function_define.h" - "ADC INIT"
  59.                 ADCsumH = 0x0000;
  60.                 ADCsumL = 0x0000;
  61.                        
  62.                 for(i=0;i<16;i++)
  63.     {
  64.                         clr_ADCF;
  65.                         set_ADCS;                                                                        // ADC start trig signal
  66.       while(ADCF == 0);
  67.                         ADCdataH[i] = ADCRH;
  68.                         ADCdataL[i] = ADCRL;
  69.                 }               
  70.                
  71.                 for(i=0;i<16;i++)
  72.     {
  73.                         ADCsumH = ADCsumH + ADCdataH[i];
  74.                         ADCsumL = ADCsumL + ADCdataL[i];
  75.                 }                               

  76.                 ADCavgH = ADCsumH/16;
  77.                 ADCavgL = ADCsumL/16;
  78.                 P05 = 1;
  79.                 while(1);
  80. }


 楼主| yiy 发表于 2017-11-9 21:17 | 显示全部楼层
通过16BIT,高低8位16次求平均,这个方法值得大家参考。
jiekou001 发表于 2017-11-9 22:26 | 显示全部楼层
   Enable_ADC_AIN0;                                                // Enable AIN0 P1.7 as ADC input, Find in "Function_define.h" - "ADC INIT"
使用ADC这么简单,一个指令搞定了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

yiy

114

主题

1966

帖子

4

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