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

📄 text1.c

📁 ds18b20实验
💻 C
字号:
#include"reg51.h"
#include <absacc.h>
#include <intrins.h> 
#define uchar unsigned char
#define uint unsigned int

sbit rs=P1^6;
sbit rw=P1^5;
sbit ep=P1^4;
sbit DQ=P3^6;
unsigned char DS18B20_F=0; 

uchar code table[]={"0123456789"};

unsigned char x=0;

void delay(unsigned int i)//延时函数
{
  while(i--);
}


void xmjc(void)	  //闲忙检测
{
  rs=0;
  rw=1;
  ep=1;
  while(P2&0x80)
  ;
  ep=0;
}


void xml(unsigned char comm)  //写命令
{
  xmjc();
  rs=0;
  rw=0;
  ep=1;
  P2=comm;
  ep=0;
}

void xsj(unsigned char dat)
{
 xmjc();
 rs=1;
 rw=0;
 ep=1;
 P2=dat;
 ep=0;
}
void lcd_init(void)	 //1602初始化
{
  xml(0x38);
  xml(0x06);//光标和显示模式设置
  xml(0x0c);
  xml(0x01);
}


void aa(uchar str,uchar lengch)
{
  uchar i;
  for(i=0;i<lengch;i++)
  {
     xsj(str);
   }
}



//unsigned char templ=0;
//unsigned char temph=0;
//float temperature ;
//sbit LED1=P1^3;
//sbit LED2=P1^2;
/*********************************************************************
函数功能:延时子程序
入口参数:time
出口参数:
**********************************************************************/

void wait1(unsigned int j)
{   while(j--);
}								   

/************************
 初始化
*************************/
void Init_DS18B20(void)                  //复位成功时返回1,否则返回0
{   //unsigned char n=255;
    DQ=1;
	_nop_();
	_nop_();
    DQ=0;                                         //主机拉低1-Wire总线,延时540us产生复位信号
    wait1(60);
   // wait1(180);
   // wait1(180);
    DQ=1;                                         //主机释放1-Wire总线,延时60us等待DS18B20应答
    wait1(2);
    
   DS18B20_F=DQ;
   wait1(50);
}
 /******************************************************************
 函数功能:数码管显示子程序
 入口参数:
 出口参数:

 
void display1(int k)
{
  P1=0xfe;
  P2=tab[k/1000];	
  wait1(100);
  P1=0xfd;
  P2=tab[k%1000/100];
  wait1(100);
  P1=0xfb;
  P2=tab[k%100/10];
  wait1(100);
  P1=0xf7;
  P2=tab[k%10];
  wait1(100);  
 }
 void LedDisp(unsigned char disp)//温度显示子程序
{    unsigned int time=0;
     while(time<1000)
       {
         P2=tab[disp/10];
         //LED1=1;
         //LED2=0;
		 P1=0xf8;
         wait1(30);
         P2=tab[disp%10];
		 P1=0xf9;
         //LED1=0;
        // LED2=1;
         wait1(30);
         ++time;
      }
}  

*********************************************************************/ 
 /*********************************************************************
 函数功能:向ds18b20读一个字节数据
 入口参数:
 出口参数:dat
 *********************************************************************/


 unsigned char Readonechar(void)
{   unsigned char i=0,c;
    c=0;
    for(i=8;i>0;i--)
    {
	  DQ=1;
	  _nop_();_nop_();
	  DQ=0; 
	  _nop_();_nop_();                                   //主机拉低1-Wire总线,延时2us
      //wait1(2);
      c>>=1;
      DQ=1;                             
      //wait1(2);
      if(DQ) 
	    c|=0x80;
       wait1(7);
     }
    return(c);
}  
 /**********************************************************************
 函数功能:向ds18b20写一字节的数据
 入口参数:dat
 出口参数:
 **********************************************************************/

 void Writeonechar(unsigned char c)
{  unsigned char  i;
    for(i=8;i>0;i--)
        {
		 DQ=0;                                    //主机拉低1-Wire总线,延时2us
         
		 DQ=c&0x01; 
		 wait1(6);                            	  //延时60us等待DS18B20采集数据
         DQ=1;                                    //主机释放总线
        
         c>>=1;                             	    
        }
		_nop_();_nop_();
		//wait1(7);
}
 /***********************************************************************
 函数功能:向ds18b20读温度值
 入口参数:
 出口参数:temperature
************************************************************************/
 Readtemperature(void)
{ unsigned char temph=0,templ=0;
  unsigned int temperature=0;
  float temp=0;
  Init_DS18B20();
  if(!DS18B20_F)  
  {
	//DS18B20_F=0xff;
    Writeonechar(0xcc);
    Writeonechar(0x44);
 	//wait1(50000);
	//wait1(50000);
	 Init_DS18B20();
	 if(!DS18B20_F) 
	  {
	 	Writeonechar(0xcc);
        Writeonechar(0xbe);
        templ=Readonechar();
        temph=Readonechar();
		temperature=temph;
		temperature<<=8;
		temperature=temperature|templ;
		temp=temperature*0.0625;
		temp=temp+0.5;
		//temperature=temp+0.5;
		//temperature=temperature/100;
		//temperature=((temph*256)+templ)*0.0625;
		//return(temperature);
		return(temp);
	  }
    
     	//DS18B20_F=Init_DS18B20();
    	//while(DS18B20_F==0xff);
	    //DS18B20_F=0xff;
    
 								  
    						  
   }
}


/***********************************************************************
函数功能:主程序
入口参数:
出口参数:

 void main()
{
unsigned char i=0;

   while(1)

   {
   	 i=Readtemperature();
	 LedDisp(i);
   }
}
************************************************************************/  
  
  
 

void main()//主程序
{
  unsigned char wendu=0,m, n;

 lcd_init();
  while(1)
  {
  wendu=ReadTemperature();//读温度
   m=wendu/10;
   n=wendu%10;
   xml(0x82);
   aa(table[m],1);
   xml(0x83);
   aa(table[n],1);         //显示温度值
  }
}

⌨️ 快捷键说明

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