Rev 51 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
28 | walter | 1 | //############################################################################ |
2 | // - LED CTRL Main |
||
3 | // - ATMEGA8 mit 8MHz |
||
4 | // - Nur für den privaten Gebrauch |
||
5 | // - Keine Garantie auf Fehlerfreiheit |
||
6 | // - Kommerzielle Nutzung nur mit meiner Zustimmung |
||
7 | // - walter Meyer @ www.freakware.de |
||
8 | // - 11.12.2007 |
||
9 | // - Make sure Fuses are programmed for internal 8 MHz RC Oscilator |
||
10 | //############################################################################*/ |
||
11 | |||
12 | #include "main.h" |
||
13 | #include "uart.h" |
||
14 | #include "twislave.h" |
||
15 | |||
16 | volatile unsigned int ppm_signal = 1500; |
||
17 | volatile unsigned char ppm_new = 0; |
||
18 | volatile unsigned char TMR1OvF = 0; |
||
19 | volatile unsigned char ch0; |
||
20 | volatile unsigned char ch1; |
||
21 | volatile unsigned char ch2; |
||
22 | volatile unsigned char ch3; |
||
23 | volatile unsigned char ch4; |
||
24 | volatile unsigned char ch5; |
||
25 | |||
26 | |||
27 | |||
28 | SIGNAL(SIG_OVERFLOW1) |
||
29 | { |
||
30 | TMR1OvF++; |
||
31 | } |
||
32 | |||
33 | |||
34 | SIGNAL(SIG_INPUT_CAPTURE1) |
||
35 | { |
||
36 | static unsigned int pos_ICR; |
||
37 | static unsigned int ppm; |
||
38 | |||
39 | if ((TCCR1B & (1<<ICES1)) != 0) //rising edge |
||
40 | { |
||
41 | TCCR1B &= ~(1<<ICES1); //set falling egde |
||
42 | TMR1OvF = 0; |
||
43 | pos_ICR = ICR1; |
||
44 | } |
||
45 | else //falling edge |
||
46 | { |
||
47 | TCCR1B |= (1<<ICES1); //set rising egde |
||
48 | ppm = (ICR1 - pos_ICR + (int) TMR1OvF * 65536); |
||
49 | if ((ppm > 600) && (ppm < 2400)) |
||
50 | { |
||
51 | if (ppm > 2100) ppm = 2100; |
||
52 | if (ppm < 900) ppm = 900; |
||
53 | ppm = (ppm_signal * 7 + ppm) / 8; |
||
54 | ppm_signal = ppm; |
||
55 | ppm_new = 1; |
||
56 | } |
||
57 | |||
58 | } |
||
59 | |||
60 | } |
||
61 | |||
62 | |||
63 | |||
64 | |||
65 | |||
66 | SIGNAL(SIG_OVERFLOW0) |
||
67 | { |
||
68 | static unsigned char counter; |
||
69 | unsigned char PORTB_BAK; |
||
70 | unsigned char PORTD_BAK; |
||
71 | |||
72 | PORTB_BAK = PORTB; |
||
73 | PORTD_BAK = PORTD; |
||
74 | |||
75 | |||
76 | if (counter == 0) |
||
77 | { |
||
78 | PORTB_BAK clrbit (CH0_B | CH1_B | CH2_B); |
||
79 | PORTD_BAK clrbit (CH3_D | CH4_D | CH5_D); |
||
80 | } |
||
81 | |||
82 | |||
83 | if (ch0 == 255) |
||
84 | { |
||
85 | PORTB_BAK setbit CH0_B; |
||
86 | } |
||
87 | if (ch0 == counter) |
||
88 | { |
||
89 | PORTB_BAK setbit CH0_B; |
||
90 | } |
||
91 | if (ch0 == 0) |
||
92 | { |
||
93 | PORTB_BAK clrbit CH0_B; |
||
94 | } |
||
95 | |||
96 | |||
97 | |||
98 | |||
99 | |||
100 | |||
101 | if (ch1 == counter) |
||
102 | { |
||
103 | PORTB_BAK setbit CH1_B; |
||
104 | } |
||
105 | if (ch2 == counter) |
||
106 | { |
||
107 | PORTB_BAK setbit CH2_B; |
||
108 | } |
||
109 | if (ch3 == counter) |
||
110 | { |
||
111 | PORTD_BAK setbit CH3_D; |
||
112 | } |
||
113 | if (ch4_BAK == counter) |
||
114 | { |
||
115 | PORTD setbit CH4_D; |
||
116 | } |
||
117 | if (ch5_BAK == counter) |
||
118 | { |
||
119 | PORTD setbit CH5_D; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | |||
124 | |||
125 | } |
||
126 | |||
127 | |||
128 | |||
129 | |||
130 | counter++; |
||
131 | |||
132 | PORTB = PORTB_BAK; |
||
133 | PORTD = PORTD_BAK; |
||
134 | |||
135 | |||
136 | } |
||
137 | |||
138 | |||
139 | |||
140 | |||
141 | |||
142 | /*##############################################################################*/ |
||
143 | void StartPWM(unsigned char txbyte) |
||
144 | { |
||
145 | //Timer 0 Config for getting right timing for IR Pattern |
||
146 | TCCR0 = (0<<CS02)|(0<<CS01)|(1<<CS00); // (@8MHz) = 1/8us Clk = 256/8 = 32us overflow |
||
147 | TIMSK setbit (1<<TOIE0); // enable Int |
||
148 | |||
149 | } |
||
150 | |||
151 | |||
152 | |||
153 | |||
154 | |||
155 | |||
156 | |||
157 | /*##############################################################################*/ |
||
158 | void StartPPM(void) |
||
159 | { |
||
160 | |||
161 | //global timer1 Config |
||
162 | TCCR1A = (0<<COM1A1)|(0<<COM1A0)|(0<<COM1B1)|(0<<COM1B0)| |
||
163 | (0<<FOC1A) |(0<<FOC1B) |(0<<WGM10) |(0<<WGM11); |
||
164 | TCCR1B = (1<<ICNC1)|(1<<ICES1)|(0<<WGM13)| |
||
165 | (0<<WGM12)|(0<<CS12)|(1<<CS11)|(0<<CS10); //ICP_POS_FLANKE |
||
166 | |||
167 | // interrupts |
||
168 | TIMSK |= (1<<TICIE1)|(1<<TOIE1); //ICP_INT_ENABLE and TIMER1_INT_ENABLE |
||
169 | |||
170 | } |
||
171 | |||
172 | |||
173 | |||
174 | int GetPPM(void) |
||
175 | { |
||
176 | //this routines seems to be nesseccary, as reading a 16 bit value |
||
177 | //on a 8 bit machine is not atomic, so if an interrupt apears between reading |
||
178 | //low and high byte of the 16 bit value a wrong result is possible |
||
179 | |||
180 | unsigned char intmask; |
||
181 | unsigned int ppm_temp; |
||
182 | |||
183 | intmask = TIMSK; //backup interupt enable bits |
||
184 | TIMSK &= ~(1<<TICIE1); //disable ppm interrupt |
||
185 | ppm_temp = ppm_signal; |
||
186 | TIMSK = intmask; //restore interupt enable bits |
||
187 | return(ppm_temp); //return ppm_signal |
||
188 | |||
189 | } |
||
190 | |||
191 | |||
192 | /*##############################################################################*/ |
||
193 | // MAIN |
||
194 | /*##############################################################################*/ |
||
195 | int main (void) |
||
196 | { |
||
197 | |||
198 | DDRC = (1<<ledred); |
||
199 | PORTC = 0x00; |
||
200 | DDRD = (1<<ledgreen); |
||
201 | PORTD = 0x00; |
||
202 | DDRB = (1<<1)|(1<<2)|(1<<3); |
||
203 | PORTB = 0x00; |
||
204 | |||
205 | |||
206 | ch0 = 0; |
||
207 | ch1 = 0; |
||
208 | ch2 = 0; |
||
209 | ch3 = 0; |
||
210 | ch4 = 0; |
||
211 | ch5 = 0; |
||
212 | |||
213 | //StartUART(); |
||
214 | StartPPM(); |
||
215 | StartI2C(); |
||
216 | StartPWM(); |
||
217 | sei(); |
||
218 | |||
219 | |||
220 | while (1) |
||
221 | { |
||
222 | //printf("%d ",ppm_signal); |
||
223 | |||
224 | |||
225 | |||
226 | |||
227 | } |
||
228 | |||
229 | |||
230 | } |