目的:用温度传感器采集温度,读给单片机,再把数据用数码管显示.这里也可以用PC机接收后保存,对温度进行时时监控.
程序:
#include <reg52.h>
#define uchar unsigned char;
sbit DQ="P1"^4;
unsigned char lab,mab;
//-------------------------------------------------------------
void delay2(int i)
{
while(--i);
}
//---------------------------------------------------------------
void init()
{
bit put;
DQ=0;
delay2(300);
DQ=1;
delay2(20);
put=DQ;
delay2(100);
}
//--------------------------------------------------------------------
uchar readbit()
{ bit p;
DQ="0";
if(p==1)
{
DQ="1";
}
delay2(5);
DQ="1";
delay2(20);
return (p);
}
//-----------------------------------------------------------------------
void writebit(char q)
{
DQ="0";
delay2(5);
if(q==1)
{
DQ="1";
delay2(20);
}
else
delay2(20);
DQ="1";
}
//------------------------------------------------------------------------
void writebyte(char dat)
{
char j,temp;
for(j=0;j<8;j++)
{
temp="dat">>j;
temp="temp"&0x01;
writebit(temp);
}
delay2(40);
}
//---------------------------------------------------------------------------
uchar readbyte()
{
char k,save;
for(k=0;k<8;k++)
{
if(readbit())
{
save="save|"(0x01<<k);
}
}
delay2(40);
}
//------------------------------------------------------------------------------
void convert_T()
{
init();
delay2(5);
writebyte(0xCC);
writebyte(0x44);
}
//----------------------------------------------------------------------------------
void readtemperature()
{
init();
delay2(5);
writebyte(0xCC);
writebyte(0xBE);
lab=readbyte();
mab=readbyte();
}
//-----------------------------------------------------------------------------------
void main()
{
do
{
P2=0x7F;
init();
delay2(5);
convert_T();
delay2(300000);
readtemperature();
}
while(1);
}
文章评论(0条评论)
登录后参与讨论