|
发表于 2013-4-1 12:47:07
|
显示全部楼层
本帖最后由 LM1876 于 2013-4-2 10:45 编辑
用二分查表法,我用的是STC 12C5A48S2,温感是常用10K的
uint code Temp[120]={
44, 47, 50, 53, 56, 59, 62, 65, 69, 72, //-11~-20 10
76, 80, 84, 89, 93, 98,102,112,118,123, //-1~-10 20
129,135,142,148,154,160,167,174,182,189, //0~9 30
196,204,212,220,228,236,244,253,262,270, //10~19 40
280,288,299,309,318,327,337,346,357,367, //20~29 50
377,393,398,408,419,429,439,450,460,470, //30~39 60
480,491,501,511,521,531,541,551,561,571, //40~49 70
581,591,600,609,619,627,637,645,654,663, //50~59 80
671,680,688,695,704,712,719,727,734,742, //60~69 90
748,756,762,769,775,781,788,793,799,805, //70~79 100
810,816,821,827,831,837,841,846,850,858, //80~89 110
861,866,869,873,877,881,885,888,895,896, //90~100 120
};
void Temp_Init()
{
uchar i;
P1ASF = 0xff; // 1111 1110,
ADC_CONTR = ADC_POWER | ADC_SPEEDLL;
for(i=0;i<50;i++){ NOP(); }
}
void Temp_Update()
{
uint AD_data;
AD_data = 1024-AD_Transform(0) ; //上拉电阻为4.7K,如果是下拉电阻 4.7K的话,就不用1024-了
Room_tmep = AD_Temp(AD_data,0); //房间温度
}
uint AD_Transform(char ch)
{
uint i;
uint tmp_Temp=0;
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START;
for(i=0;i<10;i++){ NOP(); }
// 启动A/D转换,
while (!(ADC_CONTR & ADC_FLAG));//Wait complete flag 等待完成标志
ADC_CONTR &= ~ADC_FLAG; //Close ADC
tmp_Temp = ADC_RES;
tmp_Temp = tmp_Temp<<2;
tmp_Temp = tmp_Temp|(ADC_RESL&0x03);
if (tmp_Temp<10){tmp_Temp=0;}
return tmp_Temp;
}
uchar AD_Temp(uint m_data,uchar x)
{
uchar max=120 ,min = 1,mid;
if (m_data<43){min=0xff;return min;}
while(min <= max)
{
mid = (max + min) / 2;
if(m_data > Temp[mid]){min = mid + 1;} else { max = mid - 1;}
}
return min;
} |
|