-
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.
[example] Simple example using HX711 driver
- Loading branch information
1 parent
068aa4c
commit d21e991
Showing
3 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
examples/stm32f103c8t6_blue_pill/weight_scale_hx711/main.cpp
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,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; | ||
} |
2 changes: 2 additions & 0 deletions
2
examples/stm32f103c8t6_blue_pill/weight_scale_hx711/openocd.cfg
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,2 @@ | ||
# Replace this with your custom programmer | ||
source [find interface/stlink-v2.cfg] |
16 changes: 16 additions & 0 deletions
16
examples/stm32f103c8t6_blue_pill/weight_scale_hx711/project.xml
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,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> |