AD初始化程序段:
ADC12CTL0 = ADC12ON+MSC+SHT0_8; // Turn on ADC12, extend sampling time
// to ××oid overflow of results
ADC12CTL1 = SHP+CONSEQ_3; // Use sampling timer, repeated sequence
ADC12MCTL0 = INCH_0; // ref+=××cc, channel = A0
ADC12MCTL2 = INCH_2 + EOS; // ref+=××cc, channel = A2, end seq.
ADC12IE = 0x04; // Ena××e ADC12IFG.3
AD中断函数如下:
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR ( void )
{
static unsigned int index = 0;
ad_i++;
A0results[ ad_i ] = ADC12MEM0; // Move A0 results, IFG is cleared
A1results[ ad_i ] = ADC12MEM2; // Move A1 results, IFG is cleared
if( index != 1 ) //index = 1时的初次数据无效,舍去
{
A0××erage += A0results[ ad_i ];
A1××erage += A1results[ ad_i ];
}
/*
A0××erage += ADC12MEM0;
A1××erage += ADC12MEM2;
*/
index = ( index + 1 ) % ( Num_of_Results + 1); // Increment results index, modulo;
if ( index == 0 )
{
ADC12CTL0 &= 0xfffc; //关ADC
A0temp = A0××erage / Num_of_Results;
A1temp = A1××erage / Num_of_Results;
A0××erage = 0;
A1××erage = 0;
}
}
目的是采样4次取平均,但现在的情况是单步运行OK,连续运行存在明显的系统误差:测量值与真实值存在一定的比例关系,这是为什么???
怎么还有××??那里应该是A V 字样。。。
补充:
在main函数中需要采样时,用了如下语句,可以否?
ADC12CTL0 |= 0x0003; //开ADC
while ( ADC12CTL0 & 0x0003 ); //ADC采样4次结束
...... //处理数据A0temp
调整一下时钟看,另还要注意全速运行时的程序流程结构。如果结构错了,一切都有可能。
像LZ这种情况如果确认时钟没问题,就肯定是程序流程的问题了。
AD的开关换了开关中断使能的方法:
ADC12IE = BIT2; //开ADC中断
while ( ADC12IE & BIT2 ); //ADC采样结束
之后可以了。
当然,在AD中断里关使能
if ( index == 0 )
{
ADC12IE = 0;
...
}