[技术问答] N76E003的ADC,为什么会工作不正常?

[复制链接]
2190|11
 楼主| dontium 发表于 2018-12-3 13:31 | 显示全部楼层 |阅读模式
N76E003的ADC,用PWM触发,ADC结果存入数组,并在主程序里累积。
数组大小为16,并设一指针。当指针归0时,在主程序里累积,并平均,现在这个累积,是100次转换的结果。
根据指针对16X6次的结果相加,再加4次ADC结果。
---------当执行这样的累加程序后(第一次),ADCCON0就变为0x42,处于忙状态,一直不再变空闲。

但是,又一个程序段累加了16个数据,没有使用数组指针,可以连续工作,ADCCON0为0x02,
598330983 发表于 2018-12-3 23:40 | 显示全部楼层
不知道,你是测几路的?
如果一路的,PWM触发后直接累加到SUM里,然后计数次数到100了,就求个平均值。这样不就行了,看你还搞数组还搞指针的,是不是弄太多导致变量超过256字节了?
598330983 发表于 2018-12-3 23:44 | 显示全部楼层
看你描述的很复杂,用简单的方法实现这个平均值试试。
dongnanxibei 发表于 2018-12-4 22:42 | 显示全部楼层
之前我用过这个芯片,没发现什么问题,不过我的用法跟你的不同
734774645 发表于 2018-12-5 13:55 | 显示全部楼层
ADC是正常的,你可能使用数据的方式错了。
huangcunxiake 发表于 2018-12-7 10:32 | 显示全部楼层
  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 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 发表于 2018-12-8 23:04 | 显示全部楼层
没看明白16*6是啥。
 楼主| dontium 发表于 2018-12-10 00:16 | 显示全部楼层
598330983 发表于 2018-12-3 23:40
不知道,你是测几路的?
如果一路的,PWM触发后直接累加到SUM里,然后计数次数到100了,就求个平均值。这样 ...

为的是节省内存,节约时间
 楼主| dontium 发表于 2018-12-10 00:18 | 显示全部楼层
yiy 发表于 2018-12-8 23:04
没看明白16*6是啥。

100 = 16 x 6 + 4
 楼主| dontium 发表于 2018-12-10 00:19 | 显示全部楼层
xinxianshi 发表于 2018-12-10 09:56 | 显示全部楼层
如果采样频率远大于累加一次ADC结果的时间,完全可以每次转换后就立马进行一次累加。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:官向官来民向民,穷人向的是穷人

151

主题

1176

帖子

10

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