I've written this AVR code as inline assembly within C as an attempt to initialize i2c communication as a master, transmit device that writes some (for now) nonsense data to a slave device on address 0x40 (with the left-shift and write-bit accounted for). However, it seems to never get past trying to get an ACK from the slave device and stays in that loop indefinetly. I'm new to i2c and AVR assembly so I know what to do now or even how to properly debug my code, but I'm posting this here in hope for someone that could point out what I'm doing wrong. Thanks. ![]()
#define i2cStartMaster() \
asm volatile( \
"ldi r20, 0x00 \n" /* prescale */ \
"sts 0xb9, r20 \n" /* TWSR */ \
"ldi r20, 0x30 \n" /* division factor */ \
"sts 0xb8, r20 \n" /* TWBR */ \
"ldi r20, 0b10100100 \n" /* set TWEN(twi enable), TWSTA(twi start), TWINT(twi interrupt) */ \
"sts 0xbc, r20 \n" /* TWCR */ \
"_i2c_init_wait: lds r20, 0xbc \n" \
"sbrc r20, 7 \n" /* wait until twint is gone */\
"rjmp _i2c_init_wait \n" \
"ldi r20, %0 \n" /* load i2c addr */ \
"sts 0xba, r20 \n" /* TWAR */ \
"_endless_write: \n" \
"ldi r20, 0b10101010 \n" /* TEST write data */ \
"sts 0xbb, r20 \n" /* write the data to TWDR */ \
"ldi r20, 0b10000100 \n" /* set TWEN(twi enable), TWINT(twi interrupt) */ \
"sts 0xbc, r20 \n" /* TWCR */ \
"_i2c_ack_wait: lds r20, 0xbc \n" \
"sbrc r20, 7 \n" /* wait until twint is gone, then skip next instruction */ \
"rjmp _i2c_ack_wait \n" \
"rjmp _endless_write" \
: \
: "g" (i2c_address) \
);