Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions TimerOne.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ 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;
void TimerOne::isrDefaultUnused() { /* noop */; }
#endif // not ESP32
Expand All @@ -36,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))
Expand Down
38 changes: 32 additions & 6 deletions TimerOne.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,30 +195,41 @@ 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;
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;
Serial.println("H3");
Serial.println(2*((unsigned long)pwmPeriod));
} 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;
}
totalTime = 2*((unsigned long)(pwmPeriod));
timeInterval = ((unsigned long)prescaler/(F_CPU/1000000UL));
ICR1 = pwmPeriod;
TCCR1B = _BV(WGM13) | clockSelectBits;
}
Expand All @@ -228,7 +239,9 @@ class TimerOne
//****************************
void start() __attribute__((always_inline)) {
TCCR1B = 0;
TCNT1 = 0; // TODO: does this cause an undesired interrupt?
//noInterrupts();
TCNT1 = 1; // This Causes an interupt.
//interrupts();
resume();
}
void stop() __attribute__((always_inline)) {
Expand All @@ -240,6 +253,16 @@ class TimerOne
void resume() __attribute__((always_inline)) {
TCCR1B = _BV(WGM13) | clockSelectBits;
}
inline unsigned long elapsed() __attribute__((always_inline)) {
unsigned long t0 = TCNT1;
if (TIFR1 & _BV(ICF1)) //downcount
{
t0 = (totalTime-t0)*timeInterval;
}else{ //upcount
t0 = t0*timeInterval;
}
return t0;
}

//****************************
// PWM outputs
Expand Down Expand Up @@ -303,6 +326,9 @@ class TimerOne
// properties
static unsigned short pwmPeriod;
static unsigned char clockSelectBits;
static unsigned short prescaler;
static unsigned long totalTime;
static unsigned long timeInterval;



Expand Down