Subversion Repositories FlightCtrl

Rev

Rev 976 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 976 Rev 977
1
/*############################################################################
1
/*############################################################################
2
############################################################################*/
2
############################################################################*/
3
 
3
 
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;
8
unsigned char motorread = 0;
9
unsigned char motor_rx[8];
9
unsigned char motor_rx[MOTOR_COUNT*2];
10
 
10
 
11
//############################################################################
11
//############################################################################
12
//Initzialisieren der I2C (TWI) Schnittstelle
12
//Initzialisieren der I2C (TWI) Schnittstelle
13
void i2c_init(void)
13
void i2c_init(void)
14
//############################################################################
14
//############################################################################
15
{
15
{
16
  TWSR = 0;
16
  TWSR = 0;
17
  TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;
17
  TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;
18
}
18
}
19
 
19
 
20
//############################################################################
20
//############################################################################
21
//Start I2C
21
//Start I2C
22
char i2c_start(void)
22
char i2c_start(void)
23
//############################################################################
23
//############################################################################
24
{
24
{
25
    TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);
25
    TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);
26
    return(0);
26
    return(0);
27
}
27
}
28
 
28
 
29
//############################################################################
29
//############################################################################
30
//Start I2C
30
//Start I2C
31
void i2c_stop(void)
31
void i2c_stop(void)
32
//############################################################################
32
//############################################################################
33
{
33
{
34
    TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);
34
    TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);
35
}
35
}
36
 
36
 
37
void i2c_reset(void)
37
void i2c_reset(void)
38
//############################################################################
38
//############################################################################
39
{
39
{
40
                 i2c_stop();                
40
                 i2c_stop();                
41
                 twi_state = 0;
41
                 twi_state = 0;
42
                 motor = TWDR;
42
                 motor = TWDR;
43
                 motor = 0;
43
                 motor = 0;
44
                 TWCR = 0x80;
44
                 TWCR = 0x80;
45
                 TWAMR = 0;
45
                 TWAMR = 0;
46
                 TWAR = 0;
46
                 TWAR = 0;
47
                 TWDR = 0;
47
                 TWDR = 0;
48
                 TWSR = 0;
48
                 TWSR = 0;
49
                 TWBR = 0;
49
                 TWBR = 0;
50
                 i2c_init();
50
                 i2c_init();
51
                 i2c_start();
51
                 i2c_start();
52
                 i2c_write_byte(0);
52
                 i2c_write_byte(0);
53
}
53
}
54
 
54
 
55
//############################################################################
55
//############################################################################
56
//Start I2C
56
//Start I2C
57
char i2c_write_byte(char byte)
57
char i2c_write_byte(char byte)
58
//############################################################################
58
//############################################################################
59
{
59
{
60
    TWSR = 0x00;
60
    TWSR = 0x00;
61
    TWDR = byte;
61
    TWDR = byte;
62
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
62
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
63
   
63
   
64
    return(0);
64
    return(0);
65
   
65
   
66
}
66
}
67
 
67
 
68
//############################################################################
68
//############################################################################
69
//Start I2C
69
//Start I2C
70
SIGNAL (TWI_vect)
70
SIGNAL (TWI_vect)
71
//############################################################################
71
//############################################################################
72
{
72
{
73
    switch (twi_state++)
73
    switch (twi_state++)
74
        {
74
        {
75
        case 0:
75
        case 0:
76
                i2c_write_byte(0x52+(motor*2));
76
                i2c_write_byte(0x52+(motor*2));
77
                break;
77
                break;
78
        case 1:
78
        case 1:
-
 
79
#ifdef HEXAKOPTER
-
 
80
                switch(motor++)
-
 
81
                    {
-
 
82
                    case 0:
-
 
83
                            i2c_write_byte(Motor_VorneLinks);
-
 
84
                            break;
-
 
85
                    case 1:      
-
 
86
                            i2c_write_byte(Motor_HintenRechts);
-
 
87
                            break;
-
 
88
                    case 2:
-
 
89
                            i2c_write_byte(Motor_VorneRechts);
-
 
90
                            break;
-
 
91
                    case 3:
-
 
92
                            i2c_write_byte(Motor_HintenLinks);
-
 
93
                            break;
-
 
94
                    case 4:
-
 
95
                            i2c_write_byte(Motor_Rechts);
-
 
96
                            break;
-
 
97
                    case 5:
-
 
98
                            i2c_write_byte(Motor_Links);
-
 
99
                            break;                    
-
 
100
                                        }
-
 
101
#else           
79
                switch(motor++)
102
                switch(motor++)
80
                    {
103
                    {
81
                    case 0:
104
                    case 0:
82
                            i2c_write_byte(Motor_Vorne);
105
                            i2c_write_byte(Motor_Vorne);
83
                            break;
106
                            break;
84
                    case 1:      
107
                    case 1:      
85
                            i2c_write_byte(Motor_Hinten);
108
                            i2c_write_byte(Motor_Hinten);
86
                            break;
109
                            break;
87
                    case 2:
110
                    case 2:
88
                            i2c_write_byte(Motor_Rechts);
111
                            i2c_write_byte(Motor_Rechts);
89
                            break;
112
                            break;
90
                    case 3:
113
                    case 3:
91
                            i2c_write_byte(Motor_Links);
114
                            i2c_write_byte(Motor_Links);
92
                            break;
115
                            break;
93
                    }
116
                    }
-
 
117
#endif
94
                break;
118
                break;
95
        case 2:
119
        case 2:
96
                i2c_stop();
120
                i2c_stop();
97
                if (motor<4) twi_state = 0;
121
                if (motor<MOTOR_COUNT) twi_state = 0;
98
                else motor = 0;
122
                else motor = 0;
99
                i2c_start();  
123
                i2c_start();  
100
                break;
124
                break;
101
                   
125
                   
102
        //Liest Daten von Motor
126
        //Liest Daten von Motor
103
        case 3:
127
        case 3:
104
                i2c_write_byte(0x53+(motorread*2));
128
                i2c_write_byte(0x53+(motorread*2));
105
                break;
129
                break;
106
        case 4:
130
        case 4:
107
                switch(motorread)
-
 
108
                    {
-
 
109
                    case 0:
-
 
110
                        i2c_write_byte(Motor_Vorne);
-
 
111
                        break;
-
 
112
                    case 1:
-
 
113
                        i2c_write_byte(Motor_Hinten);
131
                                TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
114
                        break;
-
 
115
                    case 2:
-
 
116
                        i2c_write_byte(Motor_Rechts);
-
 
117
                        break;
-
 
118
                    case 3:
-
 
119
                        i2c_write_byte(Motor_Links);
-
 
120
                        break;
-
 
121
                    }
-
 
122
                break;
132
                break;
123
        case 5: //1 Byte vom Motor lesen       
133
        case 5: //1 Byte vom Motor lesen       
124
                motor_rx[motorread] = TWDR;
134
                motor_rx[motorread] = TWDR;
125
 
135
 
126
        case 6:
136
        case 6:
127
                switch(motorread)
-
 
128
                    {
-
 
129
                    case 0:
-
 
130
                        i2c_write_byte(Motor_Vorne);
-
 
131
                        break;
-
 
132
                    case 1:
-
 
133
                        i2c_write_byte(Motor_Hinten);
137
                TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
134
                        break;
-
 
135
                    case 2:
-
 
136
                        i2c_write_byte(Motor_Rechts);
-
 
137
                        break;
-
 
138
                    case 3:
-
 
139
                        i2c_write_byte(Motor_Links);
-
 
140
                        break;
-
 
141
                    }
-
 
142
                break;  
138
                break;  
143
        case 7: //2 Byte vom Motor lesen       
139
        case 7: //2 Byte vom Motor lesen       
144
                motor_rx[motorread+4] = TWDR;
140
                motor_rx[motorread+MOTOR_COUNT] = TWDR;
145
                motorread++;
141
                motorread++;
146
                if (motorread>3) motorread=0;
142
                if (motorread>=MOTOR_COUNT) motorread=0;
147
                i2c_stop();
143
                i2c_stop();
148
                I2CTimeout = 10;
144
                I2CTimeout = 10;
149
                twi_state = 0;
145
                twi_state = 0;
150
                break;
146
                break;
151
        case 8: // Gyro-Offset
147
        case 8: // Gyro-Offset
152
                i2c_write_byte(0x98); // Address of the DAC
148
                i2c_write_byte(0x98); // Address of the DAC
153
                break;
149
                break;
154
        case 9:
150
        case 9:
155
                i2c_write_byte(0x10); // Update Channel A
151
                i2c_write_byte(0x10); // Update Channel A
156
                break;
152
                break;
157
        case 10:
153
        case 10:
158
                i2c_write_byte(AnalogOffsetNick); // Value
154
                i2c_write_byte(AnalogOffsetNick); // Value
159
                break;
155
                break;
160
        case 11:
156
        case 11:
161
                i2c_write_byte(0x80); // Value
157
                i2c_write_byte(0x80); // Value
162
                break;
158
                break;
163
        case 12:
159
        case 12:
164
                i2c_stop();              
160
                i2c_stop();              
165
                I2CTimeout = 10;
161
                I2CTimeout = 10;
166
                i2c_start();  
162
                i2c_start();  
167
                break;
163
                break;
168
        case 13:
164
        case 13:
169
                i2c_write_byte(0x98); // Address of the DAC
165
                i2c_write_byte(0x98); // Address of the DAC
170
                break;
166
                break;
171
        case 14:
167
        case 14:
172
                i2c_write_byte(0x12); // Update Channel B
168
                i2c_write_byte(0x12); // Update Channel B
173
                break;
169
                break;
174
        case 15:
170
        case 15:
175
                i2c_write_byte(AnalogOffsetRoll); // Value
171
                i2c_write_byte(AnalogOffsetRoll); // Value
176
                break;
172
                break;
177
        case 16:
173
        case 16:
178
                i2c_write_byte(0x80); // Value
174
                i2c_write_byte(0x80); // Value
179
                break;
175
                break;
180
        case 17:
176
        case 17:
181
                i2c_stop();              
177
                i2c_stop();              
182
                I2CTimeout = 10;
178
                I2CTimeout = 10;
183
                i2c_start();  
179
                i2c_start();  
184
                break;
180
                break;
185
        case 18:
181
        case 18:
186
                i2c_write_byte(0x98); // Address of the DAC
182
                i2c_write_byte(0x98); // Address of the DAC
187
                break;
183
                break;
188
        case 19:
184
        case 19:
189
                i2c_write_byte(0x14); // Update Channel C
185
                i2c_write_byte(0x14); // Update Channel C
190
                break;
186
                break;
191
        case 20:
187
        case 20:
192
                i2c_write_byte(AnalogOffsetGier); // Value
188
                i2c_write_byte(AnalogOffsetGier); // Value
193
                break;
189
                break;
194
        case 21:
190
        case 21:
195
                i2c_write_byte(0x80); // Value
191
                i2c_write_byte(0x80); // Value
196
                break;
192
                break;
197
        case 22:
193
        case 22:
198
                i2c_stop();              
194
                i2c_stop();              
199
                I2CTimeout = 10;
195
                I2CTimeout = 10;
200
                twi_state = 0;
196
                twi_state = 0;
201
                break;
197
                break;
202
        }
198
        }
203
 TWCR |= 0x80;
199
 TWCR |= 0x80;
204
}
200
}
205
 
201