我的代码如下.........帮忙看看....错在那儿了..........
/*****************************************************/
/* ----------------读DS18B20函数------------------- */
/*****************************************************/
void Read_DS18B20() // get the temperature
{
// EA=0;
Init_DS18B20(); // 初始化DS18B20子函数
if(Ack_sign)
{
WriteOneChar(0xcc); // 跳过ROM命令
WriteOneChar(0x44); // 发出温度转换命令
Delay(5);
//DS18b20_1dir=0; // 输入状态
Init_DS18B20(); // 初始化DS18B20子函数
if(Ack_sign)
{
WriteOneChar(0xcc); // 跳过ROM命令
WriteOneChar(0xbe); // 发出温度转换命令
dat_L=ReadOneChar(); // 连续读两个字节数据
dat_H=ReadOneChar();
dat_HL=dat_H;
dat_HL=dat_HL<<8;
dat_HL=dat_HL+dat_L;
DS18B20_3=1;
if(d<5)
{
d++; // 读DS18B20启始"随机数"控制辅助变量
}
}
}
// EA=1;
}
/************************************************/
/*------------ 初始化DS18B20子函数------------ */
/************************************************/
void Init_DS18B20()
{
DS18b20_3dir=1; // 输出状态
DS18B20_3 = 1; // DS1820复位
Delay(3); // 稍做延时 6us //48us
DS18B20_3 = 0; // 单片机将DS1820拉低
Delay(80); // 精确延时 大于 600us// 480us
DS18B20_3 = 1; // 拉高总线
Delay(7); // 精确延时 大于 60us
DS18B20_3 = 0;
DS18b20_3dir=0; // 改为输入状态
Delay(3);
responsion(); // 调用应答检查程序
Delay(50); // 精确延时 大于 120us
//DS18B20_3 = 1;
//Delay(20);
}
/**********************************************/
/*-----------应答检查子函数------------------ */
/**********************************************/
void responsion()
{
assistant=DS18B20_3;
if(assistant)
{
Ack_sign=0;
}
if(!assistant)
{
Ack_sign=1;
}
}
/**********************************************/
/*---------------读字节子函数-----------------*/
/**********************************************/
byte ReadOneChar(void)
{
uchar i = 0;
for (i=8; i>0; i--)
{
DS18b20_3dir=1; // 输出状态
DS18B20_3 = 0; // 给脉冲信号
dat >>= 1;
Delay(2); // 时间隙
DS18B20_3 = 1; // 给脉冲信号
//Delay(1); // 时间隙
DS18b20_3dir=0; // 改为输入状态
Delay(3); // 时间隙
if(DS18B20_3)
dat |= 0x80;
Delay(5);
}
return (dat);
}
/*************************************************/
/*---------------写字节子函数--------------------*/
/*************************************************/
void WriteOneChar(uchar wdat)
{
uchar i = 0;
for (i=8; i>0; i--)
{
DS18b20_3dir=1; // 输出状态
DS18B20_3 = 0;
Delay(2);
DS18B20_3 = wdat&0x01; // 写入一位数据
//Rd1820 = wdat&0x01;
Delay(6); // 延时 大于 30us
DS18B20_3 = 1;
wdat>>=1; // 右移一位数据
Delay(2);
}
}
|