DS18B20 temperature conversion

Programming flow:

Reset write command word: 0xcc (skip ROM instruction), 0x44 (start temperature conversion)

Skip the command word: because DS18b20 is a single bus device, and a single bus can mount many devices, ROM is used to store the id number of the device.

Delay 700~900ms and wait for the completion of temperature conversion

Reset write command word: 0xCC (skip ROM instruction), 0xbe (read cache, a total of 9 bytes)

Read the 0 byte and 1 byte of the high-speed register (i.e. LSB and MSB) to obtain the temperature data

Reset and the read operation ends.

Integrate data: integrate LSB and MSB into one 16 bit data

Judge whether the data symbol is positive or negative: the data is in the form of signed 16 bit complement

16 bits, 5 symbols in the upper part, 7 integers in the middle part and 4 decimals in the lower part

The high 5 bits are 0 positive and the high 5 bits are 1 negative. The resolution of DS18B20 is 0.0625

The positive number is multiplied by 0.0625 directly, and the negative number is multiplied by + 1 by 0.0625

Note that it is 0x50 and + 85 ℃ during power on reset

Problems needing attention; Whether the delay time of the bottom drive function is consistent with the clock of the MCU directly affects whether the response can be generated

Data type defined by data receiving: for example, if the temperature generates 16 bit data, it cannot be defined as char data

When waiting for the temperature to be read, the nixie tube will also display,

Specific steps:

To create a project, first put the underlying driver function into the created folder directory

Set the bit selection function, set the one bit display function of nixie tube, and set the nixie tube scanning function (the delay of the scanning function is int type 500). In order to eliminate the blanking, in addition to opening the function to be displayed, turn off other nixie tubes

Then read the DS18B20 function (there are three initialization times, the first time to open the temperature reading, the second time to open the buffer, and the third time to close). The data read in the buffer is the low-order data read first (the data reading type is char8 bits), and then define the global variable (int type 16 bits),

Receiving data: first receive the high-order data, and then shift 8 bits to the right to receive the low-order data (or upper and lower data). There are 16 bits in total,

As long as the integer moves 4 digits to the right, if you want a decimal, move 4 digits to the right and then * 10, then the low-order data and the upper 0x0f and then * 0.625 (the resolution is 0.0625 to avoid the operation of floating-point numbers), and then add the moved data and the low-order data

Move to the right and multiply by 10. Because integer variables are added after multiplying by 10, there is no need to operate on floating-point numbers. The first digit of the fourth digit after direct addition is displayed with a decimal point

#include <STC15F2K60S2.H>
#include "onewire.h"
unsigned char code SMG_NoDot[18] ={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
     0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,
     0xbf,0x7f};

unsigned char code SMG_IsDot[10] = {0x40,0x79,0x24,
	0x30,0x19,0x12,0x02,0x78,0x00,0x10};
unsigned int dat=0;
void smgdelay(unsigned int i)
{
	while(i--);
	
}
void select138(unsigned char n)
{
	switch(n)
	{
		case 4:
			P2=(0x1f&P2)|0x80;
		break;
		case 5:
			P2=(0x1f&P2)|0xa0;
		break;
		case 6:
			P2=(0x1f&P2)|0xc0;
		break;
		case 7:
			P2=(0x1f&P2)|0xe0;
		break;
		case 0:
			P2=(0x1f&P2)|0x00;
		break;
	
	}
}
void smgshow_bite(unsigned char date,unsigned char pos)
{ 	
	select138(6);
	P0=0x01<<pos;
	select138(7);
	P0=date;

}
void smgshowall()
{ 	
	select138(6);
	P0=0xff;
	select138(7);
	P0=0xff;

}
void smgshow()
{
	smgshow_bite(SMG_NoDot[dat%10],7);
	smgdelay(500);
	smgshow_bite(SMG_NoDot[(dat%100)/10],6);
	smgdelay(500);
	smgshow_bite(0xff,5);
	smgdelay(500);
	smgshow_bite(0xff,4);
	smgdelay(500);
	smgshow_bite(0xff,3);
	smgdelay(500);
	smgshow_bite(0xff,2);
	smgdelay(500);
	smgshow_bite(0xff,1);
	smgdelay(500);
	smgshow_bite(0xff,0);
	smgdelay(500);
	smgshowall();	
	
}
void delay(unsigned int i)
{
	while(i--)
	{
	smgshow();
	}
}
void temputer()
{
	unsigned char LSB,MSB;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	delay(100);
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	LSB=Read_DS18B20();
	MSB=Read_DS18B20();
	init_ds18b20();
	dat=MSB;
	dat<<=8;
	dat=dat|LSB;
	
	dat=dat>>4;
//	if((dat&0xf800)==0x0000)
//	{
//		dat>>=4;
//		dat=dat*10;
//		dat=dat+(LSB&0x0f)*0.625;
//	
//	}
}


void main()
{
	while(1)
	{
	temputer();
	smgshow();
	
	}

}

Keywords: Single-Chip Microcomputer stm32

Added by CGRRay on Thu, 27 Jan 2022 19:47:15 +0200