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=b93a4fab8502039d73d811c69b96592725c5bad5;hp=85d6c6820c4e3768a12016ceae8f75ec021ee2c4;hb=HEAD;hpb=899ce95294ad696dbd04e2f5722fbbc00f9dec2e diff --git a/projects/step-up/pwm.c b/projects/step-up/pwm.c index 85d6c68..b93a4fa 100644 --- a/projects/step-up/pwm.c +++ b/projects/step-up/pwm.c @@ -1,23 +1,22 @@ #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 > 0x0FF -#error PWM_TOP too high -#endif +/* + * Single PWM channel on OC1B (pin PB4 of Tiny45). + * Counts from 0 to 0xFF, without OCR1C compare. + */ -static uint16_t pwm[N_PWMLEDS]; -static volatile unsigned char step; +volatile unsigned char pwm_enabled; -static void enable_pll() +static void inline enable_pll() { /* Async clock */ - PLLCSR = _BV(PLLE); + PLLCSR = _BV(PLLE) | _BV(LSM); /* Synchronize to the phase lock */ _delay_us(100); @@ -28,94 +27,48 @@ static void enable_pll() void init_pwm() { - int i; + pwm_enabled = 0; + power_timer1_enable(); - step = 0; + TCCR1 = _BV(CTC1) | _BV(CS11); // pll_clk/2 + GTCCR = _BV(COM1B1) | _BV(PWM1B); - for (i = 0; i < N_PWMLEDS; i++) - pwm[i] = 0; + OCR1C = PWM_MAX; + OCR1B = 0; // initial stride is 0 - enable_pll(); - - TCCR1 = _BV(CTC1) | _BV(CS10); // no clock prescaling - GTCCR = _BV(COM1A1) | _BV(COM1B1) | _BV(PWM1A) | _BV(PWM1B); - - OCR1C = PWM_TOP; - OCR1A = OCR1B = 0; // initial stride is 0 - - DDRB &= ~(_BV( PB1 ) | _BV( PB4 )); // tristate it - PORTB &= ~(_BV( PB1 ) | _BV( PB4 )); // set to zero + DDRB &= ~(_BV( PB4 )); + PORTB &= ~_BV(PB4); // set to zero } -#if 0 void susp_pwm() { - unsigned char i; - - for (i = 0; i < N_PWMLEDS; i++) - pwm[i] = 0; - - DDRB &= ~(_BV( PB1 ) | _BV( PB3 ) | _BV( PB5 )); - TCCR1D = TCCR1C = TCCR1B = TCCR1A = 0; + DDRB &= ~(_BV( PB4 )); + PORTB &= ~(_BV( PB4 )); + TCCR1 = 0; TIMSK = 0; TIFR = 0; 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(PB4); break; - } - } -} -static void pwm_update_hw(unsigned char n) +void pwm_off() { - uint16_t stride = (pwm[n] + step) >> PWM_STEP_SHIFT; + OCR1B = 0; + DDRB &= ~_BV(PB4); - switch (n) { - case 0: - OCR1A = stride; - break; - case 1: - OCR1B = stride; - break; - } + PLLCSR &= ~(_BV(PLLE) | _BV(PCKE)); + power_timer1_disable(); + pwm_enabled = 0; } -void pwm_set(unsigned char n, uint16_t stride) +void pwm_set(uint8_t stride) { - if (stride > PWM_MAX) - stride = PWM_MAX; - - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - pwm[n] = stride; + OCR1B = stride; - pwm_update_hw(n); - - switch(n) { - case 0: DDRB |= _BV(PB1); break; - case 1: DDRB |= _BV(PB4); break; - } + if (!pwm_enabled) { + power_timer1_enable(); + enable_pll(); + DDRB |= _BV(PB4); + pwm_enabled = 1; } } - -void pwm_timer() -{ - 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); -} -