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