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

📄 ds18b20.c

📁 温度传感器18B20的C语言底层程序
💻 C
字号:
/*************************************************************
*开始
*************************************************************/
#include "reg52.h"
#include "intrins.h"
#include "ZLG7289.h" 
#define uchar unsigned char
#define uint  unsigned int
	sbit dq = P2^0;
	bit  flag;
	uint Temperature,x;
	uchar temp_buff[9]; //存储读取的字节,read scratchpad为9字节,read rom ID为8字节
	uchar *p;
	uchar i,j,m,t0;
//	sbit	add10=P1^0;
//	sbit	sub10=P1^1;
//	sbit	add1=P1^2;
//	sbit	sub1=P1^3;
//	sbit	confirm=P1^4;
//	sbit	p15=P1^5;
//	uint	i,j,m,k,s,t1,t2,t10,t20,ge,shi,ge1,shi1;  //dtong;
//	uint cod[10]={0x0C0,0x0F9,0x0A4,0x0B0,0x99,0x92,0x82,0x0F8,0x80,0x90};
/************************************************************
*Function:延时处理
*************************************************************/
void TempDelay (uchar us)
{
  	while(us--);
}
/************************************************************
*Function:18B20初始化复位
*************************************************************/
void Init18b20 (void)
{
 	dq=1;
 	_nop_();
 	dq=0;
 	TempDelay(86);   //delay 530 uS//80
 	_nop_();
 	dq=1;
 	TempDelay(14);   //delay 100 uS//14
 	_nop_();
 	_nop_();
 	_nop_();
 
 if(dq==0)
  	flag = 1;   //detect 1820 success!
 else
  	flag = 0;    //detect 1820 fail!
 	TempDelay(20);       //20
 	_nop_();
 	_nop_();
 	dq = 1;
}
/************************************************************
*Function:向18B20写入一个字节
*************************************************************/
void WriteByte (uchar wr)  //单字节写入
{
 	uchar i1;
 for (i1=0;i1<8;i1++)
 {
  	dq = 0;
  	_nop_();
  	dq = wr & 0x01;
  	TempDelay(5);   //delay 45 uS //5
  	_nop_();
  	_nop_();
  	dq = 1 ;
  	wr >>= 1;
 }
}
/************************************************************
*Function:读18B20的一个字节
*************************************************************/
uchar ReadByte (void)     //读取单字节
{
 	uchar i2,u=0;
 for(i2=0;i2<8;i2++)
 {
  	dq = 0;
  	u >>= 1;
  	dq = 1;
  if(dq==1)
  	u |= 0x80;
  	TempDelay (4);
  	_nop_();
 }
 return(u);
}
/************************************************************
*Function:读18B20
*************************************************************/
void read_bytes (uchar nn)
{
  	uchar i3;
  	for(i3=0;i3<nn;i3++)
  	{
    	*p = ReadByte();
    	p++;
  	}
}
/**********************************************延时*/
void delay1()
{	uint n=500;
	while(n--);
}
/***********************************/
/************************************************************
*Function:读取温度
*************************************************************/
void GemTemp (void)
{
	read_bytes(2);

    Temperature = temp_buff[1]*0x100 + temp_buff[0];
//  Temperature *= 0.5;

  	Temperature/= 16;
	i=Temperature/100;
	j=Temperature%100/10;
	m=Temperature%10;
  	TempDelay(1);
}
/************************************************************
*Function:内部配置
*************************************************************/
void Config18b20 (void)  //重新配置报警限定值和分辨率
{
     Init18b20();
     WriteByte(0xcc);  //skip rom
     WriteByte(0x4e);  //write scratchpad
     WriteByte(0x63);  //上限
     WriteByte(0x00);  //下限
     WriteByte(0x1f);     //set 9 bit (0.5)
     Init18b20();
     WriteByte(0xcc);  //skip rom
     WriteByte(0x48);  //保存设定值
     Init18b20();
     WriteByte(0xcc);  //skip rom
     WriteByte(0xb8);  //回调设定值
}
/************************************************************
*Function:18B20全处理
*************************************************************/
void Result0(void)
{
   	Config18b20();
 	Init18b20 ();

 	WriteByte(0xcc);   //skip rom
 	WriteByte(0x44);   //Temperature convert
}
void Result1(void)
{
 	Init18b20 ();
	WriteByte(0xcc);   //skip rom
 	WriteByte(0xbe);   //read Temperature

 	p = temp_buff;
 	GemTemp();
}
	
/************************************************************
*Function:定时器1中断程序
************************************************************/
void Timer1 (void) interrupt 3
{	TR1=0;

	while(!(t0--))
	{
		t0=30;
       
		
		Result1();
		Result0();
	}
   
	TH1=0x00;
	TL1=0x00;
	TR1=1;	
	
}

/************************************************************
*Function:主程序
************************************************************/
void main(void)
{

//	i=20;
//	k=35;
//	s=0;
	t0=30;
//	x=9;
///	SP=0x60;
	ET1=1;
	TR1=1;
	TMOD=0x11;
	TL1=0x00;
	TH1=0x00;
	EA=1;		//开放总中断

	Delay(100);    							//延时300ms,等待ZLG7289复位完毕 
 	ZLG7289_Init(4);  							//调用ZLG7289的初始化函数 
 	Delay(100); 
 
 	ZLG7289_Test();                            //测试 
 	Delay(200); 
											//复位 
 	ZLG7289_Reset(); 
 	Delay(100);  

	

	Result0();

	while(1)
	{  
	
	    ZLG7289_Download(1,0,0,m);

		ZLG7289_Download(1,1,0,j);

		ZLG7289_Download(1,2,0,i);
		Delay(200);
	
	
	
	}
	

}










	

⌨️ 快捷键说明

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