温度传感器是比较简单的外设之一,学习51单片机都会学习ds18b20来学习51的ad转换。
ds18b20是通过iic总线来进行通行的,属于比较简单的模块之一,这里贴出ds18b20的驱动程序,调用时只要直接读取对应地址的数据即可。
#include"temp.h"
void Delay1ms(uint y)
{
uint x;
for( ; y>0; y--)
{
for(x=110; x>0; x--);
}
}
uchar Ds18b20Start()
{
uchar i;
DSPORT=0;
i=70;
while(i--);
DSPORT=1;
i=0;
while(DSPORT)
{
Delay1ms(1);
i++;
if(i>5)
{return 0;}
}
return 1;
}
void Ds18b20WriteByte(uchar dat)
{
uchar i,j;
for(j=0;j<8;j++)
{
DSPORT=0;
i++;
DSPORT=dat&0x01;
i=6;
while(i--);
DSPORT=1;
dat>>=1;
}
}
uchar Ds18b20ReadByte()
{
uchar bi,byte;
uint i,j;
for(j=8;j>0;j--)
{
DSPORT=0;
i++;
DSPORT=1;
i++;
i++;
bi=DSPORT;
byte=(byte>>1)|(bi<<7);
i=4;
while(i--);
}
return byte;
}
void Ds18b20ChangeTemp()
{
Ds18b20Start();
D