各位看看有啥问题啊,结果永远是FF FF
//================================================================
void DS18b20_Rest(void)
{
//_GPIO_ENABLE_DIGITAL_PATH(P3, 4);
_GPIO_SET_PIN_MODE(P3, 4, GPIO_PMD_OUTPUT);
P34 = 1;
SYS_SysTickDelay(2);
P34 = 0;
SYS_SysTickDelay(750);
P34 = 1;
SYS_SysTickDelay(15);
}
//================================================================
uchar DS18B20_Check(void)
{
uchar rety = 0;
//_GPIO_ENABLE_DIGITAL_PATH(P3, 4);
_GPIO_SET_PIN_MODE(P3, 4, GPIO_PMD_INPUT);
while(P34&&rety<200)
{
rety++;
SYS_SysTickDelay(1);
};
if(rety>=200)
return 1;
else
rety = 0;
while(P34&&rety<240)
{
rety++;
SYS_SysTickDelay(1);
};
if(rety>=240)
return 1;
return 0;
}
//================================================================
void Write_One_Byte(uchar dat)
{
uchar i = 0;
uchar whicebit;
//_GPIO_ENABLE_DIGITAL_PATH(P3, 4);
_GPIO_SET_PIN_MODE(P3, 4, GPIO_PMD_OUTPUT);
for(i=0;i<8;i++)
{
whicebit = dat&0x01;
dat>>=1;
if(whicebit)
{
P34 = 0;
SYS_SysTickDelay(2);
P34 = 1;
SYS_SysTickDelay(60);
}
else
{
P34 = 0;
SYS_SysTickDelay(60);
P34 = 1;
SYS_SysTickDelay(2);
}
}
P34 = 1;
}
//=====================================================================
uchar Read_One_Bit()
{
uchar dat = 0;
//_GPIO_ENABLE_DIGITAL_PATH(P3, 4);
_GPIO_SET_PIN_MODE(P3, 4, GPIO_PMD_OUTPUT);
P34 = 1;
SYS_SysTickDelay(2);
P34 = 0;
SYS_SysTickDelay(2);
P34 = 1;
//_GPIO_ENABLE_DIGITAL_PATH(P3, 4);
_GPIO_SET_PIN_MODE(P3, 4, GPIO_PMD_INPUT);
SYS_SysTickDelay(12);
if(P34)
dat = 1;
else
dat = 0;
SYS_SysTickDelay(60);
return (dat);
}
//================================================================
uchar DS18B20_Read_Byte(void)
{
uchar i,j,dat;
dat = 0;
for (i=0;i<8;i++)
{
j=Read_One_Bit();
dat=(j<<i)|dat;
}
return dat;
}
//===============================================================
void DS18B20_Star_Com(void)
{
DS18b20_Rest();
DS18B20_Check();
Write_One_Byte(0xcc);
Write_One_Byte(0x44);
}
//===============================================================
void DS18B20_Read_Com(void)
{
DS18b20_Rest();
DS18B20_Check();
Write_One_Byte(0xcc);
Write_One_Byte(0xbe);
}
//===============================================================
void Get_Tmp(void)
{
float temp_map;
uchar temp_l,temp_h,flag;
DS18B20_Star_Com();
DS18B20_Read_Com();
temp_l = DS18B20_Read_Byte();
temp_h = DS18B20_Read_Byte();
// if(temp_h>7)
// {
// temp_h = ~temp_h;
// temp_l = ~temp_l;
// flag = 0;
// }
// else
// flag = 1;
temp = temp_h;
temp = temp<<8;
temp = temp|temp_l;
temp_map=temp*0.0625;
temp = (uint)(temp_map*10+5);
TX1_buf[0] = temp/256;
TX1_buf[1] = temp%256;
} |