X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=bike-lights.git;a=blobdiff_plain;f=firmware%2Fpwm.c;fp=firmware%2Fpwm.c;h=56fb0884e125113a6885a3deb45cba9efe5ad6c8;hp=8c95619a8f06ec423abce77a25f0a32f59f55258;hb=8650676310d5304199ffe174b45a9a396c9610a7;hpb=43b162ab0d886c7f75a9e8702052ab90ae462189 diff --git a/firmware/pwm.c b/firmware/pwm.c index 8c95619..56fb088 100644 --- a/firmware/pwm.c +++ b/firmware/pwm.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "lights.h" @@ -67,12 +68,14 @@ void susp_pwm() void pwm_off(unsigned char n) { - pwm[n] = 0; - - switch (n) { - case 0: DDRB &= ~_BV(PB1); break; - case 1: DDRB &= ~_BV(PB3); break; - case 2: DDRB &= ~_BV(PB5); break; + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + pwm[n] = 0; + + switch (n) { + case 0: DDRB &= ~_BV(PB1); break; + case 1: DDRB &= ~_BV(PB3); break; + case 2: DDRB &= ~_BV(PB5); break; + } } } @@ -91,17 +94,14 @@ static void pwm_update_hw(unsigned char n) case 0: TC1H = hi; OCR1A = lo; - DDRB |= _BV(PB1); break; case 1: TC1H = hi; OCR1B = lo; - DDRB |= _BV(PB3); break; case 2: TC1H = hi; OCR1D = lo; - DDRB |= _BV(PB5); break; } } @@ -111,8 +111,17 @@ void pwm_set(unsigned char n, uint16_t stride) if (((stride + (1 << PWM_STEP_SHIFT)) >> PWM_STEP_SHIFT) >= PWM_MAX) stride = PWM_MAX << PWM_STEP_SHIFT; - pwm[n] = stride; - pwm_update_hw(n); + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + pwm[n] = stride; + + pwm_update_hw(n); + + switch(n) { + case 0: DDRB |= _BV(PB1); break; + case 1: DDRB |= _BV(PB3); break; + case 2: DDRB |= _BV(PB5); break; + } + } } void pwm_timer()