在AD检测时, 当引脚的电压低于参考电压的一半时,检测值一直是参考值的一半,比如用的2.5的内部参考电压,当电压低于1.25V时检测的电压一直是1.25;
下面是MSP430的AD部分代码,麻烦查看下是否有问题?
ADC初始化
void luce_drv_msp_adc_init(void)
{
/* Enable A/D channel A1 A3*/
P6SEL |= (BIT1 | BIT3);
/*Reset REFMSTR to hand over control to ADC12_A ref control registers*/
REFCTL0 &= ~REFMSTR;
/* Turn on ADC12, set sampling time*/
ADC12CTL0 = ADC12ON+ADC12SHT0_8+ADC12MSC+ADC12REFON+ADC12REF2_5V;
/* set multiple sample conversion*/
/* Use sampling timer,slecet smclk set mode*/
ADC12CTL1 = ADC12SHP+ADC12SSEL_3+ADC12CONSEQ_3;
/* VR+ = VREF+ and VR. = AVSS, channel = A1*/
ADC12MCTL0 = ADC12SREF_1+ADC12INCH_1;
/* VR+ = VREF+ and VR. = AVSS, channel = A3, end seq. */
ADC12MCTL1 = ADC12SREF_1+ADC12INCH_3+ADC12EOS;
/* Enable ADC12IFG.1*/
//ADC12IE = 0x02;
/* Enable conversions*/
//ADC12CTL0 |= ADC12ENC;
/* Start conversion*/
//ADC12CTL0 |= ADC12SC;
}
ADC使能
void luce_drv_msp_adc_enable(void)
{
/* Enable ADC12IFG.1*/
ADC12IE = 0x02;
/* Enable conversions*/
ADC12CTL0 |= ADC12ENC;
/* Start conversion*/
ADC12CTL0 |= ADC12SC;
}
ADC中断连续取8次值取平均值
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
{
static unsigned char index = 0;
switch(__even_in_range(ADC12IV,34))
{
case 0: break; /* Vector 0: No interrupt*/
case 2: break; /* Vector 2: ADC overflow*/
case 4: break; /* Vector 4: ADC timing overflow*/
case 6: break; /* Vector 6: ADC12IFG0*/
case 8: /* Vector 8: ADC12IFG1*/
uiBattVoltResults[index] = ADC12MEM0; /* Move results*/
uiD5VSBVoltResults[index] = ADC12MEM1;
index++; /* Increment results index, modulo; Set Breakpoint1 here*/
if (index >= 8) /*8次检测完后清中断,停止ADC,置完成标志*/
{
index = 0;
ADC12IE = 0x00;
ADC12IFG = 0x00;
ADC12CTL0 &= ~( ADC12ENC | ADC12SC ); /* Disable conversions & Stop conversion*/
ucBattCheckFlag = LUCE_DRV_MSP_ADC_CHECKED;
}
break;
case 10: break; /* Vector 10: ADC12IFG2*/
case 12: break; /* Vector 12: ADC12IFG3*/
case 14: break; /* Vector 14: ADC12IFG4*/
case 16: break; /* Vector 16: ADC12IFG5*/
case 18: break; /* Vector 18: ADC12IFG6*/
case 20: break; /* Vector 20: ADC12IFG7*/
case 22: break; /* Vector 22: ADC12IFG8*/
case 24: break; /* Vector 24: ADC12IFG9*/
case 26: break; /* Vector 26: ADC12IFG10*/
case 28: break; /* Vector 28: ADC12IFG11*/
case 30: break; /* Vector 30: ADC12IFG12*/
case 32: break; /* Vector 32: ADC12IFG13*/
case 34: break; /* Vector 34: ADC12IFG14*/
default: break;
}
}
/*计算电池的平均电压值*/
for( ucIndex=0; ucIndex < Num_of_Results; ucIndex++ )
{
uiBattNowVolt+= uiBattVoltResults[ucIndex];
uiD5VSBNowVolt+= uiD5VSBVoltResults[ucIndex];
luce_drv_msp_debug_printf("ADC uiBattVoltResults=0x", uiBattVoltResults[ucIndex]);
}
g_uiBattVoltValue = (((unsigned long)( uiBattNowVolt/8)*ADC_REFER_VOLTAGE)/0xFFF)*ADC_BATT_VOLTAGE_MULT;
g_uiD5VSBVoltValue = (((unsigned long)( uiD5VSBNowVolt/8)*ADC_REFER_VOLTAGE)/0xFFF)*ADC_5VSB_VOLTAGE_MULT;
ADC_BATT_VOLTAGE_MULT 为11分压倍数
ADC_REFER_VOLTAGE 为参考电压现在值为2.43V
当电压低于13.78V时测的值一直是 13.78 uiBattNowVolt/8=0x0840 |