add unified compile target
[avr_work.git] / lline / old / timer.c
blob8624b5407f8db590efb77a29b6a34a2f9f8b5567
1 #include "defines.h"
3 #include <stdio.h>
5 #include <avr/io.h>
6 #include <avr/interrupt.h>
7 #include <avr/pgmspace.h>
8 #include <avr/power.h>
10 #include <util/atomic.h>
12 #include "timer.h"
14 static void timer0_init(void) { // 8, PWM
15 fprintf_P(stderr,PSTR("\ntimers: init: timer0"));
16 power_timer0_enable();
18 // Diable Timer
19 TCCR1B&= (uint8_t)~((1<<CS12)|(1<<CS11)|(1<<CS10));
21 DDRB|=(1<<3)|(1<<4);
23 // Pin ctrl
24 //TCCR0A&=~((1<<COM0A1)|(1<<COM0A0)|(1<<COM0B1)|(1<<COM0B0));
25 TCCR0A|=(1<<COM0A1)|(1<<COM0B1);TCCR0A&=(uint8_t)~((1<<COM0A0)|(1<<COM0B0));
28 // Mode 2, CTC.
29 //TCCR0B&=(uint8_t)~ (1<<WGM02);
30 //TCCR0A|= (1<<WGM01);
31 //TCCR0A&=(uint8_t)~ (1<<WGM00);
33 // Mode 1, PWM.
34 TCCR0B&=(uint8_t)~ (1<<WGM02);
35 TCCR0A&=(uint8_t)~ (1<<WGM01);
36 TCCR0A|= (1<<WGM00);
38 TCNT0=0;
39 OCR0A=0;
40 OCR0B=0;
42 // Interupts
43 TIMSK0=(0<<OCIE0A)|(0<<OCIE0B);
45 // Clock Select and timer enable {disable,1,8,64,256,1024,ExFalling,ExRising}
46 TCCR0B|=1;
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();
56 // Disable Timer
57 TCCR1B&= (uint8_t)~((1<<CS12)|(1<<CS11)|(1<<CS10));
59 // OC1A, OC1B set to outputs
60 DDRD|= (1<<5)|(1<<4);
62 // Pin Control
63 TCCR1A|= (1<<COM1A1)|(1<<COM1B1);
64 TCCR1A&= (uint8_t)~((1<<COM1A0)|(1<<COM1B0));
66 TCCR1B|= (1<<5); //Reserved bit
68 // Mode 8.
69 TCCR1B|= (1<<WGM13);
70 TCCR1B&= (uint8_t)~(1<<WGM12);
71 TCCR1A&= (uint8_t)~((1<<WGM11)|(1<<WGM10));
73 // Mode 1.
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) {
83 ICR1=0xFFFF;
84 TCNT1=0;
85 OCR1A=0;
86 OCR1B=0;
89 // Interupts
90 TIMSK1=(0<<ICIE1)|(0<<OCIE1B)|(0<<OCIE1A)|(0<<TOIE1);
92 // Prescale and Enable.
93 // 1
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();
105 // Disable Timer.
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));
111 // Mode 2, CTC.
112 TCCR2A|= (1<<WGM21);
113 TCCR2A&=~(1<<WGM20);
114 TCCR2B&=~(1<<WGM22);
116 // Enable OCR2A interupt
117 TIMSK2=(1<<OCIE2A);
119 TCNT2=0;
120 OCR2B=0;
122 // Clock Select
123 //8000000/78/1024 == 100 HZ
124 //8000000/125/64 == 1000 Hz
125 #if (T2HZ==100)
126 OCR2A=78;
127 TCCR2B|=(1<<CS22)|(1<<CS21)|(1<<CS20); //1024
128 #elif (T2HZ==1000)
129 OCR2A=125;
130 TCCR2B|=(1<<CS22);TCCR2B&=(uint8_t)~((1<<CS21)|(1<<CS20));//64
131 #else
132 #error Invalid Timer2 Frequency
133 #endif
135 fprintf_P(stderr,PSTR("\t[done]"));
138 ISR(TIMER2_COMPA_vect) {
139 static uint16_t sec;//=0
140 static uint8_t ct;
141 ct++;
142 if (ct>=100) {
143 ct=0;
144 ++sec;
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]"));