标题:
DS18B20与AT89C2051组成的测温系统源程序
[打印本页]
作者:
苹果也疯狂
时间:
2015-1-2 22:19
标题:
DS18B20与AT89C2051组成的测温系统源程序
用一片DS18B20构成测温系统,测量的温度精度达到0.1度,测量的温度的范围在-20度到+100度之间,用8位数码管显示出来。
下面是采用AT89C2051单片机与DS18B20的测温系统C语言源程序,硬件电路请参考本站相关文章.
#I Nclude <AT89X52.H>
#I Nclude <INTRINS.H>
Unsigned Char Code Displaybit[]={0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f};
Unsigned Char Code Displaycode[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00,0x40};
Unsigned Char Code Dotcode[32]={0,3,6,9,12,16,19,22,
25,28,31,34,38,41,44,48,
50,53,56,59,63,66,69,72,
75,78,81,84,88,91,94,97};
Unsigned Char Displaycount;
Unsigned Char Displaybuf[8]={16,16,16,16,16,16,16,16};
Unsigned Char Timecount;
Unsigned Char Readdata[8];
Sbit DQ=P3^7;
Bit Sflag;
Bit Resetpulse(Void)
{
Unsigned Char I;
DQ=0;
For(I=255;I>0;I--);
DQ=1;
For(I=60;I>0;I--);
Return(DQ);
For(I=200;I>0;I--);
}
Void Writecommandtods18b20(Unsigned Char Command)
{
Unsigned Char I;
Unsigned Char J;
For(I=0;I<8;I++)
{
If((Command & 0x01)==0)
{
DQ=0;
For(J=35;J>0;J--);
DQ=1;
}
Else
{
DQ=0;
For(J=2;J>0;J--);
DQ=1;
For(J=33;J>0;J--);
}
Command=_cror_(Command,1);
}
}
Unsigned Char Readdatafromds18b20(Void)
{
Unsigned Char I;
Unsigned Char J;
Unsigned Char Temp;
Temp=0;
For(I=0;I<8;I++)
{
Temp=_cror_(Temp,1);
DQ=0;
_nop_();
_nop_();
DQ=1;
For(J=10;J>0;J--);
If(DQ==1)
{
Temp=Temp | 0x80;
}
Else
{
Temp=Temp | 0x00;
}
For(J=200;J>0;J--);
}
Return(Temp);
}
Void Main(Void)
{
TMOD=0x01;
TH0=(65536-4000)/256;
TL0=(65536-4000)%256;
ET0=1;
EA=1;
While(Resetpulse());
Writecommandtods18b20(0xcc);
Writecommandtods18b20(0x44);
TR0=1;
While(1)
{
;
}
}
Void T0(Void) Interrupt 1 Using 0
{
Unsigned Char X;
Unsigned Int Result;
TH0=(65536-4000)/256;
TL0=(65536-4000)%256;
If(Displaycount==2)
{
P0=Displaycode[Displaybuf[Displaycount]] | 0x80;
}
Else
{
P0=Displaycode[Displaybuf[Displaycount]];
}
P2=Displaybit[Displaycount];
Displaycount++;
If(Displaycount==8)
{
Displaycount=0;
}
Timecount++;
If(Timecount==150)
{
Timecount=0;
While(Resetpulse());
Writecommandtods18b20(0xcc);
Writecommandtods18b20(0xbe);
Readdata[0]=Readdatafromds18b20();
Readdata[1]=Readdatafromds18b20();
For(X=0;X<8;X++)
{
Displaybuf[X]=16;
}
Sflag=0;
If((Readdata[1] & 0xf8)!=0x00)
{
Sflag=1;
Readdata[1]=~Readdata[1];
Readdata[0]=~Readdata[0];
Result=Readdata[0]+1;
Readdata[0]=Result;
If(Result>255)
{
Readdata[1]++;
}
}
Readdata[1]=Readdata[1]<<4;
Readdata[1]=Readdata[1] & 0x70;
X=Readdata[0];
X=X>>4;
X=X & 0x0f;
Readdata[1]=Readdata[1] | X;
X=2;
Result=Readdata[1];
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0