Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | /*############################################################################ |
2 | ############################################################################*/ |
||
3 | |||
4 | #include "main.h" |
||
5 | |||
918 | hbuss | 6 | volatile unsigned char twi_state = 0; |
1 | ingob | 7 | unsigned char motor = 0; |
8 | unsigned char motorread = 0; |
||
1209 | hbuss | 9 | unsigned char motor_rx[16],motor_rx2[16]; |
1 | ingob | 10 | |
11 | //############################################################################ |
||
12 | //Initzialisieren der I2C (TWI) Schnittstelle |
||
13 | void i2c_init(void) |
||
14 | //############################################################################ |
||
15 | { |
||
16 | TWSR = 0; |
||
17 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
||
18 | } |
||
19 | |||
20 | //############################################################################ |
||
21 | //Start I2C |
||
1209 | hbuss | 22 | void i2c_start(void) |
1 | ingob | 23 | //############################################################################ |
24 | { |
||
25 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
||
26 | } |
||
27 | |||
28 | //############################################################################ |
||
29 | void i2c_stop(void) |
||
30 | //############################################################################ |
||
31 | { |
||
32 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
||
33 | } |
||
34 | |||
173 | holgerb | 35 | void i2c_reset(void) |
1 | ingob | 36 | //############################################################################ |
173 | holgerb | 37 | { |
38 | i2c_stop(); |
||
39 | twi_state = 0; |
||
40 | motor = TWDR; |
||
41 | motor = 0; |
||
42 | TWCR = 0x80; |
||
43 | TWAMR = 0; |
||
44 | TWAR = 0; |
||
45 | TWDR = 0; |
||
46 | TWSR = 0; |
||
47 | TWBR = 0; |
||
48 | i2c_init(); |
||
49 | i2c_start(); |
||
50 | i2c_write_byte(0); |
||
51 | } |
||
52 | |||
53 | //############################################################################ |
||
1209 | hbuss | 54 | void i2c_write_byte(char byte) |
1 | ingob | 55 | //############################################################################ |
56 | { |
||
57 | TWSR = 0x00; |
||
58 | TWDR = byte; |
||
59 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
60 | } |
||
61 | |||
1209 | hbuss | 62 | /****************************************/ |
63 | /* Write to I2C */ |
||
64 | /****************************************/ |
||
65 | void I2C_WriteByte(int8_t byte) |
||
66 | { |
||
67 | // move byte to send into TWI Data Register |
||
68 | TWDR = byte; |
||
69 | // clear interrupt flag (TWINT = 1) |
||
70 | // enable i2c bus (TWEN = 1) |
||
71 | // enable interrupt (TWIE = 1) |
||
72 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
73 | } |
||
74 | |||
75 | /****************************************/ |
||
76 | /* Receive byte and send ACK */ |
||
77 | /****************************************/ |
||
78 | void I2C_ReceiveByte(void) |
||
79 | { |
||
80 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
||
81 | } |
||
82 | |||
83 | /****************************************/ |
||
84 | /* I2C receive last byte and send no ACK*/ |
||
85 | /****************************************/ |
||
86 | void I2C_ReceiveLastByte(void) |
||
87 | { |
||
88 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
89 | } |
||
90 | |||
91 | |||
92 | |||
1 | ingob | 93 | //############################################################################ |
94 | SIGNAL (TWI_vect) |
||
95 | //############################################################################ |
||
96 | { |
||
97 | switch (twi_state++) |
||
98 | { |
||
99 | case 0: |
||
1209 | hbuss | 100 | J3High; |
101 | while(Mixer.Motor[motor][0] <= 0 && motor < MAX_MOTORS-1) motor++; // skip if not used |
||
102 | if(motor == MAX_MOTORS) { motor = 0; twi_state = 3; i2c_start(); } // writing finished -> now read |
||
103 | else i2c_write_byte(0x52+(motor*2)); |
||
1 | ingob | 104 | break; |
105 | case 1: |
||
1209 | hbuss | 106 | i2c_write_byte(Motor[motor++]); |
1111 | hbuss | 107 | break; |
108 | case 2: |
||
109 | i2c_stop(); |
||
1209 | hbuss | 110 | twi_state = 0; |
1111 | hbuss | 111 | i2c_start(); |
112 | break; |
||
113 | //Liest Daten von Motor |
||
114 | case 3: |
||
1209 | hbuss | 115 | J5High; |
116 | while(Mixer.Motor[motorread][0] <= 0 && MAX_MOTORS-1) motorread++; |
||
117 | if(motorread >= MAX_MOTORS) {motorread = 0;J4High;} |
||
118 | else i2c_write_byte(0x53+(motorread*2)); |
||
1111 | hbuss | 119 | break; |
120 | case 4: |
||
1209 | hbuss | 121 | //Transmit 1st byte for reading |
122 | I2C_ReceiveByte(); |
||
1111 | hbuss | 123 | break; |
1209 | hbuss | 124 | case 5: //Read 1st byte and transmit 2nd Byte |
1111 | hbuss | 125 | motor_rx[motorread] = TWDR; |
1209 | hbuss | 126 | I2C_ReceiveLastByte(); //nack |
127 | break; |
||
1111 | hbuss | 128 | case 6: |
1209 | hbuss | 129 | //Read 2nd byte |
130 | motor_rx2[motorread++] = TWDR; |
||
1111 | hbuss | 131 | i2c_stop(); |
1209 | hbuss | 132 | twi_state = 0; |
1111 | hbuss | 133 | I2CTimeout = 10; |
134 | break; |
||
135 | case 8: // Gyro-Offset |
||
136 | i2c_write_byte(0x98); // Address of the DAC |
||
137 | break; |
||
138 | case 9: |
||
139 | i2c_write_byte(0x10); // Update Channel A |
||
140 | break; |
||
141 | case 10: |
||
142 | i2c_write_byte(AnalogOffsetNick); // Value |
||
143 | break; |
||
144 | case 11: |
||
145 | i2c_write_byte(0x80); // Value |
||
146 | break; |
||
147 | case 12: |
||
148 | i2c_stop(); |
||
149 | I2CTimeout = 10; |
||
150 | i2c_start(); |
||
151 | break; |
||
152 | case 13: |
||
153 | i2c_write_byte(0x98); // Address of the DAC |
||
154 | break; |
||
155 | case 14: |
||
156 | i2c_write_byte(0x12); // Update Channel B |
||
157 | break; |
||
158 | case 15: |
||
159 | i2c_write_byte(AnalogOffsetRoll); // Value |
||
160 | break; |
||
161 | case 16: |
||
162 | i2c_write_byte(0x80); // Value |
||
163 | break; |
||
164 | case 17: |
||
165 | i2c_stop(); |
||
166 | I2CTimeout = 10; |
||
167 | i2c_start(); |
||
168 | break; |
||
169 | case 18: |
||
170 | i2c_write_byte(0x98); // Address of the DAC |
||
171 | break; |
||
172 | case 19: |
||
173 | i2c_write_byte(0x14); // Update Channel C |
||
174 | break; |
||
175 | case 20: |
||
176 | i2c_write_byte(AnalogOffsetGier); // Value |
||
177 | break; |
||
178 | case 21: |
||
179 | i2c_write_byte(0x80); // Value |
||
180 | break; |
||
181 | case 22: |
||
182 | i2c_stop(); |
||
183 | I2CTimeout = 10; |
||
184 | twi_state = 0; |
||
185 | break; |
||
1209 | hbuss | 186 | default: twi_state = 0; |
187 | break; |
||
188 | } |
||
1111 | hbuss | 189 | TWCR |= 0x80; |
1209 | hbuss | 190 | J3Low; |
191 | J4Low; |
||
192 | J5Low; |
||
1111 | hbuss | 193 | } |
1209 | hbuss | 194 | /* |
1111 | hbuss | 195 | //############################################################################ |
196 | SIGNAL (TWI_vect) |
||
197 | //############################################################################ |
||
198 | { |
||
199 | switch (twi_state++) |
||
200 | { |
||
201 | case 0: |
||
202 | i2c_write_byte(0x52+(motor*2)); |
||
203 | break; |
||
204 | case 1: |
||
205 | switch(motor++) |
||
206 | { |
||
207 | case 0: |
||
1 | ingob | 208 | i2c_write_byte(Motor_Vorne); |
209 | break; |
||
210 | case 1: |
||
211 | i2c_write_byte(Motor_Hinten); |
||
212 | break; |
||
213 | case 2: |
||
214 | i2c_write_byte(Motor_Rechts); |
||
215 | break; |
||
216 | case 3: |
||
217 | i2c_write_byte(Motor_Links); |
||
218 | break; |
||
219 | } |
||
220 | break; |
||
221 | case 2: |
||
222 | i2c_stop(); |
||
223 | if (motor<4) twi_state = 0; |
||
224 | else motor = 0; |
||
225 | i2c_start(); |
||
226 | break; |
||
227 | |||
228 | //Liest Daten von Motor |
||
229 | case 3: |
||
230 | i2c_write_byte(0x53+(motorread*2)); |
||
231 | break; |
||
232 | case 4: |
||
233 | switch(motorread) |
||
234 | { |
||
235 | case 0: |
||
236 | i2c_write_byte(Motor_Vorne); |
||
237 | break; |
||
238 | case 1: |
||
239 | i2c_write_byte(Motor_Hinten); |
||
240 | break; |
||
241 | case 2: |
||
242 | i2c_write_byte(Motor_Rechts); |
||
243 | break; |
||
244 | case 3: |
||
245 | i2c_write_byte(Motor_Links); |
||
246 | break; |
||
247 | } |
||
248 | break; |
||
249 | case 5: //1 Byte vom Motor lesen |
||
250 | motor_rx[motorread] = TWDR; |
||
173 | holgerb | 251 | |
1 | ingob | 252 | case 6: |
253 | switch(motorread) |
||
254 | { |
||
255 | case 0: |
||
256 | i2c_write_byte(Motor_Vorne); |
||
257 | break; |
||
258 | case 1: |
||
259 | i2c_write_byte(Motor_Hinten); |
||
260 | break; |
||
261 | case 2: |
||
262 | i2c_write_byte(Motor_Rechts); |
||
263 | break; |
||
264 | case 3: |
||
265 | i2c_write_byte(Motor_Links); |
||
266 | break; |
||
267 | } |
||
268 | break; |
||
269 | case 7: //2 Byte vom Motor lesen |
||
270 | motor_rx[motorread+4] = TWDR; |
||
271 | motorread++; |
||
272 | if (motorread>3) motorread=0; |
||
273 | i2c_stop(); |
||
173 | holgerb | 274 | I2CTimeout = 10; |
1 | ingob | 275 | twi_state = 0; |
918 | hbuss | 276 | break; |
277 | case 8: // Gyro-Offset |
||
278 | i2c_write_byte(0x98); // Address of the DAC |
||
279 | break; |
||
280 | case 9: |
||
281 | i2c_write_byte(0x10); // Update Channel A |
||
282 | break; |
||
283 | case 10: |
||
284 | i2c_write_byte(AnalogOffsetNick); // Value |
||
285 | break; |
||
286 | case 11: |
||
287 | i2c_write_byte(0x80); // Value |
||
288 | break; |
||
289 | case 12: |
||
290 | i2c_stop(); |
||
291 | I2CTimeout = 10; |
||
292 | i2c_start(); |
||
293 | break; |
||
294 | case 13: |
||
295 | i2c_write_byte(0x98); // Address of the DAC |
||
296 | break; |
||
297 | case 14: |
||
298 | i2c_write_byte(0x12); // Update Channel B |
||
299 | break; |
||
300 | case 15: |
||
301 | i2c_write_byte(AnalogOffsetRoll); // Value |
||
302 | break; |
||
303 | case 16: |
||
304 | i2c_write_byte(0x80); // Value |
||
305 | break; |
||
306 | case 17: |
||
307 | i2c_stop(); |
||
308 | I2CTimeout = 10; |
||
309 | i2c_start(); |
||
310 | break; |
||
311 | case 18: |
||
312 | i2c_write_byte(0x98); // Address of the DAC |
||
313 | break; |
||
314 | case 19: |
||
315 | i2c_write_byte(0x14); // Update Channel C |
||
316 | break; |
||
317 | case 20: |
||
318 | i2c_write_byte(AnalogOffsetGier); // Value |
||
319 | break; |
||
320 | case 21: |
||
321 | i2c_write_byte(0x80); // Value |
||
322 | break; |
||
323 | case 22: |
||
324 | i2c_stop(); |
||
325 | I2CTimeout = 10; |
||
326 | twi_state = 0; |
||
327 | break; |
||
1 | ingob | 328 | } |
173 | holgerb | 329 | TWCR |= 0x80; |
1 | ingob | 330 | } |
1209 | hbuss | 331 | */ |