This article records the author's learning of the DS18B20 temperature sensor and further learning of the 1-Wire single-bus communication protocol and the learning summary.
Package of DS18B20
There are only two packages for DS18B20, one is To-92, and the other is 8-pin SOIC (150mil). Usually we learn and use To-92 package.
Features of DS18B20
According to the official documentation, I picked a few features that I think are important.
1. Using the 1-Wire communication protocol, only one pin is needed to communicate.
2. It can be powered by the data line (parasitic power supply), and the power supply range is 3.0V to 5.5V.
3. Measure the temperature from -55°C to 125°C. The Fahrenheit equivalent is -67°F to 257°F.
4. In the temperature range of -10°C to 85°C, the measurement error is ±0.5°C.
5. The resolution of the thermometer can be programmed from 9 to 12 digits.
6. Under 12-bit resolution, the conversion time does not exceed 750ms.
Composition of DS18B20
DS18B20 has four main data components: 1.
64-bit laser ROM
2. Temperature sensor
3. Non-volatile temperature alarm triggering TH and TL
4. Configuration register
Temperature and data conversion relationship
After DS18B20 performs temperature conversion, its temperature data is stored in Scratchpad memory in 16-bit sign-extended two's complement format.
Under 12-bit resolution, the relationship between internal data and temperature conversion is shown in the figure below.
For example, such as -25.0625°C.
The binary code is 1111 1110 0110 1111. Because the data is complement, the original data is bit-inverted and then added by one to get the original code as 0000 0001 1001 0001. Then substituting the weights of everybody to get 1x2 4 +1x2 3 +1x2 0 +1x2 -4 x0.0625=25.0625.
1-Wire single bus protocol program
The protocol consists of several types of signals: reset pulse, presence pulse, write 0, write 1, read 0, read 1. All these signals, except for pulses, are initiated by other hosts.
The reset pulse and the presence pulse are collectively referred to as initialization. Before the host performs each operation on the slave, it needs to be initialized.
The sequence diagram of initialization is as follows.
The DQ line of the 1-Wire single-bus communication protocol is used as both an output line and an input line.
In the communication process, it is necessary to switch the pin working mode at any time, and the mode can be easily changed through library functions in STM32. When the bus is used as an output, the pin needs to be set to push-pull output mode. When the bus is used as an input, the pin needs to be set to the pull-up input mode (for related procedures, refer to the wildfire expansion module DS18B20 temperature sensor experimental routine).
static void DS18B20_Rst(void)
{
DS18B20_Mode_Out_PP();//将DQ引脚设置为推挽输出模式
DS18B20_DQ_0;//拉低DQ引脚
Delay_us(750);//延时750us 480~960us
DS18B20_DQ_1;//拉高DQ引脚
Delay_us(15);//等待从机响应时间 15~60us
}
static uint8_t DS18B20_Presence(void)
{
uint8_t pulse_time = 0;
DS18B20_Mode_IPU();//将DQ引脚设置为上拉输入模式
while( DS18B20_DQ_IN() && pulse_time<100 )//等待低电平脉冲到来时间 15~60us
{
pulse_time++;
Delay_us(1);
}
if( pulse_time >=100 )//等待时间超时
return 1;
else
pulse_time = 0;
while( !DS18B20_DQ_IN() && pulse_time<240 )//低电平脉冲持续时间 60~240us
{
pulse_time++;
Delay_us(1);
}
if( pulse_time >=240 )//低电平持续时间超时
return 1;
else
return 0;
}
static uint8_t DS18B20_ReadBit(void)//读取一个bit
{
uint8_t dat;
DS18B20_Mode_Out_PP();//将DQ引脚设置为推挽输出模式
DS18B20_DQ_0;
Delay_us(10);//进行读数据操作前,需由主机产生不超过15us的低电平脉冲
DS18B20_Mode_IPU();//将DQ引脚设置为上拉输入模式
if( DS18B20_DQ_IN() == SET )
dat = 1;
else
dat = 0;
Delay_us(45);//整个读数据时间(60~120us)减去前面低电平脉冲时间
return dat;
}
static uint8_t DS18B20_ReadByte(void)//读取一个byte 低位先行
{
uint8_t i, j, dat = 0;
for(i=0; i<8; i++)
{
j = DS18B20_ReadBit();
dat = (dat) | (j<<i);
}
return dat;
}
static void DS18B20_WriteByte(uint8_t dat)//写入一个byte 低位先行
{
uint8_t i, testb;
DS18B20_Mode_Out_PP();//将DQ引脚设置为推挽输出模式
for( i=0; i<8; i++ )
{
testb = dat&0x01;
dat = dat>>1;//写1/0时间 高低电平持续时间 60~120us
if (testb)
{
DS18B20_DQ_0;
Delay_us(8);//写入1前 需要先拉低总线 1~15us
DS18B20_DQ_1;
Delay_us(70);
}
else
{
DS18B20_DQ_0;
Delay_us(70);
DS18B20_DQ_1;
Delay_us(2);//恢复到下一个周期 需重信拉高总线 1us~无穷
}
}
}
FUNCTIONS | VALUE |
---|---|
Read ROM | 0x33 |
Match ROM | 0x55 |
Skip ROM | 0xCC |
Search ROM | 0xF0 |
Alarm Search | 0xEC |
Write Scratchpad | 0x4E |
Read Scratchpad | 0xBE |
Copy Scratchpad | 0x48 |
Convert T | 0x44 |
Recall E2 | 0xB8 |
Read Power Supply | 0xB4 |
static void DS18B20_SkipRom ( void )
{
DS18B20_Rst();
DS18B20_Presence();
DS18B20_WriteByte(0XCC);//写入0xCC 执行skipROM操作
}
float DS18B20_GetTemp_SkipRom ( void )
{
uint8_t tpmsb, tplsb;
short s_tem;
float f_tem;
DS18B20_SkipRom ();
DS18B20_WriteByte(0X44);//转换命令
DS18B20_SkipRom ();
DS18B20_WriteByte(0XBE);//读取命令
tplsb = DS18B20_ReadByte();//低位先行
tpmsb = DS18B20_ReadByte();
s_tem = tpmsb<<8;
s_tem = s_tem | tplsb;
if( s_tem < 0 )
f_tem = (~s_tem+1) * (-0.0625);//负数二进制补码 取反加一后为原码
else
f_tem = s_tem * 0.0625;
return f_tem;
}
In addition, when there are multiple slaves, you can use the Match ROM mode to communicate with different slaves. The 64-bit laser ROM is composed of:
In matching ROM mode, the steps for reading temperature data are as follows.
static void DS18B20_MatchRom ( void )
{
DS18B20_Rst();
DS18B20_Presence();
DS18B20_WriteByte(0X55);//匹配ROM命令
}
float DS18B20_GetTemp_MatchRom ( uint8_t * ds18b20_id )
{
uint8_t tpmsb, tplsb, i;
short s_tem;
float f_tem;
DS18B20_WriteByte(0x33);//读ROM命令 低位先行
for ( i = 0; i < 8; i ++ )
ds18b20_id [ uc ] = DS18B20_ReadByte();
DS18B20_MatchRom ();//匹配ROM
for(i=0;i<8;i++)
DS18B20_WriteByte ( ds18b20_id [ i ] ); //写入ID数据
DS18B20_WriteByte(0X44);//转换命令
DS18B20_MatchRom ();//匹配ROM
for(i=0;i<8;i++)
DS18B20_WriteByte ( ds18b20_id [ i ] ); //写入ID数据
DS18B20_WriteByte(0XBE);
tplsb = DS18B20_ReadByte();
tpmsb = DS18B20_ReadByte();
s_tem = tpmsb<<8;
s_tem = s_tem | tplsb;
if( s_tem < 0 )
f_tem = (~s_tem+1) * (-0.0625);
else
f_tem = s_tem * 0.0625;
return f_tem;
}
DS18B20 can change the resolution size by changing the corresponding two-bit binary value in an 8-bits configuration register in the memo memory.
The structure of the memo memory is as shown in the figure below.
The structure of the configuration register is shown in the figure below.
The corresponding resolution of R1R0 value is shown in the table below.