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

📄 18b20.c

📁 自动温度监测系统
💻 C
字号:
/*****************************************************************
*	浙江大学仪器系短学期实习项目:
*	电仪2004级
*       小组成员:韩灿 刘清文赵岩 许琳燕
*
*	文件名 	:
*	开发人  :韩灿
*	开发时间:2007-8-20
*	版本   	:
*	修改者  :  
*	修改时间:
*	修改说明:
******************************************************************/
#include<18b20.h>
#include<7279.h>



void init18b20(void)
{
	unsigned char x=0;
	DQ=1;
	_nop_();
	DQ=0;
	delay15us(36);  //延时480us
	DQ=1;
	delay15us(4);; //延时60us
	x=DQ;
	DQ=1;
	delay15us(18); //延时270us
}



void write18b20(unsigned char ch)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ=0;
  		delay15us(1);    //首先将DQ拉低,并延时15us
  		DQ=ch&0x01;      //从低8位开始写入
  		ch=ch>>1;        //数据右移一位
  		delay15us(4);    //写入数据后并延时45us以上
  		DQ=1;
}
}



unsigned int read18b20()
{
	unsigned char i;
	unsigned char ch=0;
	for(i=0;i<8;i++)
	{DQ=0;
  	ch=ch>>1;
  	DQ=1;
  	delay15us(1);
  	if(DQ==0) {ch=ch&0x7f; }
  	else     {ch=ch|0x80; }
  	delay15us(3);
  //DQ=1;
}
return(ch);
}



delay15us(unsigned char n)
{  
	_nop_();
   	for(;n>1;n--){_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
}

/******
*读温度函数
*******/

unsigned char readtemp(unsigned char *z)
{
	unsigned char t,x,y;
  	init18b20();
 	write18b20(0xCC);      //跳过skip rom
 	write18b20(0x44);      //启动温度转换
 	init18b20();
  	write18b20(0xCC);      //跳过多路传感器识别 skip rom
  	write18b20(0xBE);      //读暂存器,
  	x=read18b20();         //返回读出温度值  16位补码形式
  	y=read18b20();
 	*z=x;
 	*z=*z&0x0f;
  	*z=*z*10/16;
  	x=x>>4;
  	y=(y&0x07)<<4;
  	t=x|y;
  	return t;
}


/*************************************
*** 显示温度函数
*************************************/
/*showtemp()
{ 
	unsigned char t,x,y,*z;
    t=readtemp(z);
    if(t>0)
    {
		x=t/10;
        t=t%10;
        y=t/1;
        //z=t%1;
      //  write(DECODE0,0x00);
        write(DECODE0+1,x);
        write(DECODE0+2,0x80+y);
     }
     else   
	 {
	 	t=~t;
        t=t+1;
        x=t/10;
        t=t%10;
        y=t/1;
        //z=t%1;
        write(DECODE0,0x0a);
        write(DECODE0+1,x);
        write(DECODE0+2,0x80+y);
      }
}*/

⌨️ 快捷键说明

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