|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Raphael Lehmann |
| 3 | + * |
| 4 | + * This file is part of the modm project. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + */ |
| 10 | + |
| 11 | +#include <modm/board.hpp> |
| 12 | + |
| 13 | +using namespace Board; |
| 14 | + |
| 15 | +int |
| 16 | +main() |
| 17 | +{ |
| 18 | + Board::initialize(); |
| 19 | + LedD13::setOutput(); |
| 20 | + |
| 21 | + MODM_LOG_INFO << "modm example: Servo PWM" << modm::endl << modm::endl; |
| 22 | + MODM_LOG_INFO << "Generates a 50 Hz PWM signal with a pulse width between 1 ms and 2 ms suitable" << modm::endl; |
| 23 | + MODM_LOG_INFO << "for many servo motors or RC electronic speed controllers on pin B7." << modm::endl; |
| 24 | + MODM_LOG_INFO << "1.5 ms pulse width is output when the user button is pressed, " << modm::endl; |
| 25 | + MODM_LOG_INFO << "otherwise 1 ms pulse width." << modm::endl << modm::endl; |
| 26 | + |
| 27 | + Timer4::connect<GpioB7::Ch2>(); |
| 28 | + Timer4::enable(); |
| 29 | + Timer4::setMode(Timer4::Mode::UpCounter); |
| 30 | + |
| 31 | + constexpr uint16_t overflow = 0xffff; |
| 32 | + constexpr uint16_t minPwm = (overflow / 20.f) * 1.f; // 1 ms pulse width |
| 33 | + constexpr uint16_t targetPwm = (overflow / 20.f) * 1.5f; // 1.5 ms pulse width |
| 34 | + constexpr uint16_t maxPwm = (overflow / 20.f) * 2.f; // 2 ms pulse width |
| 35 | + |
| 36 | + // 170 MHz / 52 / 2^16 = approx. 50 Hz |
| 37 | + Timer4::setPrescaler(52); |
| 38 | + Timer4::setOverflow(overflow); |
| 39 | + |
| 40 | + Timer4::configureOutputChannel(2, Timer4::OutputCompareMode::Pwm, 0); |
| 41 | + |
| 42 | + Timer4::applyAndReset(); |
| 43 | + |
| 44 | + Timer4::start(); |
| 45 | + Timer4::enableOutput(); |
| 46 | + |
| 47 | + MODM_LOG_INFO << "Teaching sequence for electronic speed controllers:" << modm::endl; |
| 48 | + MODM_LOG_INFO << "1.0 ms pulse width for 2 s" << modm::endl; |
| 49 | + MODM_LOG_INFO << "2.0 ms pulse width for 2 s" << modm::endl; |
| 50 | + MODM_LOG_INFO << "1.0 ms pulse width for 1 s" << modm::endl; |
| 51 | + LedD13::set(); |
| 52 | + Timer4::configureOutputChannel(2, Timer4::OutputCompareMode::Pwm, minPwm); |
| 53 | + modm::delay(2000ms); |
| 54 | + Timer4::configureOutputChannel(2, Timer4::OutputCompareMode::Pwm, maxPwm); |
| 55 | + modm::delay(2000ms); |
| 56 | + Timer4::configureOutputChannel(2, Timer4::OutputCompareMode::Pwm, minPwm); |
| 57 | + LedD13::reset(); |
| 58 | + modm::delay(1000ms); |
| 59 | + |
| 60 | + MODM_LOG_INFO << modm::endl; |
| 61 | + |
| 62 | + while (true) |
| 63 | + { |
| 64 | + LedD13::toggle(); |
| 65 | + modm::delay(100ms); |
| 66 | + |
| 67 | + if (!Button::read()) { |
| 68 | + Timer4::setCompareValue(2, targetPwm); |
| 69 | + MODM_LOG_INFO << "^"; |
| 70 | + } |
| 71 | + else { |
| 72 | + Timer4::setCompareValue(2, minPwm); |
| 73 | + MODM_LOG_INFO << "-"; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return 0; |
| 78 | +} |
0 commit comments