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 14:16:26 2016 +0000
Revision:
5:16627e96c8de
Parent:
4:f59264464b5b
removed TODO; should not be done

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 0:5fa72cacd94b 36 // set the channel and the gain factor for the next reading using the clock pin
megrootens 0:5fa72cacd94b 37 for (unsigned int i = 0; i < gain_; i++) {
megrootens 0:5fa72cacd94b 38 sck_.write(HIGH);
megrootens 0:5fa72cacd94b 39 sck_.write(LOW);
megrootens 0:5fa72cacd94b 40 }
megrootens 0:5fa72cacd94b 41
megrootens 0:5fa72cacd94b 42 // Datasheet indicates the value is returned as a two's complement value
megrootens 0:5fa72cacd94b 43 // Flip all the bits
megrootens 0:5fa72cacd94b 44 data[2] = ~data[2];
megrootens 0:5fa72cacd94b 45 data[1] = ~data[1];
megrootens 0:5fa72cacd94b 46 data[0] = ~data[0];
megrootens 0:5fa72cacd94b 47
megrootens 0:5fa72cacd94b 48 // Replicate the most significant bit to pad out a 32-bit signed integer
megrootens 0:5fa72cacd94b 49 if ( data[2] & 0x80 ) {
megrootens 0:5fa72cacd94b 50 filler = 0xFF;
megrootens 0:5fa72cacd94b 51 } else if ((0x7F == data[2]) && (0xFF == data[1]) && (0xFF == data[0])) {
megrootens 0:5fa72cacd94b 52 filler = 0xFF;
megrootens 0:5fa72cacd94b 53 } else {
megrootens 0:5fa72cacd94b 54 filler = 0x00;
megrootens 0:5fa72cacd94b 55 }
megrootens 0:5fa72cacd94b 56
megrootens 0:5fa72cacd94b 57 // Construct a 32-bit signed integer
megrootens 0:5fa72cacd94b 58 value = ( static_cast<uint32_t>(filler) << 24
megrootens 0:5fa72cacd94b 59 | static_cast<uint32_t>(data[2]) << 16
megrootens 0:5fa72cacd94b 60 | static_cast<uint32_t>(data[1]) << 8
megrootens 0:5fa72cacd94b 61 | static_cast<uint32_t>(data[0]) );
megrootens 0:5fa72cacd94b 62
megrootens 0:5fa72cacd94b 63 // ... and add 1
megrootens 0:5fa72cacd94b 64 return static_cast<int>(++value);
megrootens 0:5fa72cacd94b 65 }
megrootens 0:5fa72cacd94b 66
megrootens 0:5fa72cacd94b 67
megrootens 3:9bbfb7f8c751 68 uint8_t Hx711::shiftInMsbFirst() {
megrootens 0:5fa72cacd94b 69 uint8_t value = 0;
megrootens 0:5fa72cacd94b 70
megrootens 3:9bbfb7f8c751 71 for (uint8_t i = 0; i < 8; ++i) {
megrootens 0:5fa72cacd94b 72 sck_.write(HIGH);
megrootens 3:9bbfb7f8c751 73 value |= dt_.read() << (7 - i);
megrootens 0:5fa72cacd94b 74 sck_.write(LOW);
megrootens 0:5fa72cacd94b 75 }
megrootens 0:5fa72cacd94b 76 return value;
megrootens 0:5fa72cacd94b 77 }