找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 4714|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

DS18B20數(shù)字溫度傳感器實(shí)驗(yàn)(XHX格式)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
實(shí)驗(yàn)器件 :
Arduino 控制板 1 塊、USB 數(shù)據(jù)線 1 根、面包板 1 塊、面包板專用跳線 1 盒、鑷子 1 把、DS1307 RTC 模塊。

實(shí)驗(yàn)電路圖:



實(shí)驗(yàn)原理 :
本實(shí)驗(yàn)需要使用 OneWire 庫。DS1307 RTC模塊,上面集成了一個(gè)DS18B20溫度傳感器,還集成了另外一個(gè)存儲(chǔ)芯片。

Arduino代碼 :
  1. #include <OneWire.h>
  2. // DS18S20 Temperature chip i/o
  3. OneWire ds(2); // on pin 2
  4. void setup(void) {
  5. // initialize inputs/outputs
  6. // start serial port
  7. Serial.begin(9600);
  8. }
  9. void loop(void) {
  10. byte i;
  11. byte present = 0;
  12. byte data[12];
  13. byte addr[8];
  14. if ( !ds.search(addr)) {
  15. Serial.print("No more addresses.\n");
  16. ds.reset_search();
  17. return;
  18. }
  19. Serial.print("R=");
  20. for( i = 0; i < 8; i++) {
  21. Serial.print(addr[i], HEX);
  22. Serial.print(" ");
  23. }
  24. if ( OneWire::crc8( addr, 7) != addr[7]) {
  25. Serial.print("CRC is not valid!\n");
  26. return;
  27. }
  28. if ( addr[0] == 0x10) {
  29. Serial.print("Device is a DS18S20 family device.\n");
  30. }
  31. else if ( addr[0] == 0x28) {
  32. Serial.print("Device is a DS18B20 family device.\n");
  33. }
  34. else {
  35. Serial.print("Device family is not recognized: 0x");
  36. Serial.println(addr[0],HEX);
  37. return;
  38. }
  39. ds.reset();
  40. ds.select(addr);
  41. ds.write(0x44,1); // start conversion, with parasite power on at the end
  42. delay(1000); // maybe 750ms is enough, maybe not
  43. // we might do a ds.depower() here, but the reset will take care of it.
  44. present = ds.reset();
  45. ds.select(addr);
  46. ds.write(0xBE); // Read Scratchpad
  47. Serial.print("P=");
  48. Serial.print(present,HEX);
  49. Serial.print(" ");
  50. for ( i = 0; i < 9; i++) { // we need 9 bytes
  51. data[i] = ds.read();
  52. Serial.print(data[i], HEX);
  53. Serial.print(" ");
  54. }
  55. Serial.print(" CRC=");
  56. Serial.print( OneWire::crc8( d
  57. ata, 8), HEX);
  58. Serial.println();
  59. }
復(fù)制代碼

[創(chuàng)客集結(jié)號(hào)轉(zhuǎn)載]
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表