From 1f0dd329452ace1f1c7635353ef0a810b99811a8 Mon Sep 17 00:00:00 2001 From: AlbertEEWork Date: Sun, 8 Jun 2025 11:24:10 +0200 Subject: [PATCH 1/4] Added a change to the Sart function where interupt triggered by reseting TCNT1 = 0; --- TimerOne.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/TimerOne.h b/TimerOne.h index e2b2bd7..eec8b6d 100644 --- a/TimerOne.h +++ b/TimerOne.h @@ -228,7 +228,9 @@ class TimerOne //**************************** void start() __attribute__((always_inline)) { TCCR1B = 0; - TCNT1 = 0; // TODO: does this cause an undesired interrupt? + noInterrupts(); + TCNT1 = 0; // This Causes an interupt. + interrupts(); resume(); } void stop() __attribute__((always_inline)) { From 075d3fa54c5eadbe23f0ccc7f001a6cf5fd6e60a Mon Sep 17 00:00:00 2001 From: AlbertEEWork Date: Sun, 8 Jun 2025 16:38:46 +0200 Subject: [PATCH 2/4] Update TimerOne.h --- TimerOne.h | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/TimerOne.h b/TimerOne.h index eec8b6d..b831d2d 100644 --- a/TimerOne.h +++ b/TimerOne.h @@ -198,27 +198,35 @@ class TimerOne const unsigned long cycles = ((F_CPU/100000 * microseconds) / 20); if (cycles < TIMER1_RESOLUTION) { clockSelectBits = _BV(CS10); + prescaler = 1; pwmPeriod = cycles; } else if (cycles < TIMER1_RESOLUTION * 8) { clockSelectBits = _BV(CS11); - pwmPeriod = cycles / 8; + prescaler = 8; + pwmPeriod = cycles / prescaler; } else if (cycles < TIMER1_RESOLUTION * 64) { clockSelectBits = _BV(CS11) | _BV(CS10); - pwmPeriod = cycles / 64; + prescaler = 64; + pwmPeriod = cycles / prescaler; } else if (cycles < TIMER1_RESOLUTION * 256) { clockSelectBits = _BV(CS12); - pwmPeriod = cycles / 256; + prescaler = 256; + pwmPeriod = cycles / prescaler; } else if (cycles < TIMER1_RESOLUTION * 1024) { clockSelectBits = _BV(CS12) | _BV(CS10); - pwmPeriod = cycles / 1024; + prescaler = 1024; + pwmPeriod = cycles / prescaler; + } else { clockSelectBits = _BV(CS12) | _BV(CS10); + prescaler = 1024; pwmPeriod = TIMER1_RESOLUTION - 1; } + ICR1 = pwmPeriod; TCCR1B = _BV(WGM13) | clockSelectBits; } @@ -305,6 +313,7 @@ class TimerOne // properties static unsigned short pwmPeriod; static unsigned char clockSelectBits; + static unsigned short prescaler; From fc812ff15265195af9af6a02a6eaa0c473e4730c Mon Sep 17 00:00:00 2001 From: AlbertEEWork Date: Sun, 8 Jun 2025 23:31:19 +0200 Subject: [PATCH 3/4] Added Getting Elapsed functionality, this took to Fn long,.....fugggg --- TimerOne.cpp | 1 + TimerOne.h | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/TimerOne.cpp b/TimerOne.cpp index 0e0cb26..ab815dc 100644 --- a/TimerOne.cpp +++ b/TimerOne.cpp @@ -23,6 +23,7 @@ TimerOne Timer1; // preinstantiate #if !defined(ESP32) unsigned short TimerOne::pwmPeriod = 0; unsigned char TimerOne::clockSelectBits = 0; +unsigned short TimerOne::prescaler = 1; void (*TimerOne::isrCallback)() = TimerOne::isrDefaultUnused; void TimerOne::isrDefaultUnused() { /* noop */; } #endif // not ESP32 diff --git a/TimerOne.h b/TimerOne.h index b831d2d..c099425 100644 --- a/TimerOne.h +++ b/TimerOne.h @@ -195,7 +195,7 @@ class TimerOne setPeriod(microseconds); } void setPeriod(unsigned long microseconds) __attribute__((always_inline)) { - const unsigned long cycles = ((F_CPU/100000 * microseconds) / 20); + const unsigned long cycles = ((F_CPU/1000000 * microseconds) / 2); if (cycles < TIMER1_RESOLUTION) { clockSelectBits = _BV(CS10); prescaler = 1; @@ -236,9 +236,9 @@ class TimerOne //**************************** void start() __attribute__((always_inline)) { TCCR1B = 0; - noInterrupts(); - TCNT1 = 0; // This Causes an interupt. - interrupts(); + //noInterrupts(); + TCNT1 = 1; // This Causes an interupt. + //interrupts(); resume(); } void stop() __attribute__((always_inline)) { @@ -250,6 +250,19 @@ class TimerOne void resume() __attribute__((always_inline)) { TCCR1B = _BV(WGM13) | clockSelectBits; } + inline unsigned long elapsed() __attribute__((always_inline)) { + unsigned long t0 = TCNT1; + unsigned long time = 0; + delayMicroseconds(200); + time = TCNT1; + if ((int)((int)time-(int)t0)>0) //upcount + { + time = t0*(prescaler/(F_CPU/1000000UL)); + }else{ //downcount + time = (2*pwmPeriod-t0)*(prescaler/(F_CPU/1000000UL)); + } + return time; + } //**************************** // PWM outputs From c84e820029db168cadad0070f36c9707cff48e04 Mon Sep 17 00:00:00 2001 From: AlbertEEWork Date: Sun, 22 Jun 2025 16:50:53 +0200 Subject: [PATCH 4/4] Fixed a bug with overflow int. AVR 328p int is only 16bits. Also added using TIFR register to check for IC flag --- TimerOne.cpp | 3 +++ TimerOne.h | 20 +++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/TimerOne.cpp b/TimerOne.cpp index ab815dc..722397f 100644 --- a/TimerOne.cpp +++ b/TimerOne.cpp @@ -22,6 +22,8 @@ TimerOne Timer1; // preinstantiate #if !defined(ESP32) unsigned short TimerOne::pwmPeriod = 0; +unsigned long TimerOne::totalTime = 0; +unsigned long TimerOne::timeInterval = 0; unsigned char TimerOne::clockSelectBits = 0; unsigned short TimerOne::prescaler = 1; void (*TimerOne::isrCallback)() = TimerOne::isrDefaultUnused; @@ -37,6 +39,7 @@ ISR(TIMER1_COMPA_vect) #elif defined(__AVR__) ISR(TIMER1_OVF_vect) { + TIFR1 &= _BV(ICF1); Timer1.isrCallback(); } #elif defined(__arm__) && defined(TEENSYDUINO) && (defined(KINETISK) || defined(KINETISL)) diff --git a/TimerOne.h b/TimerOne.h index c099425..2daa8e3 100644 --- a/TimerOne.h +++ b/TimerOne.h @@ -215,6 +215,8 @@ class TimerOne clockSelectBits = _BV(CS12); prescaler = 256; pwmPeriod = cycles / prescaler; + Serial.println("H3"); + Serial.println(2*((unsigned long)pwmPeriod)); } else if (cycles < TIMER1_RESOLUTION * 1024) { clockSelectBits = _BV(CS12) | _BV(CS10); @@ -226,7 +228,8 @@ class TimerOne prescaler = 1024; pwmPeriod = TIMER1_RESOLUTION - 1; } - + totalTime = 2*((unsigned long)(pwmPeriod)); + timeInterval = ((unsigned long)prescaler/(F_CPU/1000000UL)); ICR1 = pwmPeriod; TCCR1B = _BV(WGM13) | clockSelectBits; } @@ -252,16 +255,13 @@ class TimerOne } inline unsigned long elapsed() __attribute__((always_inline)) { unsigned long t0 = TCNT1; - unsigned long time = 0; - delayMicroseconds(200); - time = TCNT1; - if ((int)((int)time-(int)t0)>0) //upcount + if (TIFR1 & _BV(ICF1)) //downcount { - time = t0*(prescaler/(F_CPU/1000000UL)); - }else{ //downcount - time = (2*pwmPeriod-t0)*(prescaler/(F_CPU/1000000UL)); + t0 = (totalTime-t0)*timeInterval; + }else{ //upcount + t0 = t0*timeInterval; } - return time; + return t0; } //**************************** @@ -327,6 +327,8 @@ class TimerOne static unsigned short pwmPeriod; static unsigned char clockSelectBits; static unsigned short prescaler; + static unsigned long totalTime; + static unsigned long timeInterval;