hx711.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*********************************************************************
  2. *
  3. * Load Cell Amplifier HX711 library for Fraise pic18f device
  4. *
  5. *********************************************************************
  6. * Author Date Comment
  7. *********************************************************************
  8. * Antoine Rousseau sept 2016 Original.
  9. ********************************************************************/
  10. /*
  11. # This program is free software; you can redistribute it and/or
  12. # modify it under the terms of the GNU General Public License
  13. # as published by the Free Software Foundation; either version 2
  14. # of the License, or (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program; if not, write to the Free Software
  22. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  23. # MA 02110-1301, USA.
  24. */
  25. #include <hx711.h>
  26. static long valA = 0 , valB = 0;
  27. static unsigned char gainA = 0;
  28. static unsigned char currentFront = 0; // chanA=24+2 bits, chanB=25 or 27 bits = 51 or 53 bits total = 102 or 106 fronts.
  29. void hx711Init(unsigned char _gainA)
  30. {
  31. gainA = (_gainA != 0);
  32. digitalClear(HX711_SCK_PIN);
  33. pinModeDigitalOut(HX711_SCK_PIN);
  34. pinModeDigitalIn(HX711_DATA_PIN);
  35. }
  36. static unsigned char shiftIn() // shift HX711_DATA_PIN with HX711_SCK_PIN, MSB first.
  37. {
  38. unsigned char v = 0;
  39. unsigned char i = 8;
  40. while(i--) {
  41. v <<= 1;
  42. digitalSet(HX711_SCK_PIN);
  43. /*nop();
  44. nop();*/
  45. digitalClear(HX711_SCK_PIN);
  46. if(digitalRead(HX711_DATA_PIN)) v += 1;
  47. }
  48. return v;
  49. }
  50. static long format(unsigned long in)
  51. {
  52. #if 0
  53. in = ~in;
  54. if( ((in & 0x00FF0000) != 0x00800000) && (in != 0xFF7FFFFF))
  55. in &= 0x00FFFFFF;
  56. in += 1;
  57. return (long)in;
  58. #endif
  59. return in^0x800000;
  60. }
  61. void hx711Service()
  62. {
  63. static unsigned long tmpVal;
  64. static unsigned char curBit = 0;
  65. if(((curBit == 0) || (curBit == 25)) && digitalRead(HX711_DATA_PIN)) return;
  66. if((curBit == 0) || (curBit == 26)) tmpVal = 0;
  67. curBit += 1;
  68. digitalSet(HX711_SCK_PIN);
  69. Nop();
  70. Nop();
  71. Nop();
  72. Nop();
  73. digitalClear(HX711_SCK_PIN);
  74. tmpVal <<= 1;
  75. if(digitalRead(HX711_DATA_PIN)) tmpVal += 1;
  76. if(curBit == 24) valA = format(tmpVal);
  77. if(curBit == 50) valB = format(tmpVal);
  78. if((gainA==0) && (curBit == 51)) curBit = 0;
  79. if(curBit >= 53) curBit = 0;
  80. }
  81. long hx711Read(unsigned char channel)
  82. {
  83. if(channel == 0) return valA;
  84. else return valB;
  85. #if 0
  86. unsigned char i;
  87. unsigned long value = 0;
  88. unsigned char data[3] = { 0 };
  89. unsigned char filler = 0x00;
  90. if(mode>2) mode=2;
  91. mode += 1;
  92. // pulse the clock pin 24 times to read the data
  93. data[2] = shiftIn();
  94. data[1] = shiftIn();
  95. data[0] = shiftIn();
  96. // set the channel and the gain factor for the next reading using the clock pin
  97. for (i = 0; i < mode; i++) {
  98. digitalSet(HX711_SCK_PIN);
  99. digitalClear(HX711_SCK_PIN);
  100. }
  101. // Datasheet indicates the value is returned as a two's complement value
  102. // Flip all the bits
  103. data[2] = ~data[2];
  104. data[1] = ~data[1];
  105. data[0] = ~data[0];
  106. // Replicate the most significant bit to pad out a 32-bit signed integer
  107. if ( data[2] & 0x80 ) {
  108. filler = 0xFF;
  109. } else if ((0x7F == data[2]) && (0xFF == data[1]) && (0xFF == data[0])) {
  110. filler = 0xFF;
  111. } else {
  112. filler = 0x00;
  113. }
  114. /*// Construct a 32-bit signed integer
  115. value = ( static_cast<unsigned long>(filler) << 24
  116. | static_cast<unsigned long>(data[2]) << 16
  117. | static_cast<unsigned long>(data[1]) << 8
  118. | static_cast<unsigned long>(data[0]) );
  119. // ... and add 1
  120. return static_cast<long>(++value);*/
  121. value = (unsigned long)(filler) << 24
  122. | ((unsigned long)(data[2]) << 16)
  123. | ((unsigned long)(data[1]) << 8)
  124. | ((unsigned long)(data[0]) );
  125. value += 1;
  126. return value;
  127. #endif
  128. }
  129. /*void hx711Service_()
  130. {
  131. static unsigned long tmpVal;
  132. //unsigned char curBit;
  133. if((currentFront == 0) || (currentFront == 54)) tmpVal = 0;
  134. currentFront += 1;
  135. if(currentFront & 1) {
  136. digitalSet(HX711_SCK_PIN);
  137. return;
  138. }
  139. digitalClear(HX711_SCK_PIN);
  140. tmpVal <<= 1;
  141. if(digitalRead(HX711_DATA_PIN)) tmpVal += 1;
  142. if(currentFront == 48) valA = format(tmpVal);
  143. if(currentFront == 100) valB = format(tmpVal);
  144. if((gainA==0) && (currentFront == 102)) currentFront = 0;
  145. if(currentFront >= 106) currentFront = 0;
  146. } */