Library to read out HX711 24-Bit Analog-to-Digital Converter (ADC) for Weigh Scales by AVIA Semiconductor. Tested with K22F with SCK pint at D13 and DT pin at D12

Dependents:   SmartCrutches

Committer:
megrootens
Date:
Thu Jun 23 12:45:26 2016 +0000
Revision:
4:f59264464b5b
Parent:
3:9bbfb7f8c751
Child:
5:16627e96c8de
TODO: stop sending unnecessary gain commands!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
megrootens 0:5fa72cacd94b 1 #include "mbed.h"
megrootens 0:5fa72cacd94b 2 #include "Hx711.h"
megrootens 0:5fa72cacd94b 3
megrootens 0:5fa72cacd94b 4 void Hx711::set_gain(uint8_t gain) {
megrootens 0:5fa72cacd94b 5 switch (gain) {
megrootens 0:5fa72cacd94b 6 case 128: // channel A, gain factor 128
megrootens 0:5fa72cacd94b 7 gain_ = 1;
megrootens 0:5fa72cacd94b 8 break;
megrootens 0:5fa72cacd94b 9 case 64: // channel A, gain factor 64
megrootens 0:5fa72cacd94b 10 gain_ = 3;
megrootens 0:5fa72cacd94b 11 break;
megrootens 0:5fa72cacd94b 12 case 32: // channel B, gain factor 32
megrootens 0:5fa72cacd94b 13 gain_ = 2;
megrootens 0:5fa72cacd94b 14 break;
megrootens 0:5fa72cacd94b 15 }
megrootens 0:5fa72cacd94b 16
megrootens 0:5fa72cacd94b 17 sck_.write(LOW);
megrootens 0:5fa72cacd94b 18 read();
megrootens 0:5fa72cacd94b 19 }
megrootens 0:5fa72cacd94b 20
megrootens 2:f118f8c456a4 21 uint32_t Hx711::readRaw() {
megrootens 0:5fa72cacd94b 22 // wait for the chip to become ready
megrootens 1:197ef68ad6de 23 // TODO: this is not ideal; the programm will hang if the chip never
megrootens 1:197ef68ad6de 24 // becomes ready...
megrootens 0:5fa72cacd94b 25 while (!is_ready());
megrootens 0:5fa72cacd94b 26
megrootens 0:5fa72cacd94b 27 uint32_t value = 0;
megrootens 0:5fa72cacd94b 28 uint8_t data[3] = { 0 };
megrootens 0:5fa72cacd94b 29 uint8_t filler = 0x00;
megrootens 0:5fa72cacd94b 30
megrootens 0:5fa72cacd94b 31 // pulse the clock pin 24 times to read the data
megrootens 3:9bbfb7f8c751 32 data[2] = shiftInMsbFirst();
megrootens 3:9bbfb7f8c751 33 data[1] = shiftInMsbFirst();
megrootens 3:9bbfb7f8c751 34 data[0] = shiftInMsbFirst();
megrootens 0:5fa72cacd94b 35
megrootens 4:f59264464b5b 36 // TODO: this costs a significant amount of time; should not be done!
megrootens 0:5fa72cacd94b 37 // set the channel and the gain factor for the next reading using the clock pin
megrootens 0:5fa72cacd94b 38 for (unsigned int i = 0; i < gain_; i++) {
megrootens 0:5fa72cacd94b 39 sck_.write(HIGH);
megrootens 0:5fa72cacd94b 40 sck_.write(LOW);
megrootens 0:5fa72cacd94b 41 }
megrootens 0:5fa72cacd94b 42
megrootens 0:5fa72cacd94b 43 // Datasheet indicates the value is returned as a two's complement value
megrootens 0:5fa72cacd94b 44 // Flip all the bits
megrootens 0:5fa72cacd94b 45 data[2] = ~data[2];
megrootens 0:5fa72cacd94b 46 data[1] = ~data[1];
megrootens 0:5fa72cacd94b 47 data[0] = ~data[0];
megrootens 0:5fa72cacd94b 48
megrootens 0:5fa72cacd94b 49 // Replicate the most significant bit to pad out a 32-bit signed integer
megrootens 0:5fa72cacd94b 50 if ( data[2] & 0x80 ) {
megrootens 0:5fa72cacd94b 51 filler = 0xFF;
megrootens 0:5fa72cacd94b 52 } else if ((0x7F == data[2]) && (0xFF == data[1]) && (0xFF == data[0])) {
megrootens 0:5fa72cacd94b 53 filler = 0xFF;
megrootens 0:5fa72cacd94b 54 } else {
megrootens 0:5fa72cacd94b 55 filler = 0x00;
megrootens 0:5fa72cacd94b 56 }
megrootens 0:5fa72cacd94b 57
megrootens 0:5fa72cacd94b 58 // Construct a 32-bit signed integer
megrootens 0:5fa72cacd94b 59 value = ( static_cast<uint32_t>(filler) << 24
megrootens 0:5fa72cacd94b 60 | static_cast<uint32_t>(data[2]) << 16
megrootens 0:5fa72cacd94b 61 | static_cast<uint32_t>(data[1]) << 8
megrootens 0:5fa72cacd94b 62 | static_cast<uint32_t>(data[0]) );
megrootens 0:5fa72cacd94b 63
megrootens 0:5fa72cacd94b 64 // ... and add 1
megrootens 0:5fa72cacd94b 65 return static_cast<int>(++value);
megrootens 0:5fa72cacd94b 66 }
megrootens 0:5fa72cacd94b 67
megrootens 0:5fa72cacd94b 68
megrootens 3:9bbfb7f8c751 69 uint8_t Hx711::shiftInMsbFirst() {
megrootens 0:5fa72cacd94b 70 uint8_t value = 0;
megrootens 0:5fa72cacd94b 71
megrootens 3:9bbfb7f8c751 72 for (uint8_t i = 0; i < 8; ++i) {
megrootens 0:5fa72cacd94b 73 sck_.write(HIGH);
megrootens 3:9bbfb7f8c751 74 value |= dt_.read() << (7 - i);
megrootens 0:5fa72cacd94b 75 sck_.write(LOW);
megrootens 0:5fa72cacd94b 76 }
megrootens 0:5fa72cacd94b 77 return value;
megrootens 0:5fa72cacd94b 78 }