完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
47个回答
|
|
公司电脑 不太好弄 我有一个 STM32F030 读取DS18B20的程序 上传不上去
《DS18B20.c》 #include "stm32f0xx_hal.h" #include "tim.h" #include "DS18B20.h" /* 微秒级延时用函数 */ void Delay_us(uint16_t nus) { __HAL_TIM_SetCounter(&htim17, 0); __HAL_TIM_ENABLE(&htim17); while(__HAL_TIM_GetCounter(&htim17) < (48 * nus)); /* Disable the Peripheral */ __HAL_TIM_DISABLE(&htim17); } /* 复位DS18B20 */ void DS18B20_Rst() { DS18B20_IO_OUT_SET(); // HAL_GPIO_WritePin(DS18B20_GPIO_Port, DS18B20_Pin, GPIO_PIN_RESET); //拉低DQ Delay_us(500); //拉低500us HAL_GPIO_WritePin(DS18B20_GPIO_Port, DS18B20_Pin, GPIO_PIN_SET); //DQ = 1 Delay_us(30); } /* 等待DS18B20的响应 返回1:未检测到DS18B20的存在 返回0:存在 */ uint8_t DS18B20_Check(void) { uint8_t retry = 0; DS18B20_IO_IN_SET(); while(HAL_GPIO_ReadPin(DS18B20_GPIO_Port, DS18B20_Pin) && (retry < 200)) { retry++; Delay_us(1); } if(retry >= 200) return 1; else retry = 0; while(!HAL_GPIO_ReadPin(DS18B20_GPIO_Port, DS18B20_Pin) && (retry < 240)) { retry++; Delay_us(1); } if(retry >= 240) return 1; return 0; } /* 从DS18B20读取一个位 返回值:1/0 */ uint8_t DS18B20_Read_Bit(void) { uint8_t data = 0; DS18B20_IO_OUT_SET();//推挽输出设置 HAL_GPIO_WritePin(DS18B20_GPIO_Port, DS18B20_Pin, GPIO_PIN_RESET); //DQ = 0 Delay_us(2); HAL_GPIO_WritePin(DS18B20_GPIO_Port, DS18B20_Pin, GPIO_PIN_SET); //DQ = 1 DS18B20_IO_IN_SET(); //输入上拉 Delay_us(12); if(HAL_GPIO_ReadPin(DS18B20_GPIO_Port, DS18B20_Pin)) data = 1; else data = 0; Delay_us(50); return data; } /* 从DS18B20读取一个字节 返回值:读取到的数据 */ uint8_t DS18B20_Read_Byte(void) //read one byte { uint8_t i,j,dat; dat = 0; for(i = 1;i <= 8;i++) { j = DS18B20_Read_Bit(); dat = (j << 7) | (dat >> 1); } return dat; } /* 写一个字节到DS18B20 dat:要写入的字节 */ void DS18B20_Write_Byte(uint8_t dat) { uint8_t j; uint8_t testb; DS18B20_IO_OUT_SET();//推挽输出设置 for(j = 1;j <= 8;j++) { testb = dat & 0x01; dat = dat >> 1; if(testb) { HAL_GPIO_WritePin(DS18B20_GPIO_Port, DS18B20_Pin, GPIO_PIN_RESET); //DQ = 0 write 1 Delay_us(2); HAL_GPIO_WritePin(DS18B20_GPIO_Port, DS18B20_Pin, GPIO_PIN_SET); //DQ = 1 Delay_us(60); } else { HAL_GPIO_WritePin(DS18B20_GPIO_Port, DS18B20_Pin, GPIO_PIN_RESET); //DQ = 0 write 0 Delay_us(60); HAL_GPIO_WritePin(DS18B20_GPIO_Port, DS18B20_Pin, GPIO_PIN_SET); //DQ = 1 Delay_us(2); } } } /* 开始温度转换 */ void DS18B20_Start(void) //ds18b20 start convert { DS18B20_Rst(); DS18B20_Check(); DS18B20_Write_Byte(0xcc);//skip rom DS18B20_Write_Byte(0x44);//convert } /* 设置为9位精度 */ void DS18B20_Comfig2(void) // { DS18B20_Rst(); DS18B20_Check(); DS18B20_Write_Byte(0xcc);//skip rom DS18B20_Write_Byte(0x4e);//write } /* 初始化DS18B20的IO口DQ同时检测DS的存在 返回1:不存在 返回0:存在 */ //uint8_t DS18B20_init(void) //{ // ; //} /* 从DS18B20得到温度值 精度:0.5摄氏度 返回值:温度值 */ uint16_t DS18B20_Get_Temp(void) { uint8_t temp; uint8_t i = 0; uint8_t TL,TH; uint16_t tem; #ifdef CONFIG_DS18B20 uint8_t FD,FH,RG; #endif #ifdef CONFIG_DS18B20 DS18B20_Comfig2(); DS18B20_Write_Byte(100); DS18B20_Write_Byte(0); DS18B20_Write_Byte(0x1f); // DS18B20_Start(); #endif DS18B20_Rst(); i = DS18B20_Check(); if(i == 1) return 0xffee; DS18B20_Write_Byte(0xcc);//skip rom DS18B20_Write_Byte(0xbe);//read data TL = DS18B20_Read_Byte();//LSB TH = DS18B20_Read_Byte();//MSB #ifdef CONFIG_DS18B20 FD = DS18B20_Read_Byte(); FH = DS18B20_Read_Byte(); RG = DS18B20_Read_Byte(); printf("RG = %Xrn",RG); #endif if(TH > 7) { TH = ~TH; TL = ~TL; temp = 0; //温度为负 } else temp = 1; //温度为正 tem = TH; //获得高8位 tem <<= 8; tem += TL;//获得低8位 tem = (float)(tem >> 3) * 0.5 *100;//转换 if(temp) return tem; //返回温度值 else return -tem; } 《DS18B20.h》 #ifndef __DS18B20_H #define __DS18B20_H #define DS18B20_IO_OUT_SET() //{DS18B20_GPIO_Port->MODER &= 0xFFFFFFF3;GPIOF->MODER |= 1 << 2;GPIOF->PUPDR &= 0xFFFFFFF3;} //输出推挽 其余设置 默认 #define DS18B20_IO_IN_SET() //{DS18B20_GPIO_Port->MODER &= 0xFFFFFFF3;GPIOF->PUPDR &= 0xFFFFFFF3;GPIOF->PUPDR |= 1 << 2;} //设置输入上拉 //#define CONFIG_DS18B20 //设置DS18B20的 分辨率 /* 开始温度转换 */ void DS18B20_Start(void); //ds18b20 start convert /* 从DS18B20得到温度值 精度:0.5摄氏度 返回值:温度值 */ uint16_t DS18B20_Get_Temp(void); /* 微秒级延时用函数 */ void Delay_us(uint16_t nus); #endif 《gpio.c》 /** ****************************************************************************** * File Name : gpio.c * Description : This file provides code for the configuration * of all used GPIO pins. ****************************************************************************** * * COPYRIGHT(c) 2016 STMicroelectronics * * 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 "gpio.h" /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /*----------------------------------------------------------------------------*/ /* Configure GPIO */ /*----------------------------------------------------------------------------*/ /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /** Configure pins as * Analog * Input * Output * EVENT_OUT * EXTI */ void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; /* GPIO Ports Clock Enable */ __GPIOF_CLK_ENABLE(); __GPIOA_CLK_ENABLE(); __GPIOB_CLK_ENABLE(); /*Configure GPIO pins : PAPin PAPin PAPin PAPin PAPin */ GPIO_InitStruct.Pin = CD4053_Pin|C_S1_Pin|C_S2_Pin|C_S3_Pin |C_S4_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /*Configure GPIO pin : PA7 */ GPIO_InitStruct.Pin = GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /*Configure GPIO pin : PtPin */ GPIO_InitStruct.Pin = DS18B20_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; HAL_GPIO_Init(DS18B20_GPIO_Port, &GPIO_InitStruct); /* EXTI interrupt init*/ HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0); HAL_NVIC_EnableIRQ(EXTI4_15_IRQn); } /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /** * @} */ /** * @} */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 《tim.c》 /** ****************************************************************************** * File Name : TIM.c * Description : This file provides code for the configuration * of the TIM instances. ****************************************************************************** * * COPYRIGHT(c) 2016 STMicroelectronics * * 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 "tim.h" /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ TIM_HandleTypeDef htim14; TIM_HandleTypeDef htim16; TIM_HandleTypeDef htim17; /* TIM14 init function */ void MX_TIM14_Init(void) { htim14.Instance = TIM14; htim14.Init.Prescaler = 0; htim14.Init.CounterMode = TIM_COUNTERMODE_UP; htim14.Init.Period = 65535; htim14.Init.ClockDivision = TIM_CLOCKDIVISION_DIV2; HAL_TIM_Base_Init(&htim14); } /* TIM16 init function */ void MX_TIM16_Init(void) { htim16.Instance = TIM16; htim16.Init.Prescaler = TIM16_PSC; htim16.Init.CounterMode = TIM_COUNTERMODE_UP; htim16.Init.Period = 65535; htim16.Init.ClockDivision = TIM_CLOCKDIVISION_DIV2; htim16.Init.RepetitionCounter = 0; HAL_TIM_Base_Init(&htim16); } /* TIM17 init function */ void MX_TIM17_Init(void) { htim17.Instance = TIM17; htim17.Init.Prescaler = 0; htim17.Init.CounterMode = TIM_COUNTERMODE_UP; htim17.Init.Period = 65535; htim17.Init.ClockDivision = TIM_CLOCKDIVISION_DIV2; htim17.Init.RepetitionCounter = 0; HAL_TIM_Base_Init(&htim17); } void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { if(htim_base->Instance==TIM14) { /* USER CODE BEGIN TIM14_MspInit 0 */ /* USER CODE END TIM14_MspInit 0 */ /* Peripheral clock enable */ __TIM14_CLK_ENABLE(); /* USER CODE BEGIN TIM14_MspInit 1 */ /* USER CODE END TIM14_MspInit 1 */ } else if(htim_base->Instance==TIM16) { /* USER CODE BEGIN TIM16_MspInit 0 */ /* USER CODE END TIM16_MspInit 0 */ /* Peripheral clock enable */ __TIM16_CLK_ENABLE(); /* USER CODE BEGIN TIM16_MspInit 1 */ /* USER CODE END TIM16_MspInit 1 */ } else if(htim_base->Instance==TIM17) { /* USER CODE BEGIN TIM17_MspInit 0 */ /* USER CODE END TIM17_MspInit 0 */ /* Peripheral clock enable */ __TIM17_CLK_ENABLE(); /* USER CODE BEGIN TIM17_MspInit 1 */ /* USER CODE END TIM17_MspInit 1 */ } } void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base) { if(htim_base->Instance==TIM14) { /* USER CODE BEGIN TIM14_MspDeInit 0 */ /* USER CODE END TIM14_MspDeInit 0 */ /* Peripheral clock disable */ __TIM14_CLK_DISABLE(); /* USER CODE BEGIN TIM14_MspDeInit 1 */ /* USER CODE END TIM14_MspDeInit 1 */ } else if(htim_base->Instance==TIM16) { /* USER CODE BEGIN TIM16_MspDeInit 0 */ /* USER CODE END TIM16_MspDeInit 0 */ /* Peripheral clock disable */ __TIM16_CLK_DISABLE(); /* USER CODE BEGIN TIM16_MspDeInit 1 */ /* USER CODE END TIM16_MspDeInit 1 */ } else if(htim_base->Instance==TIM17) { /* USER CODE BEGIN TIM17_MspDeInit 0 */ /* USER CODE END TIM17_MspDeInit 0 */ /* Peripheral clock disable */ __TIM17_CLK_DISABLE(); /* USER CODE BEGIN TIM17_MspDeInit 1 */ /* USER CODE END TIM17_MspDeInit 1 */ } } /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /** * @} */ /** * @} */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
最佳答案
|
|
|
|
|
|
设置成开漏输出,外接上拉电阻,使用时不需要切换输入输出,试试
|
|
|
|
应该配置为输入浮空。
|
|
|
|
DS18B20数据端是否加了上拉电阻。
|
|
|
|
GPIO应设置为漏极开路
|
|
|
|
其实这种Onewire的,要注意时序问题
|
|
|
|
确定总线时间和手册给出的时间是否严格匹配,最好留有余量。。。
|
|
|
|
检查电路,DQ是否做了上拉
|
|
|
|
|
|
程序中。先拉低,然后将IO设成输入,用示波器看是否有复位脉冲。这样可以确保硬件没问题
|
|
|
|
|
|
|
|
STM32的DS18B20的测温
设置成开漏输出,外接上拉电阻, 没有说明芯片类型,如是F103直接设成开漏输出。 如是L152直接设成开漏输出。最好用寄操作存器保证时序。 |
|
|
|
有做上啦设置吗?
|
|
|
|
本帖最后由 wambob 于 2016-5-4 17:44 编辑
18b20是单总线器件,外部需要上拉,而且时序对延时有要求,我开始跳这个,也没调成,最后还是用别人的例子移植成功了。这是我的18b20例程,希望对你有用 http://www.stmcu.org/module/forum/thread-600041-1-1.html |
|
|
|
首先确定传感器是不是好的
|
|
|
|
|
|
|
|
楼主,关键就在初始化的480us,不同厂家的芯片这几百微秒都不尽相同,有的需要650us,有的要700us.你多试几次就OK了。
|
|
|
|
建议看一下看一下各端口的波形是否正确,是否是自己需要的延时,检查管脚功能是否设置是正确的,达到自己预定的效果
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
1665 浏览 1 评论
1399 浏览 1 评论
MCU友好过渡MPU,米尔基于STM32MP135开发板裸机开发应用笔记
678 浏览 0 评论
自定义系统时钟频率后,用库函数和微带两种方式点亮LED灯,为什么灯的闪烁频率不一致?
1886 浏览 2 评论
2149 浏览 2 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-5-6 21:03 , Processed in 0.698891 second(s), Total 93, Slave 74 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号