From 1adcdc77d7a5b1c163491ba927efcc3c3b670936 Mon Sep 17 00:00:00 2001 From: Urias Uhle Date: Sat, 29 Aug 2026 02:00:55 +0200 Subject: [PATCH] Support 8MHz boards via is16MHz flag in begin() --- src/PPMEncoder.cpp | 15 ++++++++++----- src/PPMEncoder.h | 2 ++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/PPMEncoder.cpp b/src/PPMEncoder.cpp index 6aa4b7f..a83f750 100644 --- a/src/PPMEncoder.cpp +++ b/src/PPMEncoder.cpp @@ -3,14 +3,18 @@ PPMEncoder ppmEncoder; void PPMEncoder::begin(uint8_t pin) { - begin(pin, PPM_DEFAULT_CHANNELS, false); + begin(pin, PPM_DEFAULT_CHANNELS, false, true); } void PPMEncoder::begin(uint8_t pin, uint8_t ch) { - begin(pin, ch, false); + begin(pin, ch, false, true); } void PPMEncoder::begin(uint8_t pin, uint8_t ch, boolean inverted) { + begin(pin, ch, inverted, true); +} + +void PPMEncoder::begin(uint8_t pin, uint8_t ch, boolean inverted, boolean is16MHz) { cli(); // Store on/off-State in variable to avoid another if in timing-critical interrupt @@ -27,6 +31,7 @@ void PPMEncoder::begin(uint8_t pin, uint8_t ch, boolean inverted) { numChannels = ch; outputPin = pin; + clockMultiplier = is16MHz ? 2 : 1; for (uint8_t ch = 0; ch < numChannels; ch++) { setChannelPercent(ch, 0); @@ -72,7 +77,7 @@ void PPMEncoder::interrupt() { if (state) { digitalWrite(outputPin, onState); - OCR1A = PPM_PULSE_LENGTH_uS * 2; + OCR1A = PPM_PULSE_LENGTH_uS * clockMultiplier; } else { digitalWrite(outputPin, offState); @@ -80,10 +85,10 @@ void PPMEncoder::interrupt() { if (currentChannel >= numChannels) { currentChannel = 0; elapsedUs = elapsedUs + PPM_PULSE_LENGTH_uS; - OCR1A = (PPM_FRAME_LENGTH_uS - elapsedUs) * 2; + OCR1A = (PPM_FRAME_LENGTH_uS - elapsedUs) * clockMultiplier; elapsedUs = 0; } else { - OCR1A = (channels[currentChannel] - PPM_PULSE_LENGTH_uS) * 2; + OCR1A = (channels[currentChannel] - PPM_PULSE_LENGTH_uS) * clockMultiplier; elapsedUs = elapsedUs + channels[currentChannel]; currentChannel++; diff --git a/src/PPMEncoder.h b/src/PPMEncoder.h index bacc0c9..fa8a9b9 100644 --- a/src/PPMEncoder.h +++ b/src/PPMEncoder.h @@ -23,6 +23,7 @@ class PPMEncoder { byte outputPin; boolean state; boolean enabled; + uint8_t clockMultiplier; uint8_t onState; uint8_t offState; @@ -38,6 +39,7 @@ class PPMEncoder { void begin(uint8_t pin); void begin(uint8_t pin, uint8_t ch); void begin(uint8_t pin, uint8_t ch, boolean inverted); + void begin(uint8_t pin, uint8_t ch, boolean inverted, boolean is16MHz); void disable(); void enable();