本帖最后由 lam2083 于 2012-10-18 21:46 编辑
我用dspic33fj128mc706进行AD采样,扫描采样方式,由于懒得用DMA,就直接进中断读buf来存数。实验时候采用的是2通道扫描, 我对AD的各个寄存器采用了如下设置
void initadc1()
{
AD1CON1bits.FORM = 0;
// Data Output Format: Signed Fraction (Q15 format)
AD1CON1bits.SSRC = 0; // Sample human
AD1CON1bits.ASAM = 0;
// ADC Sample Control: Sampling begins immediately after conversion
AD1CON1bits.AD12B = 0; // 10-bit ADC operation
AD1CON2bits.CSCNA = 1;
// Scan Input Selections for CH0+ during Sample A bit
AD1CON2bits.CHPS = 0; // Converts CH0
AD1CON3bits.ADRC = 0; // ADC Clock is derived from Systems Clock
AD1CON3bits.ADCS = 63;
AD1CON2bits.SMPI = 1; // 4 ADC Channel is scanned
AD1CSSLbits.CSS4=1; // Enable AN4 for channel scan
AD1CSSLbits.CSS5=1;
// Enable AN5 for channel scan
AD1PCFGL=0xFFFF;
AD1PCFGLbits.PCFG4 = 0; // AN4 as Analog Input
AD1PCFGLbits.PCFG5 = 0; // AN5 as Analog Input
IFS0bits.AD1IF = 0; // Clear the A/D interrupt flag bit
IEC0bits.AD1IE = 1; // Enable A/D interrupt
}
void __attribute__((interrupt, no_auto_psv)) _ADC1Interrupt(void)
{
switch (scanCounter)
{
case 0:
ain4Buff[sampleCounter]=ADC1BUF0;
break;
case 1:
ain5Buff[sampleCounter]=ADC1BUF0;
break;
case 2:
ain10Buff[sampleCounter]=ADC1BUF0;
break;
case 3:
ain13Buff[sampleCounter]=ADC1BUF0;
break;
//default:
//break;
}
scanCounter++;
if(scanCounter==2)
{
scanCounter=0;
sampleCounter++;
}
if(sampleCounter==8)
sampleCounter=0;
IFS0bits.AD1IF = 0; // Clear the ADC1 Interrupt Flag
}
int main()
{
uint8 qq;
initadc1();
AD1CON1bits.ADON=1;
for(qq=0;qq<16;qq+1)
{
AD1CON1bits.SAMP=1;
DelayMS(100);
AD1CON1bits.SAMP=0;
while(!AD1CON1bits.DONE);
qq=qq+1;
}
设置采用的是两通道的输入扫描,分别是AN4和AN5,按照手册上说,是每完成两个采用,产生一次中断。可是buf是会被覆盖的啊,按道理说我在中断程序中只能读到采到的一个值,为什么我确实读到了两个呢?究竟是怎么实现的 ?请大神给解答一下~谢啦!!
补充说明,附上我的主程序和中断程序,在调试中发现一个问题,只有在主程序中采用循环时候才能采到2个值,这个程序里采的就是2路个八个数值,一共16个数,所以程序里必须进行16次循环,但是我还是不明白为什么进中断以后第一路的值也能很好的覆盖,还请大神看过解释解释,小弟诚心发问,确实不明白 |