?? ds1620.c
字號:
/****************************************************************
/* 這是直接用C51高級語言編寫的DS1620轉(zhuǎn)換程序-沒有調(diào)用匯編語言。*/
/* 文件名DS1620C.C */
/* 功能:本程序主要是讀出DS1620芯片的溫度轉(zhuǎn)換值,轉(zhuǎn)換值先放 */
/* 在變量temp1中。轉(zhuǎn)換值范圍-55℃~125℃。 若flag0=0為 */
/* 正溫度,若flag0=1為負溫度值,最終結(jié)果在浮點變量cc中。 */
/* 本程序適合DS1620 和51系列單片機的連接,晶振為12MHZ左右。*/
/****************************************************************/
#include "REG52.h"
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
sbit DQ = P1^0;
sbit CLK_CONV = P1^1;
sbit RST = P1^2;
uint idata temp1;
uchar idata val,flag,temp2;
float idata cc;
void write_byte(uchar val) //寫字節(jié)子程序
{
uchar i;
uchar b;
b=1;
for(i=0;i<8;i++)
{
CLK_CONV=0;
DQ=(val&b);
CLK_CONV=1;
b<<=1;
}
}
uchar read_byte(void) //讀字節(jié)子程序
{
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 開始轉(zhuǎn)換
{
RST=1;
write_byte(0xEE);
RST=0;
return 0x00;
}
uchar DS1620ReadConf(void) //DS1620讀配置 返回值為配置寄存器內(nèi)容
{
uchar tmp;
RST=1;
write_byte(0xAC);
tmp=read_byte();
RST=0;
return tmp;
}
//DS1620 寫配置,入回參數(shù)為配置寄存器新配置內(nèi)容
uchar DS1620SetConf(uchar val)
{
uchar tmp;
RST=1;
write_byte(0x0C);
write_byte(val);
RST=0;
return tmp;
}
//DS1620 讀溫度轉(zhuǎn)換數(shù)據(jù),在返回值的低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; //保留數(shù)值有用部分
if (temp1>0xff) {
flag=1;
temp2=temp1-256;
temp2=~temp2+1;
temp1=temp2;
}
cc=(float)temp1/2.0; //計算出溫度值
while(1);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -