Based on C51 microcontroller + DS18B20 temperature sensor + LCD1602 display

One, DS18B20 sensor related introduction

DS18B20 features

  1. Unique single bus interface, two-way communication (temperature measurement) can be realized by only one line
  2. Temperature measurement range: -55℃~+125℃, 9-12 digit resolution can be set by programming, and the corresponding resolution temperature is 0.5, 0.25, 0.125, 0.0625℃ respectively.
  3. Supports multi-point networking (multiple DS18B20 temperature sensors can be connected), multiple DS18B20s can be connected in parallel (3 or 2 wires) to achieve multiple networking temperature measurement, but pay attention to more than 8 to solve the power supply problem, otherwise the voltage will be too low The transmission is unstable and the data is inaccurate.
  4. Working voltage: 3.0~5.5V (power can be supplied by data line in parasitic power mode)
  5. No peripheral circuits are needed during use, and all the sensing elements and conversion circuits are in the chip. (Pull-up resistor)
  6. The temperature measurement result is directly digital output, single-bus serial transmission mode, and can transmit CRC check code (check whether the data collection is correct), with strong anti-interference and error correction capabilities.
  7. In the case of 9-bit resolution, the temperature can be converted to a number in 93.75ms at most, and in the case of 12-bit resolution, the temperature value can be converted into a number in 750ms at most.
  8. Negative pressure characteristics: When the power supply polarity is reversed, the chip will not be burnt due to heat, but it will not work normally.

Package form and pin description

Pin

Power supply mode (external power supply, parasitic power supply, parasitic power supply strong pull-up)

Insert picture description here
Insert picture description here

Insert picture description here

DS18B20 instruction (ROM instruction operation)

Serial number instruction Code Description
1 Read ROM 33h Read the 64-bit serial number of DS18B20 (only applicable to only one DS18B20 on the bus)
2 Write register 4EH After the write register instruction, write data to the DS18B20 register TH.TL and the configuration register.
3 Read register WELL After sending this command, DS18B20 will start from one byte and send out 9 bytes in sequence. If you don't want to read all the bytes. The controller can issue a reset command at any time to suspend the reading or not to read directly.
4 Copy register 48H Copy the contents of TH.TL and the configuration register to the EEPROM. If a parasitic power supply is used, the bus controller must start the strong pull-up within 10us after this command is issued and keep it for at least 10ms.
5 Start temperature conversion command 44H After the temperature conversion is completed, it is stored in the first and second bytes. If it is a parasitic power supply, the bus must initiate a strong pull-up within 10 us after issuing this command.
6 Copy EEPROM instruction B8H Copy the value of TH.TL and configuration register back to the scratchpad. This copy operation is automatically executed when the DS18B20 is powered on. After power on, valid data is stored in the temporary memory.
7 Read power supply mode command B4H After sending to DS18B20, send out the read time interval again, then return to the power mode: 0 is parasitic power, 1 is external power.

DS18B20 program code

/********************************************
******************DS18B20********************
*********************************************/ 
void delay_18B20(unsigned int i)//延时1微秒
{
    
    
   while(i--);
}

void ds1820rst(void)  //DS18B20复位
{
    
     
	unsigned char x=0;
	DS = 1;           //DQ复位
	delay_18B20(4);   //延时
	DS = 0;           //DQ拉低
    TR0=0;
	delay_18B20(100); //精确延时大于
    TR0=1;
	DS = 1;           //拉高
	delay_18B20(40); 
} 

uchar ds1820rd(void)//读数据
{
    
     
	unsigned char i=0;
	unsigned char dat = 0;
    TR0=0;
	for (i=8;i>0;i--)
	{
    
       
		DS = 0; //给脉冲信号
		dat>>=1;
		DS = 1; //给脉冲信号
		if(DS)
		dat|=0x80;
		delay_18B20(10);
	}
   return(dat);
}

void ds1820wr(uchar wdata)//写数据
{
    
    
	unsigned char i=0;
    TR0=0;
    for (i=8; i>0; i--)
    {
    
     
		DS = 0;
		DS = wdata&0x01;
		delay_18B20(10);
		DS = 1;
		wdata>>=1;
   }
}



uint get_temper()//获取温度
{
    
      
     
	uchar a,b;

	ds1820rst();    
	ds1820wr(0xcc);//跳过读序列号
	ds1820wr(0x44);//启动温度转换
	ds1820rst();    
	ds1820wr(0xcc);//跳过读序列号 
	ds1820wr(0xbe);//读取温度 
	a=ds1820rd();
	b=ds1820rd();
   
	tvalue=b;
	tvalue<<=8;
	tvalue=tvalue|a;
    TR0=1;
    if(tvalue<0x0fff)   tflag=0;
    else {
    
    tvalue=~tvalue+1;tflag=1;}
	  tvalue=tvalue*(0.625);//温度值扩大10倍,精确到1位小数
	  temp=tvalue;
	return temp;
}


void dis_temp(int t)//显示温度
{
    
    
	uchar d0,d1,d2,d3;
	if(tflag==0)
	{
    
    
		d0=t/1000+0x30;//百位
		d1=t%1000/100+0x30;//十位
		d2=t%100/10+0x30;//个位
		d3=t%10+0x30;//小数位
		if(d0==0x30)
		{
    
    
			wr_com(0x80+10);
			wr_data(d1);
			wr_com(0x80+11);
			wr_data(d2);
			wr_com(0x80+12);
			wr_data(0x2e);
			wr_com(0x80+13);
			wr_data(d3);
		}
		else
		{
    
    
			wr_com(0x80+10);
			wr_data(d0);
			wr_com(0x80+11);
			wr_data(d1);
			wr_com(0x80+12);
			wr_data(d2);
			wr_com(0x80+13);
			wr_data(' ');
		}
		
	}
	else
	{
    
    
		wr_com(0x80+10);
		wr_data('-');
		wr_com(0x80+11);
		wr_data(d1);
		wr_com(0x80+12);
		wr_data(d2);
		wr_com(0x80+13);
		wr_data(' ');
	}
	wr_com(0x80+14); //摄氏度符号
	wr_data(0xdf);
	temper=t/10;
}

2. LCD1602 displays temperature

Interface signal description

Insert picture description here

Basic operation steps and timing

Insert picture description here
Insert picture description here

Initialization process

Delay 15ms
write instruction 38H (do not detect busy signal)
Delay 5ms
write instruction 38H (do not detect busy signal)
Delay 5ms
write instruction 38H (do not detect busy signal)
(Each subsequent write instruction, read/write data before the operation Need to detect busy signal) Write instruction 38H: display mode setting
Write instruction 08H: display off
Write instruction 01H: display clear screen
Write instruction 06H: display cursor movement setting
Write instruction 0CH: display on and cursor setting

LCD1602 program code

/********************************************
******************LCD1602********************
*********************************************/ 
void delay(i)//延时函数
{
    
    
	uint j;
	for(i;i>0;i--)
	for(j=110;j>0;j--);
}

void wr_com(uchar ml)//LCD液晶写命令
{
    
    
	lcdrs=0;  //寄存器低电平选择指令寄存器
	P0=ml;
	delay(5);
	lcden=1;
	delay(5);
	lcden=0;

}

void wr_data(uchar shuju)//LCD液晶写数据
{
    
    
	lcdrs=1;
	P0=shuju;
	delay(5);
	lcden=1;
	delay(5);
	lcden=0;

}

void init()      //按照时序操作的初始化
{
    
    	
	lcdrw=0;     //低电平为写操作
	wr_com(0x38);//显示模式设置,设置为16*2显示,5*7点阵,八位数据口
	wr_com(0x0c);//开显示,但不开光标,光标不闪
	wr_com(0x06);//显示光标移动设置
	wr_com(0x01);// 清屏
	wr_com(0x80);// 数据指针初始化
	for(num=0;num<16;num++)
		{
    
    
			wr_data(str1[num]);//实际温度
		}
	wr_com(0x80+0x40);         //地址初始化
	for(num=0;num<16;num++)
		{
    
    
			wr_data(str2[num]);//设置温度
		}	 
}

Three, temperature control system PID

void PIDInit (struct PID *p) 
{
    
     
	memset ( p,0,sizeof(struct PID)); //用参数0初始化p
} 



unsigned int PIDCalc( struct PID *p, unsigned int NextPoint ) //PID计算
{
    
     
	unsigned int dError,Error; 
	Error = p->SetPoint - NextPoint; // 偏差 
	p->SumError += Error; // 积分 
	dError = p->LastError - p->PrevError; // 当前微分 
	p->PrevError = p->LastError; 
	p->LastError = Error; 
	return (p->Proportion * Error//比例
	+ p->Integral * p->SumError  //积分项
	+ p->Derivative * dError); //   微分项 
} 

Four, project display

Insert picture description here
Temperature control system code based on MCU AT89C52 The
above is the code of the system, you can also leave a message and send me an email.

Guess you like

Origin blog.csdn.net/a2145565/article/details/114175661