当前位置:网站首页>Use of STM32 expansion board temperature sensor and temperature humidity sensor
Use of STM32 expansion board temperature sensor and temperature humidity sensor
2022-07-01 04:50:00 【Flowers bloom in half】
DS18B20

DS18B20 It is a digital temperature sensor with single wire interface , The measurement range is -55℃~+125℃,-10℃~+85℃ The accuracy within the range is ±0.5℃, The measurement resolution is 9~12 position ( The reset value is 12 position , The maximum conversion time is 750ms).

DS18B20 Including parasitic power circuit 、64 position ROM And single line interface circuit 、 Register 、EEPROM、8 position CRC Generator and temperature sensor, etc . The parasitic power supply circuit can realize external power supply and single line parasitic power supply ,64 position ROM Stored in 48 Bit serial number is used to identify multiple connected on the same single line DS18B20, To achieve multi-point temperature measurement .
64 position ( Thrill ) Lithographic read only memory
Each DS18B20 There is a unique storage in ROM Medium 64 Bit code . front 8 Bit is a single line serial code :28H. And then 48 Bit is a unique sequence number column . Last 8 Bit is above 56 Bit CRC code .CRC For a detailed explanation of CRC Generator section .64 position ROM and ROM The operation controller allows DS18B20 As a single bus device, it works according to the single bus protocol detailing the single bus system section .


Temperature sensor timing ( Operation steps ) Please also check the manual ..
Chinese Manual
link :https://pan.baidu.com/s/1AJiUh0PfLi-EYOQFXrFkzA?pwd=8elx
Extraction code :8elx Code
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
Show the temperature
__IO uint32_t lcd_uwTick = 0; // control lcd_proc() Execution speed of
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

Interface specification

It is recommended that the length of the connecting wire be shorter than 20 When rice is used 5K Pull up resistance , Greater than 20 When measuring meters, use appropriate pull-up resistance according to the actual situation .


dht11 manual
link :https://pan.baidu.com/s/1y5T_SZC5eOOCBZOybGxxmQ?pwd=hnkp
Extraction code :hnkp * Reset DHT11

// Reset DHT11
void DHT11_Rst(void)
{
DHT11_PIN_OUT(); // Set as output
DHT11_PIN_OUT_L; // Low level
HAL_Delay(20); // Pull down at least 18ms
DHT11_PIN_OUT_H; // High level
Delay_us(60); // The main engine is pulled up 20~40us
}* Send a start signal and respond

// wait for DHT11 The response of the
// return 1: Not detected DHT11 The existence of
// return 0: There is
uint8_t DHT11_Check(void)
{
uint8_t re = 0;
DHT11_PIN_INPUT(); // Set as output
while (DHT11_PIN_IN && re < 100) //DHT11 Will pull down 40~80us
{
re++;
Delay_us(1);
};
if(re >= 100)return 1;
else re = 0;
while (!DHT11_PIN_IN && re < 100) //DHT11 After pulling down, it will pull up again 40~80us
{
re++;
Delay_us(1);
};
if(re >= 100)return 1;
return 0;
}* read 0 And reading 1

// from DHT11 Read a bit
uint8_t DHT11_Read_Bit(void)
{
uint8_t re = 0;
while(DHT11_PIN_IN && re < 110) // Wait until it goes low
{
re++;
Delay_us(1);
}
re = 0;
while(!DHT11_PIN_IN && re < 110) // Wait for high level
{
re++;
Delay_us(1);
}
Delay_us(80);// wait for 40us
if(DHT11_PIN_IN)return 1;
else return 0;
}
// from DHT11 Read a byte
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;
}Reading data
// from DHT11 Read data once
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;
}Implementation code
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);
}
// Reset DHT11
void DHT11_Rst(void)
{
DHT11_PIN_OUT(); // Set as output
DHT11_PIN_OUT_L; // Low level
HAL_Delay(20); // Pull down at least 18ms
DHT11_PIN_OUT_H; // High level
Delay_us(60); // The main engine is pulled up 20~40us
}
// wait for DHT11 The response of the
// return 1: Not detected DHT11 The existence of
// return 0: There is
uint8_t DHT11_Check(void)
{
uint8_t re = 0;
DHT11_PIN_INPUT(); // Set as output
while (DHT11_PIN_IN && re < 100) //DHT11 Will pull down 40~80us
{
re++;
Delay_us(1);
};
if(re >= 100)return 1;
else re = 0;
while (!DHT11_PIN_IN && re < 100) //DHT11 After pulling down, it will pull up again 40~80us
{
re++;
Delay_us(1);
};
if(re >= 100)return 1;
return 0;
}
// from DHT11 Read a bit
uint8_t DHT11_Read_Bit(void)
{
uint8_t re = 0;
while(DHT11_PIN_IN && re < 110) // Wait until it goes low
{
re++;
Delay_us(1);
}
re = 0;
while(!DHT11_PIN_IN && re < 110) // Wait for high level
{
re++;
Delay_us(1);
}
Delay_us(80);// wait for 40us
if(DHT11_PIN_IN)return 1;
else return 0;
}
// from DHT11 Read a byte
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;
}
// from DHT11 Read data once
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;
}
// initialization DHT11 Of IO mouth , Simultaneous detection DHT11 The existence of
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);// initialization DHT11
uint8_t DHT11_Read_Data(uint8_t *temp, uint8_t *humi); // Read the temperature and humidity
uint8_t DHT11_Read_Byte(void);// Read a byte
uint8_t DHT11_Read_Bit(void);// Read one bit
uint8_t DHT11_Check(void);// Detect the presence of DHT11
void DHT11_Rst(void);// Reset DHT11
#endif
Display temperature and humidity
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);
}边栏推荐
- 字符输入流与字符输出流
- 技术分享| 融合调度中的广播功能设计
- Serialization and deserialization of objects
- Neural network - nonlinear activation
- Common interview questions ①
- Talk about testdeploy
- 科研狗可能需要的一些工具
- JS to solve the problem of floating point multiplication precision loss
- [pat (basic level) practice] - [simple simulation] 1064 friends
- Pytorch(三) —— 函数优化
猜你喜欢

pytorch 卷积操作

Kodori tree board
![AssertionError assert I.ndim == 4 and I.shape[1] == 3](/img/b1/0109bb0f893eb4c8915df36c100907.png)
AssertionError assert I.ndim == 4 and I.shape[1] == 3

STM32 photoresistor sensor & two channel AD acquisition
![[2020 overview] overview of link prediction based on knowledge map embedding](/img/69/22983c5f37bb67a8dc0e2b87c73238.jpg)
[2020 overview] overview of link prediction based on knowledge map embedding

STM32 光敏电阻传感器&两路AD采集

Pytoch (III) -- function optimization

Difficulties in the development of knowledge map & the importance of building industry knowledge map

最长递增子序列及最优解、动物总重量问题

C#读写应用程序配置文件App.exe.config,并在界面上显示
随机推荐
Neural networks - use of maximum pooling
STM32扩展板 数码管显示
Pytoch (II) -- activation function, loss function and its gradient
[daily question in summer] first time, second time, deal!
[daily question in summer] Luogu p7222 [rc-04] informatics competition
[2020 overview] overview of link prediction based on knowledge map embedding
Pytorch(四) —— 可视化工具 Visdom
RuntimeError: “max_pool2d“ not implemented for ‘Long‘
线程类的几大创建方法
Leecode question brushing record 1310 subarray XOR query
C#读写应用程序配置文件App.exe.config,并在界面上显示
How to use common datasets in pytorch
[daily question in summer] letter delivery by p1629 postman in Luogu (to be continued...)
最长递增子序列及最优解、动物总重量问题
Kodori tree board
Summary of testing experience - Testing Theory
Pytorch(一) —— 基本语法
分布式锁的实现
[daily question in summer] function of rogu p3742 UMI
Query long transaction