欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標題: DS18B20讀寫程序 [打印本頁]

作者: Kent_Cheung    時間: 2018-4-11 16:39
標題: DS18B20讀寫程序
//----------------------------------------------------------
  // 模塊名稱:DS18B20.h
  // 模塊功能:DS18B20 讀寫操作
  //----------------------------------------------------------
  sbit DS = P1^0;
  //----------------------------------------------------------
  // 函數名稱:void DelayTmp(unsigned int count)
  // 函數功能:延時
  //----------------------------------------------------------
  void DelayTmp(unsigned int count)
  {
  unsigned int i;
  while(count)
  {
  i=200;while(i》0)i--;
  count--;
  }
  }
  //----------------------------------------------------------
  // 函數名稱:void DsReset(void)
  // 函數功能:復位
  //----------------------------------------------------------
  void DsReset(void)
  {
  unsigned int i;
  DS=0;
  i=103; while(i》0)i--;
  DS=1;
  i=4;while(i》0)i--;
  }
  //----------------------------------------------------------
  // 函數名稱:bit TmpReadBit(void)
  // 函數功能:讀取一位
  //----------------------------------------------------------
  bit TmpReadBit(void)
  {
  unsigned int i;
  bit dat;
  DS=0;i++;
  DS=1;i++;i++;
  dat=DS;
  i=8;while(i》0)i--;
  return (dat);
  }
  //----------------------------------------------------------
  // 函數名稱:unsigned char TmpRead(void)
  // 函數功能:讀取一字
  //----------------------------------------------------------
  unsigned char TmpRead(void)
  {
  unsigned char i,j,dat;
  dat=0;
  for(i=1;i《=8;i++)
  {j=TmpReadBit();
  dat=(j《《7)|(dat》》1); }
  return(dat);
  }
  //----------------------------------------------------------
  // 函數名稱:void TmpWriteByte(unsigned char dat)
  // 函數功能:寫入一字節
  //----------------------------------------------------------
  void TmpWriteByte(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) //write 1
  {
  DS=0;
  i++;i++;
  DS=1;
  i=8;while(i》0)i--;
  }
  else
  {
  DS=0; //write 0
  i=8;while(i》0)i--;
  DS=1;
  i++;i++;
  }
  }
  }
  //----------------------------------------------------------
  // 函數名稱:void TmpChange(void)
  // 函數功能:溫度轉換
  //----------------------------------------------------------
  void TmpChange(void)
  {
  DsReset();
  DelayTmp(1);
  TmpWriteByte(0xcc);
  TmpWriteByte(0x44);
  }
  //----------------------------------------------------------
  // 函數名稱:unsigned int Tmp()
  // 函數功能:獲取溫度
  //----------------------------------------------------------
  unsigned int Tmp()
  {
  float tt;
  unsigned char a,b;
  unsigned int temp;
  DsReset();
  DelayTmp(1);
  TmpWriteByte(0xcc);
  TmpWriteByte(0xbe);
  a=TmpRead();
  b=TmpRead();
  temp=b;
  temp《《=8;
  temp=temp|a;
  tt=temp*0.0625;
  temp=tt*10+0.5;
  return temp;
  }
  //----------------------------------------------------------
  // 函數名稱:void RefreshTmp()
  // 函數功能:刷新溫度
  //----------------------------------------------------------
  void RefreshTmp()
  {
  TmpChange();
  DisplayTmp(Tmp());
  }






歡迎光臨 (http://m.raoushi.com/bbs/) Powered by Discuz! X3.1