📄 ds1820.c
字号:
// 以下是四个DS1820温度读取程序
//使用时先调用"rom()"子程序读出每个器件的序列号
//再将各个序列号填入SN数组中才能读出各个温度
/*************************************************************************
光刻ROM中的64位序列号是出厂前被光刻好的,它可以看作是该DS18B20的地址序列码。
64位光刻ROM的排列是:开始8位(28H)是产品类型标号,接着的48位是该DS18B20自
身的序列号,最后8位是前面56位的循环冗余校验码(CRC=X8+X5+X4+1)。光刻ROM的
作用是使每一个DS18B20都各不相同,这样就可以实现一根总线上挂接多个DS18B20的
目的。********************************************************************/
#include <reg51.h>
#define uchar unsigned char
sbit TMDAT = P3^4; //根据实实际情况设定
uchar TMP[4]; //读取后的4个温度值,将其除以2即可得出实际温度;
uchar SN[4][8]=
{
{0x28,0x28,0x17,0x5E,0x00,0x00,0x00,0xE9}, //1
{40,162,36,94,0,0,0,6},
{40,155,243,88,0,0,0,123},
}; //4个器件的序列号,先读出单个序列号后填上才可以读取温度
uchar f[4]; //结果是否为负温,“0”为正温,“1”为负温。
/***************延时部分*************/
void dmsec (unsigned int count) //
{ //
unsigned int i; //
while (count) //
{ //
i = 115; //
while (i>0) i--; //
count--; //
} //
} //
/***************发送复位*************/
void tmreset (void) //
{ //
unsigned int i; //
TMDAT = 0; //
i = 103; //
while (i>0) i--; // 延时 900 uS(11.0592Mhz时) //
TMDAT = 1; //
i = 4; //
while (i>0) i--; //
} //
/***************判断DS1820是否存在的子程序。
最好不要用,因为当器件不存在时将会进入死循环
等待存在脉冲*******************************/
void tmpre (void) //判断器件是否存在 //
{ //
unsigned int i; //
while (TMDAT); //
while (~TMDAT); //
i = 4; while (i>0) i--; //
} //
/*************读一位*****************/
bit tmrbit (void) //
{ //
unsigned int i; //
bit dat; //
TMDAT = 0; i++; //
TMDAT = 1; i++; i++; //微量延时 //
dat = TMDAT; //
i = 8; while (i>0) i--; // 延时 //
return (dat); //
} //
/************** 读一个字节****************/
unsigned char tmrbyte (void) //
{ //
unsigned char i,j,dat; //
dat = 0; //
for (i=1;i<=8;i++) //
{ //
j = tmrbit (); //
dat = (j << 7) | (dat >> 1); //
} //
return (dat); //
} //
/************写一个字节****************/
void tmwbyte (unsigned char dat) //
{ //
unsigned int i; //
unsigned char j; //
bit testb; //
for (j=1;j<=8;j++) //
{ //
testb = dat & 0x01; //
dat = dat >> 1; //
if (testb) //
{ //
TMDAT = 0; // 写0 //
i++; i++; //
TMDAT = 1; //
i = 8; while (i>0) i--; //
} //
else //
{ //
TMDAT = 0; // 写0 //
i = 8; while (i>0) i--; //
TMDAT = 1; //
i++; i++; //
} //
} //
} //
/*************发送ds1820 开始转换*****************/
void tmstart (void) //
{ //
tmreset (); //复位 //
//tmpre (); //等待存在脉冲 //
dmsec (1); //延时 //
tmwbyte (0xcc); //跳过序列号命令,对所有器件有效 //
tmwbyte (0x44); //发转换命令 44H, //
} //
/****************读取温度*******************/
void tmrtemp () //
{ //
uchar i,j; //
uchar a,b; //
for(j=0;j<4;j++) //
{ //
tmreset(); //复位 //
dmsec(1); //延时 //
tmwbyte(0x55); //发送ROM匹配命令 //
for(i=0;i<8;i++) //
{ //
tmwbyte(SN[j][i]); //发送64位序列号 //
} //
tmwbyte (0xbe); //发送读取命令 //
a = tmrbyte (); //连续读取两位温度 //
b = tmrbyte (); //
f[j]=b; //若b为1则为负温 //
if(f[j]) //
{ //
TMP[j]=~a+1; //如果为负温则去除其补码 //
} //
else //
{ //
TMP[j]=a; //
} //
} //
} //
/************************************************************
//以下是读取器件序列号的子程序,需要读取序列时可在程序中调用;
//调用时确保总线中只有一只器件,若有多个器件时则读取出来的号码无效;
//将读取出来的序列号填到前面的数组中即可使用;
//平时可以将此子程序删除以节约空间。
//读取器件序列号子程序**************************************/
void rom() //
{ //
//以下是定义8个序列号变量 //
uchar sn1; //
uchar sn2; //
uchar sn3; //
uchar sn4; //
uchar sn5; //
uchar sn6; //
uchar sn7; //
uchar sn8; //
//
tmreset (); //复位 //
dmsec (1); //延时 //
tmwbyte(0x33); //发送读序列号子程序 //
sn1=tmrbyte(); //读取第一个序列号,应为16H; //
sn2=tmrbyte(); //读取第二个序列号,应为10H; //
sn3=tmrbyte(); //
sn4=tmrbyte(); //
sn5=tmrbyte(); //
sn6=tmrbyte(); //
sn7=tmrbyte(); //
sn8=tmrbyte(); //
} //
/***********************************/
main() //
{ //
do{ //
rom(); //调用读序列号子程序 //
//tmstart(); //
//dmsec(100); //如果是不断地读取的话可以不延时 //
//tmrtemp(); //读取温度,执行完毕温度将存于TMP[]数组中//
}while(1); //
} //
//////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -