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;h=d68eeb2c3fdf370f69f32a8aa10b1afd68ff48a9;hp=56fb0884e125113a6885a3deb45cba9efe5ad6c8;hb=7f7665a5234d4d5cbd94fed9be55862772f3dc12;hpb=71966b0545b53262ef946c0ae26007664eff32ce diff --git a/firmware/pwm.c b/firmware/pwm.c index 56fb088..d68eeb2 100644 --- a/firmware/pwm.c +++ b/firmware/pwm.c @@ -5,6 +5,12 @@ #include "lights.h" +#define PWM_STEP_SHIFT 2 /* sub-LSB precision */ +#define PWM_TOP (((PWM_MAX) + (4 << (PWM_STEP_SHIFT))) >> (PWM_STEP_SHIFT)) +#if PWM_TOP > 0x1FF +#error PWM_TOP too high +#endif + static uint16_t pwm[N_PWMLEDS]; static volatile unsigned char step; @@ -38,11 +44,11 @@ void init_pwm() TCCR1D = 0; TCCR1B = _BV(CS10); // no clock prescaling - TC1H = PWM_MAX >> 8; - OCR1C = PWM_MAX & 0xFF; // TOP value + TC1H = PWM_TOP >> 8; + OCR1C = PWM_TOP & 0xFF; // TOP value - TC1H = PWM_MAX >> 8; // PWM3 is inverted - OCR1D = PWM_MAX & 0xFF; + TC1H = PWM_TOP >> 8; // PWM3 is inverted + OCR1D = PWM_TOP & 0xFF; TC1H = 0x00; OCR1B = OCR1A = 0; // initial stride is 0 @@ -85,7 +91,7 @@ static void pwm_update_hw(unsigned char n) uint16_t stride = (pwm[n] + step) >> PWM_STEP_SHIFT; if (n == 2) - stride = PWM_MAX - stride; + stride = PWM_TOP - stride; hi = stride >> 8; lo = stride & 0xFF; @@ -108,8 +114,8 @@ static void pwm_update_hw(unsigned char n) 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; + if (stride > PWM_MAX) + stride = PWM_MAX; ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { pwm[n] = stride;