前段时间在使用STM8S在一个锂电池管理项目上,但是发现给MCU供电不是很好处理,故改用STM8L051 发现一个比较奇怪的问题!
单独转换通道 AD值还是比较准确的,但是4个通道轮番扫描一次就 ,AD值就有比较大的误差,程序如下
程序放在中断里面,无关部分省略…………
for(ADCCH=0;ADCCH<4;)
{
if(ADCCH == 0)
{
InputTep = InputTep + ADC_CONV(ADCCH);
}
if(ADCCH == 1)
{
BattTep = BattTep + ADC_CONV(ADCCH);
}
if(ADCCH == 2)
{
VBusTep = VBusTep + ADC_CONV(ADCCH);
}
if(ADCCH == 3)
{
LoadCurrTep = LoadCurrTep + ADC_CONV(ADCCH);
}
ADCCH++;
}
ADCCNT++;
if(ADCCNT == 8)
{
InputAVGOld = InputAVG;
BattAVGOld = BattAVG;
VBusAVGOld = VBusAVG;
LoadCurrAVGOld = LoadCurrAVG;
InputAVG = InputTep>>3;
BattAVG = BattTep>>3;
VBusAVG = VBusTep>>3;
LoadCurrAVG = LoadCurrTep>>3;
InputTep = 0;
BattTep = 0;
VBusTep = 0;
LoadCurrTep = 0;
ADCCNT = 0;
}
void Init_AD(void)
{
ADC1_CR2 = 0x83;
ADC1_CR3 = 0x80;
ADC1_SQR1 = 0x80;
ADC1_SQR2 = 0;
ADC1_SQR4 = 0;
}
unsigned int ADC_CONV(unsigned char ADCH)
{
unsigned int DataTep;
unsigned int DataTep1;
ADC1_SQR3 = 0x08<
2024-5-15 14:48:25
ADC_CONV函数里面可以对ADC1_CR1进行清零一下
ADC_CONV函数里面可以对ADC1_CR1进行清零一下
举报