3,10 → 3,9 |
|
#include "main.h" |
|
unsigned char twi_state = 0; |
unsigned char motor = 0; |
unsigned char motorread = 0; |
unsigned char motor_rx[8]; |
volatile unsigned char twi_state = 0; |
volatile unsigned char motor = 0; |
volatile unsigned char motor_rx[8]; |
|
//############################################################################ |
//Initzialisieren der I2C (TWI) Schnittstelle |
43,19 → 42,37 |
} |
|
//############################################################################ |
// I2C receive byte and send ACK |
void i2c_receive_byte(void) |
//############################################################################ |
{ |
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
} |
|
//############################################################################ |
// I2C receive last byte and send NOT ACK |
void i2c_receive_last_byte(void) |
//############################################################################ |
{ |
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
} |
|
//############################################################################ |
//Start I2C |
SIGNAL (TWI_vect) |
//############################################################################ |
{ |
switch (twi_state++) // First i2c_start from SendMotorData() |
{ |
// Master Transmit |
case 0: // Address Slave SL+W |
static unsigned char motorread = 0; |
|
switch (twi_state++) // First i2c_start from SendMotorData() |
{ |
// Master Transmit |
case 0: // Address Slave SL+W |
i2c_write_byte(0x52+(motor*2)); |
break; |
case 1: // Send Data |
case 1: // Send Data |
switch(motor++) |
{ |
{ |
case 0: |
i2c_write_byte(Motor_Vorne); |
break; |
68,28 → 85,31 |
case 3: |
i2c_write_byte(Motor_Links); |
break; |
} |
} |
break; |
case 2: // Repeat case 0+1 for all Slaves |
case 2: // Repeat case 0+1 for all Slaves |
if (motor < 4) twi_state = 0; |
i2c_start(); // Repeated start -> switch salve and switch Master Transmit <-> Master Receive respectively |
i2c_start(); // Repeated start -> switch salve or switch Master Transmit -> Master Receive |
break; |
|
|
// Master Receive |
case 3: // Address Slave SL+R |
i2c_write_byte(0x53+(motorread*2)); |
break; |
case 4: //1. Byte vom Motor lesen |
case 4: //1. Byte vom Motor übertragen |
i2c_receive_byte(); |
break; |
case 5: // 1. Byte lesen und 2. Byte übertragen |
motor_rx[motorread] = TWDR; |
TWCR |= (1<<TWINT); |
break; |
case 5: //2. Byte vom Motor lesen |
i2c_receive_last_byte(); |
break; |
case 6: //2. Byte lesen |
motor_rx[motorread+4] = TWDR; |
motorread++; |
if (motorread > 3) motorread=0; |
default: |
if (motorread > 3) motorread=0; |
default: |
i2c_stop(); |
twi_state = 0; |
motor = 0; |
} |
motor = 0; |
} |
} |