ADC工作不正常,希望高手分析一下是什么原因。
程序如下:
/*--------------------------ADC初始化程序-------------------------------*/
void ADC0_Init (void)
{
unsigned ADC0_decima
tion;
REF0CN &= ~0x01; // disable internal vref,启用内部晶振
// REF0CN |= 0x01; // (enable if using internal vref)
ADC0CN = 0x10; // 双极性工作方式,1倍增益输出
// ADC0CN = 0x01; // Bipolar output codes, GAIN=1
// ADC0CF = 0x00; // interrupts upon SINC3 filter output
// and uses internal VREF
// ADC0CF = 0x04; // 电压基准设置为外部
// and uses external VREF
ADC0CF = 0x00;
ADC0CLK = (SYSCLK/MDCLK)-1; // Ideally, MDCLK = 2.4576 MHz(时钟分频)
// Generate MDCLK for modulator.
// program decimation rate for desired OWR
ADC0_decimation = (unsigned long) SYSCLK/ (unsigned long) OWR /
(unsigned long) (ADC0CLK+1)/(unsigned long)128;
ADC0_decimation--;
ADC0DEC = ADC0_decimation;
ADC0BUF = 0x00; // 关闭输入缓冲
ADC0MUX = 0x01; //
// AIN+ => AIN0.0
// AIN- => AIN0.1
ADC0MD = 0x80; // 使能(IDLE Mode)
}
/*---------------------------ADC转换程序-----------------------------*/
void ADC24 (void)
{
unsigned int i,g;
float average;
unsigned char e11,e22,e33,e44;
unsigned int e1,e2,e3,e4;
unsigned long int average1;
// PCA0MD &= ~0x40; // disable watchdog timer
// SYSCLK_Init(); // Initialize system clock to 49 MHz
// PORT7_Init(); // Initialize cros***ar and GPIO
// OSCILLATOR_Init (); //晶振初始化
// PORT_Init (); //端口初始化
ADC0_Init();
for(i=0;i<128;i++)
{
sample_array
=0;
}
// ADC0_Init(); // 初始化ADC0
// UART0_Init(); // 初始化UART0
EA = 1; // 使能总中断
// printf("nMeasurements using the 24-bit ADC in C8051F350n");
// printf("nCalibrating ...n");
EIE1 &= ~0x08; // 不使能 ADC0 中断
ADC0MD |= 0x01; // 完全内部校准
while (!AD0CALC); // 等待转换完成
ADC0MD &= ~0x07; // ADC0 为理想模式
// printf("Calibration completenn");
AD0INT = 0; // clear pending sample indication
ADC0MD |= 0x83; // 启动连续转换
while(1) //处理采样值
{
// 捕捉128次采样
// printf ("Collecting 128 samples...n");
for (i = 0; i < 128; i++)
{
while(!AD0INT); //等待转换完成
AD0INT = 0; //清 AD0 中断标志
// concatenate ADC0 data bytes to form the 24-bit value
ADC_OutputVal = ADC0H;
ADC_OutputVal =ADC_OutputVal<< 16;
ADC_OutputVal += (long)ADC0L + ((long)ADC0M << 8);
sample_array = ADC_OutputVal;
}
}