本帖最后由 来自下邳的少年 于 2015-11-12 09:35 编辑
我使用STM8L151单片机,使用三个AD通道0、1、2三个,单次转换,0通道检测电池电压,目前单步运行是发现第一次结果正确,接下来的结果接错误了,使用16M晶振,程序如下:
GPIO_Init(BATTER_PORT, BATTER_PIN, GPIO_Mode_In_FL_No_IT);//电池检测管脚初始化
GPIO_Init(TEL_AD_PORT, TEL_AD_PINS, GPIO_Mode_In_FL_No_IT);
void AD1_Init(void)//AD初始化
{
CLK_PeripheralClockConfig(CLK_Peripheral_ADC1,ENABLE);
ADC_SchmittTriggerConfig(ADC1,BAT_AD_CHANNEL, DISABLE);
ADC_SchmittTriggerConfig(ADC1,TEL_AD_CHANNEL1, DISABLE);
ADC_SchmittTriggerConfig(ADC1,TEL_AD_CHANNEL2, DISABLE);
ADC_Init(ADC1,ADC_ConversionMode_Single,ADC_Resolution_10Bit,ADC_Prescaler_1);
ADC_SamplingTimeConfig(ADC1,ADC_Group_SlowChannels,ADC_SamplingTime_384Cycles);
ADC_Cmd(ADC1,ENABLE);
ADC_DMACmd(ADC1,DISABLE);
ADC_ChannelCmd(ADC1,BAT_AD_CHANNEL,ENABLE);
ADC_SoftwareStartConv( ADC1);
}
void AD_Function(void)//AD程序
{
u16 Temp1,Temp2;
if(ADC1->SR & ADC_FLAG_EOC){
Temp1 = ADC1->DRL;
Temp2 = ADC1->DRH;
Temp2 = (Temp2 << 8) | Temp1;
ADBuf[ADChannel] = Temp2;
switch(ADChannel){
case uaBAT://电池电压检测
if(ADBuf[uaBAT] < 341){ //1024*(1.1/3.3)
if(ADBuf[uaBAT]<311) //1024*(1/3.3)
{Flag_off=TRUE;}
Change_WriteData(0x12,0x40,5);
}
else
{Change_WriteData(0x12,0x40,6);}
break;
case uaTEL1:
if(ADBuf[uaTEL1] > 931) //1024*(3/3.3)
{ErrIn();}
break;
case uaTEL2:
if(ADBuf[uaTEL2] > 931) //1024*(3/3.3)
{ErrIn();}
break;
default:break;
}
ADChannel++;
if(ADChannel == uaMax){
ADChannel = uaBAT;
}
ADC1->SR &= (~ADC_FLAG_EOC);
ADC1->SQR[3] &= (~ADC_SQR4_CHSELS);
ADC1->SQR[3] |= ADChannel; /*使能通道*/
ADC1->CR1 |= ADC_CR1_START;
}
}
typedef enum {
uaBAT = 0,//电池电压
uaTEL1,
uaTEL2,
uaMax
}T_AD_CHANNEL;
求大神给个解释。。。。。。
|