当前位置:网站首页>18.[stm32] read the ROM of DS18B20 temperature sensor and realize multi-point temperature measurement
18.[stm32] read the ROM of DS18B20 temperature sensor and realize multi-point temperature measurement
2022-07-05 15:48:00 【According to point_ DW】
Author's brief introduction : Hello, everyone , My name is DW, Share some of my new knowledge every day , Look forward to making progress with you
Series column :STM32
Small experimental target : Read 3 individual DS18B20 Of ROM And realize multi-point temperature measurement
If there is anything that is not well written, you are welcome to correctDevelopment board : The punctual atoms STM32F103Mini edition
Creation time :2022 year 6 month 6 Japan
obtain DS18B20ROM surface
Written before DS18B20 On the basis of the article , Modify slightly , You can measure each DS18B20 Of 64 position ROM Serial number .
One byte is 8 position , We need to read 8 byte ,8 Byte is 64 Bit data , We put 8 Bytes of data are stored in an array , Then print it out through serial port . On the basis of the previous article , Add a read DS18B20ROM Function of .
// Read ROM
void DS18B20_Read_Rom(void){
u8 rom[8];
DS18B20_Start_Signal();// Start signal
DS18B20_Write_Byte(0x33);// read ROM
for(u8 i=0;i<8;i++){
rom[i] = DS18B20_Read_Byte();// Read a byte
printf("%#.2x,",rom[i]);//%#x: Print hexadecimal results preceded by 0X
}
printf("\r\n");
}We use this function , You can read 8 Bytes of data , And print it out through the serial port , This experiment needs to use 3 individual DS18B20, Cannot read all at once 3 individual DS18B20 Of 64 position ROM Serial number , You need to read one by one , Read three times , get 64 position ROM After the serial number, they can be connected to a bus , Then read the temperature at the same time .
Here is what we got 3 individual DS18B20 Corresponding three groups ROM surface .

Single bus multipoint temperature measurement
obtain 3 Group 64 position ROM after , We created 3 An array , Store the read ROM, Only through 64 position ROM We can search the corresponding DS18B20, After through ROM matching , We can match the corresponding on the bus DS18B20, And then realize the conversion of temperature .
u8 rom1[]={0x28,0xd3,0xf6,0x48,0xf6,0xdd,0x3c,0xd5,}; //64 ROM
u8 rom2[]={0x28,0xa3,0xf9,0x96,0xf0,0x01,0x3c,0xf2,};
u8 rom3[]={0x28,0x18,0xa8,0x96,0xf0,0x01,0x3c,0x55,};matching ROM function
Because you need to call the array directly , In order to obtain more accurate data , So we need to set a pointer type variable rom,8 Bytes of ROM Need to write 8 Time , Write one byte at a time .
You need to send a start signal before writing each byte , Then send the match ROM Instructions 0X55, Finally, write a byte .
// matching ROM
void DS18B20_Match_Rom(u8 *rom){
DS18B20_Start_Signal();// Start signal
DS18B20_Write_Byte(0x55); // matching ROM
for(u8 i=0;i<8;i++){
DS18B20_Write_Byte(*rom++);// Write Bytes
}
}stay DS18B20 Based on the temperature conversion function , Modify slightly , Add the match just written ROM function , The modified function is as follows .
void DS18B20_Read_Temperature(u8 *rom,u16 *data){
u8 LSB = 0,MSB = 0;
u16 Temp = 0;
// Temperature conversion
DS18B20_Match_Rom(rom);// matching ROM
DS18B20_Write_Byte(0X44);// Temperature change
delay_ms(750);
//12 Bit accuracy 750ms The nixie tube itself has a time delay , So this delay can be removed
DS18B20_Match_Rom(rom);
DS18B20_Write_Byte(0XBE);// Read temporary storage
LSB = DS18B20_Read_Byte();
MSB = DS18B20_Read_Byte();
Temp = (MSB<<8) | LSB;// Merge data into 16 position
if((Temp&0XF800) == 0XF800){ // Negative temperature ;s=1 Positive temperature : s=0
*data =(((~Temp+0X01)* -0.0625)+0.5)*10.0;
}
else{
*data =((Temp*0.0625)+0.5)*10.0;
}
}Last , stay main Function to call the temperature conversion function and LCD1602 Just display the function .
#include "delay.h"
#include "lcd1602.h"
#include "DS18b20.h"
#include "usart.h"
u8 rom1[]={0x28,0xd3,0xf6,0x48,0xf6,0xdd,0x3c,0xd5,}; //64 ROM
u8 rom2[]={0x28,0xa3,0xf9,0x96,0xf0,0x01,0x3c,0xf2,};
u8 rom3[]={0x28,0x18,0xa8,0x96,0xf0,0x01,0x3c,0x55,};
int main(){
u16 Temp = 0;
delay_init();
LCD1602_UserConfig();
LCD1602_Init();
DS18B20_UserConfig();
uart_init(115200);
while(1){
DS18B20_Read_Rom();// measurement ROM
delay_ms(1000);
DS18B20_Read_Temperature(rom1,&Temp);
LCD1602_Number_Display(0XC0,Temp);
DS18B20_Read_Temperature(rom2,&Temp);
LCD1602_Number_Display(0XC6,Temp);
DS18B20_Read_Temperature(rom3,&Temp);
LCD1602_Number_Display(0XCC,Temp);
}
}
Okay , Let's take a look at the experimental renderings ! stay LCD1602 Displayed on the 3 A place DS18B20 Measured temperature , Realize multiple DS18B20 Operation connected to a bus .

Tips :
This article selects two pieces 74HC595 drive LCD1602 liquid crystal , If you don't have this chip, you can see the previous fourth article , hold LCD1602 Part of the function can be replaced .
3 individual DS18B20 It is connected through a bus , Data pin PB0 All connected to DS18B20 On the data bus .
This chapter ends , I'll see you in the next chapter
Reference material :
1.STM32 Firmware library manual
2. The punctual atoms STM32 Incomplete manual _ Library function version
4.4.STM32F103-LCD1602 Show characters
5.17.[STM32] Use only three wires to drive you LCD1602 liquid crystal
Data uploaded , You need to take it yourself
边栏推荐
- lv_font_conv离线转换
- SQL injection sqllabs (basic challenges) 1-10
- Noi / 1.5 06: element maximum span value of integer sequence
- 数学建模之层次分析法(含MATLAB代码)
- Redis distributed lock principle and its implementation with PHP (1)
- Example of lvgl display picture
- Write a go program with vscode in one article
- Appium automation test foundation - appium basic operation API (II)
- 【簡記】解决IDE golang 代碼飄紅報錯
- Bugku's Eval
猜你喜欢

MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!

六种常用事务解决方案,你方唱罢,我登场(没有最好只有更好)

sql server学习笔记
![P1451 calculate the number of cells / 1329: [example 8.2] cells](/img/c4/c62f3464608dbd6cf776c2cd7f07f3.png)
P1451 calculate the number of cells / 1329: [example 8.2] cells

Detailed explanation of QT creator breakpoint debugger

First PR notes

MySQL overview
![1330: [example 8.3] minimum steps](/img/69/9cb13ac4f47979b498fa2254894ed1.gif)
1330: [example 8.3] minimum steps

MySQL 巨坑:update 更新慎用影响行数做判断!!!

I spring and autumn blasting-1
随机推荐
Noi / 1.3 01: a+b problem
RepLKNet:不是大卷积不好,而是卷积不够大,31x31卷积了解一下 | CVPR 2022
keep-alive
lvgl 显示图片示例
Stop B makes short videos, learns Tiktok to die, learns YouTube to live?
Common PHP interview questions (1) (written PHP interview questions)
Data communication foundation - Ethernet port mirroring and link aggregation
Arduino controls a tiny hexapod 3D printing robot
Summary of the third class
Usage and usage instructions of JDBC connection pool
Basic JSON operations of MySQL 5.7
【 note 】 résoudre l'erreur de code IDE golang
Bugku's steganography
Vulnhub-Moneybox
21.[STM32]I2C协议弄不懂,深挖时序图带你编写底层驱动
swiper. JS to achieve barrage effect
Object. defineProperty() - VS - new Proxy()
SQL Server learning notes
Redis' transaction mechanism
CODING DevSecOps 助力金融企业跑出数字加速度