先用到STM8L的AD转换输入,输入端为0V,AD转换后都有0.375V,不知道怎么回事?基准是同电源Vcc连接一起的3.3V,请大侠看看。
void ADCinit(void)
{
ADC1_CR1 = 0x00;
ADC1_CR2 = 0x02;//0x02 0~23通道AD16分频寄存器
ADC1_SR = 0x00;
ADC1_SQR1 = 0x80;//禁用DMA模块
ADC1_SQR2 = 0x00;
ADC1_SQR3 = 0x00;
ADC1_SQR4 = 0x08;//
ADC1_CR1 |= 0x01;//使能ADC
Delay_us(20);
ADC1_CR1 |= 0x02;//启动ADC
}
void ADConvert(void)
{
unsigned int ADCValue=0;
if(!(ADC1_SR&0x01))return;
ADC1_SR &= 0xfe;//清除转换标志
//ADCValue = ADC1_DRL;
//ADCValue |= ADC1_DRH << 8;
ADCValue = ADC1_DRH << 8;
ADCValue += ADC1_DRL;
ADV += ADCValue;
wdt_reset();
if(ADCunter < 15)//15
ADCunter ++;
else
{
ADCunter = 0;
switch(ADStep)
{
case 3:{PowerVolat = ((float)ADV/65536)*10;break;}
case 4:{UsbBatVolat[0] = ((float)ADV/65536) * 10;break;}
case 5:{UsbBatVolat[1] = ((float)ADV/65536)* 10;break;}
case 6:{UsbBatVolat[2] = ((float)ADV/65536)* 10;break;}
case 7:{UsbBatVolat[3] = ((float)ADV/65536)* 10;break;}
case 8:{UsbBatVolat[4] = ((float)ADV/65536)* 10;break;}
case 9:{UsbBatVolat[5] = ((float)ADV/65536)* 10;break;}
case 10:{UsbBatVolat[6] = ((float)ADV/65536)* 10;break;}
case 11:{UsbBatVolat[7] = ((float)ADV/65536)* 10;break;}
case 12:{UsbBatVolat[8] = ((float)ADV/65536)* 10;break;}
case 13:{UsbBatVolat[9] = ((float)ADV/65536)* 10;break;}
case 14:{UsbSwVolat[0] = ((float)ADV/65536)* 10;break;}
case 15:{UsbSwVolat[1] = ((float)ADV/65536)* 10;break;}
case 16:{UsbSwVolat[2] = ((float)ADV/65536)* 10;break;}
case 17:{UsbSwVolat[3] = ((float)ADV/65536)* 10;break;}
case 18:{UsbSwVolat[4] = ((float)ADV/65536)* 10;break;}
case 19:{UsbSwVolat[5] = ((float)ADV/65536)* 10;break;}
case 20:{UsbSwVolat[6] = ((float)ADV/65536)* 10;break;}
case 21:{UsbSwVolat[7] = ((float)ADV/65536)* 10;break;}
case 22:{UsbSwVolat[8] = ((float)ADV/65536)* 10;break;}
case 23:{UsbSwVolat[9] = ((float)ADV/65536)* 10;break;}
}
if(ADStep < 23)ADStep ++;
else ADStep = 3;
ADV = 0;
}
wdt_reset();
ADC1_CR1 = 0x00;
ADC1_CR2 = 0x02;//0~23通道AD16分频寄存器
ADC1_SQR2 = 0;
ADC1_SQR3 = 0;
ADC1_SQR4 = 0;
if(ADStep <= 7)
{
ADC1_SQR4 = (1<<ADStep);
}
else if(ADStep <= 15)
{
ADC1_SQR3 = (1<<(ADStep-8));
}
else if (ADStep <= 23)
{
ADC1_SQR2 = (1<<(ADStep-16));
}
ADC1_CR1 |= 0x01;
Delay_us(20);
ADC1_CR1 |= 0x02;
} |