首先你可以搭建个读ds18b20的64位ROM的电路,这样你就可以读出每个你要用的DS18B20的序列号了,以上部分我有电路图和程序。
当你知道了每个的序列号时,你可以自己编程发匹配rom指令55h,后面跟上你的64位序列号,是哪个1820的号,哪个就响应你了。
电路对应的程序如下:
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P2^2;
sbit lcdrw=P2^1;
sbit lcdrs=P2^0;
sbit DQ=P2^3;
uchar num,i,t,a,b;
uchar a1,a2,a3,a4,a5,a6,a7,a8;
uchar a11,a12;
uchar a21,a22;
uchar a31,a32;
uchar a41,a42;
uchar a51,a52;
uchar a61,a62;
uchar a71,a72;
uchar a81,a82;
uchar code table[]="DS18B20 ROM IS:";
void delayms(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void delay_15us(uchar n)
{
do
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();
n--;
}
while(n);
}
uchar DS18B20_init(void)
{
uchar x=0;
DQ=1;
delay_15us(6);
DQ=0;
delay_15us(50);
DQ=1;
delay_15us(3);
x=DQ;
delay_15us(30);
return (x);
}
uchar Rdbit(void)
{
bit s;
DQ=1;
_nop_();
DQ=0;
_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();
DQ=1;
_nop_();
s=DQ;
_nop_();
delay_15us(4);
return(s);
}
uchar Rdbyte(void)
{
uchar j;
uchar i=0;
uchar dat=0;
for(i=8;i>0;i--)
{
j=Rdbit();
dat=(j<<7)|(dat>>1);
}
return(dat);
}
void Wrbyte(uchar dat)
{
unsigned char i=0;
for(i=8;i>0;i--)
{
DQ=0;
delay_15us(1);
DQ=dat&0x01;
delay_15us(3);
DQ=1;
dat>>=1;
}
delay_15us(1);
}
void int_(void)
{
while(DS18B20_init());
delayms(1);
Wrbyte(0xcc);
Wrbyte(0x44);
}
void write_com(uchar com)
{
lcdrs=0;
lcdrw=0;
P0=com;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void write_dat(uchar dat)
{
lcdrs=1;
lcdrw=0;
P0=dat;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void DispCharacter(uchar x,uchar y,uint dat)
{
uint temp;
temp=y&0x0f;
x&=0x01;
if(x)temp|=0x40;
write_com(0x80|temp);
write_dat(dat);
}
void init_LCD()
{
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void main()
{
init_LCD();
write_com(0x80);
for(i=0;i<15;i++)
{
write_dat(table[i]);
}
while(DS18B20_init());
delayms(1);
Wrbyte(0x33);
a1=Rdbyte();
a2=Rdbyte();
a3=Rdbyte();
a4=Rdbyte();
a5=Rdbyte();
a6=Rdbyte();
a7=Rdbyte();
a8=Rdbyte();
{a11=a1/16;
a12=a1%16;
if(a11>9)a11+=0x37;else a11+=0x30;
if(a12>9)a12+=0x37;else a12+=0x30;}
{a21=a2/16;
a22=a2%16;
if(a21>9)a21+=0x37;else a21+=0x30;
if(a22>9)a22+=0x37;else a22+=0x30;}
{a31=a3/16;
a32=a3%16;
if(a31>9)a31+=0x37;else a31+=0x30;
if(a32>9)a32+=0x37;else a32+=0x30;}
{a41=a4/16;
a42=a4%16;
if(a41>9)a41+=0x37;else a41+=0x30;
if(a42>9)a42+=0x37;else a42+=0x30;}
{a51=a5/16;
a52=a5%16;
if(a51>9)a51+=0x37;else a51+=0x30;
if(a52>9)a52+=0x37;else a52+=0x30;}
{a61=a6/16;
a62=a6%16;
if(a61>9)a61+=0x37;else a61+=0x30;
if(a62>9)a62+=0x37;else a62+=0x30;}
{a71=a7/16;
a72=a7%16;
if(a71>9)a71+=0x37;else a71+=0x30;
if(a72>9)a72+=0x37;else a72+=0x30;}
{a81=a8/16;
a82=a8%16;
if(a81>9)a81+=0x37;else a81+=0x30;
if(a82>9)a82+=0x37;else a82+=0x30;}
while(1)
{
DispCharacter(1,0,a11);
DispCharacter(1,1,a12);
DispCharacter(1,2,a21);
DispCharacter(1,3,a22);
DispCharacter(1,4,a31);
DispCharacter(1,5,a32);
DispCharacter(1,6,a41);
DispCharacter(1,7,a42);
DispCharacter(1,8,a51);
DispCharacter(1,9,a52);
DispCharacter(1,10,a61);
DispCharacter(1,11,a62);
DispCharacter(1,12,a71);
DispCharacter(1,13,a72);
DispCharacter(1,14,a81);
DispCharacter(1,15,a82);
}
}
这是自己刚写的,能用,呵呵