I am trying to use the Arduino Uno (Atmega328p) to controll a small OLED display (SSD1306 driver) using the I2C protocoll. Sending the START signal and slave address both give ACK signal back, which is good. But when sending the first data byte, the TWINT flag in the TWCR register is not getting set (indicating transmission). The wiring is good, I've tried using the u8glib library, and it controls the display with success. I've also scanned for the display, and it shows up at the correct address. I've based my code on the C code given in the Atmega48a datasheet (which had multiple code bugs). Here is my code.
// Send START signal to slave
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
// Block until transmitted
while (!(TWCR & (1<<TWINT)));
// Check if status is START (it is)
if ((TWSR & 0xF8) != 0x08)
i2c_err();
TWDR = 0x79;
TWCR = (1<<TWINT) | (1<<TWEN);
// Block until ACK is received
while (!(TWCR & (1<<TWINT)));
// Check if address has been sent
if ((TWSR & 0xF8) == 0x20)
i2c_err();
Send first data byte
TWDR = B01000000;
TWCR = (1<<TWINT)|(1<<TWEN);
// This while loop never stops
while (!(TWCR & (1<<TWINT)));
It freezes at the last line. Does anybody know why it doesn't transmit?