Timing functions micros2 and delay2 + fix of delay_keep_alive().

This commit is contained in:
Robert Pelnar 2019-01-27 17:23:53 +01:00
parent 579fb42be2
commit fc0b55b213
5 changed files with 71 additions and 9 deletions

View File

@ -17,6 +17,7 @@
#include <avr/eeprom.h> #include <avr/eeprom.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include "timer02.h"
#include "fastio.h" #include "fastio.h"
#include "Configuration.h" #include "Configuration.h"

View File

@ -7809,10 +7809,10 @@ void delay_keep_alive(unsigned int ms)
if (ms == 0) if (ms == 0)
break; break;
else if (ms >= 50) { else if (ms >= 50) {
delay(50); delay2(50);
ms -= 50; ms -= 50;
} else { } else {
delay(ms); delay2(ms);
ms = 0; ms = 0;
} }
} }

View File

@ -45,11 +45,6 @@
#include "Configuration_prusa.h" #include "Configuration_prusa.h"
extern "C" {
extern void timer02_init(void);
extern void timer02_set_pwm0(uint8_t pwm0);
}
//=========================================================================== //===========================================================================
//=============================public variables============================ //=============================public variables============================

View File

@ -87,7 +87,6 @@ ISR(TIMER2_OVF_vect)
// (volatile variables must be read from memory on every access) // (volatile variables must be read from memory on every access)
unsigned long m = timer0_millis; unsigned long m = timer0_millis;
unsigned char f = timer0_fract; unsigned char f = timer0_fract;
m += MILLIS_INC; m += MILLIS_INC;
f += FRACT_INC; f += FRACT_INC;
if (f >= FRACT_MAX) if (f >= FRACT_MAX)
@ -95,9 +94,45 @@ ISR(TIMER2_OVF_vect)
f -= FRACT_MAX; f -= FRACT_MAX;
m += 1; m += 1;
} }
timer0_fract = f; timer0_fract = f;
timer0_millis = m; timer0_millis = m;
timer0_overflow_count++; timer0_overflow_count++;
} }
unsigned long micros2()
{
unsigned long m;
uint8_t oldSREG = SREG, t;
cli();
m = timer0_overflow_count;
#if defined(TCNT2)
t = TCNT2;
#elif defined(TCNT2L)
t = TCNT2L;
#else
#error TIMER 2 not defined
#endif
#ifdef TIFR2
if ((TIFR2 & _BV(TOV2)) && (t < 255))
m++;
#else
if ((TIFR & _BV(TOV2)) && (t < 255))
m++;
#endif
SREG = oldSREG;
return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
}
void delay2(unsigned long ms)
{
uint32_t start = micros2();
while (ms > 0)
{
yield();
while ( ms > 0 && (micros2() - start) >= 1000)
{
ms--;
start += 1000;
}
}
}

31
Firmware/timer02.h Normal file
View File

@ -0,0 +1,31 @@
//timer02.h
// use atmega timer2 as main system timer instead of timer0
// timer0 is used for fast pwm (OC0B output)
// original OVF handler is disabled
#ifndef TIMER02_H
#define TIMER02_H
#include <inttypes.h>
#if defined(__cplusplus)
extern "C" {
#endif //defined(__cplusplus)
extern uint8_t timer02_pwm0;
extern void timer02_set_pwm0(uint8_t pwm0);
extern void timer02_init(void);
extern unsigned long micros2(void);
extern void delay2(unsigned long ms);
#if defined(__cplusplus)
}
#endif //defined(__cplusplus)
#endif //TIMER02_H