From 0963c889f065b60c9e42b524483dc94d260ba549 Mon Sep 17 00:00:00 2001 From: Robert Pelnar Date: Mon, 21 Jan 2019 17:57:07 +0100 Subject: [PATCH] Fast PWM for bed - timer2 used as system timer --- Firmware/temperature.cpp | 17 +++++-- Firmware/temperature.h | 4 +- Firmware/timer02.c | 103 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 Firmware/timer02.c diff --git a/Firmware/temperature.cpp b/Firmware/temperature.cpp index cae3fada..97300955 100644 --- a/Firmware/temperature.cpp +++ b/Firmware/temperature.cpp @@ -42,6 +42,12 @@ #include "ConfigurationStore.h" +extern "C" { +extern void timer02_init(void); +extern void timer02_set_pwm0(uint8_t pwm0); +} + + //=========================================================================== //=============================public variables============================ //=========================================================================== @@ -983,7 +989,6 @@ static void updateTemperaturesFromRawValues() CRITICAL_SECTION_END; } - void tp_init() { #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1)) @@ -1050,10 +1055,12 @@ void tp_init() adc_init(); + timer02_init(); + // Use timer0 for temperature measurement // Interleave temperature interrupt with millies interrupt - OCR0B = 128; - TIMSK0 |= (1< +#include +#include + + +uint8_t timer02_pwm0 = 0; + +void timer02_set_pwm0(uint8_t pwm0) +{ + if (timer02_pwm0 == pwm0) return; + if (pwm0) + { + TCCR0A |= (2 << COM0B0); + OCR0B = pwm0 - 1; + } + else + { + TCCR0A &= ~(2 << COM0B0); + OCR0B = 0; + } +} + +void timer02_init(void) +{ + //save sreg + uint8_t _sreg = SREG; + //disable interrupts for sure + cli(); + //mask timer0 interrupts - disable all + TIMSK0 &= ~(1<> 3) +#define FRACT_MAX (1000 >> 3) + +extern volatile unsigned long timer0_overflow_count; +extern volatile unsigned long timer0_millis; +unsigned char timer0_fract = 0; + +ISR(TIMER2_OVF_vect) +{ + // copy these to local variables so they can be stored in registers + // (volatile variables must be read from memory on every access) + unsigned long m = timer0_millis; + unsigned char f = timer0_fract; + + m += MILLIS_INC; + f += FRACT_INC; + if (f >= FRACT_MAX) + { + f -= FRACT_MAX; + m += 1; + } + + timer0_fract = f; + timer0_millis = m; + timer0_overflow_count++; +} +