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

📄 qds18b20.c

📁 18B20程序
💻 C
字号:
 /********************************************************************************************/
typedef unsigned char      uint8;                // 无符号8位整型变量
typedef signed   char      int8;                 // 有符号8位整型变量
typedef unsigned short     uint16;               // 无符号16位整型变量
typedef signed   short     int16;                // 有符号16位整型变量
typedef unsigned int       uint32;               // 无符号32位整型变量
typedef signed   int       int32;                // 有符号32位整型变量
typedef float              fp32;                 // 单精度浮点数(32位长度)
 /********************************************************************************************/
sbit DQ = P1 ^ 3;            //不能在主程序中,只能在此定义DS18B20的单总线                                              
/********************************************************************************************
* 函数名称:DS18B20_Delay()
* 功    能:DS18B20软件延时专用
* 入口参数:count    延时参数,值越大,延时越长
* 出口参数:无
*********************************************************************************************/
void DS18B20_Delay(uint16 count)//延时函数
{
  while(count--);
}

/********************************************************************************************
* 函数名称:DS18B20_Init()
* 功    能:DS18B20初始化
* 入口参数:无
* 出口参数:返回值为1则初始化成功
*           返回值为0则初始化失败
*********************************************************************************************/
uint8 DS18B20_Init(void)
{
	uint8 x = 0;

	DQ = 1;                                      // DQ复位
	DS18B20_Delay(8);                            // 稍做延时

	DQ = 0;                                      // 单片机将DQ拉低
	DS18B20_Delay(80);                           // 精确延时,大于480us

	DQ = 1;                                      // 拉高总线
	DS18B20_Delay(14);                           // 稍做延时后

	x = DQ;                                      // 若x=0则初始化成功,若x=1则初始化失败
    DS18B20_Delay(20);

	return (~x);
}

/********************************************************************************************
* 函数名称:DS18B20_Read1Byte()
* 功    能:从DS18B20读一个字节
* 入口参数:无
* 出口参数:从DS18B20读出的一个字节
*********************************************************************************************/
uint8 DS18B20_Read1Byte(void)
{
  uint8 i = 0;
  uint8 dat = 0;

  for(i=8;i>0;i--)
	{
  	  DQ = 0; // 给脉冲信号
  	  dat >>= 1;
  	  DQ = 1; // 给脉冲信号

	  if(DQ) dat |= 0x80;
  	  DS18B20_Delay(4);
	}

	return (dat);
}

/********************************************************************************************
* 函数名称:DS18B20_Write1Byte()
* 功    能:向DS18B20写一个字节
* 入口参数:dat  要写入DS18B20的一字节数据
* 出口参数:无
*********************************************************************************************/
void DS18B20_Write1Byte(uint8 dat)
{
  uint8 i = 0;

  for(i=8;i>0;i--)
	{
  	  DQ = 0;
  	  DQ = dat&0x01;
  	  DS18B20_Delay(5);

  	  DQ = 1;
  	  dat>>=1;
	}
}

/********************************************************************************************
* 函数名称:Read_Temperature()
* 功    能:读取并显示温度
* 入口参数:无
* 出口参数:无
*********************************************************************************************/

uint8 idata curtemp[4]={0x00,0x02,0x06,0x09}; //26.9
//uint32 a = 0, b = 0, c = 0; 
void Read_Temperature(void)
{
  uint32 t = 0;
  fp32 tt = 0;

  DS18B20_Init();
  DS18B20_Write1Byte(0xCC);                      // 跳过读序号列号的操作
  DS18B20_Write1Byte(0x44);                      // 启动温度转换

  DS18B20_Init();
  DS18B20_Write1Byte(0xCC);                      // 跳过读序号列号的操作
  DS18B20_Write1Byte(0xBE);                      // 读取温度寄存器

  curtemp[1] = DS18B20_Read1Byte();
  curtemp[2] = DS18B20_Read1Byte();

  t = curtemp[2];
  t <<= 8;
  t = t | curtemp[1];

  tt = t * 0.0625;
 t = tt * 10 + 0.5;                             // 放大10倍输出并四舍五入

  curtemp[1] = t / 100;                                   // 十位
  curtemp[2] = t / 10 - curtemp[1] * 10;                     // 个位 
  curtemp[3] = t - curtemp[1] * 100 - curtemp[2] * 10;   // 小数位
  
 }

⌨️ 快捷键说明

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