当前位置:网站首页>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 .
边栏推荐
- 园区运营效率提升,小程序容器技术加速应用平台化管理
- Voici le programme de formation des talents de SHARE Creators!
- 联想X86服务器重启管理控制器(XClarity Controller)或TSM的方法
- Pychar open remote directory remote host
- Image preloading in JS
- SAP ALV summary is inconsistent with exported excel summary data
- 鼠标悬停效果七
- Complete training and verification of a neural network based on pytorch
- [2022] Jiangxi postgraduate mathematical modeling scheme and code
- Pulsar theme compression
猜你喜欢

PCB defect detection based on OpenCV and image subtraction

AI 边缘计算平台 - BeagleBone AI 64 简介

Dell server restart Idrac method

Gartner研究:在中国,混合云的采用已成为主流趋势

基于Pytorch完整的训练一个神经网络并进行验证

UE4渲染管线学习笔记

5款主流智能音箱入门款测评:苹果小米华为天猫小度,谁的表现更胜一筹?

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

nacos配置中心使用教程

单片机 MCU 固件打包脚本软件
随机推荐
Mouse over effect 9
JS anti shake and throttling
I want to know how to open a stock account? Is it safe to open an account online?
Restcloud ETL data realizes incremental data synchronization through timestamp
鼠标悬停效果五
股票开户安全吗?上海股票开户步骤。
Mouse over effect V
Dart training and sphygmomanometer inflation pump power control DPC
import tensorflow. contrib. Slim as slim error
js中的原型和原型链
js防抖和节流
VirtualBox installation enhancements
Dell服务器重启iDRAC方法
Share Creators萌芽人才培养计划来了!
PCB defect detection based on OpenCV and image subtraction
鼠标悬停效果三
SAP ALV summary is inconsistent with exported excel summary data
Restcloud ETL practice to realize incremental data synchronization without identification bit
Evaluation of the entry-level models of 5 mainstream smart speakers: apple, Xiaomi, Huawei, tmall, Xiaodu, who is better?
Thread Detach