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;fp=firmware%2Fpwm.c;h=0c4f3d081a5b78ddbc9f2493255a17b0da6496bf;hp=0000000000000000000000000000000000000000;hb=f956c1fa7f47b0e8b8afe323c2eff1b6c2607c2a;hpb=f7cba12a75f10da7267c2e4d4488bb5ccddbb9d9 diff --git a/firmware/pwm.c b/firmware/pwm.c new file mode 100644 index 0000000..0c4f3d0 --- /dev/null +++ b/firmware/pwm.c @@ -0,0 +1,75 @@ +#include +#include +#include + +#include "lights.h" + +void init_pwm() +{ + /* Async clock */ + PLLCSR = _BV(PLLE); + + /* Synchronize to the phase lock */ + _delay_ms(1); + while ((PLLCSR & _BV(PLOCK)) == 0) + ; + PLLCSR |= _BV(PCKE); + + TCCR1C = _BV(COM1D0) | _BV(COM1D1) | _BV(PWM1D); + TCCR1A = _BV(COM1A0) | _BV(COM1A1) | _BV(COM1B0) | _BV(COM1B1) | _BV(PWM1A) | _BV(PWM1B); + TCCR1B = _BV(7) // PWM1X: PWM inversion mode + | _BV(CS10) // no clock prescaling + ; + TC1H = 0x01; + OCR1C = 0xFF; // TOP value + + TC1H = 0x00; + OCR1D = 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 +} + +void pwm_on(unsigned char n) +{ + switch (n) { + case 0: DDRB |= _BV(PB1); break; + case 1: DDRB |= _BV(PB3); break; + case 2: DDRB |= _BV(PB5); break; + } +} + +void pwm_off(unsigned char n) +{ + switch (n) { + case 0: DDRB &= ~_BV(PB1); break; + case 1: DDRB &= ~_BV(PB3); break; + case 2: DDRB &= ~_BV(PB5); break; + } +} + +void pwm_set(unsigned char n, unsigned char stride) +{ + TC1H = 0x00; + switch (n) { + case 0: OCR1A = stride; break; + case 1: OCR1B = stride; break; + case 2: OCR1D = stride; break; + } +} + +#if 0 +static void inline pwm_handler() +{ + OCR1A = pwmval[0]; + OCR1B = pwmval[1]; + OCR1D = pwmval[2]; + TIMSK &= ~_BV(TOIE1); +} + +ISR(TIMER1_OVF_vect) +{ + pwm_handler(); +} +#endif +