当前位置:网站首页>STM32扩展板 温度传感器和温湿度传感器的使用
STM32扩展板 温度传感器和温湿度传感器的使用
2022-07-01 04:40:00 【花开半朵ོ】
DS18B20

DS18B20是单线接口数字温度传感器,测量范围是-55℃~+125℃,-10℃~+85℃范围内精度是±0.5℃,测量分辨率为9~12位(复位值为12位,最大转换时间为750ms)。

DS18B20包括寄生电源电路、64位ROM和单线接口电路、暂存器、EEPROM、8位CRC生成器和温度传感器等。寄生电源电路可以实现外部电源供电和单线寄生供电,64位ROM中存放的48位序列号用于识别同一单线上连接的多个DS18B20,以实现多点测温。
64位(激)光刻只读存储器
每只DS18B20都有一个唯一存储在ROM中的64位编码。最前面8位是单线系列编码:28H.接着的48位是一个唯一的序号列。最后8位是以上56位的CRC编码。CRC的详细解释见CRC发生器节。64位ROM和ROM操作控制器允许DS18B20作为单总线器件并按照详述单总线系统节的单总线协议工作。


温度传感器时序(操作步骤) 还请查看手册。。
中文手册
链接:https://pan.baidu.com/s/1AJiUh0PfLi-EYOQFXrFkzA?pwd=8elx
提取码:8elx 代码
ds18b20_hal.c
#include "ds18b20_hal.h"
#define delay_us(X) delay((X)*80/5)
void delay(unsigned int n)
{
while(n--);
}
void ds18b20_init_x(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = OW_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH ;
GPIO_InitStruct.Pull = GPIO_PULLUP ;
HAL_GPIO_Init(OW_PIN_PORT, &GPIO_InitStruct);
}
//
void mode_input1(void )
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
GPIO_InitStruct.Pin = OW_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(OW_PIN_PORT, &GPIO_InitStruct);
}
void mode_output1(void )
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = OW_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pull = GPIO_NOPULL ;
HAL_GPIO_Init(OW_PIN_PORT, &GPIO_InitStruct);
}
//
uint8_t ow_reset(void)
{
uint8_t err;
OW_DIR_OUT(); // pull OW-Pin low for 480us
OW_OUT_LOW(); // disable internal pull-up (maybe on from parasite)
delay_us(400); //about 480us
// set Pin as input - wait for clients to pull low
OW_DIR_IN(); // input
delay_us(66);
err = OW_GET_IN(); // no presence detect
// nobody pulled to low, still high
// after a delay the clients should release the line
// and input-pin gets back to high due to pull-up-resistor
delay_us(480 - 66);
if( OW_GET_IN() == 0 ) // short circuit
err = 1;
return err;
}
uint8_t ow_bit_io( uint8_t b )
{
OW_DIR_OUT(); // drive bus low
OW_OUT_LOW();
delay_us(1); // Recovery-Time wuffwuff was 1
if ( b ) OW_DIR_IN(); // if bit is 1 set bus high (by ext. pull-up)
#define OW_CONF_DELAYOFFSET 5
delay_us(15 - 1 - OW_CONF_DELAYOFFSET);
if( OW_GET_IN() == 0 ) b = 0; // sample at end of read-timeslot
delay_us(60 - 15);
OW_DIR_IN();
return b;
}
uint8_t ow_byte_wr( uint8_t b )
{
uint8_t i = 8, j;
do
{
j = ow_bit_io( b & 1 );
b >>= 1;
if( j ) b |= 0x80;
}
while( --i );
return b;
}
//
uint8_t ow_byte_rd( void )
{
return ow_byte_wr( 0xFF );
}
uint_least16_t ds18b20_read(void)
{
uint8_t low,high;
uint_least16_t temp;
ow_reset();
ow_byte_wr(OW_SKIP_ROM);
ow_byte_wr(DS18B20_CONVERT);
delay(750000);
ow_reset();
ow_byte_wr(OW_SKIP_ROM);
ow_byte_wr(DS18B20_READ);
low = ow_byte_rd();
high = ow_byte_rd();
temp = (high << 8 | low) & 0x07ff;
return temp;
}
ds18b20_hal.h
#ifndef __DS18B20_HAL_H
#define __DS18B20_HAL_H
#include "stm32g4xx_hal.h"
#define OW_PIN_PORT GPIOA
#define OW_PIN GPIO_PIN_6
#define OW_DIR_OUT() mode_output1()
#define OW_DIR_IN() mode_input1()
#define OW_OUT_LOW() (HAL_GPIO_WritePin(OW_PIN_PORT, OW_PIN, GPIO_PIN_RESET))
#define OW_GET_IN() (HAL_GPIO_ReadPin(OW_PIN_PORT, OW_PIN))
#define OW_SKIP_ROM 0xCC
#define DS18B20_CONVERT 0x44
#define DS18B20_READ 0xBE
void ds18b20_init_x(void);
uint_least16_t ds18b20_read(void);
#endif
显示温度
__IO uint32_t lcd_uwTick = 0; //控制lcd_proc()的执行速度
uint8_t lcd_disp_string[20];
void lcd_proc()
{
if(uwTick - lcd_uwTick < 200)
return;
lcd_uwTick = uwTick;
sprintf((char *)lcd_disp_string,"temp:%.2f",ds18b20_read()/16.);
LCD_DisplayStringLine(Line2,lcd_disp_string);
}DHT11

接口说明

建议连接线长度短于20米时用5K上拉电阻,大于20米时根据实际情况使用合适的上拉电阻。


dht11手册
链接:https://pan.baidu.com/s/1y5T_SZC5eOOCBZOybGxxmQ?pwd=hnkp
提取码:hnkp *复位DHT11

//复位DHT11
void DHT11_Rst(void)
{
DHT11_PIN_OUT(); //设置为输出
DHT11_PIN_OUT_L; //低电平
HAL_Delay(20); //拉低至少18ms
DHT11_PIN_OUT_H; //高电平
Delay_us(60); //主机拉高20~40us
}*发出开始信号和回应

//等待DHT11的回应
//返回1:未检测到DHT11的存在
//返回0:存在
uint8_t DHT11_Check(void)
{
uint8_t re = 0;
DHT11_PIN_INPUT(); //设置为输出
while (DHT11_PIN_IN && re < 100) //DHT11会拉低40~80us
{
re++;
Delay_us(1);
};
if(re >= 100)return 1;
else re = 0;
while (!DHT11_PIN_IN && re < 100) //DHT11拉低后会再次拉高40~80us
{
re++;
Delay_us(1);
};
if(re >= 100)return 1;
return 0;
}* 读0和读1

//从DHT11读取一个位
uint8_t DHT11_Read_Bit(void)
{
uint8_t re = 0;
while(DHT11_PIN_IN && re < 110) //等待变为低电平
{
re++;
Delay_us(1);
}
re = 0;
while(!DHT11_PIN_IN && re < 110) //等待变高电平
{
re++;
Delay_us(1);
}
Delay_us(80);//等待40us
if(DHT11_PIN_IN)return 1;
else return 0;
}
//从DHT11读取一个字节
uint8_t DHT11_Read_Byte(void)
{
uint8_t i, dat;
dat = 0;
for (i = 0; i < 8; i++)
{
dat <<= 1;
dat |= DHT11_Read_Bit();
}
return dat;
}读取数据
//从DHT11读取一次数据
uint8_t DHT11_Read_Data(uint8_t *temp, uint8_t *humi)
{
uint8_t buf[5];
uint8_t i;
DHT11_Rst();
if(DHT11_Check() == 0)
{
for(i = 0; i < 5; i++)
{
buf[i] = DHT11_Read_Byte();
}
if((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4])
{
*humi = buf[0];
*temp = buf[2];
}
}
else return 1;
return 0;
}实现代码
dht11.c
#include "dht11.h"
#define Delay_us(X) delay((X)*80/5)
void delay(unsigned int n)
{
while(n--);
}
void DHT11_PIN_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/**/
GPIO_InitStruct.Pin = DHT11_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(DHT11_PIN_PORT, &GPIO_InitStruct);
}
void DHT11_PIN_INPUT(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
DHT11_PIN_CLOCK;
/**/
GPIO_InitStruct.Pin = DHT11_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(DHT11_PIN_PORT, &GPIO_InitStruct);
}
//复位DHT11
void DHT11_Rst(void)
{
DHT11_PIN_OUT(); //设置为输出
DHT11_PIN_OUT_L; //低电平
HAL_Delay(20); //拉低至少18ms
DHT11_PIN_OUT_H; //高电平
Delay_us(60); //主机拉高20~40us
}
//等待DHT11的回应
//返回1:未检测到DHT11的存在
//返回0:存在
uint8_t DHT11_Check(void)
{
uint8_t re = 0;
DHT11_PIN_INPUT(); //设置为输出
while (DHT11_PIN_IN && re < 100) //DHT11会拉低40~80us
{
re++;
Delay_us(1);
};
if(re >= 100)return 1;
else re = 0;
while (!DHT11_PIN_IN && re < 100) //DHT11拉低后会再次拉高40~80us
{
re++;
Delay_us(1);
};
if(re >= 100)return 1;
return 0;
}
//从DHT11读取一个位
uint8_t DHT11_Read_Bit(void)
{
uint8_t re = 0;
while(DHT11_PIN_IN && re < 110) //等待变为低电平
{
re++;
Delay_us(1);
}
re = 0;
while(!DHT11_PIN_IN && re < 110) //等待变高电平
{
re++;
Delay_us(1);
}
Delay_us(80);//等待40us
if(DHT11_PIN_IN)return 1;
else return 0;
}
//从DHT11读取一个字节
uint8_t DHT11_Read_Byte(void)
{
uint8_t i, dat;
dat = 0;
for (i = 0; i < 8; i++)
{
dat <<= 1;
dat |= DHT11_Read_Bit();
}
return dat;
}
//从DHT11读取一次数据
uint8_t DHT11_Read_Data(uint8_t *temp, uint8_t *humi)
{
uint8_t buf[5];
uint8_t i;
DHT11_Rst();
if(DHT11_Check() == 0)
{
for(i = 0; i < 5; i++)
{
buf[i] = DHT11_Read_Byte();
}
if((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4])
{
*humi = buf[0];
*temp = buf[2];
}
}
else return 1;
return 0;
}
//初始化DHT11的IO口,同时检测DHT11的存在
uint8_t DHT11_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
DHT11_PIN_CLOCK;;
GPIO_InitStruct.Pin = DHT11_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(DHT11_PIN_PORT, &GPIO_InitStruct);
DHT11_Rst();
return DHT11_Check();
}
dht11.h
#ifndef __DHT11_H
#define __DHT11_H
#include "main.h"
#define DHT11_PIN_PORT GPIOA
#define DHT11_PIN GPIO_PIN_7
#define DHT11_PIN_CLOCK __HAL_RCC_GPIOA_CLK_ENABLE()
#define DHT11_PIN_OUT_H HAL_GPIO_WritePin(DHT11_PIN_PORT, DHT11_PIN, GPIO_PIN_SET)
#define DHT11_PIN_OUT_L HAL_GPIO_WritePin(DHT11_PIN_PORT, DHT11_PIN, GPIO_PIN_RESET)
#define DHT11_PIN_IN HAL_GPIO_ReadPin(DHT11_PIN_PORT, DHT11_PIN)
uint8_t DHT11_Init(void);//初始化DHT11
uint8_t DHT11_Read_Data(uint8_t *temp, uint8_t *humi); //读取温湿度
uint8_t DHT11_Read_Byte(void);//读出一个字节
uint8_t DHT11_Read_Bit(void);//读出一个位
uint8_t DHT11_Check(void);//检测是否存在DHT11
void DHT11_Rst(void);//复位DHT11
#endif
显示温度湿度
void Lcd_Proc()
{
if(uwTick - lcd_uwTick < 200)
return;
lcd_uwTick = uwTick;
DHT11_Read_Data(&temp,&damp);
sprintf((char *)lcd_disp_string,"temp: %d damp: %d",(uint32_t)temp,(uint32_t)damp);
HAL_Delay(200);
LCD_DisplayStringLine(Line3,lcd_disp_string);
}边栏推荐
- Rule method: number of effective triangles
- Research on medical knowledge atlas question answering system (I)
- I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
- Summary of testing experience - Testing Theory
- PgSQL failed to start after installation
- TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 桥接器与交换机 / 3.4.2 多属性注册协议(Multiple Registration Protocol (MRP))
- Simple implementation of slf4j
- Codeforces Round #721 (Div. 2)B1. Palindrome Game (easy version)B2. Palindrome game (hard version)
- selenium打开chrome浏览器时弹出设置页面:Mircrosoft Defender 防病毒要重置您的设置
- Openresty rewrites the location of 302
猜你喜欢

(12) Somersault cloud case (navigation bar highlights follow)

How to do the performance pressure test of "Health Code"

2022 gas examination question bank and online simulation examination

RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead

About the transmission pipeline of stage in spark

CF1638E. Colorful operations Kodori tree + differential tree array
![AssertionError assert I.ndim == 4 and I.shape[1] == 3](/img/b1/0109bb0f893eb4c8915df36c100907.png)
AssertionError assert I.ndim == 4 and I.shape[1] == 3

Pytorch(四) —— 可视化工具 Visdom

2022年化工自动化控制仪表操作证考试题库及答案

2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
随机推荐
Applications and features of VR online exhibition
LeetCode_28(实现 strStr())
Learn Chapter 20 of vue3 (keep alive cache component)
VIM easy to use tutorial
Openresty rewrites the location of 302
LeetCode_53(最大子数组和)
2022年上海市安全员C证考试题模拟考试题库及答案
Codeforces Round #721 (Div. 2)B1. Palindrome Game (easy version)B2. Palindrome game (hard version)
Question bank and answers for chemical automation control instrument operation certificate examination in 2022
C language games (I) -- guessing games
Task04 mathematical statistics
JS image path conversion Base64 format
Summary of testing experience - Testing Theory
测量三相永磁同步电机的交轴直轴电感
Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions
LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机
What is uid? What is auth? What is a verifier?
Selenium opens the Chrome browser and the settings page pops up: Microsoft defender antivirus to reset your settings
2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
神经网络-非线性激活