OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2525|回复: 10

DS18B20的应答有问题

[复制链接]

29

主题

135

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
329
金钱
329
注册时间
2018-10-19
在线时间
28 小时
发表于 2018-10-31 15:16:15 | 显示全部楼层 |阅读模式
25金钱
我将F429的DS18B20的HAL库代码移植到了L432KC的板子的板子上。
但是应答的时候,DS18B20出现了问题:DS18B20收到REST命令后,确实应答了有120us,但是没有完全拉低DQ总线,拉低了一半,然后我的L432KC就没办法识别这个应答。
我不知道该怎么解决他
我的代码应该没有问题,但我还是贴出来了。我觉得是硬件哪里出了问题,但解决不了。

DQ信号图

DQ信号图

硬件连接

硬件连接

main.c:
/**  ******************************************************************************  * @file    Templates/Src/main.c   * @Author  MCD Application Team  * @brief   Main program body  ******************************************************************************  * @attention  *  * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>  *  * Redistribution and use in source and binary forms, with or without modification,  * are permitted provided that the following conditions are met:  *   1. Redistributions of source code must retain the above copyright notice,  *      this list of conditions and the following disclaimer.  *   2. Redistributions in binary form must reproduce the above copyright notice,  *      this list of conditions and the following disclaimer in the documentation  *      and/or other materials provided with the distribution.  *   3. Neither the name of STMicroelectronics nor the names of its contributors  *      may be used to endorse or promote products derived from this software  *      without specific prior written permission.  *  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  *  ******************************************************************************  *//* Includes ------------------------------------------------------------------*/#include "main.h"#include "delay.h"#include "ds18b20.h"#include "led.h"/** @addtogroup STM32L4xx_HAL_Examples  * @{  *//** @addtogroup Templates  * @{  *//* Private typedef -----------------------------------------------------------*//* Private define ------------------------------------------------------------*//* Private macro -------------------------------------------------------------*//* Private variables ---------------------------------------------------------*//* Private function prototypes -----------------------------------------------*/static void SystemClock_Config(void);/* Private functions ---------------------------------------------------------*//**  * @brief  Main program  * @param  None  * @retval None  */int main(void){    uint8_t t=0;    short i=1;    short X;      /* STM32L4xx HAL library initialization:       - Configure the Flash prefetch, Flash preread and Buffer caches       - Systick timer is configured by default as source of time base, but user              can eventually implement his proper time base source (a general purpose              timer for example or other time source), keeping in mind that Time base              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and              handled in milliseconds basis.       - Low Level Initialization     */  HAL_Init();//HAL library initialization    LED_Init();//LED initialization  /* Configure the System clock to have a frequency of 80 MHz */  SystemClock_Config();    delay_init(80);//delay function initialization    while(DS18B20_Init())    //DS18B20 initilization    {        delay_ms(400);//give DS18B20 a chance to response        //only break when DS18B20 exists    }  /* Add your application code here     */  /* Infinite loop */    while(1)    {        if(t%10==0)//read data per 100ms        {            X=DS18B20_Get_Temp();        }        delay_ms(10);        t++;        if(t==20)        {            t=0;            LED(i);            i=!i;        }    }}/**  * @brief  System Clock Configuration  *         The system Clock is configured as follow :   *            System Clock source            = PLL (MSI)  *            SYSCLK(Hz)                     = 80000000  *            HCLK(Hz)                       = 80000000  *            AHB Prescaler                  = 1  *            APB1 Prescaler                 = 1  *            APB2 Prescaler                 = 1  *            MSI Frequency(Hz)              = 4000000  *            PLL_M                          = 1  *            PLL_N                          = 40  *            PLL_R                          = 2  *            PLL_P                          = 7  *            PLL_Q                          = 4  *            Flash Latency(WS)              = 4  * @param  None  * @retval None  */static void SystemClock_Config(void){  RCC_ClkInitTypeDef RCC_ClkInitStruct;  RCC_OscInitTypeDef RCC_OscInitStruct;  /* MSI is enabled after System reset, activate PLL with MSI as source */  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;  RCC_OscInitStruct.MSIState = RCC_MSI_ON;  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;  RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;  RCC_OscInitStruct.PLL.PLLM = 1;  RCC_OscInitStruct.PLL.PLLN = 40;  RCC_OscInitStruct.PLL.PLLR = 2;  RCC_OscInitStruct.PLL.PLLP = 7;  RCC_OscInitStruct.PLL.PLLQ = 4;  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)  {    /* Initialization Error */    while(1);  }    /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2      clocks dividers */  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;    if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)  {    /* Initialization Error */    while(1);  }}#ifdef  USE_FULL_ASSERT/**  * @brief  Reports the name of the source file and the source line number  *         where the assert_param error has occurred.  * @param  file: pointer to the source file name  * @param  line: assert_param error line source number  * @retval None  */void assert_failed(char *file, uint32_t line){   /* User can add his own implementation to report the file name and line number,     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */  /* Infinite loop */  while (1)  {  }}#endif/**  * @}  */ /**  * @}  */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

ds18b20.c:#include "ds18b20.h"#include "delay.h"#include "led.h"//复位DS18B20void DS18B20_Rst(void){    DS18B20_IO_OUT();//设置为输出    DS18B20_DQ_OUT(0);//拉低DQ;    delay_us(750);//pulldown 750 us    DS18B20_DQ_OUT(1);//pullup    delay_us(15);//pulldown 15us}//wait for answer from DS18B20//return 1: DS18B20 doesnt exist//return 0: DS18B20 existsuint8_t DS18B20_Check(void){    uint8_t retry=0;    DS18B20_IO_IN();//set as input    while (DS18B20_DQ_IN&&retry<200)    {        retry++;        delay_us(1);    }    if(retry>=200)return 1;    else retry=0;    while(!DS18B20_DQ_IN&&retry<240)    {        retry++;        delay_us(1);    }    if(retry>=240) return 1;    return 0;}//read a bit from DS18B20//return value: 1/0uint8_t DS18B20_Read_Bit(void){    uint8_t data;    DS18B20_IO_OUT();//set as output    DS18B20_DQ_OUT(0);    delay_us(2);    DS18B20_DQ_OUT(1);    DS18B20_IO_IN();//set as input    delay_us(12);    if(DS18B20_DQ_IN)data=1;    else data=0;    delay_us(50);    return data;}//read a byte from DS18B20//return value: read datauint8_t DS18B20_Read_Byte(void){    uint8_t i,j,dat;    dat=0;    for(i=1;i<=8;i++)    {        j=DS18B20_Read_Bit();        dat=(j<<7|(dat>>1));    }    return dat;}//write a byte to DS18B20//dat: data to writevoid DS18B20_Write_Byte(uint8_t dat){    uint8_t j;    uint8_t testb;    DS18B20_IO_OUT();//set as output    for(j=1;j<=8;j++)    {        testb=dat&0x01;        dat=dat>>1;        //write 1        if(testb)        {            DS18B20_DQ_OUT(0);            delay_us(2);            DS18B20_DQ_OUT(1);            delay_us(60);        }        //wtire 0        else         {            DS18B20_DQ_OUT(0);            delay_us(60);            DS18B20_DQ_OUT(1);            delay_us(2);        }    }}//start to convert temperaturevoid DS18B20_Start(void){    DS18B20_Rst();    DS18B20_Check();    DS18B20_Write_Byte(0xcc);//skip rom    DS18B20_Write_Byte(0x44);//convert}//initialise DS18B20's IO and detect DS18B20's existence//return 1: DS18B20 doesnt exist//return 0: DS18B20 existsuint8_t DS18B20_Init(void){    GPIO_InitTypeDef GPIO_Initure;    __HAL_RCC_GPIOB_CLK_ENABLE();//open GPIOB clock        GPIO_Initure.Pin=GPIO_PIN_0;//PB0    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;//push-pull output    GPIO_Initure.Pull=GPIO_PULLUP;//pullup    GPIO_Initure.Speed=GPIO_SPEED_FREQ_HIGH;//high speed    HAL_GPIO_Init(GPIOB,&GPIO_Initure);//initialise         DS18B20_Rst();    return DS18B20_Check();    }//get temperature from DS18B20//accuracy: 0.1C//return value: temperature value(-55.0~125.0)short DS18B20_Get_Temp(void){    uint8_t temp;    uint8_t TL,TH;    short tem;    //start to convert    DS18B20_Start();    DS18B20_Rst();    DS18B20_Check();    DS18B20_Write_Byte(0xcc);//skip rom    DS18B20_Write_Byte(0xbe);//convert    TL=DS18B20_Read_Byte();//LSB    TH=DS18B20_Read_Byte();//MSB    if(TH>7)    {        TH=~TH;        TL=~TL;        temp=0;//negative temperature    }    else temp=1;//posstive temperature    tem=TH;//get upper byte    tem<<=8;    tem+=TL;//get lower byte    tem=(double)tem*0.0625;//convert the number    LED(0);//light off    if(temp)return tem;//return temperature difference    else return -tem;}






























最佳答案

查看完整内容[请看2#楼]

我知道错误在哪里了,原因在与我并没有释放总线,因为我用的是推挽输出,上拉。当我输出为1时,为强1,DS18B20而且也接了一个上拉电阻与VCC3.3,这样当我输出推挽输出高时,DS18B20拉低总线接地,就只能有一半的拉低了。
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

29

主题

135

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
329
金钱
329
注册时间
2018-10-19
在线时间
28 小时
 楼主| 发表于 2018-10-31 15:16:16 | 显示全部楼层
我知道错误在哪里了,原因在与我并没有释放总线,因为我用的是推挽输出,上拉。当我输出为1时,为强1,DS18B20而且也接了一个上拉电阻与VCC3.3,这样当我输出推挽输出高时,DS18B20拉低总线接地,就只能有一半的拉低了。
回复

使用道具 举报

29

主题

135

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
329
金钱
329
注册时间
2018-10-19
在线时间
28 小时
 楼主| 发表于 2018-10-31 15:16:58 | 显示全部楼层
码乱了....我再发一遍
回复

使用道具 举报

29

主题

135

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
329
金钱
329
注册时间
2018-10-19
在线时间
28 小时
 楼主| 发表于 2018-10-31 15:17:34 | 显示全部楼层
/**
  ******************************************************************************
  * @file    Templates/Src/main.c
  * @Author  MCD Application Team
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "delay.h"
#include "ds18b20.h"
#include "led.h"

/** @addtogroup STM32L4xx_HAL_Examples
  * @{
  */

/** @addtogroup Templates
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void SystemClock_Config(void);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
        uint8_t t=0;
        short i=1;
        short X;
       
  /* STM32L4xx HAL library initialization:
       - Configure the Flash prefetch, Flash preread and Buffer caches
       - Systick timer is configured by default as source of time base, but user
             can eventually implement his proper time base source (a general purpose
             timer for example or other time source), keeping in mind that Time base
             duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
             handled in milliseconds basis.
       - Low Level Initialization
     */
  HAL_Init();//HAL library initialization
        LED_Init();//LED initialization
  /* Configure the System clock to have a frequency of 80 MHz */
  SystemClock_Config();
        delay_init(80);//delay function initialization
        while(DS18B20_Init())    //DS18B20 initilization
        {
                delay_ms(400);//give DS18B20 a chance to response
                //only break when DS18B20 exists
        }
  /* Add your application code here
     */

  /* Infinite loop */
        while(1)
        {
                if(t%10==0)//read data per 100ms
                {
                        X=DS18B20_Get_Temp();
                }
                delay_ms(10);
                t++;
                if(t==20)
                {
                        t=0;
                        LED(i);
                        i=!i;
                }
        }

}

/**
  * @brief  System Clock Configuration
  *         The system Clock is configured as follow :
  *            System Clock source            = PLL (MSI)
  *            SYSCLK(Hz)                     = 80000000
  *            HCLK(Hz)                       = 80000000
  *            AHB Prescaler                  = 1
  *            APB1 Prescaler                 = 1
  *            APB2 Prescaler                 = 1
  *            MSI Frequency(Hz)              = 4000000
  *            PLL_M                          = 1
  *            PLL_N                          = 40
  *            PLL_R                          = 2
  *            PLL_P                          = 7
  *            PLL_Q                          = 4
  *            Flash Latency(WS)              = 4
  * @param  None
  * @retval None
  */
static void SystemClock_Config(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_OscInitTypeDef RCC_OscInitStruct;

  /* MSI is enabled after System reset, activate PLL with MSI as source */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 40;
  RCC_OscInitStruct.PLL.PLLR = 2;
  RCC_OscInitStruct.PLL.PLLP = 7;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    /* Initialization Error */
    while(1);
  }
  
  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
     clocks dividers */
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;  
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;  
  if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    /* Initialization Error */
    while(1);
  }
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(char *file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
回复

使用道具 举报

29

主题

135

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
329
金钱
329
注册时间
2018-10-19
在线时间
28 小时
 楼主| 发表于 2018-10-31 15:18:11 | 显示全部楼层
#include "ds18b20.h"
#include "delay.h"
#include "led.h"

//复位DS18B20
void DS18B20_Rst(void)
{
        DS18B20_IO_OUT();//设置为输出
        DS18B20_DQ_OUT(0);//拉低DQ;
        delay_us(750);//pulldown 750 us
        DS18B20_DQ_OUT(1);//pullup
        delay_us(15);//pulldown 15us
}

//wait for answer from DS18B20
//return 1: DS18B20 doesnt exist
//return 0: DS18B20 exists
uint8_t DS18B20_Check(void)
{
        uint8_t retry=0;
        DS18B20_IO_IN();//set as input
        while (DS18B20_DQ_IN&&retry<200)
        {
                retry++;
                delay_us(1);
        }
        if(retry>=200)return 1;
        else retry=0;
        while(!DS18B20_DQ_IN&&retry<240)
        {
                retry++;
                delay_us(1);
        }
        if(retry>=240) return 1;
        return 0;
}

//read a bit from DS18B20
//return value: 1/0
uint8_t DS18B20_Read_Bit(void)
{
        uint8_t data;
        DS18B20_IO_OUT();//set as output
        DS18B20_DQ_OUT(0);
        delay_us(2);
        DS18B20_DQ_OUT(1);
        DS18B20_IO_IN();//set as input
        delay_us(12);
        if(DS18B20_DQ_IN)data=1;
        else data=0;
        delay_us(50);
        return data;
}

//read a byte from DS18B20
//return value: read data
uint8_t DS18B20_Read_Byte(void)
{
        uint8_t i,j,dat;
        dat=0;
        for(i=1;i<=8;i++)
        {
                j=DS18B20_Read_Bit();
                dat=(j<<7|(dat>>1));
        }
        return dat;
}

//write a byte to DS18B20
//dat: data to write
void DS18B20_Write_Byte(uint8_t dat)
{
        uint8_t j;
        uint8_t testb;
        DS18B20_IO_OUT();//set as output
        for(j=1;j<=8;j++)
        {
                testb=dat&0x01;
                dat=dat>>1;
                //write 1
                if(testb)
                {
                        DS18B20_DQ_OUT(0);
                        delay_us(2);
                        DS18B20_DQ_OUT(1);
                        delay_us(60);
                }
                //wtire 0
                else
                {
                        DS18B20_DQ_OUT(0);
                        delay_us(60);
                        DS18B20_DQ_OUT(1);
                        delay_us(2);
                }
        }
}

//start to convert temperature
void DS18B20_Start(void)
{
        DS18B20_Rst();
        DS18B20_Check();
        DS18B20_Write_Byte(0xcc);//skip rom
        DS18B20_Write_Byte(0x44);//convert
}

//initialise DS18B20's IO and detect DS18B20's existence
//return 1: DS18B20 doesnt exist
//return 0: DS18B20 exists
uint8_t DS18B20_Init(void)
{
        GPIO_InitTypeDef GPIO_Initure;
        __HAL_RCC_GPIOB_CLK_ENABLE();//open GPIOB clock
       
        GPIO_Initure.Pin=GPIO_PIN_0;//PB0
        GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;//push-pull output
        GPIO_Initure.Pull=GPIO_PULLUP;//pullup
        GPIO_Initure.Speed=GPIO_SPEED_FREQ_HIGH;//high speed
        HAL_GPIO_Init(GPIOB,&GPIO_Initure);//initialise
       
        DS18B20_Rst();
        return DS18B20_Check();
       
}

//get temperature from DS18B20
//accuracy: 0.1C
//return value: temperature value(-55.0~125.0)
short DS18B20_Get_Temp(void)
{
        uint8_t temp;
        uint8_t TL,TH;
        short tem;
        //start to convert
        DS18B20_Start();
        DS18B20_Rst();
        DS18B20_Check();
        DS18B20_Write_Byte(0xcc);//skip rom
        DS18B20_Write_Byte(0xbe);//convert
        TL=DS18B20_Read_Byte();//LSB
        TH=DS18B20_Read_Byte();//MSB

        if(TH>7)
        {
                TH=~TH;
                TL=~TL;
                temp=0;//negative temperature
        }
        else temp=1;//posstive temperature
        tem=TH;//get upper byte
        tem<<=8;
        tem+=TL;//get lower byte
        tem=(double)tem*0.0625;//convert the number
        LED(0);//light off
        if(temp)return tem;//return temperature difference
        else return -tem;
}

回复

使用道具 举报

29

主题

135

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
329
金钱
329
注册时间
2018-10-19
在线时间
28 小时
 楼主| 发表于 2018-10-31 15:18:38 | 显示全部楼层
#ifndef __DS18B20_H
#define __DS18B20_H
#include "stm32l4xx.h"

//修改A组PIN12的MODE寄存器
//IO方向设置
#define DS18B20_IO_IN() {GPIOB->MODER&=~(GPIO_MODER_MODER0<<(12*2));GPIOB->MODER|=GPIO_MODE_INPUT<<(12*2);}//输入模式
#define DS18B20_IO_OUT() {GPIOB->MODER&=~(GPIO_MODER_MODER0<<(12*2));GPIOB->MODER|=GPIO_MODE_OUTPUT_PP<<(12*2);}//输出模式

//IO操作函数
#define DS18B20_DQ_OUT(n) (n?HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET):HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET))//数据端口PB0
#define DS18B20_DQ_IN HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)//数据端口PB0

uint8_t DS18B20_Init(void);//初始化DS18B20
short DS18B20_Get_Temp(void);//获取温度
void DS18B20_Start(void);//开始温度转换
void DS18B20_Write_Byte(uint8_t dat);//写入一个字节
uint8_t DS18B20_Read_Byte(void);//读出一个字节
uint8_t DS18B20_Read_Bit(void);//读出一个位
uint8_t DS18B20_Check(void);//检测是否存在DS18B20
void DS18B20_Rst(void);//复位DS18B20
#endif
回复

使用道具 举报

29

主题

135

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
329
金钱
329
注册时间
2018-10-19
在线时间
28 小时
 楼主| 发表于 2018-10-31 15:23:43 | 显示全部楼层
在线等,没办法做下去....
回复

使用道具 举报

29

主题

135

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
329
金钱
329
注册时间
2018-10-19
在线时间
28 小时
 楼主| 发表于 2018-10-31 15:30:56 | 显示全部楼层
DS18B20模块是正常的,我用阿波罗测试过了
回复

使用道具 举报

29

主题

135

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
329
金钱
329
注册时间
2018-10-19
在线时间
28 小时
 楼主| 发表于 2018-10-31 17:37:49 | 显示全部楼层
1.png
回复

使用道具 举报

29

主题

135

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
329
金钱
329
注册时间
2018-10-19
在线时间
28 小时
 楼主| 发表于 2018-10-31 17:41:58 | 显示全部楼层
解决的方法是改为开漏输出,当输出为0时,不影响I0口的输出,我不知道原子哥的是怎么实现的,原子哥的是推挽输出,且上拉,DB18B20仍然正常拉低总线,推挽的输出1,并没有影响到DS18B20的地影响,这一点还是没有解决。
回复

使用道具 举报

29

主题

135

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
329
金钱
329
注册时间
2018-10-19
在线时间
28 小时
 楼主| 发表于 2018-10-31 17:42:26 | 显示全部楼层
自问自答,无奈⊙﹏⊙.....
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-8 14:24

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表