⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds18b20_4.h

📁 AT89C51的温度采集报警系统..可串口与PC通信
💻 H
字号:
			 	 	  	 #include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
 uint temp4;            // variable of temperature
sbit DS4=P1^3;        //define interface
void delay4(uchar count)
{
    while(count>0) count--;
}

void reset4(void)       //send reset and initialization command
{
  DS4=0;
  delay4(100);
  DS4=1;
  delay4(4);
  delay4(200);
}

bit read_bit4(void)       //read a bit
{
    bit temp;
    DS4=0;
    _nop_();
    DS4=1;
    _nop_();
    temp=DS4;
    delay4(200);
    return temp; 
}

uchar read_byte4(void)   //read a byte date
{

     uchar i,byte=0;
     bit j;
     for(i=0;i<8;i++)
     {
         byte=_cror_(byte ,1);
         j=read_bit4();
         if(j==0) byte=byte|0x00;
         else   byte=byte|0x80;
     }
     return byte;
}

void write_byte4(uchar command)   //write a byte to ds18b20
{
  uchar i;
  for(i=0;i<8;i++) 
    { 
      if((command & 0x01)==0) 
        { 
          DS4=0; 
          delay4(8); 
          DS4=1;
          _nop_();
        } 
       else 
          { 
            DS4=0; 
            _nop_();
            DS4=1; 
            delay4(8);
          } 
      command=_cror_(command,1);      
    } 
}

void tmpchange4(void)  //DS18B20 begin change
{
  reset4();
  write_byte4(0xcc);  //直接向18b20发送温度变换命令      
  write_byte4(0x44);  //启动18b20进行温度转换
}                       

uint tmp4()               //get the temperature
{
  float tt;
  uchar a,b;
  reset4();
  write_byte4(0xcc); //直接向18b20发送温度变换命令
  write_byte4(0xbe); //读取温度寄存器的温度值
  a=read_byte4();//读低八位
  b=read_byte4();//读高八位
  temp4=b;
  temp4<<=8;    //two byte  compose a int variable
  temp4=temp4|a;
  tt=temp4*0.0625;
  temp4=tt*10+0.5;
  return temp4 ;
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -