找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3984|回复: 0
收起左侧

DS18B20数字温度传感器实验(XHX格式)

[复制链接]
ID:370757 发表于 2018-9-30 14:42 | 显示全部楼层 |阅读模式
实验器件 :
Arduino 控制板 1 块、USB 数据线 1 根、面包板 1 块、面包板专用跳线 1 盒、镊子 1 把、DS1307 RTC 模块。

实验电路图:

创客集结号

创客集结号



实验原理 :
本实验需要使用 OneWire 库。DS1307 RTC模块,上面集成了一个DS18B20温度传感器,还集成了另外一个存储芯片。

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. }
复制代码

[创客集结号转载]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表