📄 ds18b20_tlc2543.c
字号:
#include "include.h"
uchar begin2543()
{ uint result12=0x0000,yiwei=0x0800;
uchar ch_bit=0x90; //9通道,12位精度 ,高位先传,无符号
uchar jizun=0x82,i;
uchar result8;
//P1|=0X0F;
clk2543_0;
cs2543_1;
NOP(); NOP();
cs2543_0;
for(i=0;i<12;i++)
{ if(ch_bit&0x80)
{
din2543_1;
}
else
{
din2543_0;
}
//din2543=(bit)(ch_bit&0x80);
NOP(); NOP();
ch_bit=ch_bit<<1;
if(dout2543)
{
result12|=yiwei;
}
else
{
result12&=(~yiwei);
}
NOP(); NOP();
clk2543_1; //上跳DIN2543移入数据
NOP(); NOP();
clk2543_0; //下跳DOUT2543移出下一位的数据
yiwei=yiwei>>1;
}
cs2543_1;
// vlot =result12;
if(debug)
{ uart0_txd((uchar)(result12>>8));
uart0_txd((uchar)result12);
}
result12=result12>>4;
result8=(uchar)result12;
// adread=result8;
if(result8>=0x82)
return 0x00;
if(result8==0x81)
return 0x01;
if(result8==0x80)
return 0x02;
if(result8==0x7f)
return 0x03;
if(result8==0x7e)
return 0x04;
if((result8>=0x7b)&&(result8<0x7e) )
return 0x05;
if((result8>=0x78)&&(result8<0x7b) )
return 0x06;
if((result8>=0x74)&&(result8<0x78) )
return 0x07;
if((result8>=0x6f)&&(result8<0x74) )
return 0x08;
if((result8>=0x68)&&(result8<0x6f) )
return 0x09;
if((result8>=0x5f)&&(result8<0x68) )
return 0x0a;
if((result8>=0x5a)&&(result8<0x5f) )
return 0x0b;
if((result8>=0x55)&&(result8<0x5a) )
return 0x0c;
if((result8>=0x50)&&(result8<0x55) )
return 0x0d;
if((result8>=0x48)&&(result8<0x50) )
return 0x0e;
if((result8>=0x40)&&(result8<0x48) )
return 0x0f;
if((result8>=0x38)&&(result8<0x40) )
return 0x10;
if((result8>=0x30)&&(result8<0x38) )
return 0x11;
if((result8>=0x2a)&&(result8<0x30) )
return 0x12;
if((result8>=0x24)&&(result8<0x2a) )
return 0x13;
if((result8>=0x1f)&&(result8<0x24) )
return 0x14;
if((result8>=0x1a)&&(result8<0x1f) )
return 0x15;
if((result8>=0x12)&&(result8<0x1a) )
return 0x16;
if((result8>=0x0f)&&(result8<0x12) )
return 0x17;
if((result8>=0x0d)&&(result8<0x0f) )
return 0x18;
if((result8>=0x0a)&&(result8<0x0d) )
return 0x19;
if((result8>=0x09)&&(result8<0x0a) )
return 0x1a;
if(result8==0x08)
return 0x1b;
if((result8>=0x06)&&(result8<0x08) )
return 0x1c;
if(result8==0x05)
return 0x1d;
if((result8>=0x03)&&(result8<0x05) )
return 0x1e;
if(result8<0x03)
return 0x1f;
}
void begin18b20(uchar *sign ,uchar *numb)
{uchar bcd1=0,bcd2=0;
uchar temp_h,temp_l,temp_sign,temp_num;
if(resetb20())
{ writeb20(0xcc);
writeb20(0x44);
delay(120); //ts480
delay(120); //ts480
delay(120); //ts480
resetb20();
writeb20(0xcc);
writeb20(0xbe);
temp_l=readb20();
temp_h=readb20();
temp_sign=0x2b;
if(temp_h&0x80) // 负温度处理
{temp_sign=0x2d;
temp_h=~temp_h;
temp_l=~temp_l;
if(temp_l==255)
{temp_l=0;temp_h++;} //取反加一 如果取反后为255 则加一为0 ,低8位向高位进位
else
{temp_l++;}
}
temp_num=0;
if(temp_l&0x08)
{temp_num=1;}
temp_l=temp_l>>4;
temp_h=temp_h<<4;
temp_l=temp_l|temp_h;
temp_num=temp_num+temp_l;
// temperature=temp_num ;
// if(debug)
// uart0_txd(temp_num);
bcd1=temp_num/10;
bcd2=temp_num%10;
bcd1=bcd1<<4 ;
temp_num=bcd1|bcd2;
/*为什么一定得转成BCD码*/
// config[1024]=temp_sign;
// config[1025]=temp_num;
*sign=temp_sign;
*numb=temp_num;
}
else
{uart0_str("ds18b20 error ");}
}
uchar resetb20()
{temp_0;
delay(120); //ts480拉低480US后使总线为高 这样产生一个上跳沿
temp_1;
delay(15); //TS60 60US后如果器件反应则把总线拉低
if(!tempin)
{
delay(120); //ts480
return 1;
}
delay(120); //ts480
return 0;
}
void writeb20(uchar ww)
{uchar i;
for(i=0;i<8;i++)
{ temp_0;
delay(1);
if(0x01&ww)
{temp_1; }
else
{ temp_0;}
//temp=(bit)(0x01&ww) ;// 写 1 时 拉低时间仅为 delay(1);
delay(12); //a写0时拉低时间为 delay(1); delay(4); delay(4);
temp_1;
ww=ww>>1;
NOP(); NOP();
}
temp_1;
}
uchar readb20()
{uchar i,read=0;
for(i=0;i<8;i++)
{ read=read>>1;
temp_1 ;
NOP(); NOP(); NOP(); NOP();NOP();NOP();
temp_0; //拉低至少一秒
NOP(); NOP(); NOP(); NOP();NOP();NOP();
temp_1; //然后交出总线
delay(1);
if(tempin)
read|=0x80;
else read&=0x7f;
delay(15);
}
temp_1;
return read;
}
void delay(uchar x)
{ while(x>0)
{ NOP(); NOP();NOP(); NOP();NOP(); NOP();NOP(); NOP(); NOP();
NOP(); NOP();NOP(); NOP();NOP(); NOP();NOP(); NOP(); NOP();
NOP(); NOP();NOP(); NOP();NOP(); NOP();NOP(); NOP(); NOP();
x-- ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -