-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8648be
commit 068aa4c
Showing
4 changed files
with
157 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |