From 19e35b473a4f4b83da3f8357c17f8e7aeba1ae57 Mon Sep 17 00:00:00 2001 From: Josh Bailey Date: Sun, 5 Jan 2020 18:05:11 +1300 Subject: [PATCH] Fix large error in PW when PW close to ISR period. --- CRIO.cpp | 37 ++++++++++++++----------------------- CRIO.h | 5 +++-- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/CRIO.cpp b/CRIO.cpp index 4fe3475..0decaa0 100644 --- a/CRIO.cpp +++ b/CRIO.cpp @@ -20,7 +20,7 @@ DigitalPin _fixedVarInPin(INPUT, LOW); DigitalPin _percussionEnableInPin(INPUT, LOW); #include "CRIO.h" -#include "constants.h" + CRLiquidCrystal lcd(lcd_rs, lcd_rw, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7); @@ -89,15 +89,6 @@ inline void CRIO::pulseOff() { _pulseState = false; } -void CRIO::oneShotPulse() { - if (_oneShotPulseUs) { - pulseOn(); - delayMicroseconds(_oneShotPulseUs); - pulseOff(); - _oneShotPulseUs = 0; - } -} - void CRIO::handlePulse() { ++_ticksSinceLastPulse; if (scheduled) { @@ -114,20 +105,12 @@ void CRIO::handlePulse() { void CRIO::startPulse() { if (scheduled) { _ticksSinceLastPulse = 0; - if (_multiShotPulses) { - if (_oneShotPulseUs) { - if (_oneShotPulseUs >= (masterClockPeriodUs - oneShotPulsePadUs)) { - ++_multiShotPulses; - } else if (_oneShotPulseUs > oneShotPulsePadUs) { - delayMicroseconds((masterClockPeriodUs - _oneShotPulseUs) - oneShotPulsePadUs); - pulseOn(); - } - _oneShotPulseUs = 0; - } - } else { - oneShotPulse(); - } scheduled = false; + if (_oneShotPulseUs) { + delayMicroseconds(masterClockPeriodUs - _oneShotPulseUs); + _oneShotPulseUs = 0; + pulseOn(); + } } } @@ -149,6 +132,14 @@ void CRIO::schedulePulse(cr_fp_t pulseUs) { } else { _multiShotPulses = 0; } + if (_oneShotPulseUs) { + if (_oneShotPulseUs > oneShotRemainderPulsePadUs) { + ++_multiShotPulses; + _oneShotPulseUs = 0; + } else if (_oneShotPulseUs < oneShotPulsePadUs) { + _oneShotPulseUs = oneShotPulsePadUs; + } + } } inline cr_fp_t CRIOLcd::_scalePot(uint8_t pin) { diff --git a/CRIO.h b/CRIO.h index e1c99a1..c1d37b6 100644 --- a/CRIO.h +++ b/CRIO.h @@ -12,12 +12,14 @@ #define CRIO_H #include "types.h" +#include "constants.h" const uint8_t lcdWidth = 16; const uint8_t lcdLines = 2; const uint8_t analogBits = 12; const cr_fp_t maxAnalogRead = cr_fp_t(1) / cr_fp_t(4095); -const uint8_t oneShotPulsePadUs = 2; +const uint8_t oneShotPulsePadUs = 1; +const uint8_t oneShotRemainderPulsePadUs = masterClockPeriodUs - oneShotPulsePadUs; const uint8_t potPins = 3; const uint8_t potSampleWindow = 5; @@ -45,7 +47,6 @@ class CRIO { uint8_t maxPitch; cr_fp_t maxChargePct; private: - void oneShotPulse(); void pulseOff(); void pulseOn(); bool _pulseState;