]> www.fi.muni.cz Git - tinyboard.git/blob - projects/step-up/pwm.c
pwm.c: single PWM channel only
[tinyboard.git] / projects / step-up / pwm.c
1 #include <avr/io.h>
2 #include <avr/interrupt.h>
3 #include <util/delay.h>
4 #include <util/atomic.h>
5
6 #include "lights.h"
7
8 /*
9  * Single PWM channel on OC1B (pin PB4 of Tiny45).
10  * Counts from 0 to 0xFF, without OCR1C compare.
11  */
12
13 static void inline enable_pll()
14 {
15         /* Async clock */
16         PLLCSR = _BV(PLLE);
17
18         /* Synchronize to the phase lock */
19         _delay_us(100);
20         while ((PLLCSR & _BV(PLOCK)) == 0)
21                 ;
22         PLLCSR |= _BV(PCKE);
23 }
24
25 void init_pwm()
26 {
27         enable_pll();
28
29         TCCR1 = _BV(CTC1) | _BV(CS10);  // no clock prescaling
30         GTCCR = _BV(COM1B1) | _BV(PWM1B);
31
32         OCR1C = PWM_MAX;
33         OCR1B = 0;              // initial stride is 0
34
35         DDRB  &= ~_BV(PB4); // tristate it
36         PORTB &= ~_BV(PB4); // set to zero
37 }
38
39 #if 0
40 void susp_pwm()
41 {
42         unsigned char i;
43
44         for (i = 0; i < N_PWMLEDS; i++)
45                 pwm[i] = 0;
46
47         DDRB &= ~(_BV( PB1 ) | _BV( PB3 ) | _BV( PB5 ));
48         TCCR1D = TCCR1C = TCCR1B = TCCR1A = 0;
49         TIMSK = 0;
50         TIFR = 0;
51
52         PLLCSR &= ~(_BV(PLLE) | _BV(PCKE));
53 }
54 #endif
55
56 void pwm_off()
57 {
58         OCR1B = 0;
59         DDRB &= ~_BV(PB4);
60 }
61
62 void pwm_set(uint8_t stride)
63 {
64         OCR1B = stride;
65         DDRB |= _BV(PB4);
66 }