![](/media/cache/profiles/Cristian_Castro_-_Foto.jpg.50x50_q85.jpg)
Lectura de una celda de Carga HX711.
Hx711.cpp@1:392a3fb6d177, 2021-09-03 (annotated)
- Committer:
- CCastrop1012
- Date:
- Fri Sep 03 04:51:59 2021 +0000
- Revision:
- 1:392a3fb6d177
Lectura de una celda de Carga.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CCastrop1012 | 1:392a3fb6d177 | 1 | #include "mbed.h" |
CCastrop1012 | 1:392a3fb6d177 | 2 | #include "Hx711.h" |
CCastrop1012 | 1:392a3fb6d177 | 3 | |
CCastrop1012 | 1:392a3fb6d177 | 4 | void Hx711::set_gain(uint8_t gain) { |
CCastrop1012 | 1:392a3fb6d177 | 5 | switch (gain) { |
CCastrop1012 | 1:392a3fb6d177 | 6 | case 128: // channel A, gain factor 128 |
CCastrop1012 | 1:392a3fb6d177 | 7 | gain_ = 1; |
CCastrop1012 | 1:392a3fb6d177 | 8 | break; |
CCastrop1012 | 1:392a3fb6d177 | 9 | case 64: // channel A, gain factor 64 |
CCastrop1012 | 1:392a3fb6d177 | 10 | gain_ = 3; |
CCastrop1012 | 1:392a3fb6d177 | 11 | break; |
CCastrop1012 | 1:392a3fb6d177 | 12 | case 32: // channel B, gain factor 32 |
CCastrop1012 | 1:392a3fb6d177 | 13 | gain_ = 2; |
CCastrop1012 | 1:392a3fb6d177 | 14 | break; |
CCastrop1012 | 1:392a3fb6d177 | 15 | } |
CCastrop1012 | 1:392a3fb6d177 | 16 | |
CCastrop1012 | 1:392a3fb6d177 | 17 | sck_.write(LOW); |
CCastrop1012 | 1:392a3fb6d177 | 18 | read(); |
CCastrop1012 | 1:392a3fb6d177 | 19 | } |
CCastrop1012 | 1:392a3fb6d177 | 20 | |
CCastrop1012 | 1:392a3fb6d177 | 21 | uint32_t Hx711::readRaw() { |
CCastrop1012 | 1:392a3fb6d177 | 22 | // wait for the chip to become ready |
CCastrop1012 | 1:392a3fb6d177 | 23 | // TODO: this is not ideal; the programm will hang if the chip never |
CCastrop1012 | 1:392a3fb6d177 | 24 | // becomes ready... |
CCastrop1012 | 1:392a3fb6d177 | 25 | while (!is_ready()); |
CCastrop1012 | 1:392a3fb6d177 | 26 | |
CCastrop1012 | 1:392a3fb6d177 | 27 | uint32_t value = 0; |
CCastrop1012 | 1:392a3fb6d177 | 28 | uint8_t data[3] = { 0 }; |
CCastrop1012 | 1:392a3fb6d177 | 29 | uint8_t filler = 0x00; |
CCastrop1012 | 1:392a3fb6d177 | 30 | |
CCastrop1012 | 1:392a3fb6d177 | 31 | // pulse the clock pin 24 times to read the data |
CCastrop1012 | 1:392a3fb6d177 | 32 | data[2] = shiftInMsbFirst(); |
CCastrop1012 | 1:392a3fb6d177 | 33 | data[1] = shiftInMsbFirst(); |
CCastrop1012 | 1:392a3fb6d177 | 34 | data[0] = shiftInMsbFirst(); |
CCastrop1012 | 1:392a3fb6d177 | 35 | |
CCastrop1012 | 1:392a3fb6d177 | 36 | // set the channel and the gain factor for the next reading using the clock pin |
CCastrop1012 | 1:392a3fb6d177 | 37 | for (unsigned int i = 0; i < gain_; i++) { |
CCastrop1012 | 1:392a3fb6d177 | 38 | sck_.write(HIGH); |
CCastrop1012 | 1:392a3fb6d177 | 39 | sck_.write(LOW); |
CCastrop1012 | 1:392a3fb6d177 | 40 | } |
CCastrop1012 | 1:392a3fb6d177 | 41 | |
CCastrop1012 | 1:392a3fb6d177 | 42 | // Datasheet indicates the value is returned as a two's complement value |
CCastrop1012 | 1:392a3fb6d177 | 43 | // Flip all the bits |
CCastrop1012 | 1:392a3fb6d177 | 44 | data[2] = ~data[2]; |
CCastrop1012 | 1:392a3fb6d177 | 45 | data[1] = ~data[1]; |
CCastrop1012 | 1:392a3fb6d177 | 46 | data[0] = ~data[0]; |
CCastrop1012 | 1:392a3fb6d177 | 47 | |
CCastrop1012 | 1:392a3fb6d177 | 48 | // Replicate the most significant bit to pad out a 32-bit signed integer |
CCastrop1012 | 1:392a3fb6d177 | 49 | if ( data[2] & 0x80 ) { |
CCastrop1012 | 1:392a3fb6d177 | 50 | filler = 0xFF; |
CCastrop1012 | 1:392a3fb6d177 | 51 | } else if ((0x7F == data[2]) && (0xFF == data[1]) && (0xFF == data[0])) { |
CCastrop1012 | 1:392a3fb6d177 | 52 | filler = 0xFF; |
CCastrop1012 | 1:392a3fb6d177 | 53 | } else { |
CCastrop1012 | 1:392a3fb6d177 | 54 | filler = 0x00; |
CCastrop1012 | 1:392a3fb6d177 | 55 | } |
CCastrop1012 | 1:392a3fb6d177 | 56 | |
CCastrop1012 | 1:392a3fb6d177 | 57 | // Construct a 32-bit signed integer |
CCastrop1012 | 1:392a3fb6d177 | 58 | value = ( static_cast<uint32_t>(filler) << 24 |
CCastrop1012 | 1:392a3fb6d177 | 59 | | static_cast<uint32_t>(data[2]) << 16 |
CCastrop1012 | 1:392a3fb6d177 | 60 | | static_cast<uint32_t>(data[1]) << 8 |
CCastrop1012 | 1:392a3fb6d177 | 61 | | static_cast<uint32_t>(data[0]) ); |
CCastrop1012 | 1:392a3fb6d177 | 62 | |
CCastrop1012 | 1:392a3fb6d177 | 63 | // ... and add 1 |
CCastrop1012 | 1:392a3fb6d177 | 64 | return static_cast<int>(++value); |
CCastrop1012 | 1:392a3fb6d177 | 65 | } |
CCastrop1012 | 1:392a3fb6d177 | 66 | |
CCastrop1012 | 1:392a3fb6d177 | 67 | |
CCastrop1012 | 1:392a3fb6d177 | 68 | uint8_t Hx711::shiftInMsbFirst() { |
CCastrop1012 | 1:392a3fb6d177 | 69 | uint8_t value = 0; |
CCastrop1012 | 1:392a3fb6d177 | 70 | |
CCastrop1012 | 1:392a3fb6d177 | 71 | for (uint8_t i = 0; i < 8; ++i) { |
CCastrop1012 | 1:392a3fb6d177 | 72 | sck_.write(HIGH); |
CCastrop1012 | 1:392a3fb6d177 | 73 | value |= dt_.read() << (7 - i); |
CCastrop1012 | 1:392a3fb6d177 | 74 | sck_.write(LOW); |
CCastrop1012 | 1:392a3fb6d177 | 75 | } |
CCastrop1012 | 1:392a3fb6d177 | 76 | return value; |
CCastrop1012 | 1:392a3fb6d177 | 77 | } |