3,9 → 3,6 |
|
#include "main.h" |
|
unsigned char twi_state = 0; |
unsigned char motor = 0; |
unsigned char motorread = 0; |
unsigned char motor_rx[8]; |
|
//############################################################################ |
19,15 → 16,14 |
|
//############################################################################ |
//Start I2C |
char i2c_start(void) |
void i2c_start(void) |
//############################################################################ |
{ |
TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
return(0); |
} |
|
//############################################################################ |
//Start I2C |
//Stop I2C |
void i2c_stop(void) |
//############################################################################ |
{ |
35,23 → 31,23 |
} |
|
//############################################################################ |
//Start I2C |
char i2c_write_byte(char byte) |
//Write to I2C |
void i2c_write_byte(char byte) |
//############################################################################ |
{ |
TWSR = 0x00; |
TWDR = byte; |
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
|
return(0); |
|
} |
|
//############################################################################ |
//Start I2C |
//ISR |
SIGNAL (TWI_vect) |
//############################################################################ |
{ |
static unsigned char twi_state = 0; |
static unsigned char motor = 0; |
static unsigned char motorread = 0; |
|
switch (twi_state++) |
{ |
case 0: |
75,9 → 71,7 |
} |
break; |
case 2: |
i2c_stop(); |
if (motor<4) twi_state = 0; |
else motor = 0; |
i2c_start(); |
break; |
|
86,46 → 80,19 |
i2c_write_byte(0x53+(motorread*2)); |
break; |
case 4: |
switch(motorread) |
{ |
case 0: |
i2c_write_byte(Motor_Vorne); |
i2c_write_byte(0xFF); |
break; |
case 1: |
i2c_write_byte(Motor_Hinten); |
break; |
case 2: |
i2c_write_byte(Motor_Rechts); |
break; |
case 3: |
i2c_write_byte(Motor_Links); |
break; |
} |
break; |
case 5: //1 Byte vom Motor lesen |
case 5: // 1. Byte vom Motor lesen |
motor_rx[motorread] = TWDR; |
case 6: |
switch(motorread) |
{ |
case 0: |
i2c_write_byte(Motor_Vorne); |
i2c_write_byte(0xFF); |
break; |
case 1: |
i2c_write_byte(Motor_Hinten); |
break; |
case 2: |
i2c_write_byte(Motor_Rechts); |
break; |
case 3: |
i2c_write_byte(Motor_Links); |
break; |
} |
break; |
case 7: //2 Byte vom Motor lesen |
case 6: // 2. Byte vom Motor lesen |
motor_rx[motorread+4] = TWDR; |
motorread++; |
if (motorread>3) motorread=0; |
default: |
i2c_stop(); |
twi_state = 0; |
motor = 0; |
} |
} |