This commit is contained in:
2025-05-04 20:29:47 +02:00
parent 16925fba79
commit 9bb4d59e11

26
main.c
View File

@@ -49,7 +49,7 @@
#define MOTOR_B_PIN_A_OCR OCR0 #define MOTOR_B_PIN_A_OCR OCR0
#define MOTOR_B_PIN_B_OCR OCR1B #define MOTOR_B_PIN_B_OCR OCR1B
#define Slave_Address 0x20 #define Slave_Address 0x69
// I2C Slave Register Map // I2C Slave Register Map
#define REGISTER_COUNT 27 #define REGISTER_COUNT 27
@@ -228,16 +228,16 @@ void update_motor(ServoMotor *motor)
control_motor(motor, pwm, direction); control_motor(motor, pwm, direction);
} }
uint16_t i = 127; uint16_t i = 64535;
uint8_t reg_pointer = 0; uint8_t reg_pointer = 0;
bool expecting_address = true; bool expecting_address = true;
int main(void) int main(void)
{ {
I2C_Slave_Init(Slave_Address);
DDRA = (1 << 7); // LED DDRA = (1 << 7); // LED
PORTA = (1 << 7); PORTA = (1 << 7);
I2C_Slave_Init(Slave_Address);
*(motor_a.pin_a_ddr) |= (1 << motor_a.pin_a_bit); // Direction pin output *(motor_a.pin_a_ddr) |= (1 << motor_a.pin_a_bit); // Direction pin output
*(motor_a.pin_b_ddr) |= (1 << motor_a.pin_b_bit); // Direction pin output *(motor_a.pin_b_ddr) |= (1 << motor_a.pin_b_bit); // Direction pin output
@@ -247,21 +247,21 @@ int main(void)
setup_pwm_motor_a(); setup_pwm_motor_a();
setup_pwm_motor_b(); setup_pwm_motor_b();
while (1) while (1)
{ {
if (!i++) if (!i++)
{ {
PORTA ^= (1 << 7); PORTA ^= (1 << 7);
i = 60000; i = 64535;
} }
update_motor(&motor_a); update_motor(&motor_a);
update_motor(&motor_b); update_motor(&motor_b);
switch (I2C_Slave_Listen()) if (TWCR & (1 << TWINT))
{ {
case 0: /* Wait to be addressed */
int8_t status = TWSR & 0xF8; /* Read TWI status register with masking lower three bits */
if (status == 0x60 || status == 0x68) /* Check weather own SLA+W received & ack returned (TWEA = 1) */
{ {
// WRITE
do do
{ {
int8_t byte = I2C_Slave_Receive(); int8_t byte = I2C_Slave_Receive();
@@ -334,10 +334,8 @@ int main(void)
} }
} while (1); } while (1);
expecting_address = true; // Reset for next transaction expecting_address = true; // Reset for next transaction
break;
} }
case 1: if (status == 0xA8 || status == 0xB0) /* Check weather own SLA+R received & ack returned (TWEA = 1) */
{ {
// READ // READ
char ret; char ret;
@@ -396,8 +394,10 @@ int main(void)
reg_pointer++; reg_pointer++;
break; break;
} }
default: if (status == 0x70 || status == 0x78) /* Check weather general call received & ack returned (TWEA = 1) */
break; continue; /* If yes then return 2 to indicate ack returned */
else
continue; /* Else continue */
} }
} }
} }