.C文件
#include "ad.h" unsigned int xdata sAD1[10] ; unsigned int xdata AD_Value; unsigned int xdata TEMP_WENDU; unsigned char xdata Error_Code; bit Erroring; bit AD_OK; unsigned int xdata AD_TABLE[]= { 3783 //0 , 3768 //1 , 3752 //2 , 3736 //3 , 3719 //4 , 3702 //5 , 3684 //6 , 3665 //7 , 3646 //8 , 3627 //9 , 3606 //10 , 3585 //11 , 3564 //12 , 3541 //13 , 3518 //14 , 3495 //15 , 3471 //16 , 3446 //17 , 3420 //18 , 3394 //19 , 3367 //20 , 3340 //21 , 3312 //22 , 3283 //23 , 3254 //24 , 3224 //25 , 3194 //26 , 3163 //27 , 3131 //28 , 3099 //29 , 3067 //30 , 3034 //31 , 3000 //32 , 2966 //33 , 2931 //34 , 2896 //35 , 2861 //36 , 2825 //37 , 2789 //38 , 2753 //39 , 2716 //40 , 2679 //41 , 2641 //42 , 2604 //43 , 2566 //44 , 2528 //45 , 2490 //46 , 2452 //47 , 2414 //48 , 2375 //49 , 2337 //50 , 2298 //51 , 2260 //52 , 2222 //53 , 2183 //54 , 2145 //55 , 2107 //56 , 2069 //57 , 2032 //58 , 1994 //59 , 1957 //60 , 1920 //61 , 1883 //62 , 1847 //63 , 1811 //64 , 1775 //65 , 1740 //66 , 1705 //67 , 1670 //68 , 1636 //69 , 1602 //70 , 1568 //71 , 1535 //72 , 1503 //73 , 1470 //74 , 1439 //75 , 1408 //76 , 1377 //77 , 1347 //78 , 1317 //79 , 1288 //80 , 1259 //81 , 1230 //82 , 1203 //83 , 1175 //84 , 1149 //85 , 1122 //86 , 1097 //87 , 1071 //88 , 1046 //89 , 1022 //90 , 998 //91 , 975 //92 , 952 //93 , 930 //94 , 908 //95 , 886 //96 , 866 //97 , 845 //98 , 825 //99 , 805 //100 , 786 //101 , 767 //102 , 749 //103 , 731 //104 , 714 //105 }; void get_adc_value(void) { static unsigned char adc_cnt= 0; unsigned int xdata ad_min = 0; unsigned int xdata ad_max = 0; unsigned int xdata k = 0; sAD1[adc_cnt] = Get_ADC(7); adc_cnt++; if (adc_cnt == 6) { ad_min = sAD1[0]; ad_max = sAD1[0]; k = sAD1[0]; for (adc_cnt = 1; adc_cnt < 6; adc_cnt++) { if (sAD1[adc_cnt] < ad_min) { ad_min = sAD1[adc_cnt]; } else if (sAD1[adc_cnt] > ad_max) { ad_max = sAD1[adc_cnt]; } k += sAD1[adc_cnt]; } k -= ad_min; k -= ad_max; k >>= 2; AD_Value = k; k = 0; adc_cnt = 0; } } void Get_Temp() { unsigned char xdata index; static bit Read_AD_Once; static unsigned int xdata Difference_Value; static unsigned int xdata AD_Value_Temp; unsigned int xdata space,poor,val,temp_val; if(Read_AD_Once) { if(AD_Value>AD_Value_Temp) { Difference_Value=AD_Value-AD_Value_Temp; } else if(AD_Value<AD_Value_Temp) { Difference_Value=AD_Value_Temp-AD_Value; } if(Difference_Value<2) { AD_Value=AD_Value_Temp; } else { Read_AD_Once=0; } } if(Read_AD_Once==0) { Read_AD_Once=1; AD_Value_Temp=AD_Value; } for(index=0; index<105; index++) //查表 { if(AD_TABLE[index]>AD_Value) { AD_OK=1; TEMP_WENDU=index; space=AD_TABLE[index]-AD_TABLE[index+1];//计算出两个AD值间距 poor=AD_TABLE[index]-AD_Value; //计算出此次AD值比前一个大多少 val=poor*10/space; // 计算出小数点数 TEMP_WENDU=(index*10)+val; //结合 } } if(AD_Value<10) //NTC短路 { Error_Code=0xE2; Erroring=1; } else if(AD_Value>4080) //NTC开路 { Error_Code=0xE1; Erroring=1; } else { Erroring=0; Error_Code=0x00; } TEMP_WENDU=TEMP_WENDU; if(AD_OK) { AD_OK=0; /* if(TEMP<0) TEMP=0; if(TEMP>99) TEMP=99;*/ } }
.H文件
#ifndef _ad_h #define _ad_h extern unsigned char xdata Error_Code; extern bit Erroring; extern bit AD_OK; extern unsigned int xdata TEMP_WENDU; extern unsigned int xdata AD_TABLE[]; extern void get_adc_value(void); extern void Get_Temp(); #endif