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

📄 ds18b20显示温度.c

📁 DS18B20程序传给有用的人看阿拉丁及为激发沃尔夫奇偶数肯德基罚款
💻 C
字号:
/*====功能:DS18B20测温,用LED显示,实时扫描=====*/
/*====作者:张贺平=====*/
/*====时间:20080331=====*/

#pragma db code
#include<reg51.h>
#include <intrins.h> // _nop_()function
#define NOP _nop_( )

#define uchar unsigned char
#define uint  unsigned int

sbit PORT_BIT_1820=P1^6;
/*=====数码管位定义=====*/ 
sbit LED_0=P1^0;
sbit LED_1=P1^1;
sbit LED_2=P1^2;
sbit LED_3=P1^3;
sbit DB=P0^7;

/*=====数码管码定义0,1,2,3,4,5,6,7,8,9==共阳接法=====*/
uchar a[ ]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8, 
            0x80,0x90}; 
              
uchar one,two,three,four;      //定义全局变量

/**************子函数声明****************/
void wr_1820(uchar  val);
uint get_tmpr(void);
void init_1820(void);
uint rd_1820(void);
void delay(uint t);
void start(void);
void display(void);
void led_analyze(uint i);

void delay(uint t)	//延时
{
  uint i;
  for(i=0;i<t;i++);		 
}  

//********************************************
void start(void)
{
   init_1820();
   wr_1820(0xcc);
   wr_1820(0x44);
}

uint get_tmpr(void)
{
   uint tmpr;
   	
   EA=0;
   init_1820();
   wr_1820(0xcc);
   wr_1820(0x44);
   delay(250);
   init_1820();
   wr_1820(0xcc);
   wr_1820(0xbe);
   tmpr=rd_1820();
 /*  init_1820();
   wr_1820(0xcc);
   wr_1820(0x44);   /* start convert */
   EA=1;
   return tmpr;
}

void init_1820(void)
{  uchar  i;
   PORT_BIT_1820=1;       //1.6us
   PORT_BIT_1820=0; /* 1--8+7us */
   for(i=0;i<100;i++);/* delay 400--960us */  //300us
     PORT_BIT_1820=1;
   for(i=0;i<200;i++)  /* delay 15--60us */
    { if (PORT_BIT_1820==0) break;}
   for(i=0;i<=200;i++)
    { if (PORT_BIT_1820==1)  break;}
   for(i=0;i<60;i++); /* delay 480us */
}

void wr_1820(uchar val)
{
   uchar i,j;
   PORT_BIT_1820=1;  //1.6us
   NOP;              //3.6us
   for(j=0;j<8;j++)
    { 
	 PORT_BIT_1820=0;
     NOP;
	 NOP;
	 NOP;
	 NOP;
     NOP;  /* delay 5us  */
     if ((val&0x01)==1)
       PORT_BIT_1820=1;
     else
       PORT_BIT_1820=0;
     for(i=0;i<30;i++)NOP;    /* delay 56 us */   //20.6us
       PORT_BIT_1820=1;
       val=val>>1;
   }
}
uint rd_1820(void)
{ 
   uchar  j,i,k,tt[9],vv;
   float tmpr;
   vv=0;
   for(k=0;k<8;k++)
     {  
	  for(j=0;j<8;j++)
       { 
	     PORT_BIT_1820=0;
         NOP;NOP;NOP;NOP;NOP;
         PORT_BIT_1820=1;
         vv=vv>>1;
         if(PORT_BIT_1820) vv=vv|0x80;
         for(i=0;i<60;i++)NOP;    /*delay 120 us */
         PORT_BIT_1820=1;
        }
       tt[k]=vv;
      }
   i=tt[1]&0xf8;
   if( (i!=0)&&(i!=0xf8)) return 0x000;
   tmpr=tt[1]*256.0;
   tmpr=(tmpr+tt[0])/16.0;
   if(tmpr>100.0 ) return 0x000;
   return  tmpr*10;
}

/*====显示函数=====*/ 
void display(void)
{
  //P0=a[one];     //调用显示数据 
  //LED_0=0;       //显示开 				/*也可以先开显示再调用显示数据
  //delay(100);    //延时 
  //LED_0=1;       //关显示 
  P0=a[two];
  LED_1=0;
  delay(150);
  LED_1=1;
  
 
  P0=a[three];     //若只需两位显示,此上程序段删除即可
  DB=0;
  LED_2=0;
  delay(150);
  LED_2=1;
  DB=1;

  P0=a[four];
  LED_3=0;
  delay(150);
  LED_3=1; 
}

/*====分解显示数据=====*/ 
void led_analyze(uint i)
{	//i=i*10;
    //one=i/1000;
    two=(i/100)%10;
    three=(i%100)/10;			 //用四位一体数码管中间两位显示
    four=i%10;
}
main()           //主程序
{
    init_1820();
    wr_1820(0x33); 
	while(1)
   {
    //uchar ds[8];							

    start();
    led_analyze(get_tmpr());
    display( );
    }  
}

⌨️ 快捷键说明

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