新手求助!各路大侠帮忙看下DS18B20的程序
时间:10-02
整理:3721RD
点击:
程序如下: #include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit GPIO=P1^0; //í¨?????ú
sbit DOT=P2^0; //D?êyμ?
int tempt=0; //???è
uchar dis[]={0,0xfc,0xfc,0xfc,0xfc,0xfc}; //ò????y?oo?????êy×?
uchar col_sel[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf}; //??????
uchar code num[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xe6}; //0~9????
void Delay(uchar ms); //?óê±×óoˉêy
void ChangeTempt(); //???è×a??×óoˉêy
void ReadTempt(); //???è?áè?×óoˉêy
void WriteCom(uchar com); //?òDS18B20D′?üá?
void ReadDat(); //?áè?16?????è?μ21??
void Display(void); //??ê?×óoˉêy
uchar Initial(); //3?ê??ˉ×óoˉêy
void Trans(int tempt); //??21??×a?ˉ?a???è
void main()
{
while(1)
{
Display();
ChangeTempt();
ReadTempt();
Trans(tempt);
Display();
}
}
void Delay(uchar ms)
{
uchar i;
while(ms--)
{
for(i=0;i<200;i++)
{
_nop_();
_nop_();
}
}
}
uchar Initial()
{
uchar i=85;
GPIO=0; //à-μí×ü??2¢??3?ò???ê±??
while(i--);
GPIO=1; //êí·?×ü??
while(GPIO) //?D??ê?·?óDó|′e
{
uint j=0;
j++;
if(j>2500) //3?ê±?Tó|′e3?ê??ˉ꧰ü
{
return 0;
}
}
return 1; //3?ê??ˉ3é1|·μ??1
}
void WriteCom(uchar com)
{
uchar i;
for(i=0;i<8;i++)
{
uchar j=6;
com>>=1; //êy?Y′ó×?μí???aê?D′
GPIO=0; //?è°?×ü??à-μí1us
_nop_();
GPIO=CY; //D′è?ò???
while(j--); //3?D?ê±???áéù60us
GPIO=1; //à-??×ü???áéù1us
_nop_();
}
}
void ReadDat()
{
uchar k;
for(k=0;k<16;k++)
{
uchar g=4;
GPIO=0;
_nop_();
GPIO=1; //?èà-μí×ü??1us?ùêí·?×ü??
_nop_();
_nop_(); //?óê±μè′yêy?Y?è?¨
if(GPIO)
{
tempt|=0x8000;
}
tempt>>=1; //′ó×?μí???aê??á
while(g--); //?á?ü?ú?áéùDèòa60us
}
}
void Display(void)
{
uchar h,n;
uchar g=10;
P3=0xff;
while(g--)
{
for(h=0;h<6;h++)
{
P2=dis[h]; //??P2?3????
if(h==3) //?óD?êyμ?
{
DOT=1;
}
P3=col_sel[h]; //??P3?ú?3????
n=300;
while(n--);
P3=0xff; //1?±?????
}
}
}
void ChangeTempt()
{
uchar m;
if(Initial())
{
Delay(3);
WriteCom(0xcc); //ì?1yROM
WriteCom(0x44); //???è×a???üá?
for(m=0;m<150;m++)
{
Delay(1);
Display();
}
}
}
void ReadTempt()
{
if(Initial())
{
Delay(3);
WriteCom(0xcc); //ì?1yROM
WriteCom(0xbe); //?áè????è?üá?
ReadDat(); //?áè????è21??
}
}
void Trans(int tempt)
{
if(tempt&0xf000) //è?1??áè?μ????èê??o?μ
{
dis[0]=0x02;
tempt--;
tempt=~tempt;
tempt=tempt*0.0625*100+0.5;
}
else
{
tempt=tempt*0.0625*100+0.5;
}
dis[1]=num[tempt/10000]; //°ù??
tempt=tempt%10000;
dis[2]=num[tempt/1000]; //ê???
tempt=tempt%1000;
dis[3]=num[tempt/100]; //????
tempt=tempt%100;
dis[4]=num[tempt/10]; //μúò???D?êy
tempt=tempt%10;
dis[5]=num[tempt]; //μú?t??D?êy
}
(注释不知道为什么变乱码了)
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit GPIO=P1^0; //í¨?????ú
sbit DOT=P2^0; //D?êyμ?
int tempt=0; //???è
uchar dis[]={0,0xfc,0xfc,0xfc,0xfc,0xfc}; //ò????y?oo?????êy×?
uchar col_sel[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf}; //??????
uchar code num[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xe6}; //0~9????
void Delay(uchar ms); //?óê±×óoˉêy
void ChangeTempt(); //???è×a??×óoˉêy
void ReadTempt(); //???è?áè?×óoˉêy
void WriteCom(uchar com); //?òDS18B20D′?üá?
void ReadDat(); //?áè?16?????è?μ21??
void Display(void); //??ê?×óoˉêy
uchar Initial(); //3?ê??ˉ×óoˉêy
void Trans(int tempt); //??21??×a?ˉ?a???è
void main()
{
while(1)
{
Display();
ChangeTempt();
ReadTempt();
Trans(tempt);
Display();
}
}
void Delay(uchar ms)
{
uchar i;
while(ms--)
{
for(i=0;i<200;i++)
{
_nop_();
_nop_();
}
}
}
uchar Initial()
{
uchar i=85;
GPIO=0; //à-μí×ü??2¢??3?ò???ê±??
while(i--);
GPIO=1; //êí·?×ü??
while(GPIO) //?D??ê?·?óDó|′e
{
uint j=0;
j++;
if(j>2500) //3?ê±?Tó|′e3?ê??ˉ꧰ü
{
return 0;
}
}
return 1; //3?ê??ˉ3é1|·μ??1
}
void WriteCom(uchar com)
{
uchar i;
for(i=0;i<8;i++)
{
uchar j=6;
com>>=1; //êy?Y′ó×?μí???aê?D′
GPIO=0; //?è°?×ü??à-μí1us
_nop_();
GPIO=CY; //D′è?ò???
while(j--); //3?D?ê±???áéù60us
GPIO=1; //à-??×ü???áéù1us
_nop_();
}
}
void ReadDat()
{
uchar k;
for(k=0;k<16;k++)
{
uchar g=4;
GPIO=0;
_nop_();
GPIO=1; //?èà-μí×ü??1us?ùêí·?×ü??
_nop_();
_nop_(); //?óê±μè′yêy?Y?è?¨
if(GPIO)
{
tempt|=0x8000;
}
tempt>>=1; //′ó×?μí???aê??á
while(g--); //?á?ü?ú?áéùDèòa60us
}
}
void Display(void)
{
uchar h,n;
uchar g=10;
P3=0xff;
while(g--)
{
for(h=0;h<6;h++)
{
P2=dis[h]; //??P2?3????
if(h==3) //?óD?êyμ?
{
DOT=1;
}
P3=col_sel[h]; //??P3?ú?3????
n=300;
while(n--);
P3=0xff; //1?±?????
}
}
}
void ChangeTempt()
{
uchar m;
if(Initial())
{
Delay(3);
WriteCom(0xcc); //ì?1yROM
WriteCom(0x44); //???è×a???üá?
for(m=0;m<150;m++)
{
Delay(1);
Display();
}
}
}
void ReadTempt()
{
if(Initial())
{
Delay(3);
WriteCom(0xcc); //ì?1yROM
WriteCom(0xbe); //?áè????è?üá?
ReadDat(); //?áè????è21??
}
}
void Trans(int tempt)
{
if(tempt&0xf000) //è?1??áè?μ????èê??o?μ
{
dis[0]=0x02;
tempt--;
tempt=~tempt;
tempt=tempt*0.0625*100+0.5;
}
else
{
tempt=tempt*0.0625*100+0.5;
}
dis[1]=num[tempt/10000]; //°ù??
tempt=tempt%10000;
dis[2]=num[tempt/1000]; //ê???
tempt=tempt%1000;
dis[3]=num[tempt/100]; //????
tempt=tempt%100;
dis[4]=num[tempt/10]; //μúò???D?êy
tempt=tempt%10;
dis[5]=num[tempt]; //μú?t??D?êy
}
(注释不知道为什么变乱码了)
电路图
仿真后不管温度为多少都会变成这样