|
void delay1(uint t)
{
while(--t);
}
void init_18b20()
{
uchar flag;
DQ=1;
delay1(10); //延時(shí)
DQ=0;
delay1(500); //*延時(shí),要求精度,要求大于480us*
DQ=1;
delay1(200); //*延時(shí),要求精度,要求大于15us*
flag=DQ; //DQ管腳送出60-240us的0脈沖,以示初始化成功
delay1(10); //延時(shí)
}
//**************************************************************************************************
//寫一個(gè)字節(jié)函數(shù)
//**************************************************************************************************
void write_byte(uchar t)
{
uchar i;
for(i=0;i<8;i++) //循環(huán)8次寫入1字節(jié)
{
DQ=0; //數(shù)據(jù)線置低
delay1(10); //延時(shí)
DQ=t&0x01; //發(fā)送1位數(shù)據(jù),最低位開始
delay1(50); //*延時(shí),要求精度*
DQ=1; //數(shù)據(jù)線置高
t=t>>1; //右移1位
}
}
//**************************************************************************************************
//讀一個(gè)字節(jié)函數(shù)
//**************************************************************************************************
uchar read_byte()
{
uchar i,value=0;;
for(i=0;i<8;i++) //循環(huán)8次讀取1字節(jié)
{
value=value>>1; //右移1位
DQ=0; //數(shù)據(jù)線置低
delay1(10); //延時(shí)
DQ=1; //數(shù)據(jù)線置高
delay1(10); //延時(shí)
if(DQ==1)value=value|0x80;//判斷接收的1位數(shù)據(jù)是否為1
delay1(50); //*延時(shí),要求精度*
}
return(value);
}
//**************************************************************************************************
//數(shù)據(jù)處理子函數(shù)
//**************************************************************************************************
//**************************************************************************************************
//溫度采集函數(shù)
//**************************************************************************************************
uint get_temp()
{
uint dat;
uchar wenl,wenh;
init_18b20(); //復(fù)位
write_byte(0xcc); //不進(jìn)行編號(hào)匹配
write_byte(0x44); //進(jìn)行溫度轉(zhuǎn)換
init_18b20(); //復(fù)位
write_byte(0xcc); //不進(jìn)行編號(hào)匹配
write_byte(0xbe); //發(fā)讀命令
wenl=read_byte(); //溫度低八位
wenh=read_byte(); //溫度高八位
dat=(wenh<<8)+wenl; //數(shù)據(jù)高低8位合并
return(dat); //返回測量結(jié)果
}
void chuli(uint temp)
{
uint y,t;
uchar ge,shi;
if(temp&0x8000) //判斷是否為負(fù)數(shù)
{
temp=~temp+1;//取反加1
LCD_PutString(180,40,"-",0x0000,0x7FFF);
t=(int)(temp*0.0625);
}
else
{
LCD_PutString(180,40,"+",0x0000,0x7FFF);
t=(int)(temp*0.0625+0.05);
}
}
|
|