DS1820 C51 子程序 (一線數據傳輸) - 免費下載

單片機編程資源 文件大小:27 K

?? 資源詳細信息

文件格式
DOC
所屬分類
上傳用戶
上傳時間
文件大小
27 K
所需積分
2 積分
推薦指數
????? (5/5)

?? 溫馨提示:本資源由用戶 zm250063191 上傳分享,僅供學習交流使用。如有侵權,請聯系我們刪除。

資源簡介

//芯片資料請到www.elecfans.com查找

//DS1820 C51 子程序
//這里以11.0592M晶體為例,不同的晶體速度可能需要調整延時的時間
//sbit DQ =P2^1;//根據實際情況定義端口

typedef unsigned char byte;
typedef unsigned int  word;

//延時
void delay(word useconds)
{
  for(;useconds>0;useconds--);
}

//復位
byte ow_reset(void)
{
  byte presence;
  DQ = 0; //pull DQ line low
  delay(29); // leave it low for 480us
  DQ = 1; // allow line to return high
  delay(3); // wait for presence
  presence = DQ; // get presence signal
  delay(25); // wait for end of timeslot
  return(presence); // presence signal returned
}     // 0=presence, 1 = no part

//從 1-wire 總線上讀取一個字節
byte read_byte(void)
{
  byte i;
  byte value = 0;
  for (i=8;i>0;i--)
  {
    value>>=1;
    DQ = 0; // pull DQ low to start timeslot
    DQ = 1; // then return high
    delay(1);  //for (i=0; i<3; i++);
    if(DQ)value|=0x80;
    delay(6); // wait for rest of timeslot
  }
  return(value);
}

//向 1-WIRE 總線上寫一個字節
void write_byte(char val)
{
  byte i;
  for (i=8; i>0; i--) // writes byte, one bit at a time
  {
    DQ = 0; // pull DQ low to start timeslot
    DQ = val&0x01;
    delay(5); // hold value for remainder of timeslot
    DQ = 1;
    val=val/2;
  }
  delay(5);
}

//讀取溫度
char Read_Temperature(void)
{
  union{
    byte c[2];
    int x;
  }temp;

  ow_reset();
  write_byte(0xCC); // Skip ROM
  write_byte(0xBE); // Read Scratch Pad
  temp.c[1]=read_byte();
  temp.c[0]=read_byte();
  ow_reset();
  write_byte(0xCC); //Skip ROM
  write_byte(0x44); // Start Conversion
  return temp.x/2;
}

立即下載此資源

提示:下載后請用壓縮軟件解壓,推薦使用 WinRAR 或 7-Zip

資源說明

?? 下載說明

  • 下載需消耗 2積分
  • 24小時內重復下載不扣分
  • 支持斷點續傳
  • 資源永久有效

?? 使用說明

  • 下載后用解壓軟件解壓
  • 推薦 WinRAR 或 7-Zip
  • 如有密碼請查看說明
  • 解壓后即可使用

?? 積分獲取

  • 上傳資源獲得積分
  • 每日簽到免費領取
  • 邀請好友注冊獎勵
  • 查看詳情 →

相關標簽

點擊標簽查看更多相關資源:

相關資源推薦