6 #include <avr/interrupt.h>
7 #include <avr/pgmspace.h>
10 #include <util/atomic.h>
14 static void timer0_init(void) { // 8, PWM
15 fprintf_P(stderr
,PSTR("\ntimers: init: timer0"));
16 power_timer0_enable();
19 TCCR1B
&= (uint8_t)~((1<<CS12
)|(1<<CS11
)|(1<<CS10
));
24 //TCCR0A&=~((1<<COM0A1)|(1<<COM0A0)|(1<<COM0B1)|(1<<COM0B0));
25 TCCR0A
|=(1<<COM0A1
)|(1<<COM0B1
);TCCR0A
&=(uint8_t)~((1<<COM0A0
)|(1<<COM0B0
));
29 //TCCR0B&=(uint8_t)~ (1<<WGM02);
30 //TCCR0A|= (1<<WGM01);
31 //TCCR0A&=(uint8_t)~ (1<<WGM00);
34 TCCR0B
&=(uint8_t)~ (1<<WGM02
);
35 TCCR0A
&=(uint8_t)~ (1<<WGM01
);
43 TIMSK0
=(0<<OCIE0A
)|(0<<OCIE0B
);
45 // Clock Select and timer enable {disable,1,8,64,256,1024,ExFalling,ExRising}
49 fprintf_P(stderr
,PSTR("\t[done]"));
52 static void timer1_init(void) { // 16, PWM
53 fprintf_P(stderr
,PSTR("\ntimers: init: timer1"));
54 power_timer1_enable();
57 TCCR1B
&= (uint8_t)~((1<<CS12
)|(1<<CS11
)|(1<<CS10
));
59 // OC1A, OC1B set to outputs
63 TCCR1A
|= (1<<COM1A1
)|(1<<COM1B1
);
64 TCCR1A
&= (uint8_t)~((1<<COM1A0
)|(1<<COM1B0
));
66 TCCR1B
|= (1<<5); //Reserved bit
70 TCCR1B
&= (uint8_t)~(1<<WGM12
);
71 TCCR1A
&= (uint8_t)~((1<<WGM11
)|(1<<WGM10
));
74 //TCCR1B&= (uint8_t)~((1<<WGM13)|(1<<WGM12));
75 //TCCR1A&= (uint8_t)~(1<<WGM11);
76 //TCCR1A|= (1<<WGM10);
78 // Input noise canceler (irrelavent in pwm mode 8?)
79 //TCCR1B&= (uint8_t)~(1<<ICNC1);
81 // TOP (mode 8, mode 1 top = 0xFF)
82 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
) {
90 TIMSK1
=(0<<ICIE1
)|(0<<OCIE1B
)|(0<<OCIE1A
)|(0<<TOIE1
);
92 // Prescale and Enable.
94 //TCCR1B&= (uint8_t)~((1<<CS12)|(1<<CS11));
95 TCCR1B
|= (uint8_t) (1<<CS10
);
97 fprintf_P(stderr
,PSTR("\t[done]"));
101 void timer2_init(void) { // 8, RTC
102 fprintf_P(stderr
,PSTR("\ntimers: init: timer2"));
103 power_timer2_enable();
106 TCCR2B
&=(uint8_t)~((1<<CS22
)|(1<<CS21
)|(1<<CS20
));
108 // Disable Pin outputs
109 TCCR2A
&=(uint8_t)~((1<<COM2A1
)|(1<<COM2A0
)|(1<<COM2B1
)|(1<<COM2B0
));
116 // Enable OCR2A interupt
123 //8000000/78/1024 == 100 HZ
124 //8000000/125/64 == 1000 Hz
127 TCCR2B
|=(1<<CS22
)|(1<<CS21
)|(1<<CS20
); //1024
130 TCCR2B
|=(1<<CS22
);TCCR2B
&=(uint8_t)~((1<<CS21
)|(1<<CS20
));//64
132 #error Invalid Timer2 Frequency
135 fprintf_P(stderr
,PSTR("\t[done]"));
138 ISR(TIMER2_COMPA_vect
) {
139 static uint16_t sec
;//=0
145 fprintf(stderr
,PSTR("\n[debug] tick"));
150 void timers_init(void) {
151 fprintf_P(stderr
,PSTR("\ntimers: init:\tstart"));
153 //timer0_init(); //PWM 8
154 //timer1_init(); //PWM 16
155 timer2_init(); //RTC 8
157 fprintf_P(stderr
,PSTR("\ntimers: init:\t[done]"));