DS18B20 1-Wire Digital Thermometer

No Comments

參考資料

簡介

  • -0.5V to 6V gpio
  • 3.0V to 5.5V Vdd
  • needs 1.5mA for sampling and conversion。and also the eeprom write(it takes 2 to 10ms)
  • Tsc,sampling and conversion time/max:9bit(93.75ms)10bit(187.5ms)11bit(375ms)12bit(750ms)
  • -55’C to 125’C
  • +/- 0.5’C 的精度於 -10’C to 85’C。餘區間最大誤差 2’C
  • 9 bit to 12 bit 數位攝氏精度(default 12 bit)
  • 可設定上下界超標警示,連同精度設定皆可存於 eeprom
  • 外部及寄生兩種供電模式。當使用寄生電源時將 Vdd 接地
  • single-drop & multi-drop 支援,即,可超過一個 device 共存於此 bus 上。
  • 64-bit device 序號,MSB2LSB 是 8-bit CRC + 56-bit unique serial number(56 = 48-bit unique + 8-bit family code[28h])

Block diagram

軟體用組件

  • 如圖 figure 3,
  • eeprom 部份,包含了 64-bit device id,alarm threshold TH/TL,及精度設定。為非揮發部份
  • scratchpad memory(sram)中,temperature sensor 佔 2 bytes,如 figure 4。餘皆佔 1 byte。TH/TL 若不使用可轉為一般存儲用途
  • 故可知,有單寫 sram 指令,及從 sram 到 rom 的寫入(copy scratchpad [48h])等獨立的指令
  • device 被 power on 後,eeprom 相關的 data 會自動被回存至 sram 中
  • 當精度不同時,temperature register 的使用情況如下,
    1. 12bit,全用上
    2. 11bit,bit0 undefined
    3. 10bit,bit0 bit1 undefined
    4. 9bit,bit0 bit1 bit2 undefined
    5. 唯有於 master 每發出一次 convert command [44h] 後(max 10us 開始取樣動作) temperature register 內容才會被更新一次。在此 command 後,master 可再發出一個 read time slot,以收 response,1 表完成,0 表尚取樣轉換中;此 response 前提僅適用於外部供電。因為寄生供電的狀況時,在 convert 發出後時限內就須轉為強上拉供電,在此前提下 response 的狀態,是無法被拉 low 的。
    6. 承上,一旦取樣轉換完畢,並且 TH/TL 有被使用,則 device 還會做比對以設定 alarm 狀態;當 >= 或 <= 時被設定。因此在查詢 alarm 前最好先測溫一次。注意 TH/TL 是帶號 8-bit,所以比對僅就所對應的 [temp] 暫存器的 bit
    7. 最後注意 [temp]/TH/TL 數值的表示,其為非浮點之帶小數;正數沒問題,照著 figure 4 表,此時 signs 為 0。當負數時,其是由正數變補加 1 所成;所以若無適當函式以轉換,就將該負數數值減一後變補即成正數,截位成整數小數後,再自己加上負號。反之例如欲設定 TL=-10(無小數因為 8bit),則將 10 表示出來後,變補加一即成。

供電源

  • master 如何探知使用外部或寄生電源,下以下 commands:skip rom[CCh] -> read power supply[B4h] -> read time slot,得到 0 是寄生電源,1 是外部電源

Memory map

Configuration register

CRC 校驗

  • 使用 CRC8(8/5/4/0),有兩筆,分別對於 device id 及 scratchpad,如前圖
  • 就 device id 而言是固定不變的,就 scratchpad 則每次資料更新/暫存器值被更動,便會重新計算回填一次
  • 筆者未查明 arduino 是否有提供 crc functions,及也對 crc 不熟,且它僅用於校驗更正,故筆者略過它不用

1-wire bus 協定之時序摘要

  • 訊號種類
    1. reset pulse, presence pulse, write 0, write 1, read 0, and read 1
  • 初始化程序/initialization
    1. reset pulse by the master then presence pulse by the devices
  • 除了 presence pulse 是由 devices 所回應,其餘都是由 master 所發起
  • reset pulse
    1. master 至少拉 low 480us(Tx),後放掉進入 Rx (input)狀態
    2. 注意此 reset pulse 也適用於全局普適狀況;只要拉 low 超過 480us,所有 bus 上的 devices 都會被 reset
  • presence pulse
    1. 當 devices 偵測到(reset pulse 及)上緣階段,便再等待 15 至 60us 後,便拉 low 60 至 240us 後會放掉
    2. 而 master 於此 Rx 階段需至少等待 480us,於此期間便可偵測是否有 devices 存在
    3. 因此從 master 放掉 bus 算起:除了上緣時期,最快 15us,最慢 60us,bus 便會被拉 low;最慢 300us bus 回 high
    4. 因上緣時期非零,故我們在 master 放掉 bus 算起,master 最慢 15us 後就需已開始偵測下緣,最快 60us 才可以看低準位,最慢 75us 看上緣,最快 300us 看高準位。
    5. 此期間佔時至少 480us,master 不僅要知道 bus 有被拉 low,也要確認有回復 high。
  • The read/write time slots
    1. The bus master writes data to the DS18B20 during write time slots and reads data from the DS18B20 during read time slots
    2. One bit of data per time slot
    3. 一個 write/read time slot 至少佔時 60us,duration。並還需要有一個 recovery time 至少 1us,用以間隔每個 write/read time slot。此 recovery time 被定義成可以無限長
  • write time slots
    1. write 1 time slot:master 拉 low,最慢 15us 就須釋放,最快 1us 後
    2. write 0 time slot:master 拉 low,最快 60us 才能釋放,不需超過 120us 才釋放
    3. spec 說道 devices 從偵測到下緣後 15us 才開始取樣至 60us 前結束
  • read time slots
    1. devices 只有當 master 發起 read time slots,才會做 transmit 動作
    2. 所謂發起 read time slot,便是 master 發送特定指令完成後的動作,
    3. Read Scratchpad [BEh]
    4. Read Power Supply [B4h]
    5. Convert T [44h]/有延遲。note:有 response 適用於外部供電;若使用 parasite power 則不能使用 response
    6. Recall E [B8h]/有延遲
    7. read time slot,master 拉 low 後,最快 1us 便釋放 bus/應盡快,以讓 devices transmit。因如果拉 low 過久才釋放,將會與 device transmit 動作衝突,因,spec 說道 data 從 master 拉 low 後 15us 內為有效值
    8. 因 read data 有些需要時間,故可適當延遲後再做 read time slot
  • 注意,所有對 devices 的每一次行為,都必須以 initialization 作為起始,並且是固定的此三步驟:(除了這兩支 commands:search rom[F0h],search alarm[ECh];且每一次 cycle 結束後需重下 initialization)
    1. initialization,齊步走
    2. rom commands,用以尋找與指定 device
    3. function commands,對該 device 作用及存取
  • 注意,規定 byte-wise LSB first,bit-wise LSb first

Rom commands

  • search rom[F0h]
    1. 使用 weak-pullup AND op 及消去法逐一列舉出所有 devices(並記錄下來);每一次 cycle 中,逐 bit 發二次 read time slot,devices 會依序將它的該 bit 及 反向 bit 送出(拉/不拉 bus),接著 master 再發出一次 write time slot 以消去不符合者(不符者將成 idle 直至 reset)。兩次 read time slots 當中,weak-pullup AND 所成的結果 11 不可能/表沒有 device,01 與 10 表所有皆符合,00 表至少有一 device 的該 bit 與其他 devices 的相異。每完成 1 cycle(至少 64-8=54 bits)我們就可取得一支 device id,並就必須再 initialization 一次走下個 cycle
    2. 其次,我們說,在第一次 cycle 中,遇到哪幾個 bits 衝突,必須記錄下來,例如 2, 30, 51,我們將倒退地消去,即第二次 cycle,同樣消去 2, 30 的那些相異者,來到 51 後,這次選擇前次被消去者,但注意,必須接著走到最後一個 bit,因為還有可能衝突,例如增生成為 2, 30, 52, 53 的新堆疊,如此依此類推。而所有 cycles 中我們都可以拿最新一個相符候選者來參考之因為它是在 LSB first 序下的所有衝突者中的最大衝突者。
  • read rom[33h]
    1. 將 eeprom 讀取出來。僅適用於 single-drop
  • match rom[55h]
    1. 後接參數 device id(64-bit or 56-bit?)。適用 single-drop and multi-drop
    2. 成功後,被指定的 device 便會回應 function commands
    3. 其他 devices 不動作並等待 reset pulse
  • skip rom[CCh]
    1. 指定為所有 devices,故可接著下 function commands 例如 conversion
    2. 可接 Read Scratchpad [BEh],但僅適用於 single-drop
  • alarm search[ECh]
    1. 等同 search rom,但唯有 alarm 狀態被設者才會回應

Function commands

  • convert T[44h]
    1. 前面已有介紹
  • write scratchpad[4Eh]
    1. 以 LSB first 方式寫入 sram 第 2/3/4 bytes
    2. 若程序未完成就發 reset 指令,可能會造成 sram 內資料錯誤
  • read scratchpad[BEh]
    1. 以 LSB first 方式讀取 sram 中所有 bytes
    2. 可 reset 中斷讀取當已獲得所要者
  • copy scratchpad[48h]
    1. 將 bytes 2/3/4 寫入 eeprom
    2. 若使用寄生電源,在 10us 之內就須轉成強上拉以供足電源
    3. 寫入為時最久 10ms
  • recall eeprom[B8h]
    1. 將 bytes 2/3/4 載入 sram,可搭配 response
    2. 當 device 上電後,便會自動做一次載入
  • read power supply[B4h]
    1. 前面已有介紹

總結

相當經典的 1-wire bus 及 parasite power 的應用是很值得評估測試的。

  • 補充:前一句話,經典,筆者後來意會到一點瑕疪。前一篇文章 aht10 sensor code,恰遭遇到熱經由導線傳導是不容忽略的。
    因此 多顆 temp sensors 在同一條 bus 上,乍看之下很吸引人只要透過一條地線及一條訊號線便可巡訪環境週遭各個溫測點但卻是有疑慮的。然則 temp sensor 取材容易亦貼近實用,實驗之亦無不可。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

PHP Code Snippets Powered By : XYZScripts.com