Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple driver for HX711 weight scale / bridge amplifier and integrated ADC #372

Merged
merged 2 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
123 changes: 123 additions & 0 deletions examples/stm32f103c8t6_blue_pill/weight_scale_hx711/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include <modm/debug/logger.hpp>
#include <modm/processing/timer.hpp>
#include <modm/processing/protothread.hpp>
#include <modm/driver/adc/hx711.hpp>
using namespace modm::literals;

using namespace Board;

// ----------------------------------------------------------------------------
// Set the log level
#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::DEBUG

// Create an IODeviceWrapper around the Uart Peripheral we want to use
modm::IODeviceWrapper< Usart1, modm::IOBuffer::BlockIfFull > loggerDevice;

// Set all four logger streams to use the UART
modm::log::Logger modm::log::debug(loggerDevice);
modm::log::Logger modm::log::info(loggerDevice);
modm::log::Logger modm::log::warning(loggerDevice);
modm::log::Logger modm::log::error(loggerDevice);


struct hx711_config : public modm::hx711::Config
{
using Sck = GpioOutputA9;
using Data = GpioInputA10;
// static const modm::hx711::InputChannelAndGain mode = modm::hx711::InputChannelAndGain::ChA_64;
};
using Hx711 = modm::Hx711< hx711_config >;

class Hx711Thread : public modm::pt::Protothread
{
public:
bool
run()
{
PT_BEGIN();

while (true)
{
result = PT_CALL(hx711.singleConversion());
MODM_LOG_DEBUG.printf("%" PRIi32 "\n", result);
}

PT_END();
}

protected:
Hx711 hx711;
int32_t result;
};

Hx711Thread hx711_thread;


class BlinkThread : public modm::pt::Protothread
{
public:
bool
run()
{
PT_BEGIN();

while (true) {
PT_WAIT_UNTIL(timer.execute());
LedGreen::toggle();
}

PT_END();
}

protected:
modm::ShortPeriodicTimer timer{1'000};
};

BlinkThread blink_thread;


/*
* Blinks the green user LED with 1 Hz while measuring.
*
* Make the following connections to HX711:
* Data PA10
* Clock PA9
*/
int
main()
{
Board::initialize();

// Initialize Uart1 for MODM_LOG_*
Usart1::connect<GpioOutputB6::Tx, GpioInputB7::Rx>();
Usart1::initialize<Board::SystemClock, 115200_Bd>();

// Use the logging streams to print some messages.
MODM_LOG_DEBUG << "HX711 demo" << modm::endl;

hx711_config::Sck::setOutput();
hx711_config::Data::setInput();

LedGreen::set();

while (true)
{
blink_thread.run();
hx711_thread.run();
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Replace this with your custom programmer
source [find interface/stlink-v2.cfg]
16 changes: 16 additions & 0 deletions examples/stm32f103c8t6_blue_pill/weight_scale_hx711/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<library>
<extends>modm:blue-pill</extends>
<options>
<option name="modm:build:build.path">../../../build/stm32f103c8t6_blue_pill/weight_scale_hx711</option>
<option name="modm:build:openocd.cfg">openocd.cfg</option>
</options>
<modules>
<module>modm:debug</module>
<module>modm:platform:gpio</module>
<module>modm:platform:uart:1</module>
<module>modm:processing:timer</module>
<module>modm:processing:protothread</module>
<module>modm:build:scons</module>
<module>modm:driver:hx711</module>
</modules>
</library>
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