15,7 → 15,7 |
/**************************************************/ |
/* Initialize I2C (TWI) */ |
/**************************************************/ |
void i2c_init(void) |
void I2C_Init(void) |
{ |
uint8_t sreg = SREG; |
cli(); |
40,7 → 40,7 |
/****************************************/ |
/* Start I2C */ |
/****************************************/ |
void i2c_start(void) |
void I2C_Start(void) |
{ |
// TWI Control Register |
// clear TWI interrupt flag (TWINT=1) |
56,7 → 56,7 |
/****************************************/ |
/* Stop I2C */ |
/****************************************/ |
void i2c_stop(void) |
void I2C_Stop(void) |
{ |
// TWI Control Register |
// clear TWI interrupt flag (TWINT=1) |
72,10 → 72,10 |
/****************************************/ |
/* Reset I2C */ |
/****************************************/ |
void i2c_reset(void) |
void I2C_Reset(void) |
{ |
// stop i2c bus |
i2c_stop(); |
I2C_Stop(); |
twi_state = 0; |
motor = TWDR; // ?? |
motor = 0; |
85,15 → 85,15 |
TWDR = 0; |
TWSR = 0; |
TWBR = 0; |
i2c_init(); |
i2c_start(); |
i2c_write_byte(0); |
I2C_Init(); |
I2C_Start(); |
I2C_WriteByte(0); |
} |
|
/****************************************/ |
/* Write to I2C */ |
/****************************************/ |
void i2c_write_byte(int8_t byte) |
void I2C_WriteByte(int8_t byte) |
{ |
// move byte to send into TWI Data Register |
TWDR = byte; |
107,7 → 107,7 |
/****************************************/ |
/* Receive byte and send ACK */ |
/****************************************/ |
void i2c_receive_byte(void) |
void I2C_ReceiveByte(void) |
{ |
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
} |
115,7 → 115,7 |
/****************************************/ |
/* I2C receive last byte and send no ACK*/ |
/****************************************/ |
void i2c_receive_last_byte(void) |
void I2C_ReceiveLastByte(void) |
{ |
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
} |
133,41 → 133,41 |
{ |
// Master Transmit |
case 0: // Send SLA-W |
i2c_write_byte(0x52+(motor*2)); |
I2C_WriteByte(0x52+(motor*2)); |
break; |
case 1: // Send Data to Salve |
switch(motor++) |
{ |
case 0: |
i2c_write_byte(Motor_Front); |
I2C_WriteByte(Motor_Front); |
break; |
case 1: |
i2c_write_byte(Motor_Rear); |
I2C_WriteByte(Motor_Rear); |
break; |
case 2: |
i2c_write_byte(Motor_Right); |
I2C_WriteByte(Motor_Right); |
break; |
case 3: |
i2c_write_byte(Motor_Left); |
I2C_WriteByte(Motor_Left); |
break; |
} |
break; |
case 2: // repeat case 0+1 for all Slaves |
if (motor<4) twi_state = 0; |
i2c_start(); // Repeated start -> switch salve or switch Master Transmit -> Master Receive |
I2C_Start(); // Repeated start -> switch salve or switch Master Transmit -> Master Receive |
break; |
|
// Master Receive |
case 3: // Send SLA-R |
i2c_write_byte(0x53+(motorread*2)); |
I2C_WriteByte(0x53+(motorread*2)); |
break; |
case 4: |
//Transmit 1st byte |
i2c_receive_byte(); |
I2C_ReceiveByte(); |
break; |
case 5: //Read 1st byte and transmit 2nd Byte |
motor_rx[motorread] = TWDR; |
i2c_receive_last_byte(); |
I2C_ReceiveLastByte(); |
break; |
case 6: |
//Read 2nd byte |
176,7 → 176,7 |
if (motorread > 3) motorread=0; |
|
default: |
i2c_stop(); |
I2C_Stop(); |
twi_state = 0; |
I2CTimeout = 10; |
motor = 0; |