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

📄 ds18b20.c

📁 单片机为AT89S52
💻 C
字号:
#include<reg52.h>
#include<string.h>
#include <intrins.h>
#include<stdio.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
typedef unsigned char byte;
typedef unsigned int  word;
//***********************************************//
sbit DQ=P0^0;                    //温度信号输入,根据实际情况定义端口
sbit da=P2^0;
sbit clk=P2^1;
sbit adra=P2^2;
sbit adrb=P2^3;
bit compare;
uchar value;
uchar  data DD[4];
//*********函数声明****************//
void delay(word useconds);
void sdelay();
void disp(uint value);
chage1(uint temp);
chage2(uint temp);
display(uchar val);
//****************ds18b20延时***********************//
void delay(word useconds)
{
  for(;useconds>0;useconds--);
}

//********DS18B20初始化子程序***********************//
byte ow_reset(void)
{
  byte presence;
  DQ = 0;                            //pull DQ line low
  delay(29);                         // leave it low for 480us
  DQ = 1;                            // allow line to return high
  delay(3);                          // wait for presence
  presence = DQ;                     // get presence signal
  delay(25);                         // wait for end of timeslot
  return(presence);                  // presence signal returned
}                                    // 0=presence, 1 = no part

//从 1-wire 总线上读取一个字节
byte read_byte(void)
{
  byte i;
  byte value = 0;
  for (i=8;i>0;i--)
  {
    value>>=1;
    DQ = 0;                          // pull DQ low to start timeslot
    DQ = 1;                          // then return high
    delay(1);                        //for (i=0; i<3; i++); 
    if(DQ)value|=0x80;
    delay(6);                        // wait for rest of timeslot
  }
  return(value);
}

//向 1-WIRE 总线上写一个字节
void write_byte(char val)
{
  byte i;
  for (i=8; i>0; i--)                  // writes byte, one bit at a time
  {
    DQ = 0;                            // pull DQ low to start timeslot
    DQ = val&0x01;
    delay(5);                          // hold value for remainder of timeslot
    DQ = 1;
    val=val/2;
  }
  delay(5);
}

/***读取温度
char Read_Temperature(void)
{
  union{
    byte c[2];
    int x;
  }temp;

  ow_reset();
  write_byte(0xCC);                           // Skip ROM
  write_byte(0xBE);                           // Read Scratch Pad
  temp.c[1]=read_byte();
  temp.c[0]=read_byte();
  ow_reset();
  write_byte(0xCC);                           //Skip ROM
  write_byte(0x44);                           // Start Conversion
  return(temp.x/2);
}

//*********读取温度*************/
char Read_Temperature(void)
{
uchar a=0;
uchar b=0;
uint t=0;
//float tt=0;
ow_reset();
write_byte(0xCC);  //跳过读序号列号的操作
write_byte(0xBE);  //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=read_byte();
b=read_byte();
t=b;
t<<=8;
t=t|a;
//tt=t*0.0625;
ow_reset();
write_byte(0xCC); // 跳过读序号列号的操作
write_byte(0x44);   // 启动温度转换
return(t);
}
//****led显示程序*****//

char code seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
                 0x77,0x7f,0x35,0x3f,0x75,0x71}; 
void disp(uint value)
{uint temp;
 temp=value;
 if(temp>0x270f)compare=1;
   else compare=0;
 if(compare==0){
                chage1(temp);
                DD[0]=seg[DD[0]];
				DD[1]=seg[DD[1]];
				DD[2]=seg[DD[2]];
				DD[3]=seg[DD[3]];
                adra=0,adrb=0;
                display(DD[0]);
                adra=1,adrb=0;
                display(DD[1]);
                adra=0,adrb=1;
                display(DD[2]);
                adra=1,adrb=1;
                display(DD[3]);
			   }
 if(compare==1){chage2(temp);
 adra=1,adrb=1;
 display(DD[3]);
 adra=0,adrb=1;
 display(DD[2]);
 adra=1,adrb=0;
 display(DD[1]);
 adra=0,adrb=0;
 display(DD[0]);}
 } 
//***********LED延时子程序****************************//
void sdelay(void)
{uint t;
 t=0x180;
 while(t--);
}
//***************************************//
chage1(uint temp)
{
 DD[0]=(int)(temp/1000);
 DD[1]=(int)((temp-DD[0]*1000)/100);
 DD[2]=(int)((temp-DD[0]*1000-DD[1]*100)/10);
 DD[3]=(int)(temp-DD[0]*1000-DD[1]*100-DD[2]*10);
}
//****************************************//
chage2(uint temp)
{
 temp=0;
 DD[0]=0x71;
 DD[1]=0x3E;
 DD[2]=0x38;
 DD[3]=0x38;
}
//****************************************//
display(uchar val)
{uchar i;
 bit bin;
  for(i=8;i>0;i--)
      {bin=val&0x80;
	   val = val<<1; 
	   da=bin;
	   clk=0;
	   clk=1;
      }
     sdelay();
}
//**************************//
main()
{
 uint temper;
 ow_reset();
 while(1){
          temper=Read_Temperature();
	  (uint)temper=temper/16;
          disp(temper);
         }
}

⌨️ 快捷键说明

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