1 | #include <mega162.h>
|
2 | #include <stdio.h>
|
3 | #include <i2c.h>
|
4 | #include <delay.h>
|
5 |
|
6 |
|
7 | unsigned char value_a;
|
8 | unsigned char value_b;
|
9 | unsigned char size;
|
10 | unsigned char game_size;
|
11 | unsigned char offset_a;
|
12 | unsigned char offset_b;
|
13 | char calc_a=1;
|
14 | char calc_b=1;
|
15 |
|
16 | unsigned char drehen(unsigned char value);
|
17 | void uart1en(unsigned char value);
|
18 |
|
19 | // USART0 Receiver interrupt service routine
|
20 | interrupt [USART0_RXC] void usart0_rx_isr(void)
|
21 | {
|
22 | char status,data;
|
23 | status=UCSR0A;
|
24 | }
|
25 |
|
26 | // USART1 Receiver interrupt service routine
|
27 | interrupt [USART1_RXC] void usart1_rx_isr(void)
|
28 | {
|
29 | char status,data;
|
30 | status=UCSR1A;
|
31 | }
|
32 |
|
33 |
|
34 | // Timer1 overflow interrupt service routine
|
35 | interrupt [TIM1_OVF] void timer1_ovf_isr(void)
|
36 | {
|
37 |
|
38 | }
|
39 |
|
40 | void main(void)
|
41 | {
|
42 | // Declare your local variables here
|
43 |
|
44 | // Crystal Oscillator division factor: 1
|
45 | #pragma optsize-
|
46 | CLKPR=0x80;
|
47 | CLKPR=0x00;
|
48 | #ifdef _OPTIMIZE_SIZE_
|
49 | #pragma optsize+
|
50 | #endif
|
51 |
|
52 | // Input/Output Ports initialization
|
53 | // Port A initialization
|
54 | // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
|
55 | // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
|
56 | PORTA=0x00;
|
57 | DDRA=0xFF;
|
58 |
|
59 | // Port B initialization
|
60 | // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
|
61 | // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
|
62 | PORTB=0x00;
|
63 | DDRB=0x00;
|
64 |
|
65 | // Port C initialization
|
66 | // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
|
67 | // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
|
68 | PORTC=0x00;
|
69 | DDRC=0xFF;
|
70 |
|
71 | // Port D initialization
|
72 | // Func7=In Func6=In Func5=In Func4=Out Func3=In Func2=Out Func1=In Func0=In
|
73 | // State7=T State6=T State5=T State4=0 State3=T State2=0 State1=T State0=T
|
74 | PORTD=0x00;
|
75 | DDRD=0x14;
|
76 |
|
77 | // Port E initialization
|
78 | // Func2=In Func1=In Func0=In
|
79 | // State2=T State1=T State0=T
|
80 | PORTE=0x00;
|
81 | DDRE=0x00;
|
82 |
|
83 | // Timer/Counter 0 initialization
|
84 | // Clock source: System Clock
|
85 | // Clock value: Timer 0 Stopped
|
86 | // Mode: Normal top=0xFF
|
87 | // OC0 output: Disconnected
|
88 | TCCR0=0x00;
|
89 | TCNT0=0x00;
|
90 | OCR0=0x00;
|
91 |
|
92 | // Timer/Counter 1 initialization
|
93 | // Clock source: System Clock
|
94 | // Clock value: 57,600 kHz
|
95 | // Mode: Normal top=0xFFFF
|
96 | // OC1A output: Discon.
|
97 | // OC1B output: Discon.
|
98 | // Noise Canceler: Off
|
99 | // Input Capture on Falling Edge
|
100 | // Timer1 Overflow Interrupt: On
|
101 | // Input Capture Interrupt: Off
|
102 | // Compare A Match Interrupt: Off
|
103 | // Compare B Match Interrupt: Off
|
104 | TCCR1A=0x00;
|
105 | TCCR1B=0x04;
|
106 | TCNT1H=0x00;
|
107 | TCNT1L=0x00;
|
108 | ICR1H=0x00;
|
109 | ICR1L=0x00;
|
110 | OCR1AH=0x00;
|
111 | OCR1AL=0x00;
|
112 | OCR1BH=0x00;
|
113 | OCR1BL=0x00;
|
114 |
|
115 | // Timer/Counter 2 initialization
|
116 | // Clock source: System Clock
|
117 | // Clock value: Timer2 Stopped
|
118 | // Mode: Normal top=0xFF
|
119 | // OC2 output: Disconnected
|
120 | ASSR=0x00;
|
121 | TCCR2=0x00;
|
122 | TCNT2=0x00;
|
123 | OCR2=0x00;
|
124 |
|
125 | // Timer/Counter 3 initialization
|
126 | // Clock value: Timer3 Stopped
|
127 | // Mode: Normal top=0xFFFF
|
128 | // OC3A output: Discon.
|
129 | // OC3B output: Discon.
|
130 | // Noise Canceler: Off
|
131 | // Input Capture on Falling Edge
|
132 | // Timer3 Overflow Interrupt: Off
|
133 | // Input Capture Interrupt: Off
|
134 | // Compare A Match Interrupt: Off
|
135 | // Compare B Match Interrupt: Off
|
136 | TCCR3A=0x00;
|
137 | TCCR3B=0x00;
|
138 | TCNT3H=0x00;
|
139 | TCNT3L=0x00;
|
140 | ICR3H=0x00;
|
141 | ICR3L=0x00;
|
142 | OCR3AH=0x00;
|
143 | OCR3AL=0x00;
|
144 | OCR3BH=0x00;
|
145 | OCR3BL=0x00;
|
146 |
|
147 | // External Interrupt(s) initialization
|
148 | // INT0: Off
|
149 | // INT1: Off
|
150 | // INT2: Off
|
151 | // Interrupt on any change on pins PCINT0-7: Off
|
152 | // Interrupt on any change on pins PCINT8-15: Off
|
153 | MCUCR=0x00;
|
154 | EMCUCR=0x00;
|
155 |
|
156 | // Timer(s)/Counter(s) Interrupt(s) initialization
|
157 | TIMSK=0x80;
|
158 |
|
159 | ETIMSK=0x00;
|
160 |
|
161 | // USART0 initialization
|
162 | // Communication Parameters: 8 Data, 1 Stop, No Parity
|
163 | // USART0 Receiver: On
|
164 | // USART0 Transmitter: On
|
165 | // USART0 Mode: Asynchronous
|
166 | // USART0 Baud Rate: 9600
|
167 | UCSR0A=0x00;
|
168 | UCSR0B=0x98;
|
169 | UCSR0C=0x86;
|
170 | UBRR0H=0x00;
|
171 | UBRR0L=0x5F;
|
172 |
|
173 | // USART1 initialization
|
174 | // Communication Parameters: 8 Data, 1 Stop, No Parity
|
175 | // USART1 Receiver: On
|
176 | // USART1 Transmitter: On
|
177 | // USART1 Mode: Asynchronous
|
178 | // USART1 Baud Rate: 115200
|
179 | UCSR1A=0x00;
|
180 | UCSR1B=0x18; //UCSR1B=0x98;
|
181 | UCSR1C=0x86;
|
182 | UBRR1H=0x00;
|
183 | UBRR1L=0x07;
|
184 |
|
185 | // Analog Comparator initialization
|
186 | // Analog Comparator: Off
|
187 | // Analog Comparator Input Capture by Timer/Counter 1: Off
|
188 | ACSR=0x80;
|
189 |
|
190 | // SPI initialization
|
191 | // SPI disabled
|
192 | SPCR=0x00;
|
193 |
|
194 | // I2C Bus initialization
|
195 | // I2C Port: PORTD
|
196 | // I2C SDA bit: 7
|
197 | // I2C SCL bit: 6
|
198 | // Bit Rate: 100 kHz
|
199 | // Note: I2C settings are specified in the
|
200 | // Project|Configure|C Compiler|Libraries|I2C menu.
|
201 | i2c_init();
|
202 |
|
203 | // Global enable interrupts
|
204 | #asm("sei")
|
205 | value_a=0;
|
206 | value_b=0;
|
207 | while (1)
|
208 | {
|
209 |
|
210 |
|
211 | for(value_a=0;value_a!=size;value_a++)
|
212 | {
|
213 | PORTA=drehen(offset_a+value_a);
|
214 | PORTC=drehen(offset_b+value_b);
|
215 | delay_us(300);
|
216 | }
|
217 |
|
218 | for(value_b=0;value_b!=size;value_b++)
|
219 | {
|
220 | PORTA=drehen(offset_a+value_a);
|
221 | PORTC=drehen(offset_b+value_b);
|
222 | delay_us(300);
|
223 | }
|
224 |
|
225 | for(value_a=size;value_a!=0;value_a--)
|
226 | {
|
227 | PORTA=drehen(offset_a+value_a);
|
228 | PORTC=drehen(offset_b+value_b);
|
229 | delay_us(300);
|
230 | }
|
231 |
|
232 | for(value_b=size;value_b!=0;value_b--)
|
233 | {
|
234 | PORTA=drehen(offset_a+value_a);
|
235 | PORTC=drehen(offset_b+value_b);
|
236 | delay_us(300);
|
237 | }
|
238 |
|
239 | size=20;
|
240 |
|
241 | offset_a=offset_a+calc_a;
|
242 | if (offset_a>20) calc_a=-1;
|
243 | if (offset_a<1 ) calc_a=1;
|
244 |
|
245 | offset_b=offset_b+calc_b;
|
246 | if (offset_b>20) calc_b=-1;
|
247 | if (offset_b<1 ) calc_b=1;
|
248 |
|
249 | }
|
250 | }
|
251 |
|
252 | //Zum Byte verdrehen
|
253 | unsigned char drehen(unsigned char value)
|
254 | {
|
255 | unsigned char erg;
|
256 | unsigned char i;
|
257 | if (value&0x01) erg=erg+0x80;
|
258 | if (value&0x02) erg=erg+0x40;
|
259 | if (value&0x04) erg=erg+0x20;
|
260 | if (value&0x08) erg=erg+0x10;
|
261 |
|
262 | if (value&0x10) erg=erg+0x08;
|
263 | if (value&0x20) erg=erg+0x04;
|
264 | if (value&0x40) erg=erg+0x02;
|
265 | if (value&0x80) erg=erg+0x01;
|
266 | return (erg);
|
267 | }
|
268 |
|
269 | void uart1en(unsigned char value)
|
270 | {
|
271 | if (value==1)
|
272 | {UCSR1B=UCSR1B|0x80;}
|
273 | else
|
274 | {UCSR1B=UCSR1B&0x7F;}
|
275 | }
|