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-1%2Fpwm.c;fp=projects%2Fstep-up-1%2Fpwm.c;h=b93a4fab8502039d73d811c69b96592725c5bad5;hp=0000000000000000000000000000000000000000;hb=9b7f892e6c70030468504d8a6da753bf1334ef94;hpb=aebe03ad223d877fed3f27fb4a3f459e2c947670 diff --git a/projects/step-up-1/pwm.c b/projects/step-up-1/pwm.c new file mode 100644 index 0000000..b93a4fa --- /dev/null +++ b/projects/step-up-1/pwm.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include + +#include "lights.h" + +/* + * Single PWM channel on OC1B (pin PB4 of Tiny45). + * Counts from 0 to 0xFF, without OCR1C compare. + */ + +volatile unsigned char pwm_enabled; + +static void inline enable_pll() +{ + /* Async clock */ + PLLCSR = _BV(PLLE) | _BV(LSM); + + /* Synchronize to the phase lock */ + _delay_us(100); + while ((PLLCSR & _BV(PLOCK)) == 0) + ; + PLLCSR |= _BV(PCKE); +} + +void init_pwm() +{ + pwm_enabled = 0; + power_timer1_enable(); + + 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 )); + PORTB &= ~_BV(PB4); // set to zero +} + +void susp_pwm() +{ + DDRB &= ~(_BV( PB4 )); + PORTB &= ~(_BV( PB4 )); + TCCR1 = 0; + TIMSK = 0; + TIFR = 0; + + PLLCSR &= ~(_BV(PLLE) | _BV(PCKE)); +} + +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; + + if (!pwm_enabled) { + power_timer1_enable(); + enable_pll(); + DDRB |= _BV(PB4); + pwm_enabled = 1; + } +}