請教關于DS1620的C語言驅動的問題:請教關于DS1620的溫度算法問題
各位大俠好:
小弟單片機學的不怎么樣了,現在在做了一個關于DS1620溫度傳感器方面的畢業設計。還沒有做實物,現在在軟件仿真的時候,出現了問題。就是溫度讀出來,顯示不對,只是一個靜態的值,調節也不變,搞了一段時間了,沒有找到原因。我想是不是自己的程序出了問題,故向各位大俠求救!下面是一些程序,數碼管用MAX7219驅動。小弟的郵箱是andy_1985923@163.com
void write_byte(uchar val) //寫字節子程序
{
uchar i;
uchar b;
b=1;
for(i=0;i<8;i++)
{
CLK_CONV=0;
DQ=(val&b);
CLK_CONV=1;
val=val>>1;
}
}
uchar read_byte(void) //讀字節子程序
{
uchar i;
uchar value,b;
value=0;
b=1;
for(i=0;i<8;i++)
{
DQ=1;
CLK_CONV=0;
if (DQ)
value|=b;
CLK_CONV=1;
b<<=1;
}
return(value);
}
uchar DS1620startConv(void) //DS1620 開始轉換
{
RST=1;
write_byte(0xEE);
RST=0;
return 0x00;
}
uchar DS1620ReadConf(void) //DS1620讀配置 返回值為配置寄存器內容
{
uchar tmp;
RST=1;
write_byte(0xAC);
tmp=read_byte();
RST=0;
return tmp;
}
//DS1620 寫配置,入回參數為配置寄存器新配置內容
uchar DS1620SetConf(uchar val)
{
uchar tmp;
RST=1;
write_byte(0x0C);
write_byte(val);
RST=0;
return tmp;
}
//DS1620 讀溫度轉換數據,在返回值的低9位
uint DS1620read(void)
{
uchar hbyte,lbyte;
uint temp;
RST=1;
write_byte(0xAA);
lbyte=read_byte();
hbyte=read_byte();
RST=0;
temp=hbyte;
temp<<=8;
temp|=lbyte;
return temp;
}
void main() //主程序
{
SP=0xcf;
EA=0;
flag=0;
val=0x0a;
DS1620SetConf(val);
val=DS1620ReadConf();
DS1620startConv();
temp1=DS1620read(); //把溫度值放入變量temp1中。
temp1=temp1&0x01ff; //保留數值有用部分
if (temp1>0xff) {
flag=1;
temp2=temp1-256;
temp2=~temp2+1;
temp1=temp2;
}
cc=temp1/2.0;//計算出溫度值
write_7219(7,cc/10);
write_7219(8,cc%10);
while(1);
}
本文來自: 電子工程師之家http://www.eehome.cn
[ 此貼被czypf在2009-05-24 11:50重新編輯 ]