首先,為了便於通訊,我將一個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