X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=tinyboard.git;a=blobdiff_plain;f=projects%2Fstep-up%2Fpwm.c;h=18a14253552a7c7628f1dc9dde24e1512af83437;hp=b47227375703f355198f724f63586cdba5937aa7;hb=8b54d4bdf1305a636d6c1d03e6a725061f47c612;hpb=edebb613b2f867d4f8473747744f329cb30e38fe diff --git a/projects/step-up/pwm.c b/projects/step-up/pwm.c index b472273..18a1425 100644 --- a/projects/step-up/pwm.c +++ b/projects/step-up/pwm.c @@ -1,20 +1,17 @@ #include #include +#include #include #include #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 > 0x3FF -#error PWM_TOP too high -#endif - -static uint16_t pwm[N_PWMLEDS]; -static volatile unsigned char step; +/* + * Single PWM channel on OC1B (pin PB4 of Tiny45). + * Counts from 0 to 0xFF, without OCR1C compare. + */ -static void enable_pll() +static void inline enable_pll() { /* Async clock */ PLLCSR = _BV(PLLE); @@ -28,35 +25,21 @@ static void enable_pll() void init_pwm() { - int i; - - step = 0; - - for (i = 0; i < N_PWMLEDS; i++) - pwm[i] = 0; + power_timer1_enable(); enable_pll(); - // PWM channel D is inverted, ... - TCCR1C = _BV(COM1D1) | _BV(COM1D0) | _BV(PWM1D); - // PWM channels A and B are not - TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(PWM1A) | _BV(PWM1B); - TCCR1D = 0; - TCCR1B = _BV(CS10); // no clock prescaling - - TC1H = PWM_TOP >> 8; - OCR1C = PWM_TOP & 0xFF; // TOP value + TCCR1 = _BV(CTC1) | _BV(CS10); // no clock prescaling + GTCCR = _BV(COM1B1) | _BV(PWM1B); - TC1H = PWM_TOP >> 8; // PWM3 is inverted - OCR1D = PWM_TOP & 0xFF; + OCR1C = PWM_MAX; + OCR1B = 0; // initial stride is 0 - TC1H = 0x00; - OCR1B = OCR1A = 0; // initial stride is 0 - - DDRB &= ~(_BV( PB1 ) | _BV( PB3 ) | _BV( PB5 )); // tristate it - PORTB &= ~(_BV( PB1 ) | _BV( PB3 ) | _BV( PB5 )); // set to zero + DDRB &= ~_BV(PB4); // tristate it + PORTB &= ~_BV(PB4); // set to zero } +#if 0 void susp_pwm() { unsigned char i; @@ -71,74 +54,16 @@ void susp_pwm() PLLCSR &= ~(_BV(PLLE) | _BV(PCKE)); } +#endif -void pwm_off(unsigned char n) -{ - 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; - } - } -} - -static void pwm_update_hw(unsigned char n) -{ - unsigned char hi, lo; - uint16_t stride = (pwm[n] + step) >> PWM_STEP_SHIFT; - - if (n == 2) - stride = PWM_TOP - stride; - - hi = stride >> 8; - lo = stride & 0xFF; - - switch (n) { - case 0: - TC1H = hi; - OCR1A = lo; - break; - case 1: - TC1H = hi; - OCR1B = lo; - break; - case 2: - TC1H = hi; - OCR1D = lo; - break; - } -} - -void pwm_set(unsigned char n, uint16_t stride) +void pwm_off() { - if (stride > PWM_MAX) - stride = PWM_MAX; - - 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; - } - } + OCR1B = 0; + DDRB &= ~_BV(PB4); } -void pwm_timer() +void pwm_set(uint8_t stride) { - unsigned char i; - - if (++step >= (1 << PWM_STEP_SHIFT)) - step = 0; - - for (i = 0; i < N_PWMLEDS; i++) - if (pwm[i]) - pwm_update_hw(i); + OCR1B = stride; + DDRB |= _BV(PB4); } -