Subversion Repositories FlightCtrl

Rev

Rev 1210 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1210 Rev 1211
Line 6... Line 6...
6
volatile unsigned char twi_state = 0;
6
volatile unsigned char twi_state = 0;
7
unsigned char motor = 0;
7
unsigned char motor = 0;
8
unsigned char motorread = 0,MissingMotor = 0;
8
unsigned char motorread = 0,MissingMotor = 0;
9
unsigned char motor_rx[16],motor_rx2[16];
9
unsigned char motor_rx[16],motor_rx2[16];
10
unsigned char MotorPresent[MAX_MOTORS];
10
unsigned char MotorPresent[MAX_MOTORS];
-
 
11
unsigned char MotorError[MAX_MOTORS];
Line 11... Line 12...
11
 
12
 
12
//############################################################################
13
//############################################################################
13
//Initzialisieren der I2C (TWI) Schnittstelle
14
//Initzialisieren der I2C (TWI) Schnittstelle
14
void i2c_init(void)
15
void i2c_init(void)
Line 93... Line 94...
93
 
94
 
94
//############################################################################
95
//############################################################################
95
SIGNAL (TWI_vect)
96
SIGNAL (TWI_vect)
96
//############################################################################
97
//############################################################################
97
{
98
{
98
 static unsigned char missing_motor,cnt_motor;
99
 static unsigned char missing_motor;
99
     switch(twi_state++)
100
     switch(twi_state++)
100
        {
101
        {
101
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
102
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
102
// Writing the Data
103
// Writing the Data
Line 113... Line 114...
113
                break;
114
                break;
114
        case 1:
115
        case 1:
115
                i2c_write_byte(Motor[motor++]);
116
                i2c_write_byte(Motor[motor++]);
116
                break;
117
                break;
117
        case 2:
118
        case 2:
118
                if(TWSR == 0x30) { if(!missing_motor) missing_motor = motor; }
119
                if(TWSR == 0x30) { if(!missing_motor) missing_motor = motor; if(++MotorError[motor-1] == 0) MotorError[motor-1] = 255;}
119
                i2c_stop();
120
                i2c_stop();
120
                I2CTimeout = 10;
121
                I2CTimeout = 10;
121
                twi_state = 0;
122
                twi_state = 0;
122
                i2c_start();  
123
                i2c_start();  
123
                break;
124
                break;
124
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
125
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
125
// Reading Data
126
// Reading Data
126
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
127
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
127
        case 3:
128
        case 3:
128
                //Transmit 1st byte for reading
129
               //Transmit 1st byte for reading
129
 
-
 
130
                if(TWSR != 0x40)  // Error?
130
                if(TWSR != 0x40)  // Error?
131
                                 {
131
                 {
132
                                  MotorPresent[motorread] = 0;
132
                  MotorPresent[motorread] = 0;
133
                                  motorread++;
133
                  motorread++;
134
                                  if(motorread >= MAX_MOTORS) motorread = 0;
134
                  if(motorread >= MAX_MOTORS) motorread = 0;
135
                                  i2c_stop();
135
                  i2c_stop();
136
                  twi_state = 0;
136
                  twi_state = 0;
137
                 }
137
                 }
138
                                 else
138
                 else
139
                                 {
139
                 {
140
                  MotorPresent[motorread] = ('1' - '-') + motorread;
140
                  MotorPresent[motorread] = ('1' - '-') + motorread;
-
 
141
                  I2C_ReceiveByte();
141
                                 }
142
                 }
142
                MissingMotor = missing_motor;
143
                MissingMotor = missing_motor;
143
                I2C_ReceiveByte();
144
                missing_motor = 0;
144
                break;
145
                break;
145
        case 4: //Read 1st byte and transmit 2nd Byte
146
        case 4: //Read 1st byte and transmit 2nd Byte
146
                motor_rx[motorread] = TWDR;
147
                motor_rx[motorread] = TWDR;
147
                I2C_ReceiveLastByte(); //nack
148
                I2C_ReceiveLastByte(); //nack
148
                break;
149
                break;
149
        case 5:
150
        case 5:
150
                //Read 2nd byte
151
                //Read 2nd byte
151
                motor_rx2[motorread++] = TWDR;
152
                motor_rx2[motorread++] = TWDR;
-
 
153
                if(motorread >= MAX_MOTORS) motorread = 0;
152
                i2c_stop();
154
                i2c_stop();
153
                twi_state = 0;
155
                twi_state = 0;
154
                                missing_motor = 0;
-
 
155
                break;
156
                break;
-
 
157
                       
156
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
158
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
157
// writing Gyro-Offset
159
// writing Gyro-Offset
158
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
160
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
159
        case 8:
161
        case 8:
160
                i2c_write_byte(0x98); // Address of the DAC
162
                i2c_write_byte(0x98); // Address of the DAC
Line 210... Line 212...
210
        default: twi_state = 0;
212
        default: twi_state = 0;
211
                break;        
213
                break;        
212
                }
214
                }
213
 TWCR |= 0x80;
215
 TWCR |= 0x80;
214
}
216
}
215
/*
-
 
216
//############################################################################
-
 
217
SIGNAL (TWI_vect)
-
 
218
//############################################################################
-
 
219
{
-
 
220
    switch (twi_state++)
-
 
221
        {
-
 
222
        case 0:
-
 
223
                i2c_write_byte(0x52+(motor*2));
-
 
224
                break;
-
 
225
        case 1:
-
 
226
                switch(motor++)
-
 
227
                    {
-
 
228
                    case 0:
-
 
229
                            i2c_write_byte(Motor_Vorne);
-
 
230
                            break;
-
 
231
                    case 1:      
-
 
232
                            i2c_write_byte(Motor_Hinten);
-
 
233
                            break;
-
 
234
                    case 2:
-
 
235
                            i2c_write_byte(Motor_Rechts);
-
 
236
                            break;
-
 
237
                    case 3:
-
 
238
                            i2c_write_byte(Motor_Links);
-
 
239
                            break;
-
 
240
                    }
-
 
241
                break;
-
 
242
        case 2:
-
 
243
                i2c_stop();
-
 
244
                if (motor<4) twi_state = 0;
-
 
245
                else motor = 0;
-
 
246
                i2c_start();  
-
 
247
                break;
-
 
248
                   
-
 
249
        //Liest Daten von Motor
-
 
250
        case 3:
-
 
251
                i2c_write_byte(0x53+(motorread*2));
-
 
252
                break;
-
 
253
        case 4:
-
 
254
                switch(motorread)
-
 
255
                    {
-
 
256
                    case 0:
-
 
257
                        i2c_write_byte(Motor_Vorne);
-
 
258
                        break;
-
 
259
                    case 1:
-
 
260
                        i2c_write_byte(Motor_Hinten);
-
 
261
                        break;
-
 
262
                    case 2:
-
 
263
                        i2c_write_byte(Motor_Rechts);
-
 
264
                        break;
-
 
265
                    case 3:
-
 
266
                        i2c_write_byte(Motor_Links);
-
 
267
                        break;
-
 
268
                    }
-
 
269
                break;
-
 
270
        case 5: //1 Byte vom Motor lesen      
-
 
271
                motor_rx[motorread] = TWDR;
-
 
272
 
-
 
273
        case 6:
-
 
274
                switch(motorread)
-
 
275
                    {
-
 
276
                    case 0:
-
 
277
                        i2c_write_byte(Motor_Vorne);
-
 
278
                        break;
-
 
279
                    case 1:
-
 
280
                        i2c_write_byte(Motor_Hinten);
-
 
281
                        break;
-
 
282
                    case 2:
-
 
283
                        i2c_write_byte(Motor_Rechts);
-
 
284
                        break;
-
 
285
                    case 3:
-
 
286
                        i2c_write_byte(Motor_Links);
-
 
287
                        break;
-
 
288
                    }
-
 
289
                break;  
-
 
290
        case 7: //2 Byte vom Motor lesen      
-
 
291
                motor_rx[motorread+4] = TWDR;
-
 
292
                motorread++;
-
 
293
                if (motorread>3) motorread=0;
-
 
294
                i2c_stop();
-
 
295
                I2CTimeout = 10;
-
 
296
                twi_state = 0;
-
 
297
                break;
-
 
298
        case 8: // Gyro-Offset
-
 
299
                i2c_write_byte(0x98); // Address of the DAC
-
 
300
                break;
-
 
301
        case 9:
-
 
302
                i2c_write_byte(0x10); // Update Channel A
-
 
303
                break;
-
 
304
        case 10:
-
 
305
                i2c_write_byte(AnalogOffsetNick); // Value
-
 
306
                break;
-
 
307
        case 11:
-
 
308
                i2c_write_byte(0x80); // Value
-
 
309
                break;
-
 
310
        case 12:
-
 
311
                i2c_stop();              
-
 
312
                I2CTimeout = 10;
-
 
313
                i2c_start();  
-
 
314
                break;
-
 
315
        case 13:
-
 
316
                i2c_write_byte(0x98); // Address of the DAC
-
 
317
                break;
-
 
318
        case 14:
-
 
319
                i2c_write_byte(0x12); // Update Channel B
-
 
320
                break;
-
 
321
        case 15:
-
 
322
                i2c_write_byte(AnalogOffsetRoll); // Value
-
 
323
                break;
-
 
324
        case 16:
-
 
325
                i2c_write_byte(0x80); // Value
-
 
326
                break;
-
 
327
        case 17:
-
 
328
                i2c_stop();              
-
 
329
                I2CTimeout = 10;
-
 
330
                i2c_start();  
-
 
331
                break;
-
 
332
        case 18:
-
 
333
                i2c_write_byte(0x98); // Address of the DAC
-
 
334
                break;
-
 
335
        case 19:
-
 
336
                i2c_write_byte(0x14); // Update Channel C
-
 
337
                break;
-
 
338
        case 20:
-
 
339
                i2c_write_byte(AnalogOffsetGier); // Value
-
 
340
                break;
-
 
341
        case 21:
-
 
342
                i2c_write_byte(0x80); // Value
-
 
343
                break;
-
 
344
        case 22:
-
 
345
                i2c_stop();              
-
 
346
                I2CTimeout = 10;
-
 
347
                twi_state = 0;
-
 
348
                break;
-
 
349
        }
-
 
350
 TWCR |= 0x80;
-
 
351
}
-
 
352
*/
-
 
353
217