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=18a14253552a7c7628f1dc9dde24e1512af83437;hb=HEAD;hpb=3079ccda1c6d82058c801bf8192c616230a2ef93 diff --git a/projects/step-up/pwm.c b/projects/step-up/pwm.c index 18a1425..b93a4fa 100644 --- a/projects/step-up/pwm.c +++ b/projects/step-up/pwm.c @@ -11,10 +11,12 @@ * Counts from 0 to 0xFF, without OCR1C compare. */ +volatile unsigned char pwm_enabled; + static void inline enable_pll() { /* Async clock */ - PLLCSR = _BV(PLLE); + PLLCSR = _BV(PLLE) | _BV(LSM); /* Synchronize to the phase lock */ _delay_us(100); @@ -25,45 +27,48 @@ static void inline enable_pll() void init_pwm() { + pwm_enabled = 0; power_timer1_enable(); - enable_pll(); - - TCCR1 = _BV(CTC1) | _BV(CS10); // no clock prescaling + TCCR1 = _BV(CTC1) | _BV(CS11); // pll_clk/2 GTCCR = _BV(COM1B1) | _BV(PWM1B); OCR1C = PWM_MAX; OCR1B = 0; // initial stride is 0 - DDRB &= ~_BV(PB4); // tristate it + 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() { OCR1B = 0; DDRB &= ~_BV(PB4); + + PLLCSR &= ~(_BV(PLLE) | _BV(PCKE)); + power_timer1_disable(); + pwm_enabled = 0; } void pwm_set(uint8_t stride) { OCR1B = stride; - DDRB |= _BV(PB4); + + if (!pwm_enabled) { + power_timer1_enable(); + enable_pll(); + DDRB |= _BV(PB4); + pwm_enabled = 1; + } }