DS18B20 Experiment of digital temperature sensor
STM32 Although there is a temperature sensor inside , But because of the chip temperature rise and other problems , It is quite different from the actual temperature , therefore , In this chapter, we will show you how to pass the STM32
To read the temperature of the external digital temperature sensor , To get a more accurate ambient temperature . In this chapter , We will learn to use single bus technology , Through it STM32 And external temperature sensor (
DS18B20) Communication of , The temperature obtained from the temperature sensor is displayed in the TFTLCD On the module .
1 DS18B20 brief introduction
DS18B20 By DALLAS
A new product launched by semiconductor company “ One wire bus ” Temperature sensor of interface . Compared with the traditional thermistor and other temperature measuring elements , It is a new type of small size , Wide voltage range , Digital temperature sensor with simple interface with microprocessor . One wire bus structure is simple and economical , It makes it easy for users to set up sensor networks , So as to introduce a new concept for the construction of measurement system , The measuring temperature range is -55~+125℃
, The accuracy is ±0.5℃. The field temperature is directly controlled by “ One wire bus ” Digital mode of transmission , The anti-interference performance of the system is greatly improved . It can read the temperature directly , And it can be realized by simple programming according to the actual requirements 9~l2
Digital value reading mode of bit . It works in 3~5.5V Voltage range , Various packaging forms are adopted , So the system design is flexible , convenient , The set resolution and the alarm temperature set by the user are stored in the EEPROM
in , Keep it after power down .
ROM In 64 Bit serial number is recorded by light before leaving factory , It can be seen as the DS18B20 Address sequence code of , each DS18B20 Of 64 The bit serial numbers are not the same . 64 position
ROM What's the arrangement : front 8 Bit is the product family code , next 48 The position is DS18B20 Serial number of , last 8 One is in the front 56
Bit cyclic redundancy check code (CRC=X8+X5+X4+1). ROM The role is to make every DS18B20 It's all different , In this way, it is possible to connect multiple nodes on one bus DS18B20.
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, write 1, read 0 And read
1. All these signals , Except for the reply pulse , The synchronous signal is sent by the host . And send all the commands and data are the first byte . Here we briefly introduce the timing of these signals :
①, Unique single bus interface mode ,DS18B20 Only one port line is needed to connect with microprocessor Modern microprocessors and DS18B20 Two way communication based on Internet . The anti-interference performance of the system is greatly improved .
② , Measuring range -55℃~+125℃, The accuracy is ±0.5℃.
③, Support multipoint networking , Multiple DS18B20 It can be connected in parallel on the only three wires , Parallel connection at most 8 individual , Multi point temperature measurement , If the quantity is too much , The power supply voltage will be too low , So the signal transmission is unstable .
④, Working power supply : 3.0~5.5V/DC ( Data line parasitic power supply can be used ).
⑤ , No peripheral components are needed in use .
⑥, The measurement results are as follows 9~12 Serial transmission in bit word mode .
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 will pull the single bus high , delayed 15~60 us, And enter the receiving mode (Rx). next DS18B20 Pull down the bus 60~240
us, To generate a low level reply pulse , In case of low level , Further delay 480 us.
2) Write timing
Write timing includes write timing 0 Timing and writing 1 sequential . All write timing requires at least 60us, And in 2 At least two independent write sequences are required 1us
Recovery time for , Both write sequences start when the host pulls down the bus . write 1 sequential : Host output low level , delayed 2us, Then release the bus , delayed 60us. write 0 sequential : Host output low level , delayed
60us, Then release the bus , delayed 2us.
3) Reading sequence
The single bus device is only used when the host sends the read sequence , To transmit data to the host , therefore , After the host sends the read data command , Read sequence must be generated immediately , So that the slave can transfer data . All read sequences require at least
60us, And in 2 At least two independent read sequences are required 1us Recovery time for . Each read sequence is initiated by the host , At least pull down the bus
1us. The host must release the bus during read timing , And at the beginning of the sequence 15us Internal sampling bus status . The typical reading sequence process is :
Host output low level delay 2us, Then the host goes into input mode 12us, Then read the current level of the single bus , Then delay 50us.
After understanding the single bus timing , Let's see DS18B20 Typical temperature reading process , DS18B20 The typical temperature reading process is : reset hair SKIP ROM command (
0XCC) Send the start conversion command ( 0X44) delayed reset send out SKIP ROM command ( 0XCC) Send read memory command ( 0XBE)
Read two bytes of data continuously ( That is temperature ) end .
DS18B20 encapsulation
2. Connection mode
Connect to STM32 upper Pin interface of ( Other options are available ) The connected pins are used for later programming
Single bus is a half duplex communication mode
DS18B20 share 6 Two signal types : Reset pulse , Reply pulse , write 0, write 1, read 0 And read 1. All these signals , Except for the reply pulse , The synchronous signal is sent by the host . And send all the commands and data are the first byte .
Talking about signal types , Talking about the way of code configuration , Let us know STM32 drive 18B20 process .
The signal line :PG9
//IO Direction setting
#define DS18B20_IO_IN() {GPIOG->MODER&=~(3<<(9*2));GPIOG->MODER|=0<<9*2;}
//PG9 Input module
#define DS18B20_IO_OUT() {GPIOG->MODER&=~(3<<(9*2));GPIOG->MODER|=1<<9*2;}
//PG9 Output mode
IO operation
#define DS18B20_DQ_OUT PGout(9) // Data port PG9
#define DS18B20_DQ_IN PGin(9) // Data port PG9
( 1). Reset pulse
All communication on a single bus starts with an initialization sequence . Host output low level , Keep low for at least 480
us,, To generate a reset pulse . Then the host releases the bus ,4.7K The pull-up resistance will pull the single bus high , delayed 15~60 us, And enter the receiving mode (Rx). next DS18B20 Pull down the bus 60~240
us, To generate a low level reply pulse .
// reset DS18B20 void DS18B20_Rst(void)
{
DS18B20_IO_OUT(); // Set to output mode
DS18B20_DQ_OUT=0; // Pull down DQ
delay_us(750); // Pull down 750us( at least 480us)
DS18B20_DQ_OUT=1; //DQ=1 Pull up release bus
delay_us(15); //15US
// Enter acceptance mode , Waiting for response signal .
}
② Response signal
// wait for DS18B20 My response
// return 1: Not detected DS18B20 The existence of return 0: existence
u8 DS18B20_Check(void)
{
u8 retry=0;
DS18B20_IO_IN();//SET PA0 INPUT
while (DS18B20_DQ_IN&&retry<200)
{
retry++;
delay_us(1);
};
if(retry>=200)return 1;
else retry=0;
while (!DS18B20_DQ_IN&&retry<240)
{
retry++;
delay_us(1);
};
if(retry>=240)return 1;
return 0;
}
③ Write timing
Write timing includes write timing 0 Timing and writing 1 sequential . All write timing requires at least 60us, And in 2 At least two independent write sequences are required 1us Recovery time for , Both write sequences start when the host pulls down the bus .
write 1 sequential : Host output low level , delayed 2us, Then release the bus , delayed 60us. write 0 sequential : Host output low level , delayed 60us, Then release the bus , delayed 2us.
/ Write a byte to DS18B20 //dat: Bytes to write
void DS18B20_Write_Byte(u8 dat)
{
u8 j;
u8 testb;
DS18B20_IO_OUT();// set up PA0 For output
for (j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if (testb) // High output
{
DS18B20_DQ_OUT=0;// Host output low level
delay_us(2); // delayed 2us
DS18B20_DQ_OUT=1;// Release the bus
delay_us(60); // delayed 60us
}
else // Low output
{
DS18B20_DQ_OUT=0;// Host output low level
delay_us(60); // delayed 60us
DS18B20_DQ_OUT=1;// Release the bus
delay_us(2); // delayed 2us
}
}
}
④ Reading sequence
The single bus device is only used when the host sends the read sequence , To transmit data to the host , therefore , After the host sends the read data command , Read sequence must be generated immediately , So that the slave can transfer data .
All read sequences require at least 60us, And in 2 At least two independent read sequences are required 1us Recovery time for . Each read sequence is initiated by the host , At least pull down the bus 1us. The host must release the bus during read timing , And at the beginning of the sequence 15us Internal sampling bus status .
The typical reading sequence process is : Host output low level delay 2us, Then the host goes into input mode 12us, Then read the current level of the single bus , Then delay 50us.
The typical reading sequence process is : Host output low level delay 2us, Then the host goes into input mode 12us, Then read the current level of the single bus , Then delay 50us.
// from DS18B20 Read a bit // Return value :1/0
u8 DS18B20_Read_Bit(void) // read one bit
{
u8 data;
DS18B20_IO_OUT();// Set to output
DS18B20_DQ_OUT=0; // Output low level 2us
delay_us(2);
DS18B20_DQ_OUT=1; // Pull up release bus
DS18B20_IO_IN();// Set to input
delay_us(12);// delayed 12us
if(DS18B20_DQ_IN)data=1;// Read bus data
else data=0;
delay_us(50); // delayed 50us
return data;
}
Read a byte of data
// from DS18B20 Read a byte // Return value : Data read
u8 DS18B20_Read_Byte(void) // read one byte
{
u8 i,j,dat;
dat=0;
for (i=1;i<=8;i++)
{
j=DS18B20_Read_Bit();
dat=(j<<7)|(dat>>1);
}
return dat;
}
Let's see DS18B20 Typical temperature reading process ,DS18B20 The typical temperature reading process is : reset hair SKIP
ROM command (0XCC) Send the start conversion command (0X44) delayed reset send out SKIP
ROM command (0XCC) Send read memory command (0XBE) Read two bytes of data continuously ( That is temperature ) end .
3 Finally, the final source program ( You need to put the program in the STM32F4 Library function programming )
Open our DS18B20 Digital temperature sensor experiment project can see that we added ds18b20.c Documents and their
Header file ds18b20.h file , All ds18b20 Driver code and related definitions are distributed in these two files .
//ds18b20.c code
// reset DS18B20
#include "DS18B20"
void DS18B20_Rst(void)
{
DS18B20_IO_OUT(); //SET PG11 OUTPUT
DS18B20_DQ_OUT=0; // Pull down DQ
delay_us(750); // Pull down 750us
DS18B20_DQ_OUT=1; //DQ=1
delay_us(15); //15US
}
// wait for DS18B20 My response
// return 1: Not detected DS18B20 The existence of
// return 0: existence
u8 DS18B20_Check(void)
{
u8 retry=0;
DS18B20_IO_IN();//SET PG11 INPUT
while (DS18B20_DQ_IN&&retry<200) { retry++; delay_us(1); };
if(retry>=200)return 1;
else retry=0;
while (!DS18B20_DQ_IN&&retry<240) {retry++; delay_us(1); };
if(retry>=240)return 1;
return 0;
}
// from DS18B20 Read a bit
// Return value : 1/0
u8 DS18B20_Read_Bit(void)
{
u8 data;
DS18B20_IO_OUT();//SET PG11 OUTPUT
DS18B20_DQ_OUT=0;
delay_us(2);
DS18B20_DQ_OUT=1;
DS18B20_IO_IN();//SET PG11 INPUT
delay_us(12);
if(DS18B20_DQ_IN)data=1;
else data=0;
delay_us(50);
return data;
}
// from DS18B20 Read a byte
// Return value : Data read
u8 DS18B20_Read_Byte(void)
{
u8 i,j,dat;
dat=0;
for (i=1;i<=8;i++)
{
j=DS18B20_Read_Bit();
dat=(j<<7)|(dat>>1);
}
return dat;
}
// Write a byte to DS18B20
//dat: Bytes to write
void DS18B20_Write_Byte(u8 dat)
{
u8 j;
u8 testb;
DS18B20_IO_OUT();//SET PG11 OUTPUT;
for (j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if (testb)
{
DS18B20_DQ_OUT=0;// Write 1
delay_us(2);
DS18B20_DQ_OUT=1;
delay_us(60);
}
else
{
DS18B20_DQ_OUT=0;// Write 0
delay_us(60);
DS18B20_DQ_OUT=1;
delay_us(2);
}
}
}
// Start temperature conversion
void DS18B20_Start(void)
{
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0xcc);// skip rom
DS18B20_Write_Byte(0x44);// convert
}
// initialization DS18B20 Of IO mouth DQ Simultaneous detection DS The existence of
// return 1: non-existent
// return 0: existence
u8 DS18B20_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);// Enable GPIOG Clock
//GPIOG9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;// Normal output mode
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;// Push pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//50MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;// Pull up
GPIO_Init(GPIOG, &GPIO_InitStructure);// initialization
DS18B20_Rst();
return DS18B20_Check();
}
// from ds18b20 Get the temperature value
// accuracy : 0.1C
// Return value : Temperature value ( -550~1250)
short DS18B20_Get_Temp(void)
{
u8 temp;
u8 TL,TH;
short tem;
DS18B20_Start();// ds1820 start convert
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0xcc);// skip rom
DS18B20_Write_Byte(0xbe);// convert
TL=DS18B20_Read_Byte(); // LSB
TH=DS18B20_Read_Byte(); // MSB
if(TH>7)
{
TH=~TH;
TL=~TL;
temp=0; // The temperature is negative
}else temp=1; // The temperature is positive
tem=TH; // Get the top eight
tem<<=8;
tem+=TL; // Get the bottom eight
tem=(double)tem*0.625;// transformation
if(temp)return tem; // Return temperature value
else return -tem;
}
This part of the code is read according to the single bus operation sequence we introduced earlier DS18B20 The temperature value of ,DS18B20
The temperature of DS18B20_Get_Temp Function read , The return value of this function is signed short integer data , Of the return value
The scope is -550~1250, In fact, the temperature value has expanded 10 times .
Principal function ds18b20.h
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "lcd.h"
#include "ds18b20.h"
int main(void)
{
u8 t=0;
short temperature;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// Setting system interrupt priority group 2
delay_init(168); // Initialize delay function
uart_init(115200); // Initialize serial port baud rate to 115200
LED_Init(); // initialization LED
LCD_Init();
POINT_COLOR=RED;// Set the font to red
LCD_ShowString(30,50,200,16,16,"ALIX");
LCD_ShowString(30,70,200,16,16,"DS18B20 TEST");
LCD_ShowString(30,90,200,16,16,"ALIX");
LCD_ShowString(30,110,200,16,16,"2018/7/22");
while(DS18B20_Init()) //DS18B20 initialization
{
LCD_ShowString(30,130,200,16,16,"DS18B20 Error");
delay_ms(200);
LCD_Fill(30,130,239,130+16,WHITE);
delay_ms(200);
}
LCD_ShowString(30,130,200,16,16,"DS18B20 OK");
POINT_COLOR=BLUE;// Set the font to blue
LCD_ShowString(30,150,200,16,16,"Temp: . C");
while(1)
{
if(t%10==0)// each 100ms Read once
{
temperature=DS18B20_Get_Temp();
if(temperature<0)
{
LCD_ShowChar(30+40,150,'-',16,0); // Show minus sign
temperature=-temperature; // Turn to positive
}else LCD_ShowChar(30+40,150,' ',16,0); // Remove the minus sign
LCD_ShowNum(30+40+8,150,temperature/10,2,16); // Show positive part
LCD_ShowNum(30+40+32,150,temperature%10,1,16); // Show decimal part
}
delay_ms(10); t++;
if(t==20)
{
t=0; LED0=!LED0;
}
}
Technology
Daily Recommendation