用003做了个温度采集的,用的是P0.4(AIN5)ADC丢弃了前2个包,取后3个,测VDD老是跳。
已配置 模拟输入了。
代码如下:
void main (void)
{
//unsigned char i;
double bgvalue;
double digitalValue,voltageValue,Rt;
unsigned int i;
double temp;
char xdata info[50],info2[50];
Set_All_GPIO_Quasi_Mode; // Define in Function_define.h
InitialUART0_Timer1(115200);
OLED_Init(); //OLED初始化
READ_BANDGAP();
Enable_ADC_BandGap;
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++) // All following ADC detect timing is 200uS run under 4MHz.
{
clr_ADCF;
set_ADCS;
while(ADCF == 0);
ADCdataH[i] = ADCRH;
ADCdataL[i] = ADCRL;
}
CKDIV = 0x00; // After ADC sampling, modify system clock back to 16MHz to run next code.
clr_ADCEN;
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;
VDD_Voltage = (0x1000/bgvalue)*Bandgap_Voltage/1000;
printf ("\n BG ROMMAP = %f",Bandgap_Voltage);
printf ("\n VDD voltage = %f", VDD_Voltage);
P04_Input_Mode;
Enable_ADC_AIN5; // Enable AIN0 P0.4 as ADC input, Find in "Function_define.h" - "ADC INIT"
while(1){
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++) // All following ADC detect timing is 200uS run under 4MHz.
{
clr_ADCF;
set_ADCS;
while(ADCF == 0);
ADCdataH[i] = ADCRH;
ADCdataL[i] = ADCRL;
}
CKDIV = 0x00; // After ADC sampling, modify system clock back to 16MHz to run next code.
ADCsumH=0;
ADCsumL=0;
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;
printf ("\nbgvalue = %f",bgvalue);
printf ("\n BG ROMMAP = %f",Bandgap_Voltage);
printf ("\n VDD voltage = %f", VDD_Voltage);
Res_Voltage=4095/bgvalue*Bandgap_Voltage/1000;
sprintf (&info,"Res_V= %f",Res_Voltage);
//获得端口处的电压值
digitalValue=bgvalue;
voltageValue=(digitalValue/4095)*VDD_Voltage;
printf("Current voltage value=%3f",voltageValue);
voltagePower=VDD_Voltage;
//通过分压比获得热敏电阻的阻值
Rt=((voltagePower-voltageValue)*Rs)/voltageValue;
printf("Current registor value=%3f",Rt);
//换算得到温度值
temp=(((T1*BZ)/(BZ+T1*log(Rt/R1)))-273.15);//
printf("Current temperature value=%3f",temp);
sprintf(&info2,"tempv=%3f",temp);
OLED_P8x16Str(0,0,&info);
OLED_P8x16Str(0,2,&info2);
//Res_Voltage*100000;
/*
for(i=0;i<105;i++) // use the last 3 times data to make average
{
if ((Temp_Voltage<dTemp[i])&&(Temp_Voltage>dTemp[i+1]))
temp=i-20;
}
//printf ("\nRes_Voltage = %10f",Res_Voltage);
//printf ("\nTemp_Voltage = %3f",Temp_Voltage);
sprintf (&info,"Temp = %3f",temp);
*/
//OLED_P8x16Str(0,2,&info);
Timer3_Delay100ms(10);
//OLED_CLS();
}
温度采集每次都变动很大,小的时候0.05,大的时候0.5-0.6度。
同一个电阻和3435温度电阻在arduino 里很稳定。 |