Skip to content

Commit

Permalink
[driver] Simple HX711 driver
Browse files Browse the repository at this point in the history
  • Loading branch information
strongly-typed committed Mar 29, 2020
1 parent b8648be commit 068aa4c
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 6 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,44 +193,45 @@ can easily configure them for you specific needs.
<td align="center">HD44780</td>
<td align="center">HMC58x</td>
<td align="center">HMC6343</td>
<td align="center">HX711</td>
<td align="center">I2C-EEPROM</td>
<td align="center">ITG3200</td>
</tr><tr>
<td align="center">ITG3200</td>
<td align="center">L3GD20</td>
<td align="center">LAWICEL</td>
<td align="center">LIS302DL</td>
<td align="center">LIS3DSH</td>
<td align="center">LM75</td>
<td align="center">LSM303A</td>
</tr><tr>
<td align="center">LSM303A</td>
<td align="center">LTC2984</td>
<td align="center">MAX6966</td>
<td align="center">MAX7219</td>
<td align="center">MCP23X17</td>
<td align="center">MCP2515</td>
<td align="center">NOKIA5110</td>
</tr><tr>
<td align="center">NOKIA5110</td>
<td align="center">NRF24</td>
<td align="center">TFT-DISPLAY</td>
<td align="center">PAT9125EL</td>
<td align="center">PCA8574</td>
<td align="center">PCA9535</td>
<td align="center">PCA9548A</td>
</tr><tr>
<td align="center">PCA9548A</td>
<td align="center">PCA9685</td>
<td align="center">SIEMENS-S65</td>
<td align="center">SIEMENS-S75</td>
<td align="center">SK6812</td>
<td align="center">SK9822</td>
<td align="center">SSD1306</td>
</tr><tr>
<td align="center">SSD1306</td>
<td align="center">TCS3414</td>
<td align="center">TCS3472</td>
<td align="center">TLC594X</td>
<td align="center">TMP102</td>
<td align="center">TMP175</td>
<td align="center">VL53L0</td>
</tr><tr>
<td align="center">VL53L0</td>
<td align="center">VL6180</td>
<td align="center">WS2812</td>
</tr>
Expand Down
58 changes: 58 additions & 0 deletions src/modm/driver/adc/hx711.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2020, Sascha Schade
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#ifndef MODM_HX711_HPP
#define MODM_HX711_HPP

#include <modm/architecture/interface/delay.hpp>
#include <modm/processing/resumable.hpp>

namespace modm
{

/// @ingroup modm_driver_hx711
struct hx711
{
enum class
InputChannelAndGain : uint8_t
{
ChA_128 = 1,
ChB_32 = 2,
ChA_64 = 3
};

struct Config
{
using Sck = void; // required
using Data = void; // required
static const InputChannelAndGain mode = InputChannelAndGain::ChA_128;
};
};

template <typename Cfg>
class Hx711 : public hx711, protected modm::NestedResumable<2>
{
using Sck = typename Cfg::Sck;
using Data = typename Cfg::Data;

public:
modm::ResumableResult<int32_t>
singleConversion();

private:
int32_t data;
};

} // modm namespace

#include "hx711_impl.hpp"

#endif // MODM_HX711_HPP
36 changes: 36 additions & 0 deletions src/modm/driver/adc/hx711.lb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2020, Sascha Schade
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------


def init(module):
module.name = ":driver:hx711"
module.description = """\
# HX711 Load Cell Amplifier and ADC
The HX711 is an integrated load cell amplifier designed for weight scales and
industrial control applications to interface directly with a bridge sensor.
The interface is bit-bang and timing based, so only GPIO is needed.
In a complete system the SCK pin's timing while reading must be met.
"""


def prepare(module, options):
module.depends(
":architecture:delay",
":architecture:gpio",
":processing:resumable")
return True

def build(env):
env.outbasepath = "modm/src/modm/driver/adc"
env.copy("hx711.hpp")
env.copy("hx711_impl.hpp")
56 changes: 56 additions & 0 deletions src/modm/driver/adc/hx711_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2020, Sascha Schade
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#ifndef MODM_HX711_HPP
# error "Don't include this file directly! Use 'hx711.hpp' instead."
#endif

namespace modm
{

template <typename Cfg>
ResumableResult<int32_t>
Hx711<Cfg>::singleConversion()
{
RF_BEGIN();

RF_WAIT_UNTIL(Data::read() == modm::Gpio::Low);

modm::delayMicroseconds(1);

data = 0;
for (uint8_t ii = 0; ii < 24; ++ii)
{
Sck::set();
modm::delayMicroseconds(1);
data = (data << 1) | Data::read();
modm::delayMicroseconds(1);
Sck::reset();
modm::delayMicroseconds(1);
}

// Additional pulses for mode of next conversion
for (uint8_t ii = 0; ii < static_cast<uint8_t>(Cfg::mode); ++ii) {
Sck::set();
modm::delayMicroseconds(1);
Sck::reset();
modm::delayMicroseconds(1);
}

// Fill up MSBs for negative numbers
if (data & (1 << 23)) {
data |= 0xff000000;
}

RF_END_RETURN(data);
}

} // modm namespace

0 comments on commit 068aa4c

Please sign in to comment.