OneWire.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. uint8_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);
  19. //digitalWrite(SHIFT_G, HIGH);
  20. }
  21. /**
  22. * Writes one byte on the 1-Wire Bus
  23. */
  24. void OW_writeByte(uint8_t byte) {
  25. uint8_t loop;
  26. // Loop to write each bit in the byte, LS-bit first
  27. for (loop = 0; loop < 8; loop++) {
  28. OW_writeBit(byte & 0x01);
  29. // shift the data byte for the next bit
  30. byte >>= 1;
  31. }
  32. }
  33. /**
  34. * Reads a full byte from the 1-Wire Bus
  35. */
  36. uint8_t OW_readByte(void) {
  37. uint8_t loop, result = 0;
  38. for (loop = 0; loop < 8; loop++) {
  39. // shift the result to get it ready for the next bit
  40. result >>= 1;
  41. // if result is one, then set MS bit
  42. if (OW_readBit())
  43. result |= 0x80;
  44. }
  45. return result;
  46. }
  47. /**
  48. * Simultaneous write and read from the 1-Wire Bus
  49. * OW_touchByte(0xFF) is equivalent to OW_readByte()
  50. * and OW_touchByte(data) is equivalent to OW_writeByte(data)
  51. */
  52. uint8_t OW_touchByte(uint8_t data) {
  53. uint8_t loop, result = 0;
  54. for (loop = 0; loop < 8; loop++) {
  55. // shift the result to get it ready for the next bit
  56. result >>= 1;
  57. // If sending a '1' then read a bit else write a '0'
  58. if (data & 0x01) {
  59. if (OW_readBit())
  60. result |= 0x80;
  61. } else
  62. OW_writeBit(0);
  63. // shift the data byte for the next bit
  64. data >>= 1;
  65. }
  66. return result;
  67. }
  68. /**
  69. * function which sends and receives a block of data on the 1-Wire Bus
  70. */
  71. void OW_block(uint8_t *data, uint8_t data_len) {
  72. int loop;
  73. for (loop = 0; loop < data_len; loop++) {
  74. OW_touchByte(data[loop]);
  75. }
  76. }
  77. /**
  78. * Writes one bit on the 1-Wire Bus
  79. */
  80. void OW_writeBit(uint8_t bit) {
  81. if (bit) { //1
  82. digitalWrite(OW_PIN, LOW);
  83. delayMicroseconds(OWtiming[0]);
  84. digitalWrite(OW_PIN, HIGH);
  85. delayMicroseconds(OWtiming[1]);
  86. } else {
  87. digitalWrite(OW_PIN, LOW);
  88. delayMicroseconds(OWtiming[2]);
  89. digitalWrite(OW_PIN, HIGH);
  90. delayMicroseconds(OWtiming[3]);
  91. }
  92. }
  93. /**
  94. * Reads one bit fromt he 1-Wire Bus and returns it
  95. */
  96. uint8_t OW_readBit(void) {
  97. digitalWrite(OW_PIN, LOW);
  98. delayMicroseconds(OWtiming[0]);
  99. digitalWrite(OW_PIN, HIGH);
  100. delayMicroseconds(OWtiming[4]);
  101. pinMode(OW_PIN, INPUT);
  102. uint8_t value = digitalRead(OW_PIN);
  103. delayMicroseconds(OWtiming[5]);
  104. pinMode(OW_PIN, OUTPUT);
  105. return value;
  106. }
  107. /**
  108. * Performs a Reset on the 1-Wire Bus and returns either 1 or 0:
  109. * 1: if no device is present and 0 if device(s) are present
  110. */
  111. uint8_t OW_reset() {
  112. delayMicroseconds(OWtiming[6]);
  113. digitalWrite(OW_PIN, LOW);
  114. delayMicroseconds(OWtiming[7]);
  115. digitalWrite(OW_PIN, HIGH);
  116. delayMicroseconds(OWtiming[8]);
  117. pinMode(OW_PIN, INPUT);
  118. uint8_t value = digitalRead(OW_PIN);
  119. delayMicroseconds(OWtiming[9]);
  120. pinMode(OW_PIN, OUTPUT);
  121. return value;
  122. }