我想写一个外接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

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览