ADC采集电压,时间长总会出现波动。
/*
程序功能:读取UID中带隙电压值;通过ADC,测量实际的带隙电压;得到比例系数COE;
本程序需要放在ADC正常测量前。
*/
void READ_BANDGAP()
{
unsigned int i;
set_IAPEN;
IAPCN = READ_UID;
IAPAL = 0x0d;
IAPAH = 0x00;
set_IAPGO;
BandgapLow = IAPFD;
BandgapMark = BandgapLow&0xF0;
if (BandgapMark==0x80)
{
BandgapLow = BandgapLow&0x0F;
IAPAL = 0x0C;
IAPAH = 0x00;
set_IAPGO;
BandgapHigh = IAPFD;
Bandgap_Value = (BandgapHigh<<4)+BandgapLow;
Bandgap_Voltage_Temp = Bandgap_Value*3/4;
Bandgap_Voltage = Bandgap_Voltage_Temp - 33; //the actually banggap voltage value is similar this value.
}
if (BandgapMark==0x00)
{
BandgapLow = BandgapLow&0x0F;
IAPAL = 0x0C;
IAPAH = 0x00;
set_IAPGO;
BandgapHigh = IAPFD;
Bandgap_Value = (BandgapHigh<<4)+BandgapLow;
Bandgap_Voltage= Bandgap_Value*3/4;
}
if (BandgapMark==0x90)
{
IAPAL = 0x0E;
IAPAH = 0x00;
set_IAPGO;
BandgapHigh = IAPFD;
IAPAL = 0x0F;
IAPAH = 0x00;
set_IAPGO;
BandgapLow = IAPFD;
BandgapLow = BandgapLow&0x0F;
Bandgap_Value = (BandgapHigh<<4)+BandgapLow;
Bandgap_Voltage= Bandgap_Value*3/4;
}
clr_IAPEN;
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
Enable_ADC_BandGap; //使能ADC带隙电压
CKDIV = 0x02; // IMPORTANT!! Modify system clock to 4MHz ,then add the ADC sampling clock base to add the sampling timing.
for(i=0;i<5;i++) //采样5次,不要前面三次
{
clr_ADCF;
set_ADCS;
while(ADCF == 0);
ADCdataH[i] = ADCRH;
ADCdataL[i] = ADCRL;
}
CKDIV = 0x00;
//--------均值滤波--------------------------------------------
for(i=2;i<5;i++) // use the last 3 times data to make average
{
ADCsumH = ADCsumH + ADCdataH[i];
ADCsumL = ADCsumL + ADCdataL[i];
}
ADCavgH = ADCsumH/3;
ADCavgL = ADCsumL/3;
bgvalue = (ADCavgH<<4) + ADCavgL;
Coe=(Bandgap_Voltage/bgvalue);
ADCsumH = 0;
ADCsumL = 0;
}
//void ADC_ISR (void) interrupt 11
//{
// if(ADCF)
// {
// clr_ADCF;//清除ADC转化完成标志,进行下一次转换
// set_ADCS;//当单次转换完成后,ADCS会硬件置0,需要重新使能
// RC_value= (ADCRH<<4) + ADCRL;//得到ADC转换值
// ADC_Voltage=RC_value*Coe/1000;
// //printf ("\n Value = 0x%bx",ADC_Voltage);
// printf ("\n Value = %6.2f",ADC_Voltage);
// s=ADC_Voltage*1024/5;
// //printf (" Value = %f",s);
// sprintf(aa,"%6.2f",s);
// printf (" Value = %e",RC_value);
// //printf (" Value = %e",Coe);
// Timer0_Delay1ms(500);
// }// Check the P3.0 toggle at falling edge of PWM
//}
void main (void)
{
UINT16 ss;
InitialUART0_Timer1(115200);
Set_All_GPIO_Quasi_Mode;
READ_BANDGAP();
Enable_ADC_AIN0;//配置使能P1.7,作为AIN0。
//P04_FALLINGEDGE_TRIG_ADC;// 使用P04下降沿触发中断,使 用外部触发时,可以不使能ADCS。
//set_EADC;//使能ADC中断
//EA = 1;
set_ADCS;//使能ADCS,启动ADC测量
Init_I2C();
while(1){
if(ADCF)
{
clr_ADCF;//清除ADC转化完成标志,进行下一次转换
set_ADCS;//当单次转换完成后,ADCS会硬件置0,需要重新使能
RC_value= (ADCRH<<4) + ADCRL;//得到ADC转换值
ss=RC_value;
ADC_Voltage=RC_value*Coe/1000;
//printf ("\n Value = 0x%bx",ADC_Voltage);
printf ("\n Value = %6.2f",ADC_Voltage);
s=ADC_Voltage*1024/5;
//printf (" Value = %f",s);
sprintf(aa,"%6.2f",s);
printf (" Value = %e",RC_value);
//printf (" Value = %e",Coe);
Timer0_Delay1ms(500);
}
};
}
不稳定,上面是代码,大牛帮看看问题在那 |
不觉得累加求均值有问题吗