Rev 1642 | Rev 1644 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1642 | Rev 1643 | ||
---|---|---|---|
Line 2... | Line 2... | ||
2 | ############################################################################*/ |
2 | ############################################################################*/ |
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,TransmitBlConfig = 1; |
|
Line 7... | Line 8... | ||
7 | unsigned char motor = 0,TransmitBlConfig = 0; |
8 | unsigned char motorread = 0,MissingMotor = 0; |
Line 8... | Line 9... | ||
8 | unsigned char motorread = 0,MissingMotor = 0; |
9 | unsigned char ReadTemperature = 0; |
Line 35... | Line 36... | ||
35 | TWBR = 0; |
36 | TWBR = 0; |
36 | i2c_init(); |
37 | i2c_init(); |
37 | I2C_Start(); |
38 | I2C_Start(); |
38 | i2c_write_byte(0); |
39 | i2c_write_byte(0); |
39 | } |
40 | } |
40 | /* |
- | |
41 | //############################################################################ |
- | |
42 | void i2c_write_byte(char byte) |
- | |
43 | //############################################################################ |
- | |
44 | { |
- | |
45 | TWSR = 0x00; |
- | |
46 | TWDR = byte; |
- | |
47 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
- | |
48 | } |
- | |
49 | */ |
- | |
50 | - | ||
51 | /* |
- | |
52 | void I2C_WriteByte(int8_t byte) |
- | |
53 | { |
- | |
54 | // move byte to send into TWI Data Register |
- | |
55 | TWDR = byte; |
- | |
56 | // clear interrupt flag (TWINT = 1) |
- | |
57 | // enable i2c bus (TWEN = 1) |
- | |
58 | // enable interrupt (TWIE = 1) |
- | |
59 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
- | |
60 | } |
- | |
61 | */ |
- | |
Line 62... | Line 41... | ||
62 | 41 | ||
63 | /* |
42 | /* |
64 | //############################################################################ |
43 | //############################################################################ |
65 | SIGNAL (TWI_vect) |
44 | SIGNAL (TWI_vect) |
Line 200... | Line 179... | ||
200 | 179 | ||
201 | //############################################################################ |
180 | //############################################################################ |
202 | SIGNAL (TWI_vect) |
181 | SIGNAL (TWI_vect) |
203 | //############################################################################ |
182 | //############################################################################ |
204 | { // 2 3 4 5 6 7 8 9 |
183 | { // 2 3 4 5 6 7 8 9 |
205 | unsigned char test[] = {0,0,'#',0x1F,255,30,100,64,0x00,7,8,9,10}; |
184 | unsigned char test[] = {0,0,'#',0x1F,255,30,99,49,0x00,7,8,9,10}; |
Line 206... | Line 185... | ||
206 | static unsigned char missing_motor,send = 0,crc = 0; |
185 | static unsigned char missing_motor,send = 0,crc = 0,read_more = 0; |
207 | 186 | ||
208 | switch(twi_state++) |
187 | switch(twi_state++) |
209 | { |
188 | { |
Line 221... | Line 200... | ||
221 | else i2c_write_byte(0x52+(motor*2)); |
200 | else i2c_write_byte(0x52+(motor*2)); |
222 | send = 0; |
201 | send = 0; |
223 | break; |
202 | break; |
224 | case 1: |
203 | case 1: |
225 | i2c_write_byte(Motor[motor].SetPoint); |
204 | i2c_write_byte(Motor[motor].SetPoint); |
226 | if(!(Motor[motor].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)/* || !Motor[motor].SetPointLowerBits*/) |
205 | // if(!(Motor[motor].Version & MOTOR_STATE_NEW_PROTOCOL_MASK) /*|| !Motor[motor].SetPointLowerBits*/) |
- | 206 | if(!(Motor[motor].Version & MOTOR_STATE_NEW_PROTOCOL_MASK) || !Motor[motor].SetPointLowerBits && !TransmitBlConfig) |
|
227 | twi_state++; |
207 | twi_state++; // skip |
228 | break; |
208 | break; |
229 | case 2: |
209 | case 2: |
230 | if(!send++) |
210 | if(!send++) |
231 | { |
211 | { |
232 | i2c_write_byte((Motor[motor].SetPointLowerBits << 1));// + (7 << 0)); |
212 | i2c_write_byte((Motor[motor].SetPointLowerBits << 1) & 0x07); |
233 | crc = 0xAA; |
213 | crc = 0xAA; |
234 | } |
214 | } |
235 | else |
215 | else |
236 | if(send == 9) i2c_write_byte(crc) |
216 | if(send == 9) i2c_write_byte(crc) |
237 | else |
217 | else |
Line 264... | Line 244... | ||
264 | //Transmit 1st byte for reading |
244 | //Transmit 1st byte for reading |
265 | if(TWSR != 0x40) // Error? |
245 | if(TWSR != 0x40) // Error? |
266 | { |
246 | { |
267 | Motor[motorread].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit |
247 | Motor[motorread].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit |
268 | motorread++; |
248 | motorread++; |
269 | if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; } |
249 | if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; ReadTemperature = ++ReadTemperature & 0x0f;} |
270 | I2C_Stop(); |
250 | I2C_Stop(); |
271 | twi_state = 0; |
251 | twi_state = 0; |
272 | } |
252 | } |
273 | else |
253 | else |
274 | { |
254 | { |
275 | Motor[motorread].State |= MOTOR_STATE_PRESENT_MASK; // set present bit |
255 | Motor[motorread].State |= MOTOR_STATE_PRESENT_MASK; // set present bit |
- | 256 | if(motorread == ReadTemperature || TransmitBlConfig) |
|
- | 257 | { |
|
- | 258 | read_more = 1; |
|
276 | I2C_ReceiveByte(); |
259 | I2C_ReceiveByte(); |
- | 260 | } |
|
- | 261 | else |
|
- | 262 | { |
|
- | 263 | I2C_ReceiveLastByte(); |
|
- | 264 | read_more = 0; |
|
- | 265 | } |
|
277 | } |
266 | } |
278 | MissingMotor = missing_motor; |
267 | MissingMotor = missing_motor; |
279 | missing_motor = 0; |
268 | missing_motor = 0; |
280 | break; |
269 | break; |
281 | case 5: //Read 1st byte and transmit 2nd Byte |
270 | case 5: //Read 1st byte and transmit 2nd Byte |
282 | Motor[motorread].Current = TWDR; |
271 | Motor[motorread].Current = TWDR; |
283 | I2C_ReceiveByte(); //nack |
272 | if(read_more) I2C_ReceiveByte() //nack |
- | 273 | else |
|
- | 274 | { |
|
- | 275 | motorread++; // next motor |
|
- | 276 | if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; ReadTemperature = ++ReadTemperature & 0x0f;} |
|
- | 277 | I2C_Stop(); |
|
- | 278 | twi_state = 0; |
|
- | 279 | } |
|
284 | break; |
280 | break; |
285 | case 6: |
281 | case 6: |
286 | //Read 2nd byte and transmit 3rd Byte |
282 | //Read 2nd byte and transmit 3rd Byte |
287 | Motor[motorread].MaxPWM = TWDR; |
283 | Motor[motorread].MaxPWM = TWDR; |
288 | if(TWDR == 250) |
284 | if(TWDR == 250) |
289 | { |
285 | { |
290 | Motor[motorread].Version |= MOTOR_STATE_NEW_PROTOCOL_MASK; |
286 | if(!MotorenEin) Motor[motorread].Version |= MOTOR_STATE_NEW_PROTOCOL_MASK; |
291 | } |
287 | } |
292 | else if(TransmitBlConfig) Motor[motorread].Version = 0; |
288 | else if(TransmitBlConfig) Motor[motorread].Version = 0; |
293 | I2C_ReceiveLastByte(); //nack |
289 | I2C_ReceiveLastByte(); //nack |
294 | break; |
290 | break; |
295 | case 7: // read next |
291 | case 7: // read next |
296 | Motor[motorread].Temperature = TWDR; |
292 | Motor[motorread].Temperature = TWDR; |
297 | motorread++; // next motor |
293 | motorread++; // next motor |
298 | if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; } |
294 | if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; } |
299 | I2C_Stop(); |
295 | I2C_Stop(); |
300 | twi_state = 0; |
296 | twi_state = 0; |
301 | break; |
297 | break; |
302 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
298 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
303 | // writing Gyro-Offset |
299 | // writing Gyro-Offset |
304 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
300 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
305 | case 18: |
301 | case 18: |
306 | i2c_write_byte(0x98); // Address of the DAC |
302 | i2c_write_byte(0x98); // Address of the DAC |