当前位置:网站首页>STM32 - DS18B20 temperature sampling of first-line protocol
STM32 - DS18B20 temperature sampling of first-line protocol
2022-07-01 02:47:00 【Little monk Hanshui temple has no intention】
One 、DS18B20 Sensor Introduction

Every DS18B20 When leaving the factory, a unique 64 Is the product serial number at ROM in .ROM The function of is to make every DS18B20 They're all different , In this way, multiple sensors can be connected to one bus .
1、DS18B20 First line agreement
All single bus devices require strict signal timing , To ensure the integrity of the data .DS18B20 share 6 Two signal types : Reset pulse 、 Reply pulse 、 Write 0/1、 read 0/1. All these signals except the reply signal . The host sends synchronization signals , And all the commands and data sent are the low order of bytes in front (LSB).
(1) Reset pulse and reply pulse
All communication on a single bus starts with an initialization sequence . Host output low level , Keep low for at least 480us, To generate a reset pulse . Then the host releases the bus ,4.7K The pull-up resistance of pulls up the single bus , Time delay 15 ~ 60us, And enter the receiving mode (Rx). next DS18B20 The module pulls down the bus 60 ~ 240us, To generate a low level response pulse , If a low level reply pulse is generated , Delay again 240us.
(2) Write timing
Writing sequence includes writing 0 Timing and writing 1 sequential . All writing sequences should at least 60us, And at least 1us Recovery time of , Both write timings begin with pulling down the bus .
Write 0 sequential : Host output low level , Time delay 60us, Then release the bus 2us;
Write 1 sequential : Host output low level , Time delay 2us, Then release the bus , Time delay 60us.
The slave is pulled down on the bus 30us after , Read the level . If it is low , Said to write 0, If high , Said to write 1.

(2) Reading sequence
A single bus device only when the host sends a read timing , Before sending data to the host , After the host sends the read data command , The read sequence must be generated immediately , One time slave can transmit data . All read sequences need at least 60us, And at least... Is required between two independent read sequences 1us Recovery time of .
When the bus controller pulls the data line from high level to low level , The reading sequence starts , The data cable must be kept at least 1us, Then the bus is released .DS18B20 Transmit by pulling up or down the bus “1” or “0”. When logic “0” After the end , The bus is released , Pull up the resistance to return to the rising state , from DS18B20 The output data appears after the falling edge of the read sequence 15us Effective within 6. therefore , The bus controller must stop after the start of the read sequence I/O The port drive is low , To read I/O state .

2.DS18B20 Workflow
First, carry out temperature conversion , Then read the temperature ;
Temperature conversion : initialization -> skip ROM -> Temperature conversion
Temperature reading : initialization -> skip ROM -> Read register -> Read the lower eight -> Read high eight
(1) initialization
All communication on a single bus starts with an initialization sequence , The host sends the initialization signal and waits for the response signal from the slave device , To confirm whether the slave device can work normally .
(2)ROM Operation command
The bus master detected DS18B20 After being , You can send ROM Operation command , These commands are as follows . But we generally don't care about ROM Medium 16 Bit product serial number , Usually send 0xCC skip ROM Related operations of .
(3) Memory operation command
Next, send the corresponding cache command , The command list is as follows . among 0x44 Order notice DS18B20 The sensor starts to change temperature ( Temperature sampling ), and 0xBE The command will begin to read DS18B20 Sample value of .
(4) Data processing
DS18B20 The high-speed temporary storage of is composed of 9 Byte composition . When executing the temperature conversion command (0x44) after , The converted temperature is stored in the first two bytes of the cache in the form of two byte complement .
If we only care about temperature , You only need to read the first two bytes . among Byte[0] Is the low byte of the temperature value , and Byte[1] Is the high byte of the temperature value . this 16 The format of bit data is as follows :
among BIT[3:0] Is the decimal part of the temperature value , and BIT[10:4] Is the integral part of the temperature value ,BIT[15:11] Symbol bit , If 0 Indicates that the temperature is positive , If -1 It means that the temperature is negative .
Two 、DS18B20 Temperature sampling implementation
1、 Hardware connection
DS18B20 The working voltage of the sensor is 3~5.5V, I will DS18B20 Of I/O Connect to the development board GPIO Pin PA5 On , In fact, any one GPIO All pins are OK , Just change the code ( Later said ).
2、 Code implementation
ds18b20.h
/* * ds18b20.h * * Created on: 2022 year 6 month 28 Japan * Author: 28980 */
#ifndef INC_DS18B20_H_
#define INC_DS18B20_H_
#include "main.h"
uint8_t DS18B20_Reset(void);// Initialization reset and response
void DS18B20_Writte_Byte(uint8_t byte);// Write a byte
uint8_t DS18B20_Read_Bit(void);// Read one bit
uint8_t DS18B20_Read_Byte(void);// Read a byte
uint8_t DS18B20_SampleData(float *temperature);// Temperature sampling
#endif /* INC_DS18B20_H_ */
ds18b20.c
/* * ds18b20.c * * Created on: 2022 year 6 month 28 Japan * Author: 28980 */
#include "ds18b20.h"
typedef struct w1_gpio_S
{
GPIO_TypeDef *group;
uint16_t pin;
}w1_gpio_s;
static w1_gpio_s W1DS =
{
.group = GPIOA,
.pin = GPIO_PIN_5,
};
#define W1DS_Input() \
{
\
GPIO_InitTypeDef GPIO_InitStruct = {
0};\
GPIO_InitStruct.Pin = W1DS.pin;\
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;\
GPIO_InitStruct.Pull = GPIO_PULLUP;\
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;\
HAL_GPIO_Init(W1DS.group, &GPIO_InitStruct);\
}
#define W1DS_Output() \
{
\
GPIO_InitTypeDef GPIO_InitStruct = {
0};\
GPIO_InitStruct.Pin = W1DS.pin;\
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;\
GPIO_InitStruct.Pull = GPIO_NOPULL;\
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;\
HAL_GPIO_Init(W1DS.group, &GPIO_InitStruct);\
}
#define W1DS_Write(x) HAL_GPIO_WritePin(W1DS.group, W1DS.pin,\
(x==1)?GPIO_PIN_SET : GPIO_PIN_RESET)
#define W1DQ_Read() HAL_GPIO_ReadPin(W1DS.group, W1DS.pin)
uint8_t DS18B20_Reset(void)
{
uint8_t retry = 0;
uint8_t rv;
W1DS_Output();
W1DS_Write(1);
delay_us(2);
W1DS_Write(0);
delay_us(480);
W1DS_Write(1);
delay_us(60);
W1DS_Input();
while(W1DQ_Read() && retry < 240)
{
retry++;
delay_us(1);
}
if(retry >= 240)
{
rv = 1;
}
delay_us(240);
W1DS_Output();
W1DS_Write(1);
delay_us(240);
return rv;
}
void DS18B20_Writte_Byte(uint8_t byte)
{
uint8_t i;
W1DS_Output();
for(i = 0; i< 8; i++)
{
if(byte & 0x01)
{
W1DS_Write(0);
delay_us(2);
W1DS_Write(1);
delay_us(60);
}
else
{
W1DS_Write(0);
delay_us(80);
W1DS_Write(1);
delay_us(2);
}
byte >>= 1;
delay_us(2);
}
}
uint8_t DS18B20_Read_Bit(void)
{
uint8_t rv = 0;
W1DS_Output();
W1DS_Write(0);
delay_us(2);
W1DS_Write(1);
delay_us(2);
W1DS_Input();
if(W1DQ_Read())
{
rv = 1;
}
delay_us(60);
W1DS_Output();
W1DS_Write(1);
return rv;
}
uint8_t DS18B20_Read_Byte(void)
{
uint8_t bit;
uint8_t i, Byte = 0;
for(i = 0; i < 8; i++)
{
bit = DS18B20_Read_Bit();
printf("read bit:%d\r\n", bit);
if(bit)
{
Byte |= (1 << i);
}
delay_us(2);
}
printf("Byte:%d\r\n", Byte);
return Byte;
}
uint8_t DS18B20_Start_Convet(void)
{
if(DS18B20_Reset())
{
printf("Initialization failed!\r\n");
return -1;
}
DS18B20_Writte_Byte(0xCC);
DS18B20_Writte_Byte(0x44);
return 0;
}
uint8_t DS18B20_SampleData(float *temperature)
{
uint8_t Byte[2];
uint8_t sign;
uint16_t temp;
if(!temperature)
return -1;
if(DS18B20_Start_Convet())
{
printf("Convet Temperature failed!\r\n");
return -2;
}
if(DS18B20_Reset())
{
printf("Initialization failed!\r\n");
return -3;
}
DS18B20_Writte_Byte(0xCC);
DS18B20_Writte_Byte(0xBE);
Byte[0] = DS18B20_Read_Byte();
Byte[1] = DS18B20_Read_Byte();
printf("Byte[0]:%d\r\n", Byte[0]);
printf("Byte[1]:%d\r\n", Byte[1]);
if(Byte[1] > 7)
{
sign = 0;
temp = ~(Byte[1]<<8 | Byte[0]) + 1;
}
else
{
sign = 1;
temp = Byte[1]<<8 | Byte[0];
}
*temperature = (temp>>4) + (temp & 0x0F) * 0.0625;
if(!sign)
{
*temperature = - *temperature;
}
return 0;
}
main.c
while(1)
{
if(DS18B20_SampleData(&temperature) < 0)
{
printf("ERROR:DS18B20 Sample Data failure\r\n");
}
else
{
printf("DS18B20 Sample Temperature:%.3f\r\n", temperature);
}
HAL_Delay(3000);
}
Personal summary
Before writing, I borrowed some other people's code , Then look for yourself datasheet I wrote my own code , There have been many mistakes , One of them is hidden and deadly , Let me look for a long time , Is to write the last shift of a byte ,byte >>= 1; And read a byte Byte |= (1 << i); this , Missing an equal sign , It's also a small pit , In addition, when writing drivers , Be sure to understand the sequence diagram of the data manual first , And some operations on memory .
边栏推荐
- 股票开户安全吗?上海股票开户步骤。
- Detailed explanation of pointer array and array pointer (comprehensive knowledge points)
- Share Creators萌芽人才培養計劃來了!
- Pulsar theme compression
- Densenet network paper learning notes
- VirtualBox installation enhancements
- Sampling Area Lights
- How do I hide div on Google maps- How to float a div over Google Maps?
- Small program cloud development -- wechat official account article collection
- What are the top ten securities companies? In addition, is it safe to open an account online now?
猜你喜欢

Optimal transport Series 1

nacos配置中心使用教程

Saving images of different depths in opencv

园区运营效率提升,小程序容器技术加速应用平台化管理

Complete training and verification of a neural network based on pytorch

Visual effects, picture to cartoon function

The latest wechat iPad protocol code obtains official account authorization, etc

kubernetes资源对象介绍及常用命令(二)

Desai wisdom number - other charts (parallel coordinate chart): employment of fresh majors in 2021

pycharm 软件deployment 灰色 无法点
随机推荐
Nacos configuration center tutorial
The operation efficiency of the park is improved, and the application platform management of applet container technology is accelerated
php批量excel转word
Pulsar 主题压缩
Sampling Area Lights
MCU firmware packaging Script Software
Is it safe to open a stock account? Shanghai stock account opening procedures.
在国内如何买港股的股?用什么平台安全一些?
Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
【PR #5 A】双向奔赴(状压DP)
VirtualBox installation enhancements
鼠标悬停效果四
LeetCode_栈_困难_227.基本计算器(不含乘除)
PCB defect detection based on OpenCV and image subtraction
Find the length of the common part of two line segments
Evaluation of the entry-level models of 5 mainstream smart speakers: apple, Xiaomi, Huawei, tmall, Xiaodu, who is better?
Comment réaliser la liaison entre la serrure intelligente et la lampe, la scène du moteur de rideau intelligent dans le timing intelligent?
基于Pytorch完整的训练一个神经网络并进行验证
Detailed data governance knowledge system
Scale SVG to container without mask / crop