[51 MCU] 18b20 temperature measurement display (comprehensive design report + source program + flow chart)

1 Design goals

The temperature is obtained through the 18b20 chip, and the temperature is displayed by the dynamic digital tube.

2 main functions

Realize temperature measurement and display.

3 hardware design

 The temperature register consists of two bytes, divided into low 8 bits and high 8 bits. There are 16 in total.
Among them, bits 0 to 3 store the fractional part of the temperature value.
Bits 4 to 10 store the integer part of the temperature value.
Bits 11 to 15 are sign bits. All 0s indicate a positive temperature, all 1s indicate a negative temperature.
For the values ​​in the table, if the corresponding bit is 1, it means it exists. If the corresponding bit is 0, it means not present.

1. Single bus communication initialization

 The initialization sequence includes: the reset pulse sent by the master and the response pulse sent by the slave. The host generates a reset pulse by pulling down the single bus for 480-960μs; then the host releases the bus and enters the receive mode. When the host releases the bus, it will generate a rising edge from low level to high level. After the single-bus device detects the rising edge, it will delay for 15-60μs, and then the single-bus device will pull down the bus for 60-240μs. A reply pulse is generated. After the host receives the response pulse from the slave, it indicates that there is a single-bus device online, and the initialization is completed. Then the host can start to operate the ROM command and function command on the slave.

2. Bit write timing

 Write timing: When the host pulls the data line from logic high to logic low, the write timing gap begins. There are two kinds of write time slots: the time slot for writing 1 and the time slot for writing 0. All write time gaps must last a minimum of 60us, including a recovery time of at least 1us between two write cycles. After the level on the DQ pin goes low, the DS18B20 samples the DQ pin within a time window of 15us to 60us. If the DQ pin is high, write 1, if the DQ pin is low, write 0. To generate a write 1 time gap, the host must pull the data line to a low level and then release it, and allow the data line to be pulled to a high level within 15us after the start of the write time gap. To generate a write 0 gap, the host must pull the data line to low level and keep it for 60us.

3. Bit read timing

 When the host pulls the bus from high level to low and keeps it for at least 1us, release the bus; and read the data output from DS18B20 within 15us.

4 Flowchart

5 source program

#include <REGX51.H>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P3^7;  //定义端口 
int i;
/**************************************延时***********************************/
void delay_us(uchar n)    //延时约16微妙
{
    while(n--);
}
void delay(unsigned int t)
{
  while(t--);
}
/**************************************18b20初始化***********************************/
void DS18B20_init()
{
       DQ=1;
       delay_us(1);     //稍作延时
       DQ=0;
       delay_us(80);    //延时480到960us
       DQ=1;
       i = 0;
       while(DQ)    //等待DS18B20拉低总线
       {
           delay_us(100);
           i++;
           if(i>5)//约等待>5MS
           {
			   break;
           }    
       }
}
/**************************************写一个字节***********************************/
void write_byte(uchar dat)   //写一个字节
{
   uchar i;
   for(i=0;i<8;i++)
   {
      DQ=0;  //每写入一位数据之前先把总线拉低1us
      _nop_();
     DQ=dat&0x01;    //取最低位写入
     delay_us(10);   //延时68us,持续时间最少60us
     DQ=1;   //然后释放总线
     dat=dat>>1;    //从低位开始写
   }
   delay_us(10);
}
/**************************************读一个字节***********************************/
uchar read_byte()    //读一个字节
{
  uchar i,dat=0;
  for(i=0;i<8;i++)
  {
     DQ=0;  //先将总线拉低1us
     _nop_();
     DQ=1;  //然后释放总线
     _nop_();_nop_();
     _nop_();_nop_();
     if(DQ) dat=dat|0x80;   //每次读一位
     dat=dat>>1;       //从最低位开始读
     delay_us(10);   //读取完之后等待48us再接着读取下一个数
   }
   return dat;
}
/**************************************读温度***********************************/
uint read_temper ()
{    
   uchar a,b;         
   uint t=0;
   DS18B20_init();//初始化       
   delay_us(15);
   write_byte(0xcc);   //因只有一个18b20,所以直接跳过ROM操作命令
   write_byte(0x44);     //发送启动温度转换命令
   DS18B20_init();       
   delay_us(15);
   write_byte(0xcc);    //跳过ROM操作命令
   write_byte(0xbe);      //发送读温度寄存器命令
   a=read_byte();    //先读低八位
   b=read_byte();      //再读高八位
   t=b;        
   t<<=8;      //左移八位
   t=t|a;      //t为16位的数,使高八位为b的值,低八位为a的值  
   return t;    //返回温度值
}
/**************************************温度转换***********************************/
uint temper_change()
{
    uint temper;
    float tp;
    temper=read_temper();
    if(temper<0)    //考虑负温度的情况
    {
        temper=temper-1;
        temper=~temper;
        tp=temper*0.0625;  //16位温度转换成10进制的温度
        temper=tp*100+0.5;   //留两个小数点,并四舍五入
    }
    else
    {
        tp=temper*0.0625;  //16位温度转换成10进制的温度
        temper=tp*100+0.5;  //留两个小数点,并四舍五入
    }
    return temper;
}
/******************************************数码管控制函数***************************************/
unsigned char shuzi[]={0x3f/*0*/,0x06/*1*/,0x5b/*2*/,0x4f/*3*/,0x66/*4*/,0x6d/*5*/,0x7d/*6*/,0x07/*7*/,0x7f/*8*/,0x6f/*9*/};//显示的数字(num)的数组
 
void shumaguan(unsigned char wei,num)//第几位(wei)显示
{
 switch(wei)
 {
  case 1:P0_2=0;P0_1=0;P0_0=0;break;
  case 2:P0_2=0;P0_1=0;P0_0=1;break;
  case 3:P0_2=0;P0_1=1;P0_0=0;break;
  case 4:P0_2=0;P0_1=1;P0_0=1;break;
  case 5:P0_2=1;P0_1=0;P0_0=0;break;
  case 6:P0_2=1;P0_1=0;P0_0=1;break;
  case 7:P0_2=1;P0_1=1;P0_0=0;break;
  case 8:P0_2=1;P0_1=1;P0_0=1;break;
 }
 P3=shuzi[num];
 delay(1);
}
/**************************************动态数码管显示结果***********************************/
void xianshi(int d)
{
  int k=8,m;
  while(d!=0)
  {
    m=(d%10);//在数码管上显示个位,从后往前显示
    shumaguan(k,m);
  k--;//数码管向前移位
  d=d/10;
  }
}

/**************************************主函数***********************************/
int main()
{
  int temper;
  temper_change();
  xianshi(temper);  
}

6 Conclusion

The 18b20 chip used this time belongs to single-bus communication, which is relatively simple. Compared with the previous study, for this type of chip, you only need to understand its principle and protocol to master its usage. Later, it can be used with other chips to achieve more functions.

Guess you like

Origin blog.csdn.net/m0_57007304/article/details/131493659