首先,为了便于通讯,我将一个tick的时间改为了10us,本来想改成1us,但是不知道什么原因,在1us的情况下系统无法运行,有没有大佬会用1u的迟延,教我一下,万分感谢!!!
以下是程序:
链接:https://pan.baidu.com/s/1_UhMc4SPa_zoaDFhOQZByg?pwd=ap9a
提取码:ap9a
static void DS18B20_Rst(void)//DS18B20¸´Î»
{
OutDQ0; //??????480us????????
tls_os_time_delay(10);
OutDQ1; //??????????,??????
tls_os_time_delay(10); //?????????????,??15~60us???????????
}
static u8 DS18B20_Presence(void)//检测设备是否存在
{
u8 pulse_time = 0;
u8 temp=1;
while( temp&&pulse_time<100 )
{
temp =RedDQ;
if ((temp==0)&&(pulse_time<100))
{
return 1;
}
else
{
return 0;
}
pulse_time++;
tls_os_time_delay(1);
}
}
static void DS18B20_RST(void)//DS18B20,设备复位
{
OutDQ0; //??????480us????????
tls_os_time_delay(70);
OutDQ1; //??????????,??????
tls_os_time_delay(15); //?????????????,??15~60us???????????
}
void DS18B20_WriteByte(u8 dat)//
{
u8 i, testb;
for( i=0; i<8; i++ )
{
testb = dat&0x01;
dat = dat>>1;
if (testb)
{
OutDQ0;
tls_os_time_delay(8); //1us < ???? < 15us
OutDQ1;
tls_os_time_delay(58); //58us+8us>60us
}
else
{
OutDQ0;
/* 60us < Tx 0 < 120us */
tls_os_time_delay(70);
OutDQ1;
/* 1us < Trec(????) < ???*/
tls_os_time_delay(2);
}
}
}
void DS18B20_Start(void)//Æô¶¯18b20
{
DS18B20_RST(); //???????????
DS18B20_Presence(); //??????????????
DS18B20_WriteByte(0XCC); // ?? ROM
DS18B20_WriteByte(0X44); // ????
}
u8 DS18B20_Init(void)//18b20³õʼ»¯
{
DS18B20_Rst();
return DS18B20_Presence();
}
static u8 DS18B20_Read_Bit(void) //¶ÁȡһλÊý¾Ý
{
u8 dat,temp;
OutDQ0;
tls_os_time_delay(10);
IpuDQ;
temp=RedDQ;
if(temp == 1 )
dat = 1;
else
dat = 0;
tls_os_time_delay(45);
return dat;
}
u8 DS18B20_Read_Byte(void)//¶ÁÈ¡Ò»¸ö×Ö½Ú
{
u8 i, j, dat = 0;
for(i=0; i<8; i++)
{
j = DS18B20_Read_Bit(); //?DS18B20????bit
dat = (dat) | (j<<i);
}
return dat;
}
float DS18B20_Get_Temp(void)//¶ÁȡζÈ
{
u8 tpmsb, tplsb;
short s_tem;
float f_tem;
DS18B20_Rst();
DS18B20_Presence();
DS18B20_WriteByte(0XCC); /* ?? ROM */
DS18B20_WriteByte(0X44); /* ???? */
DS18B20_Rst();
DS18B20_Presence();
DS18B20_WriteByte(0XCC); /* ?? ROM */
DS18B20_WriteByte(0XBE); / ???? /
tplsb = DS18B20_Read_Byte();
tpmsb = DS18B20_Read_Byte();
s_tem = tpmsb<<8;
s_tem = s_tem | tplsb;
if( s_tem < 0 ) /* ??? */
f_tem = (~s_tem+1) * 0.0625;
else
f_tem = (s_tem * 0.0625);
return f_tem;
}
void UserMain(void)
{
// printf("n user task n");
while(1)
{
DS18B20_Get_Temp();
printf("%\n%f",DS18B20_Get_Temp(),"%\n");
//DS18B20_Presence();
//printf("%\n%d",DS18B20_Presence(),"%\n");
tls_os_time_delay(1000000);
}
CreateDemoTask();
//Óû§×Ô¼ºµÄtask
}
这是所有写的程序,但是在串口上打印出来的只有一堆不正常的数据
代码有什么问题吗?有没有大佬帮我改一下程序?
For microsecond (no more than 1000) delays I use code like this