diff --git a/examples/arduino_nano/encoder_input/main.cpp b/examples/arduino_nano/encoder_input/main.cpp new file mode 100644 index 0000000000..65bb5e1e8a --- /dev/null +++ b/examples/arduino_nano/encoder_input/main.cpp @@ -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 +#include +#include +#include + +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 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; + } + } +} diff --git a/examples/arduino_nano/encoder_input/project.xml b/examples/arduino_nano/encoder_input/project.xml new file mode 100644 index 0000000000..703c79eff9 --- /dev/null +++ b/examples/arduino_nano/encoder_input/project.xml @@ -0,0 +1,11 @@ + + modm:arduino-nano + + + + + modm:build:scons + modm:processing:timer + modm:driver:encoder_input.bitbang + +