Rev 1638 | Rev 1642 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1638 | Rev 1639 | ||
---|---|---|---|
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,TransmitBlConfig = 0; |
8 | unsigned char motorread = 0,MissingMotor = 0; |
8 | unsigned char motorread = 0,MissingMotor = 0; |
9 | 9 | ||
10 | MotorData_t Motor[MAX_MOTORS]; |
10 | MotorData_t Motor[MAX_MOTORS]; |
11 | 11 | ||
12 | unsigned int I2CError = 0; |
12 | unsigned int I2CError = 0; |
13 | 13 | ||
14 | //############################################################################ |
14 | //############################################################################ |
15 | //Initzialisieren der I2C (TWI) Schnittstelle |
15 | //Initzialisieren der I2C (TWI) Schnittstelle |
16 | void i2c_init(void) |
16 | void i2c_init(void) |
17 | //############################################################################ |
17 | //############################################################################ |
18 | { |
18 | { |
19 | TWSR = 0; |
19 | TWSR = 0; |
20 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
20 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
21 | } |
21 | } |
22 | - | ||
23 | //############################################################################ |
- | |
24 | //Start I2C |
- | |
25 | void i2c_start(void) |
- | |
26 | //############################################################################ |
- | |
27 | { |
- | |
28 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
- | |
29 | } |
- | |
30 | - | ||
31 | //############################################################################ |
- | |
32 | void i2c_stop(void) |
- | |
33 | //############################################################################ |
- | |
34 | { |
- | |
35 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
- | |
36 | } |
- | |
37 | 22 | ||
38 | void i2c_reset(void) |
23 | void i2c_reset(void) |
39 | //############################################################################ |
24 | //############################################################################ |
40 | { |
25 | { |
41 | i2c_stop(); |
26 | I2C_Stop(); |
42 | twi_state = 0; |
27 | twi_state = 0; |
43 | motor = TWDR; |
28 | motor = TWDR; |
44 | motor = 0; |
29 | motor = 0; |
45 | TWCR = 0x80; |
30 | TWCR = 0x80; |
46 | TWAMR = 0; |
31 | TWAMR = 0; |
47 | TWAR = 0; |
32 | TWAR = 0; |
48 | TWDR = 0; |
33 | TWDR = 0; |
49 | TWSR = 0; |
34 | TWSR = 0; |
50 | TWBR = 0; |
35 | TWBR = 0; |
51 | i2c_init(); |
36 | i2c_init(); |
52 | i2c_start(); |
37 | I2C_Start(); |
53 | i2c_write_byte(0); |
38 | i2c_write_byte(0); |
54 | } |
39 | } |
55 | 40 | /* |
|
56 | //############################################################################ |
41 | //############################################################################ |
57 | void i2c_write_byte(char byte) |
42 | void i2c_write_byte(char byte) |
58 | //############################################################################ |
43 | //############################################################################ |
59 | { |
44 | { |
60 | TWSR = 0x00; |
45 | TWSR = 0x00; |
61 | TWDR = byte; |
46 | TWDR = byte; |
62 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
47 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
63 | } |
48 | } |
- | 49 | */ |
|
64 | - | ||
65 | /****************************************/ |
- | |
66 | /* Write to I2C */ |
- | |
- | 50 | ||
67 | /****************************************/ |
51 | /* |
68 | void I2C_WriteByte(int8_t byte) |
52 | void I2C_WriteByte(int8_t byte) |
69 | { |
53 | { |
70 | // move byte to send into TWI Data Register |
54 | // move byte to send into TWI Data Register |
71 | TWDR = byte; |
55 | TWDR = byte; |
72 | // clear interrupt flag (TWINT = 1) |
56 | // clear interrupt flag (TWINT = 1) |
73 | // enable i2c bus (TWEN = 1) |
57 | // enable i2c bus (TWEN = 1) |
74 | // enable interrupt (TWIE = 1) |
58 | // enable interrupt (TWIE = 1) |
75 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
59 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
76 | } |
60 | } |
77 | - | ||
78 | /****************************************/ |
- | |
79 | /* Receive byte and send ACK */ |
- | |
80 | /****************************************/ |
- | |
81 | void I2C_ReceiveByte(void) |
- | |
82 | { |
61 | */ |
83 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
- | |
84 | } |
- | |
85 | - | ||
86 | /****************************************/ |
- | |
87 | /* I2C receive last byte and send no ACK*/ |
- | |
88 | /****************************************/ |
- | |
89 | void I2C_ReceiveLastByte(void) |
- | |
90 | { |
- | |
91 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
- | |
92 | } |
- | |
93 | - | ||
94 | 62 | ||
95 | /* |
63 | /* |
96 | //############################################################################ |
64 | //############################################################################ |
97 | SIGNAL (TWI_vect) |
65 | SIGNAL (TWI_vect) |
98 | //############################################################################ |
66 | //############################################################################ |
99 | { |
67 | { |
100 | static unsigned char missing_motor; |
68 | static unsigned char missing_motor; |
101 | switch(twi_state++) |
69 | switch(twi_state++) |
102 | { |
70 | { |
103 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
71 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
104 | // Writing the Data |
72 | // Writing the Data |
105 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
73 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
106 | case 0: |
74 | case 0: |
107 | while(Mixer.Motor[motor][0] <= 0 && motor < MAX_MOTORS) motor++; // skip if not used |
75 | while(Mixer.Motor[motor][0] <= 0 && motor < MAX_MOTORS) motor++; // skip if not used |
108 | if(motor == MAX_MOTORS) // writing finished -> now read |
76 | if(motor == MAX_MOTORS) // writing finished -> now read |
109 | { |
77 | { |
110 | motor = 0; |
78 | motor = 0; |
111 | twi_state = 3; |
79 | twi_state = 3; |
112 | i2c_write_byte(0x53+(motorread*2)); |
80 | i2c_write_byte(0x53+(motorread*2)); |
113 | } |
81 | } |
114 | else i2c_write_byte(0x52+(motor*2)); |
82 | else i2c_write_byte(0x52+(motor*2)); |
115 | break; |
83 | break; |
116 | case 1: |
84 | case 1: |
117 | i2c_write_byte(Motor[motor++].SetPoint); |
85 | i2c_write_byte(Motor[motor++].SetPoint); |
118 | break; |
86 | break; |
119 | case 2: |
87 | case 2: |
120 | if(TWSR == 0x30) |
88 | if(TWSR == 0x30) |
121 | { |
89 | { |
122 | if(!missing_motor) missing_motor = motor; |
90 | if(!missing_motor) missing_motor = motor; |
123 | if((Motor[motor-1].State & MOTOR_STATE_ERROR_MASK) < MOTOR_STATE_ERROR_MASK) Motor[motor-1].State++; // increment error counter and handle overflow |
91 | if((Motor[motor-1].State & MOTOR_STATE_ERROR_MASK) < MOTOR_STATE_ERROR_MASK) Motor[motor-1].State++; // increment error counter and handle overflow |
124 | } |
92 | } |
125 | i2c_stop(); |
93 | I2C_Stop(); |
126 | I2CTimeout = 10; |
94 | I2CTimeout = 10; |
127 | twi_state = 0; |
95 | twi_state = 0; |
128 | i2c_start(); |
96 | I2C_Start(); |
129 | break; |
97 | break; |
130 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
98 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
131 | // Reading Data |
99 | // Reading Data |
132 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
100 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
133 | case 3: |
101 | case 3: |
134 | //Transmit 1st byte for reading |
102 | //Transmit 1st byte for reading |
135 | if(TWSR != 0x40) // Error? |
103 | if(TWSR != 0x40) // Error? |
136 | { |
104 | { |
137 | Motor[motorread].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit |
105 | Motor[motorread].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit |
138 | motorread++; |
106 | motorread++; |
139 | if(motorread >= MAX_MOTORS) motorread = 0; |
107 | if(motorread >= MAX_MOTORS) motorread = 0; |
140 | i2c_stop(); |
108 | I2C_Stop(); |
141 | twi_state = 0; |
109 | twi_state = 0; |
142 | } |
110 | } |
143 | else |
111 | else |
144 | { |
112 | { |
145 | Motor[motorread].State |= MOTOR_STATE_PRESENT_MASK; // set present bit |
113 | Motor[motorread].State |= MOTOR_STATE_PRESENT_MASK; // set present bit |
146 | I2C_ReceiveByte(); |
114 | I2C_ReceiveByte(); |
147 | } |
115 | } |
148 | MissingMotor = missing_motor; |
116 | MissingMotor = missing_motor; |
149 | missing_motor = 0; |
117 | missing_motor = 0; |
150 | break; |
118 | break; |
151 | case 4: //Read 1st byte and transmit 2nd Byte |
119 | case 4: //Read 1st byte and transmit 2nd Byte |
152 | Motor[motorread].Current = TWDR; |
120 | Motor[motorread].Current = TWDR; |
153 | I2C_ReceiveLastByte(); //nack |
121 | I2C_ReceiveLastByte(); //nack |
154 | break; |
122 | break; |
155 | case 5: |
123 | case 5: |
156 | //Read 2nd byte |
124 | //Read 2nd byte |
157 | Motor[motorread].MaxPWM = TWDR; |
125 | Motor[motorread].MaxPWM = TWDR; |
158 | motorread++; // next motor |
126 | motorread++; // next motor |
159 | if(motorread >= MAX_MOTORS) motorread = 0; |
127 | if(motorread >= MAX_MOTORS) motorread = 0; |
160 | i2c_stop(); |
128 | I2C_Stop(); |
161 | twi_state = 0; |
129 | twi_state = 0; |
162 | break; |
130 | break; |
163 | 131 | ||
164 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
132 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
165 | // writing Gyro-Offset |
133 | // writing Gyro-Offset |
166 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
134 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
167 | case 8: |
135 | case 8: |
168 | i2c_write_byte(0x98); // Address of the DAC |
136 | i2c_write_byte(0x98); // Address of the DAC |
169 | break; |
137 | break; |
170 | case 9: |
138 | case 9: |
171 | i2c_write_byte(0x10); // Update Channel A |
139 | i2c_write_byte(0x10); // Update Channel A |
172 | break; |
140 | break; |
173 | case 10: |
141 | case 10: |
174 | i2c_write_byte(AnalogOffsetNick); // Value |
142 | i2c_write_byte(AnalogOffsetNick); // Value |
175 | break; |
143 | break; |
176 | case 11: |
144 | case 11: |
177 | i2c_write_byte(0x80); // Value |
145 | i2c_write_byte(0x80); // Value |
178 | break; |
146 | break; |
179 | case 12: |
147 | case 12: |
180 | i2c_stop(); |
148 | I2C_Stop(); |
181 | I2CTimeout = 10; |
149 | I2CTimeout = 10; |
182 | i2c_start(); |
150 | I2C_Start(); |
183 | break; |
151 | break; |
184 | case 13: |
152 | case 13: |
185 | i2c_write_byte(0x98); // Address of the DAC |
153 | i2c_write_byte(0x98); // Address of the DAC |
186 | break; |
154 | break; |
187 | case 14: |
155 | case 14: |
188 | i2c_write_byte(0x12); // Update Channel B |
156 | i2c_write_byte(0x12); // Update Channel B |
189 | break; |
157 | break; |
190 | case 15: |
158 | case 15: |
191 | i2c_write_byte(AnalogOffsetRoll); // Value |
159 | i2c_write_byte(AnalogOffsetRoll); // Value |
192 | break; |
160 | break; |
193 | case 16: |
161 | case 16: |
194 | i2c_write_byte(0x80); // Value |
162 | i2c_write_byte(0x80); // Value |
195 | break; |
163 | break; |
196 | case 17: |
164 | case 17: |
197 | i2c_stop(); |
165 | I2C_Stop(); |
198 | I2CTimeout = 10; |
166 | I2CTimeout = 10; |
199 | i2c_start(); |
167 | I2C_Start(); |
200 | break; |
168 | break; |
201 | case 18: |
169 | case 18: |
202 | i2c_write_byte(0x98); // Address of the DAC |
170 | i2c_write_byte(0x98); // Address of the DAC |
203 | break; |
171 | break; |
204 | case 19: |
172 | case 19: |
205 | i2c_write_byte(0x14); // Update Channel C |
173 | i2c_write_byte(0x14); // Update Channel C |
206 | break; |
174 | break; |
207 | case 20: |
175 | case 20: |
208 | i2c_write_byte(AnalogOffsetGier); // Value |
176 | i2c_write_byte(AnalogOffsetGier); // Value |
209 | break; |
177 | break; |
210 | case 21: |
178 | case 21: |
211 | i2c_write_byte(0x80); // Value |
179 | i2c_write_byte(0x80); // Value |
212 | break; |
180 | break; |
213 | case 22: |
181 | case 22: |
214 | i2c_stop(); |
182 | I2C_Stop(); |
215 | I2CTimeout = 10; |
183 | I2CTimeout = 10; |
216 | twi_state = 0; |
184 | twi_state = 0; |
217 | break; |
185 | break; |
218 | default: twi_state = 0; |
186 | default: twi_state = 0; |
219 | break; |
187 | break; |
220 | } |
188 | } |
221 | TWCR |= 0x80; |
189 | TWCR |= 0x80; |
222 | } |
190 | } |
223 | */ |
191 | */ |
224 | 192 | ||
225 | /* |
193 | /* |
226 | if(DataArray[3] & 0x01) Parameter.PwmScaling = DataArray[4]; |
194 | if(DataArray[3] & 0x01) Parameter.PwmScaling = DataArray[4]; |
227 | if(DataArray[3] & 0x02) Parameter.MaxStrom = DataArray[5]; |
195 | if(DataArray[3] & 0x02) Parameter.MaxStrom = DataArray[5]; |
228 | if(DataArray[3] & 0x04) Parameter.TemperaturLimiter = DataArray[6]; |
196 | if(DataArray[3] & 0x04) Parameter.TemperaturLimiter = DataArray[6]; |
229 | if(DataArray[3] & 0x08) Parameter.SkaliereStrom = DataArray[7]; |
197 | if(DataArray[3] & 0x08) Parameter.SkaliereStrom = DataArray[7]; |
230 | if(DataArray[3] & 0x10) Parameter.Bitconfig = DataArray[8]; |
198 | if(DataArray[3] & 0x10) Parameter.Bitconfig = DataArray[8]; |
231 | */ |
199 | */ |
232 | 200 | ||
233 | //############################################################################ |
201 | //############################################################################ |
234 | SIGNAL (TWI_vect) |
202 | SIGNAL (TWI_vect) |
235 | //############################################################################ |
203 | //############################################################################ |
236 | { // 2 3 4 5 6 7 8 9 |
204 | { // 2 3 4 5 6 7 8 9 |
237 | unsigned char test[] = {0,0,'#',0x1F,255,30,100,64,0x00,7,8,9,10}; |
205 | unsigned char test[] = {0,0,'#',0x1F,255,30,100,64,0x00,7,8,9,10}; |
238 | static unsigned char missing_motor,send = 0,crc = 0; |
206 | static unsigned char missing_motor,send = 0,crc = 0, read_temperature = 0; |
- | 207 | ||
239 | switch(twi_state++) |
208 | switch(twi_state++) |
240 | { |
209 | { |
241 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
210 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
242 | // Writing the Data |
211 | // Writing the Data |
243 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
212 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
244 | case 0: |
213 | case 0: |
245 | while(Mixer.Motor[motor][0] <= 0 && motor < MAX_MOTORS) motor++; // skip if not used |
214 | while(Mixer.Motor[motor][0] <= 0 && motor < MAX_MOTORS) motor++; // skip if not used |
246 | if(motor == MAX_MOTORS) // writing finished -> now read |
215 | if(motor == MAX_MOTORS) // writing finished -> now read |
247 | { |
216 | { |
248 | motor = 0; |
217 | motor = 0; |
249 | twi_state = 4; |
218 | twi_state = 4; |
250 | i2c_write_byte(0x53+(motorread*2)); |
219 | i2c_write_byte(0x53+(motorread*2)); |
251 | } |
220 | } |
252 | else i2c_write_byte(0x52+(motor*2)); |
221 | else i2c_write_byte(0x52+(motor*2)); |
253 | send = 0; |
222 | send = 0; |
254 | break; |
223 | break; |
255 | case 1: |
224 | case 1: |
256 | i2c_write_byte(Motor[motor].SetPoint); |
225 | i2c_write_byte(Motor[motor].SetPoint); |
257 | if(!(Motor[motor].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) twi_state++; |
226 | if(!(Motor[motor].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)/* || !Motor[motor].SetPointLowerBits*/) |
- | 227 | twi_state++; |
|
258 | break; |
228 | break; |
259 | case 2: |
229 | case 2: |
260 | if(!send++) |
230 | if(!send++) |
261 | { |
231 | { |
262 | i2c_write_byte((Motor[motor].SetPointLowerBits << 1));// + (7 << 0)); |
232 | i2c_write_byte((Motor[motor].SetPointLowerBits << 1));// + (7 << 0)); |
263 | crc = 0xAA; |
233 | crc = 0xAA; |
264 | } |
234 | } |
265 | else |
235 | else |
266 | if(send == 9) i2c_write_byte(crc); |
236 | if(send == 9) i2c_write_byte(crc) |
267 | else |
237 | else |
268 | { |
238 | { |
269 | crc += test[send]; |
239 | crc += test[send]; |
270 | i2c_write_byte(test[send]); |
240 | i2c_write_byte(test[send]); |
271 | } |
241 | } |
272 | if(!MotorenEin && motor == motorread && !PC_MotortestActive && (Motor[motorread].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) if(send <= 10) twi_state--; |
242 | if(TransmitBlConfig && !MotorenEin && motor == motorread && (Motor[motorread].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) |
- | 243 | { |
|
- | 244 | if(send <= 10) twi_state--; |
|
- | 245 | } |
|
- | 246 | ||
273 | break; |
247 | break; |
274 | case 3: |
248 | case 3: |
275 | motor++; |
249 | motor++; |
276 | if(TWSR == 0x30) |
250 | if(TWSR == 0x30) |
277 | { |
251 | { |
278 | if(!missing_motor) missing_motor = motor; |
252 | if(!missing_motor) missing_motor = motor; |
279 | if((Motor[motor-1].State & MOTOR_STATE_ERROR_MASK) < MOTOR_STATE_ERROR_MASK) Motor[motor-1].State++; // increment error counter and handle overflow |
253 | if((Motor[motor-1].State & MOTOR_STATE_ERROR_MASK) < MOTOR_STATE_ERROR_MASK) Motor[motor-1].State++; // increment error counter and handle overflow |
280 | } |
254 | } |
281 | i2c_stop(); |
255 | I2C_Stop(); |
282 | I2CTimeout = 10; |
256 | I2CTimeout = 10; |
283 | twi_state = 0; |
257 | twi_state = 0; |
284 | i2c_start(); |
258 | I2C_Start(); |
285 | break; |
259 | break; |
286 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
260 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
287 | // Reading Data |
261 | // Reading Data |
288 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
262 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
289 | case 4: |
263 | case 4: |
290 | //Transmit 1st byte for reading |
264 | //Transmit 1st byte for reading |
291 | if(TWSR != 0x40) // Error? |
265 | if(TWSR != 0x40) // Error? |
292 | { |
266 | { |
293 | Motor[motorread].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit |
267 | Motor[motorread].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit |
294 | motorread++; |
268 | motorread++; |
295 | if(motorread >= MAX_MOTORS) motorread = 0; |
269 | if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; } |
296 | i2c_stop(); |
270 | I2C_Stop(); |
297 | twi_state = 0; |
271 | twi_state = 0; |
298 | } |
272 | } |
299 | else |
273 | else |
300 | { |
274 | { |
301 | Motor[motorread].State |= MOTOR_STATE_PRESENT_MASK; // set present bit |
275 | Motor[motorread].State |= MOTOR_STATE_PRESENT_MASK; // set present bit |
302 | I2C_ReceiveByte(); |
276 | I2C_ReceiveByte(); |
303 | } |
277 | } |
304 | MissingMotor = missing_motor; |
278 | MissingMotor = missing_motor; |
305 | missing_motor = 0; |
279 | missing_motor = 0; |
306 | break; |
280 | break; |
307 | case 5: //Read 1st byte and transmit 2nd Byte |
281 | case 5: //Read 1st byte and transmit 2nd Byte |
308 | Motor[motorread].Current = TWDR; |
282 | Motor[motorread].Current = TWDR; |
309 | I2C_ReceiveByte(); //nack |
283 | I2C_ReceiveByte(); //nack |
310 | break; |
284 | break; |
311 | case 6: |
285 | case 6: |
312 | //Read 2nd byte and transmit 3rd Byte |
286 | //Read 2nd byte and transmit 3rd Byte |
313 | Motor[motorread].MaxPWM = TWDR; |
287 | Motor[motorread].MaxPWM = TWDR; |
314 | if(TWDR == 250) |
288 | if(TWDR == 250) |
315 | { |
289 | { |
316 | //if(!Motor[motor].SetPoint) |
- | |
317 | Motor[motorread].Version |= MOTOR_STATE_NEW_PROTOCOL_MASK; |
290 | Motor[motorread].Version |= MOTOR_STATE_NEW_PROTOCOL_MASK; |
318 | } |
291 | } |
- | 292 | else if(TransmitBlConfig) Motor[motorread].Version = 0; |
|
319 | I2C_ReceiveLastByte(); //nack |
293 | I2C_ReceiveLastByte(); //nack |
320 | break; |
294 | break; |
321 | case 7: // read next |
295 | case 7: // read next |
322 | if(TWDR == 255) { if(!MotorenEin) Motor[motorread].Version = 0; DebugOut.Analog[24]++;} |
- | |
323 | Motor[motorread].Temperature = TWDR; |
296 | Motor[motorread].Temperature = TWDR; |
324 | motorread++; // next motor |
297 | motorread++; // next motor |
325 | if(motorread >= MAX_MOTORS) { motorread = 0; } |
298 | if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; } |
326 | i2c_stop(); |
299 | I2C_Stop(); |
327 | twi_state = 0; |
300 | twi_state = 0; |
328 | break; |
301 | break; |
329 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
302 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
330 | // writing Gyro-Offset |
303 | // writing Gyro-Offset |
331 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
304 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
332 | case 18: |
305 | case 18: |
333 | i2c_write_byte(0x98); // Address of the DAC |
306 | i2c_write_byte(0x98); // Address of the DAC |
334 | break; |
307 | break; |
335 | case 19: |
308 | case 19: |
336 | i2c_write_byte(0x10); // Update Channel A |
309 | i2c_write_byte(0x10); // Update Channel A |
337 | break; |
310 | break; |
338 | case 20: |
311 | case 20: |
339 | i2c_write_byte(AnalogOffsetNick); // Value |
312 | i2c_write_byte(AnalogOffsetNick); // Value |
340 | break; |
313 | break; |
341 | case 21: |
314 | case 21: |
342 | i2c_write_byte(0x80); // Value |
315 | i2c_write_byte(0x80); // Value |
343 | break; |
316 | break; |
344 | case 22: |
317 | case 22: |
345 | i2c_stop(); |
318 | I2C_Stop(); |
346 | I2CTimeout = 10; |
319 | I2CTimeout = 10; |
347 | i2c_start(); |
320 | I2C_Start(); |
348 | break; |
321 | break; |
349 | case 23: |
322 | case 23: |
350 | i2c_write_byte(0x98); // Address of the DAC |
323 | i2c_write_byte(0x98); // Address of the DAC |
351 | break; |
324 | break; |
352 | case 24: |
325 | case 24: |
353 | i2c_write_byte(0x12); // Update Channel B |
326 | i2c_write_byte(0x12); // Update Channel B |
354 | break; |
327 | break; |
355 | case 25: |
328 | case 25: |
356 | i2c_write_byte(AnalogOffsetRoll); // Value |
329 | i2c_write_byte(AnalogOffsetRoll); // Value |
357 | break; |
330 | break; |
358 | case 26: |
331 | case 26: |
359 | i2c_write_byte(0x80); // Value |
332 | i2c_write_byte(0x80); // Value |
360 | break; |
333 | break; |
361 | case 27: |
334 | case 27: |
362 | i2c_stop(); |
335 | I2C_Stop(); |
363 | I2CTimeout = 10; |
336 | I2CTimeout = 10; |
364 | i2c_start(); |
337 | I2C_Start(); |
365 | break; |
338 | break; |
366 | case 28: |
339 | case 28: |
367 | i2c_write_byte(0x98); // Address of the DAC |
340 | i2c_write_byte(0x98); // Address of the DAC |
368 | break; |
341 | break; |
369 | case 29: |
342 | case 29: |
370 | i2c_write_byte(0x14); // Update Channel C |
343 | i2c_write_byte(0x14); // Update Channel C |
371 | break; |
344 | break; |
372 | case 30: |
345 | case 30: |
373 | i2c_write_byte(AnalogOffsetGier); // Value |
346 | i2c_write_byte(AnalogOffsetGier); // Value |
374 | break; |
347 | break; |
375 | case 31: |
348 | case 31: |
376 | i2c_write_byte(0x80); // Value |
349 | i2c_write_byte(0x80); // Value |
377 | break; |
350 | break; |
378 | case 32: |
351 | case 32: |
379 | i2c_stop(); |
352 | I2C_Stop(); |
380 | I2CTimeout = 10; |
353 | I2CTimeout = 10; |
381 | twi_state = 0; |
354 | twi_state = 0; |
382 | break; |
355 | break; |
383 | default: twi_state = 0; |
356 | default: twi_state = 0; |
384 | break; |
357 | break; |
385 | } |
358 | } |
386 | TWCR |= 0x80; |
359 | TWCR |= 0x80; |
387 | } |
360 | } |
- | 361 | ||
388 | 362 |