//#ifndef _ds18b20_h_
//#define _ds18b20_h_
#include <at89x51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
p1=0;p2=0;p3=0;
sbit DQ=P2^6;
uchar temp[8],i,a_a,temp_buff;
uchar flag=0,crc_tmp; uint t=0;
void delayfor(uchar i);
void delay11us(uint k);
void delay1ms(uint k);
uchar ds18b20_readchar(); //read_command
void ds18b20_writechar(uchar dat); //write_command
readtemperature(void); //read_temperature
void gettemperature(void);
//uchar CRC (uchar j);
void read_18b20_info();
sbit one=P1^0;
//sbit wela=P2^7;
uchar ge=0x0e;
uchar shi=0x0d;
uchar bai=0x0b;
uchar qian=0x07;
/*uchar code CrcTable [256]={
0,94,188,226,97,63,221,131,194,156,126,32,163,253,31,65,
157,195,33,127,252,162,64,30,95,1,227,189,62,96,130,220,
35,125,159,193,66,28,254,160,225,191,93,3,128,222,60,98,
190,224,2,92,223,129,99,61,124,34,192,158,29,67,161,255,
70,24,250,164,39,121,155,197,132,218,56,102,229,187,89,7,
219,133,103,57,186,228,6,88,25,71,165,251,120,38,196,154,
101,59,217,135,4,90,184,230,167,249,27,69,198,152,122,36,
248,166,68,26,153,199,37,123,58,100,134,216,91,5,231,185,
140,210,48,110,237,179,81,15,78,16,242,172,47,113,147,205,
17,79,173,243,112,46,204,146,211,141,111,49,178,236,14,80,
175,241,19,77,206,144,114,44,109,51,209,143,12,82,176,238,
50,108,142,208,83,13,239,177,240,174,76,18,145,207,45,115,
202,148,118,40,171,245,23,73,8,86,180,234,105,55,213,139,
87,9,235,181,54,104,138,212,149,203,41,119,244,170,72,22,
233,183,85,11,136,214,52,106,43,117,151,201,74,20,246,168,
116,42,200,150,21,75,169,247,182,232,10,84,215,137,107,53};*/
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0xff,0xbf};
void delay(uint z);
void main()
{
read_18b20_info();
while(1)
{
readtemperature();
gettemperature();
if(temp[1]==0)
{temp[1]=10;
if(temp[2]==0)
{temp[2]=10;
if(temp[3]==0)temp[3]=10;
}
}
P1=ge;
//aa=temp[4];
P0=table[temp[4]];
delay(2);
P1=shi;
P0=table[temp[3]]&0x7f;
delay(2);
P1=bai;
//cc=temp[2];
P0=table[temp[2]];
delay(2);
P1=qian;
if(flag==1)temp[1]=11;
P0=table[temp[1]];
delay(2);
}
}
//子程序--18b20--
void delayfor(uchar i) //delay 20.54 us
{
for(i;i>0;i--) _nop_();
}
void delay11us(uint k) //4.32us 27.04 k=3.25
{
while(k--); //delay 20.55 us
// _nop_(); //2.17us
//_nop_();//
//_nop_();
//_nop_();
//_nop_();
//_nop_();//while(k--);
//_nop_();
//_nop_();
//_nop_();
}
void delay1ms(uint k) //delay 1ms
{
k=k*98;
while(k--);
}
/**18b20**/
void ds18b20_init(void)
{
DQ=1;
delayfor(3);
DQ=0;
delay11us(80);//delay796;
DQ=1;
delayfor(30);
DQ=1;// 25
}
//********************************************
/**ds18b20读一个字节**/
//********************************************
uchar ds18b20_readchar(void)
{
uchar i=0;
uchar dat = 0;
for(i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
delay11us(3); //(2)36.77us (3)46.50us (4)56.24 ()/9.7us
// delay11us(10);
}
return(dat);
}
//*****************************************************
/**ds18b20写一个字节**/
//*****************************************************
void ds18b20_writechar(uchar dat)
{
//uchar i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay11us(5); //delay56.20us
DQ = 1;
dat>>=1;
}
}
//********************************************************
/**读出温度**/
//********************************************************
readtemperature(void)
{
uchar a=0;
uchar b=0;
//;uint t=0;
ds18b20_init();
ds18b20_writechar(0xCC); // 跳过读序号列号的操作
ds18b20_writechar(0x44); // 启动温度转换
delay11us(500);
ds18b20_init();
ds18b20_writechar(0xCC); //跳过读序号列号的操作
ds18b20_writechar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ds18b20_readchar();
// ds18b20_writechar(0xBE);
b=ds18b20_readchar();
if((b*256+a)>2000)
{
t=~(b*256+a)+1; t=t*25/40;flag=1;//取反加1 //;
}
else
{
t=(b*256+a)*25/40;flag=0;
}
return(t);
}
//********************************************************
/**温度数值处理**/
//********************************************************
void gettemperature()
{
temp[0]=flag;
temp[1]=t/1000;
temp[2]=(t%1000)/100;
temp[3]=(t%100)/10;
temp[4]=t%10;
}
//********************************************************
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/**********************************/
/*********crc*********************/
/*uchar CRC (uchar j)
{
uchar i,crc_data=0;
temp_buff=0;
for(i=0;i<j;) //查表校验
{crc_data = CrcTable[crc_data^temp[temp_buff]];
i++;
temp_buff++;
}
return (crc_data);
}
*/
/*******************************************************/
/*read18b20 info
/*******************************************************/
void read_18b20_info(void)
{
//uchar i=0;
temp_buff=0;
ds18b20_init();
ds18b20_writechar(0x33);
for(i=0;i<8;)
{
temp[temp_buff]=ds18b20_readchar();
i++;
temp_buff++;
}
//crc_tmp=CRC(7);
//if(crc_tmp==temp[7])
// {
// }
// else
//{
//P1=shi;
// P0=table[7];
// delay(20);
//}
}
这是我以前写的你参考参考 |