-
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.
[examples] Add BitBangEncoderInput example on AVR
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2021, Thomas Sommer | ||
* | ||
* 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/driver/encoder/bitbang_encoder_input.hpp> | ||
#include <modm/math/algorithm/prescaler.hpp> | ||
#include <modm/processing/timer.hpp> | ||
|
||
using namespace modm::platform; | ||
|
||
// Connect the encoders outputs to D7 and D8 Pins (usually the outer pins) | ||
// The common third pin (usually in the middle) is connected to GND. | ||
// Don't add any resistors or filters. It's all in the MCU and the driver. | ||
modm::BitBangEncoderInput<Board::D11, Board::D12, 4> encoder; | ||
|
||
MODM_ISR(TIMER2_COMPA) | ||
{ | ||
encoder.update(); | ||
} | ||
|
||
void | ||
init_Timer2() | ||
{ | ||
constexpr float f = 1_kHz; | ||
constexpr auto prescaler = | ||
modm::Prescaler::from_list(SystemClock::Timer, f * 2, {0, 1, 8, 32, 64, 128, 256, 1024}); | ||
constexpr auto ocr = (prescaler.frequency / f) - 1; | ||
static_assert(ocr <= 255, "Can't configure Timer2 for desired f"); | ||
|
||
// Timer in CTC Mode | ||
TCCR2A = (1 << WGM21); | ||
OCR2A = ocr; | ||
TIMSK2 |= (1 << OCIE2A); | ||
TCCR2B |= prescaler.index; | ||
} | ||
|
||
int | ||
main() | ||
{ | ||
Board::initialize(); | ||
LedD13::setOutput(); | ||
|
||
encoder.connect(); | ||
|
||
init_Timer2(); | ||
enableInterrupts(); | ||
|
||
int value(0); | ||
|
||
modm::ShortPeriodicTimer heartbeat(500ms); | ||
modm::ShortPeriodicTimer outputValue(1000ms); | ||
|
||
while (true) | ||
{ | ||
if (heartbeat.execute()) Board::LedD13::toggle(); | ||
if (outputValue.execute()) | ||
{ | ||
value += encoder.getIncrement(); | ||
MODM_LOG_INFO << "value: " << value << modm::endl; | ||
} | ||
} | ||
} |
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,11 @@ | ||
<library> | ||
<extends>modm:arduino-nano</extends> | ||
<options> | ||
<option name="modm:build:build.path">../../../build/arduino_nano/encoder_input</option> | ||
</options> | ||
<modules> | ||
<module>modm:build:scons</module> | ||
<module>modm:processing:timer</module> | ||
<module>modm:driver:encoder_input.bitbang</module> | ||
</modules> | ||
</library> |