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

📄 ds18b20.h

📁 数字温度传感器DS19B20程序源代码,只要进行简单移植就可以直接应用.
💻 H
字号:
#ifndef DS18B20_1_H
#define DS18B20_1_H

#define uchar unsigned char
#define uint unsigned int

sbit DQ1=P2^0;
bit tempflag,DS18B20F;//温度正负标志位

//unsigned char Digit[5]={0,0,0,0,0};
uchar dot1,dot2;
uchar bai,shi,ge;



/*----------------------------------------------------
*   delay N ms 延时Nms
*----------------------------------------------------*/
void Delay1ms (unsigned int Nms )//
{
	unsigned char i;
	while(Nms--)
	for(i=0; i<125; i++) ;
}
/*----------------------------------------------------
*   delay N count  延时Nus
*----------------------------------------------------*/
void Delay_Count (unsigned int Count )
{
    unsigned int temp;
	temp=Count;
    while(temp>0) temp--;
}
/*----------------------------------------------------
*   start Reset Pulse  复位
----------------------------------------------------*/
void tmreset1(void)
{
    DQ1=0;//
    Delay_Count(103);
    DQ1=1;
   	Delay_Count(4);
}

/*----------------------------------------------------
*        ACK  应答
*----------------------------------------------------*/
void temp_ack1(void)
{
    while(DQ1);
    while(~DQ1);
    Delay_Count(4);
}

/*----------------------------------------------------
*   Read a bit from 1820  读一个位
*----------------------------------------------------*/
bit tmrbit1(void)
{
    int i=0;
    bit dat;
    DQ1=0;i++;
    DQ1=1;i++;i++;
    dat = DQ1;
    Delay_Count(8);
    return dat;
}

/*----------------------------------------------------
*   Read a Byte from 1820  读一个字节
*----------------------------------------------------*/
uchar tmrByte1(void)
{
   unsigned char i,j,dat=0;
    for(i=1;i<=8;i++)
    {
      j=tmrbit1();
      dat=(j<<7)|(dat>>1);
    }
    return dat;
}

/*----------------------------------------------------
*   Write a Byte from 1820  写一个字节
*----------------------------------------------------*/
void tmwByte1(unsigned char dat)
{
  signed char  i=0;
  uchar j;
  bit testb;
  for(j=1;j<=8;j++)
  {
    testb=dat & 0x01;
    dat = dat>>1;
    if(testb)
    {
      DQ1=0;
      i++;i++;
      DQ1=1;
      Delay_Count(8);
    }
    else
    {
      DQ1=0;
      Delay_Count(8);
      DQ1=1;
      i++;i++;
    }
  }
}

/*----------------------------------------------------
*   send convert command to  1820   开始转换温度
*----------------------------------------------------*/
void temp_start()
{
  DS18B20F=1;
  tmreset1();		//复位
  temp_ack1();		//应答
  Delay1ms(1);		//延时1ms
  tmwByte1(0xcc);	//跳过ROM匹配
  tmwByte1(0x44);  	//写一个字节命令
}

/*----------------------------------------------------
*   Read temperature from  1820  读出温度
*----------------------------------------------------*/
uint Read_temp(void)
{
   uchar a,b;
   uint y,c,y1; 
   tmreset1();     //复位
   temp_ack1();    //应答
   Delay1ms(1);   //延时
   tmwByte1(0xcc); //跳过ROM匹配
   tmwByte1(0xbe);  //写命令0xbe 读温度
   a = tmrByte1();  //读温度低位
   b = tmrByte1();  //读温度高位
   c = b;
   y1=(c<<8)|a;
   if(y1>=2048)		//数据高五位不为0则数据为负
	{
	  y=65536-y1;  	//温度为负时,将其转换为原码
	  tempflag=1;      //1为负温度
	}
	else
	{
	  y=((unsigned int)b)<<8;//高位8位右移8位送y
      tempflag=0;     //0为正温度
	}
   return ((y+a)&0x7ff) ;//返回11位数据
}


/*----------------------------------------------------
*    将读出的温度进行处理,以便显示
*----------------------------------------------------*/
void Temp_process()
{
  unsigned int a,b;
  unsigned char dot;
  a=Read_temp();
  dot=(a&0x0f)*6.25;
  dot1=dot/10;
  dot2=dot%10;
  b=a>>4;
  bai=b/100;
  shi=(b%100)/10;
  ge=b%10;
}
//--------------   显示函数    ---------------
disp_temp()//
{
    WriteInstruct(0x8a);
	if(tempflag) WriteData('-'+'0');
	WriteData(bai+'0');
	WriteData(shi+'0');
	WriteData(ge+'0');
	WriteString(".");
	WriteData(dot1+'0');
	WriteData(dot2+'0');
}
#endif

⌨️ 快捷键说明

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