OneWire.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * OneWire.cpp
  3. *
  4. * Created on: Feb 28, 2018
  5. * Author: sebastian
  6. */
  7. #include "OneWire.h"
  8. /*
  9. * timings in us from
  10. * https://www.maximintegrated.com/en/app-notes/index.mvp/id/126
  11. */
  12. uint16_t OWtiming[] = { 6, 64, 60, 10, 9, 55, 0, 480, 70, 410 };
  13. /**
  14. * Initializes the 1-Wire communication
  15. */
  16. void OneWire_init() {
  17. pinMode(OW_PIN, OUTPUT);
  18. //pullUpDnControl(OW_PIN, PUD_UP); the pull up is already on board
  19. }
  20. /**
  21. * Writes one byte on the 1-Wire Bus
  22. */
  23. void OW_writeByte(uint8_t byte) {
  24. uint8_t loop;
  25. // Loop to write each bit in the byte, LS-bit first
  26. for (loop = 0; loop < 8; loop++) {
  27. OW_writeBit(byte & 0x01);
  28. // shift the data byte for the next bit
  29. byte >>= 1;
  30. }
  31. }
  32. /**
  33. * Reads a full byte from the 1-Wire Bus
  34. */
  35. uint8_t OW_readByte(void) {
  36. uint8_t loop, result = 0;
  37. for (loop = 0; loop < 8; loop++) {
  38. // shift the result to get it ready for the next bit
  39. result >>= 1;
  40. // if result is one, then set MS bit
  41. if (OW_readBit())
  42. result |= 0x80;
  43. }
  44. return result;
  45. }
  46. /**
  47. * Simultaneous write and read from the 1-Wire Bus
  48. * OW_touchByte(0xFF) is equivalent to OW_readByte()
  49. * and OW_touchByte(data) is equivalent to OW_writeByte(data)
  50. */
  51. uint8_t OW_touchByte(uint8_t data) {
  52. uint8_t loop, result = 0;
  53. for (loop = 0; loop < 8; loop++) {
  54. // shift the result to get it ready for the next bit
  55. result >>= 1;
  56. // If sending a '1' then read a bit else write a '0'
  57. if (data & 0x01) {
  58. if (OW_readBit())
  59. result |= 0x80;
  60. } else
  61. OW_writeBit(0);
  62. // shift the data byte for the next bit
  63. data >>= 1;
  64. }
  65. return result;
  66. }
  67. /**
  68. * function which sends and receives a block of data on the 1-Wire Bus
  69. */
  70. void OW_block(uint8_t *data, uint8_t data_len) {
  71. int loop;
  72. for (loop = 0; loop < data_len; loop++) {
  73. OW_touchByte(data[loop]);
  74. }
  75. }
  76. /**
  77. * Writes one bit on the 1-Wire Bus
  78. */
  79. void OW_writeBit(uint8_t bit) {
  80. if (bit) { //1
  81. digitalWrite(OW_PIN, LOW);
  82. delayMicroseconds(OWtiming[0]);
  83. digitalWrite(OW_PIN, HIGH);
  84. delayMicroseconds(OWtiming[1]);
  85. } else {
  86. digitalWrite(OW_PIN, LOW);
  87. delayMicroseconds(OWtiming[2]);
  88. digitalWrite(OW_PIN, HIGH);
  89. delayMicroseconds(OWtiming[3]);
  90. }
  91. }
  92. /**
  93. * Reads one bit from the 1-Wire Bus and returns it
  94. */
  95. uint8_t OW_readBit(void) {
  96. digitalWrite(OW_PIN, LOW);
  97. delayMicroseconds(OWtiming[0]);
  98. digitalWrite(OW_PIN, HIGH);
  99. delayMicroseconds(OWtiming[4]);
  100. pinMode(OW_PIN, INPUT);
  101. uint8_t value = digitalRead(OW_PIN);
  102. delayMicroseconds(OWtiming[5]);
  103. pinMode(OW_PIN, OUTPUT);
  104. return value;
  105. }
  106. /**
  107. * Performs a Reset on the 1-Wire Bus and returns either 1 or 0:
  108. * 1: if no device is present and 0 if device(s) are present
  109. */
  110. uint8_t OW_reset() {
  111. delayMicroseconds(OWtiming[6]);
  112. digitalWrite(OW_PIN, LOW);
  113. delayMicroseconds(OWtiming[7]);
  114. digitalWrite(OW_PIN, HIGH);
  115. delayMicroseconds(OWtiming[8]);
  116. pinMode(OW_PIN, INPUT);
  117. uint8_t value = digitalRead(OW_PIN);
  118. delayMicroseconds(OWtiming[9]);
  119. pinMode(OW_PIN, OUTPUT);
  120. return value;
  121. }