我想寫一個外接ds18b20測溫模塊的程序,但是在使用的過程中出現了很多問題,有沒有大佬解答一下的

發布於 2024-05-05 12:00:50

微信圖片_20240505115107.png
首先,為了便於通訊,我將一個tick的時間改為了10us,本來想改成1us,但是不知道什麼原因,在1us的情況下系統無法運行,有沒有大佬會用1u的遲延,教我一下,萬分感謝!!!

以下是程序:
鏈接:https://pan.baidu.com/s/1_UhMc4SPa_zoaDFhOQZByg?pwd=ap9a
提取碼:ap9a

include "wm_include.h"

include "wm_gpio.h"

include "tls_common.h"

include "wm_type_def.h"

include "wm_io.h"

include "assert.h"

include "FreeRTOSconfig.h"

define OutDQ0 tls_gpio_cfg(WM_IO_PB_05, WM_GPIO_DIR_OUTPUT,WM_GPIO_ATTR_PULLLOW);//定義PA0為DQ;

define OutDQ1 tls_gpio_cfg(WM_IO_PB_05, WM_GPIO_DIR_OUTPUT,WM_GPIO_ATTR_PULLHIGH);

define IpuDQ tls_gpio_cfg(WM_IO_PB_05, WM_GPIO_DIR_INPUT,WM_GPIO_ATTR_FLOATING);

define RedDQ tls_gpio_read(WM_IO_PB_05);

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);

}

if DEMO_CONSOLE

CreateDemoTask();

endif

//Óû§×Ô¼ºµÄtask
}

這是所有寫的程序,但是在串口上打印出來的只有一堆不正常的數據
微信圖片_20240505115939.png
代碼有什麼問題嗎?有沒有大佬幫我改一下程序?

查看更多

關注者
0
被浏覽
1.5k
abcd
abcd 認證專家 2024-05-06
我只是個路過的老年人~

tick最小改到1000,不能再小了。關於延時,如果延時時間大於tick值,可以用tls_os_time_delay(N * tick)來實現,如果想延時更小的時間,可以自己寫個for循環調用__NOP();接口來延時,具體循環多少次可以用個IOpin拉高拉低,用示波器或者邏輯分析儀來測量延時時間長短。

3 個回答
130****5949
130****5949 2024-05-05
這家夥很懶,什麼也沒寫!

1

AnatolSher
AnatolSher 認證專家 2024-05-07
Lover of the ocean, yachts and Arduino

Hello! I just checked the compatibility of our Arduino core with DHT series sensors. This works with the DHTStable library. This means that it can be ported to the W80X-SDK without much difficulty or look at the 1-wire interaction algorithm
image.png

撰寫答案

請登錄後再發布答案,點擊登錄

發布
問題

分享
好友

手機
浏覽

掃碼手機浏覽