DS18B20的应答有问题

2019-07-20 05:23发布

我将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 ", 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;}






























友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。