.
.
#include
#define DO P3_7
#define SEG P0 //数码管自左至右依次为1234位
#define MS2L 0x18 //1ms的延时参数
#define MS2H 0xfc
#define uchar unsigned char
#define uint unsigned int
uchar code comm[4] = {0x01,0x02,0x04,0x08};
uchar code seg[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar code seg_add_dicimal[10] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
uchar code seg_dicimal[2] ={0xc0, 0x92};
uchar k = 0;
uchar temp_l = 0;
uchar temp_h = 0;
uchar tempsign = 0;
uchar hundreds = 0;
uchar tens = 0;
uchar ones = 0;
uchar low_four = 0;
/*延时以ms为单位的t时间*/
void Delay(uint t)
{
uint i;
while(t--)
{
for(i=0; i<125; i++)
{ }
}
}
/*us级延时,延时时间约4+2*i*/
void Delayus(uchar i)
{
while(--i);
}
/*产生复位脉冲,等待应答信号*/
void Resetpaulse()
{
DO = 0; //拉低约600us
Delayus(150);
Delayus(150);
DO = 1;//产生上升沿,延时约15~60us
Delayus(30);
while(~DO); //等待应答信号
}
/*读取数据一位*/
bit Readbit()
{
uint i = 0;
bit b = 0;
DO = 0; //产生读时隙
i++; //维持低电平至少1us
DO = 1; //1us以上后拉高
Delayus(2); //延时8us,DO下降沿15内ds18b20输出的数据才有效
b = DO; //读取数据
Delayus(40); //每个读时隙至少持续60us
return(b);
}
/*读取一个字节*/
uchar Readbyte()
{
uchar byte_read = 0;
uchar i, j;
for(i=0; i<8; i++)
{
j = Readbit();
byte_read = (j<
}
return(byte_read);
}
/*写一个字节*/
void Writebyte(uchar byte_to_write)
{
uchar i = 0;
uchar j = 0;
bit write_bit = 0;
for(j=0; j<8; j++)
{
write_bit = (byte_to_write & 0x01);
if(write_bit == 1) //写1
{
DO = 0; //产生写时隙
Delayus(3); //延时15us
DO = 1; //写1
Delayus(40); //延时,写时隙不得低于60us
}
else
{
DO = 0; //产生写时隙
Delayus(50); //延时,保持低约60us~120us
DO = 1;
i++;
}
byte_to_write = byte_to_write >> 1;
}
}
/*配置ds18b20,9位分辨率 */
void Configurate()
{
EA = 0;
Resetpaulse(); //发出复位脉冲,每次操作都从复位开始
Delay(1);
Writebyte(0xcc); //skip room命令
Writebyte(0x4e);
Writebyte(0x7f);
Writebyte(0x80);
Writebyte(0x1f);
EA = 1;
}
/*启动温度转换*/
void StartConvert(