Subversion Repositories FlightCtrl

Rev

Rev 1152 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1152 Rev 1208
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[8];
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
                switch(motor++)
79
                switch(motor++)
80
                    {
80
                    {
81
                    case 0:
81
                    case 0:
82
                            i2c_write_byte(Motor_Vorne);
82
                            i2c_write_byte(Motor_Vorne);
83
                            break;
83
                            break;
84
                    case 1:      
84
                    case 1:      
85
                            i2c_write_byte(Motor_Hinten);
85
                            i2c_write_byte(Motor_Hinten);
86
                            break;
86
                            break;
87
                    case 2:
87
                    case 2:
88
                            i2c_write_byte(Motor_Rechts);
88
                            i2c_write_byte(Motor_Rechts);
89
                            break;
89
                            break;
90
                    case 3:
90
                    case 3:
91
                            i2c_write_byte(Motor_Links);
91
                            i2c_write_byte(Motor_Links);
92
                            break;
92
                            break;
93
                    }
93
                    }
94
                break;
94
                break;
95
        case 2:
95
        case 2:
96
                i2c_stop();
96
                i2c_stop();
97
                if (motor<4) twi_state = 0;
97
                if (motor<4) twi_state = 0;
98
                else motor = 0;
98
                else motor = 0;
99
                i2c_start();  
99
                i2c_start();  
100
                break;
100
                break;
101
                   
101
                   
102
        //Liest Daten von Motor
102
        //Liest Daten von Motor
103
        case 3:
103
        case 3:
104
                i2c_write_byte(0x53+(motorread*2));
104
                i2c_write_byte(0x53+(motorread*2));
105
                break;
105
                break;
106
        case 4:
106
        case 4:
107
                switch(motorread)
107
                switch(motorread)
108
                    {
108
                    {
109
                    case 0:
109
                    case 0:
110
                        i2c_write_byte(Motor_Vorne);
110
                        i2c_write_byte(Motor_Vorne);
111
                        break;
111
                        break;
112
                    case 1:
112
                    case 1:
113
                        i2c_write_byte(Motor_Hinten);
113
                        i2c_write_byte(Motor_Hinten);
114
                        break;
114
                        break;
115
                    case 2:
115
                    case 2:
116
                        i2c_write_byte(Motor_Rechts);
116
                        i2c_write_byte(Motor_Rechts);
117
                        break;
117
                        break;
118
                    case 3:
118
                    case 3:
119
                        i2c_write_byte(Motor_Links);
119
                        i2c_write_byte(Motor_Links);
120
                        break;
120
                        break;
121
                    }
121
                    }
122
                break;
122
                break;
123
        case 5: //1 Byte vom Motor lesen       
123
        case 5: //1 Byte vom Motor lesen       
124
                motor_rx[motorread] = TWDR;
124
                motor_rx[motorread] = TWDR;
125
 
-
 
-
 
125
//                break;  // ???
126
        case 6:
126
        case 6:
127
                switch(motorread)
127
                switch(motorread)
128
                    {
128
                    {
129
                    case 0:
129
                    case 0:
130
                        i2c_write_byte(Motor_Vorne);
130
                        i2c_write_byte(Motor_Vorne);
131
                        break;
131
                        break;
132
                    case 1:
132
                    case 1:
133
                        i2c_write_byte(Motor_Hinten);
133
                        i2c_write_byte(Motor_Hinten);
134
                        break;
134
                        break;
135
                    case 2:
135
                    case 2:
136
                        i2c_write_byte(Motor_Rechts);
136
                        i2c_write_byte(Motor_Rechts);
137
                        break;
137
                        break;
138
                    case 3:
138
                    case 3:
139
                        i2c_write_byte(Motor_Links);
139
                        i2c_write_byte(Motor_Links);
140
                        break;
140
                        break;
141
                    }
141
                    }
142
                break;  
142
                break;  
143
        case 7: //2 Byte vom Motor lesen       
143
        case 7: //2 Byte vom Motor lesen       
144
                motor_rx[motorread+4] = TWDR;
144
                motor_rx[motorread+4] = TWDR;
145
                motorread++;
145
                motorread++;
146
                if (motorread>3) motorread=0;
146
                if (motorread>3) motorread=0;
147
                i2c_stop();
147
                i2c_stop();
148
                I2CTimeout = 10;
148
                I2CTimeout = 10;
149
                twi_state = 0;
149
                twi_state = 0;
150
                break;
150
                break;
151
        case 8: // Gyro-Offset
151
        case 8: // Gyro-Offset
152
                i2c_write_byte(0x98); // Address of the DAC
152
                i2c_write_byte(0x98); // Address of the DAC
153
                break;
153
                break;
154
        case 9:
154
        case 9:
155
                i2c_write_byte(0x10); // Update Channel A
155
                i2c_write_byte(0x10); // Update Channel A
156
                break;
156
                break;
157
        case 10:
157
        case 10:
158
                i2c_write_byte(AnalogOffsetNick); // Value
158
                i2c_write_byte(AnalogOffsetNick); // Value
159
                break;
159
                break;
160
        case 11:
160
        case 11:
161
                i2c_write_byte(0x80); // Value
161
                i2c_write_byte(0x80); // Value
162
                break;
162
                break;
163
        case 12:
163
        case 12:
164
                i2c_stop();              
164
                i2c_stop();              
165
                I2CTimeout = 10;
165
                I2CTimeout = 10;
166
                i2c_start();  
166
                i2c_start();  
167
                break;
167
                break;
168
        case 13:
168
        case 13:
169
                i2c_write_byte(0x98); // Address of the DAC
169
                i2c_write_byte(0x98); // Address of the DAC
170
                break;
170
                break;
171
        case 14:
171
        case 14:
172
                i2c_write_byte(0x12); // Update Channel B
172
                i2c_write_byte(0x12); // Update Channel B
173
                break;
173
                break;
174
        case 15:
174
        case 15:
175
                i2c_write_byte(AnalogOffsetRoll); // Value
175
                i2c_write_byte(AnalogOffsetRoll); // Value
176
                break;
176
                break;
177
        case 16:
177
        case 16:
178
                i2c_write_byte(0x80); // Value
178
                i2c_write_byte(0x80); // Value
179
                break;
179
                break;
180
        case 17:
180
        case 17:
181
                i2c_stop();              
181
                i2c_stop();              
182
                I2CTimeout = 10;
182
                I2CTimeout = 10;
183
                i2c_start();  
183
                i2c_start();  
184
                break;
184
                break;
185
        case 18:
185
        case 18:
186
                i2c_write_byte(0x98); // Address of the DAC
186
                i2c_write_byte(0x98); // Address of the DAC
187
                break;
187
                break;
188
        case 19:
188
        case 19:
189
                i2c_write_byte(0x14); // Update Channel C
189
                i2c_write_byte(0x14); // Update Channel C
190
                break;
190
                break;
191
        case 20:
191
        case 20:
192
                i2c_write_byte(AnalogOffsetGier); // Value
192
                i2c_write_byte(AnalogOffsetGier); // Value
193
                break;
193
                break;
194
        case 21:
194
        case 21:
195
                i2c_write_byte(0x80); // Value
195
                i2c_write_byte(0x80); // Value
196
                break;
196
                break;
197
        case 22:
197
        case 22:
198
                i2c_stop();              
198
                i2c_stop();              
199
                I2CTimeout = 10;
199
                I2CTimeout = 10;
200
                twi_state = 0;
200
                twi_state = 0;
201
                break;
201
                break;
202
        }
202
        }
203
 TWCR |= 0x80;
203
 TWCR |= 0x80;
204
}
204
}
205
 
205