Subversion Repositories FlightCtrl

Rev

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

Rev 1209 Rev 1210
Line 3... Line 3...
3
 
3
 
Line 4... Line 4...
4
#include "main.h"
4
#include "main.h"
5
 
5
 
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;
Line 8... Line 9...
8
unsigned char motorread = 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
 
11
 
11
//############################################################################
12
//############################################################################
Line 92... Line 93...
92
 
93
 
93
//############################################################################
94
//############################################################################
94
SIGNAL (TWI_vect)
95
SIGNAL (TWI_vect)
95
//############################################################################
96
//############################################################################
-
 
97
{
96
{
98
 static unsigned char missing_motor,cnt_motor;
97
    switch (twi_state++)
99
     switch(twi_state++)
-
 
100
        {
-
 
101
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
102
// Writing the Data
98
        {
103
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
99
        case 0:
-
 
100
J3High;
104
        case 0:
101
                        while(Mixer.Motor[motor][0] <= 0 && motor < MAX_MOTORS-1) motor++;  // skip if not used
105
                        while(Mixer.Motor[motor][0] <= 0 && motor < MAX_MOTORS) motor++;  // skip if not used
-
 
106
                                if(motor == MAX_MOTORS)  // writing finished -> now read 
-
 
107
                                {
-
 
108
                                 motor = 0;
-
 
109
                                 twi_state = 3;
-
 
110
                                 i2c_write_byte(0x53+(motorread*2));
102
                                if(motor == MAX_MOTORS) { motor = 0; twi_state = 3; i2c_start(); } // writing finished -> now read 
111
                                 }
103
                                else i2c_write_byte(0x52+(motor*2));
112
                                else i2c_write_byte(0x52+(motor*2));
104
                break;
113
                break;
105
        case 1:
114
        case 1:
106
                i2c_write_byte(Motor[motor++]);
115
                i2c_write_byte(Motor[motor++]);
107
                break;
116
                break;
-
 
117
        case 2:
108
        case 2:
118
                if(TWSR == 0x30) { if(!missing_motor) missing_motor = motor; }
-
 
119
                i2c_stop();
109
                i2c_stop();
120
                I2CTimeout = 10;
110
                twi_state = 0;
121
                twi_state = 0;
111
                i2c_start();  
122
                i2c_start();  
-
 
123
                break;
112
                break;
124
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
125
// Reading Data
113
        //Liest Daten von Motor
126
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
114
        case 3:
-
 
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));
-
 
119
                break;
-
 
120
        case 4:
127
        case 3:
-
 
128
                //Transmit 1st byte for reading
-
 
129
 
-
 
130
                if(TWSR != 0x40)  // Error?
-
 
131
                                 {
-
 
132
                                  MotorPresent[motorread] = 0;
-
 
133
                                  motorread++;
-
 
134
                                  if(motorread >= MAX_MOTORS) motorread = 0;
-
 
135
                                  i2c_stop();
-
 
136
                  twi_state = 0;
-
 
137
                 }
-
 
138
                                 else
-
 
139
                                 {
-
 
140
                  MotorPresent[motorread] = ('1' - '-') + motorread;
-
 
141
                                 }
121
                //Transmit 1st byte for reading
142
                MissingMotor = missing_motor;
122
                I2C_ReceiveByte();
143
                I2C_ReceiveByte();
123
                break;
144
                break;
124
        case 5: //Read 1st byte and transmit 2nd Byte
145
        case 4: //Read 1st byte and transmit 2nd Byte
125
                motor_rx[motorread] = TWDR;
146
                motor_rx[motorread] = TWDR;
126
                I2C_ReceiveLastByte(); //nack
147
                I2C_ReceiveLastByte(); //nack
127
                break;
148
                break;
128
        case 6:
149
        case 5:
129
                //Read 2nd byte
150
                //Read 2nd byte
130
                motor_rx2[motorread++] = TWDR;
151
                motor_rx2[motorread++] = TWDR;
131
                i2c_stop();
152
                i2c_stop();
132
                twi_state = 0;
153
                twi_state = 0;
133
                I2CTimeout = 10;
154
                                missing_motor = 0;
-
 
155
                break;
134
                break;
156
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
157
// writing Gyro-Offset
-
 
158
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
135
        case 8: // Gyro-Offset
159
        case 8:
136
                i2c_write_byte(0x98); // Address of the DAC
160
                i2c_write_byte(0x98); // Address of the DAC
137
                break;
161
                break;
138
        case 9:
162
        case 9:
139
                i2c_write_byte(0x10); // Update Channel A
163
                i2c_write_byte(0x10); // Update Channel A
Line 185... Line 209...
185
                break;
209
                break;
186
        default: twi_state = 0;
210
        default: twi_state = 0;
187
                break;        
211
                break;        
188
                }
212
                }
189
 TWCR |= 0x80;
213
 TWCR |= 0x80;
190
J3Low;
-
 
191
J4Low;
-
 
192
J5Low;
-
 
193
}
214
}
194
/*
215
/*
195
//############################################################################
216
//############################################################################
196
SIGNAL (TWI_vect)
217
SIGNAL (TWI_vect)
197
//############################################################################
218
//############################################################################