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

📄 ds18b20.c

📁 lyd-sy-5100单片机开发板的示例程序 分为C语言版本和ASM汇编版本。其他牌子的板子也可以使用其C语言版本。
💻 C
字号:
/*******************************************************************************
*  标题:  启东微芯LJD-SY-5100单片机开发系统演示程序                            *
*  文件:  DS18B20.C                                                            *
*  日期:  2005-7-12                                                            *
*  版本:  1.0                                                                  *
*  作者:  启东微芯                                                             *
*  网站: http://www.ljd-2008.com                                              *
********************************************************************************
*  描述:                                                                       *
*         控制DS18B20完成芯片的控制和数据传输                                  *     
*                                                                              *
*                                                                              *
********************************************************************************
* 【版权】 Copyright(C)微芯科技 http://www.ljd-2008.com    All Rights Reserved *          
* 【声明】 此程序仅用于学习与参考,引用请注明版权和作者信息!                  *
*******************************************************************************/
#include<reg51.h>
#include<absacc.h>
#include<stdio.h>
#include<math.h>

#define uchar unsigned char
#define uint unsigned  int
sbit P3_5=P3^5;

uchar TEMP;                    //温度值的变量
uchar flag1;                   //结果为正或为负的标志位

void delay(unsigned int count)
{
  unsigned int i;
  while(count)
  {
    i=200;
    while (i>0)i--;
    count--;
 }
}


void dsreset(void)                //发送复位和初始化
{
  unsigned int i;
  P3_5=0;
  i=103;
  while(i>0)i--;
  P3_5=1;
  i=4;
  while(i>0)i--;
}

bit tmpread(void)                  //读去数据的一位
{
 unsigned int i;
 bit dat;
 P3_5=0;i++;
 P3_5=0;i++;i++;                   //延时
 dat=P3_5;
 i=8;while (i>0)i--;
 return(dat);
}


unsigned char tmpread2(void)        //读一个字节
{
  unsigned char i,j,dat;
  dat=0;
  for(i=1;i<8;i++)
  {
    j=tmpread();
    dat=(j<<7)|(dat>>1);   

}
return(dat);

}


void tmpwrite(unsigned char dat)     //写一个字节
{ 
 unsigned int i;
 unsigned char j;
 bit testb;
 for (j=1;j<=8;j++)
 {
   testb=dat&0x01;
   dat =dat>>1;
   if (testb)
  {
    P3_5=0;
    i++;i++;
    P3_5=1;
    i=8;while(i>0)i--;
}
else 
{
  P3_5=0;
  i=8;while(i>0)i--;
  P3_5=1;
  i++;i++;
   }
  } 
} 

void tmpchange(void)
{
  dsreset();
  delay(1);
  tmpwrite(0xcc);
  tmpwrite(0x44);
  
}

void tmp (void)
{
  unsigned char a,b;
  dsreset();
  delay(1);
  tmpwrite(0xcc);
  tmpwrite(0xbe);
  a=tmpread2();
  b=tmpread2();
  flag1=b;
  if(flag1)
  {
    TEMP=~a+1;
}
else
{
  TEMP=a;
}

}
 rom()
{
  uchar i;
  uchar sn1;
  uchar sn2;
  dsreset();
  delay(1);
  tmpwrite(0x33);
  //sn1=tmpwrite();
  //sn2=tmpwrite();

}



main()
{
  do {
       tmpchange();
       delay(200);
       tmp();
  }
while(1);
}

⌨️ 快捷键说明

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