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

📄 ds18b20.c

📁 DS18B20源代码
💻 C
字号:
/******************************************************
*  name: ds18b20.c
*  author: panda
*  date: 2006.12.9
*  decribe: how to use the ds18b20
*  output: the temprature (JTAG/USART)
******************************************************/

#include <avr/io.h>
//#include <macros.h>
#include "delay.h"
#include "bit_set.h"
#define BIT(x)	(1<<(x))
#define NOP()	asm("nop")
#define WDR() 	asm("wdr")


static unsigned int temprature;  //the temprature,
//static unsigned char ds_status;

/*****************************************************
* initalize the ds18b20
*       总线拉低   --------------   ds拉低
*    -------------    ds拉高      ---------- 总线电平状态
******************************************************/
unsigned char reset_ds18b20(void)
{
  unsigned char flag = 0;     //flag the status of the reset processing
  RLS_DS18B20;            //主机放弃总线控制一段时间
  asm("nop");
  HLD_DS18B20;            //控制总线
  CLR_DS18B20;            //拉低总线
  delay_xus(255);
  delay_xus(255);         //根据时序图,拉低总线480us-960us
  RLS_DS18B20;            //放弃总线,
  asm("nop");

  do
  {
     delay_xus(6);
     flag++;
     if (flag == 16)
     return(0x00);
  }while(STU_DS18B20);     //如果数据线,经过了100us未被拉高,证明复位失败
  
  flag = 0 ;               //标志位清0
  
  do
  {
     delay_xus(6);
     flag++;
     if (flag == 25)
     return(0x00);
  }while(!(STU_DS18B20));  //数据线会被DS18B20拉低,如果经过了150us为拉低,证明失败

 return(0x01);
}


/**********************************************************
*  read a byte from ds18b20
*  先强制拉低,进入工作状态
**********************************************************/
unsigned char byte_from_ds18b20(void)
{
 unsigned char temp;     //读一个字节需要8次读取
 unsigned char get_temp = 0;  //存储读取到的字节
 RLS_DS18B20;            //释放总线
 for(temp=0;temp<8;temp++)
 {
  get_temp>>=1;
  HLD_DS18B20;	//Maga16控制总线
		CLR_DS18B20;	//强制拉低
		delay_xus(10);
		RLS_DS18B20;		//释放总线,DS18B20会将总线强制拉低
  delay_xus(3);

  if(STU_DS18B20)
		get_temp|=0x80;
		delay_xus(30);	 	//30us
		RLS_DS18B20;		//释放总线
		delay_xus(30);	 	//30us
 }
 delay_xus(1);
 return(get_temp);
}

/*************************************************************
* write a byte to ds18b20
*************************************************************/
unsigned char byte_to_ds18b20(unsigned char send_char)
{
 unsigned char temp;     //发一个字节需要8次总线操作
 unsigned char send_temp = 0;  //存储要发送的字节
 RLS_DS18B20;            //释放总线
 for(temp=0;temp<8;temp++)
 {
  HLD_DS18B20;
  CLR_DS18B20;
  delay_xus(15);
  send_temp = send_char>>temp;
  send_temp &= 0x01;
  if(send_temp)
  RLS_DS18B20;
  else
  CLR_DS18B20;
  delay_xus(45);
  RLS_DS18B20;
  delay_xus(1);
 }
 return 0x01;
}

/***********************************************************
* get the temprature
***********************************************************/
unsigned int get_the_temprature(void)
{
 unsigned char h_temp,l_temp;   //分别储存温度的高位和低位

 reset_ds18b20();
	byte_to_ds18b20(0xcc); 	//由于总线上只有一个设备,可以不用读ID,所以不设置ROM
	byte_to_ds18b20(0x44);		//启动温度转换过程
	delay_xus(1);

	reset_ds18b20();
	byte_to_ds18b20(0xcc);
	byte_to_ds18b20(0xbe);		//读数据


 l_temp=byte_from_ds18b20();
	h_temp=byte_from_ds18b20();
	temprature=(h_temp<<8)|l_temp;
	return temprature;
}

⌨️ 快捷键说明

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