Custom I2C Library

I'm having trouble getting low level help with the TWI interface. So I think I'll have to use the wire library. I never got far enough on my own to implement an ISR.

My code will transmit and receive. The receive routine matches the implementation example in the Atmel data sheet. However, if I loop it, the code crashes after so many iterations. If I loop the wire implementation it never crashes.

I'm wondering if it's a setup issue. The data sheet is rather lacking in that respect.

Here's the full code:

#include <Wire.h>

#define DEBUG
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22

void setup( void )
{
Serial.begin(9600);

// Init I2C
PORTC |= 0x18; // Connect Pull-Up resistors
TWSR &= 0xF8; // Set bit rate prescalar (p239)
TWBR = 0x0a; // Set two wire bit rate
TWCR = (1 << TWEN);// Enable I2C
}

void loop( void )
{
int i = 0;

while(1)
{
Serial.print( "#: " );
Serial.print( i );

start();
TX( 0x3A );
TX( 0x0F );
start();
TX( 0x3B );
RX();
stop();

Serial.print( " D: " );
Serial.println( TWDR, HEX );

i++;

delay( 1000 );
}
}

void start()
{
// ******* Send Start Sequence
/* TWINT - Clear Interrupt Flag
TWSTA - Transmit start
TWEN - Enable I2C Bus
TWEA - Enable Acknoledge */
TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWSTA) | (1 << TWEA);
while( !(TWCR & (1 << TWINT) ) );

#if defined DEBUG
Serial.print( "STA:" );
Serial.println( TWSR, HEX );
#endif
}

void TX( byte data )
{
// ******* Send register address
TWDR = data;
/* TWINT - Clear Interrupt Flag
TWEN - Enable I2C Bus
TWEA - Enable Acknoledge */
TWCR = (1 << TWINT) | (1 << TWEA) | (1 << TWEN);
while( !(TWCR & (1 << TWINT) ) );

#if defined DEBUG
Serial.print( "TX: " );
Serial.println( TWSR, HEX );
#endif
}

void RX()
{
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
while( !(TWCR & (1 << TWINT) ) );

#if defined DEBUG
Serial.print( "RX: " );
Serial.println( TWSR, HEX );
#endif
}
void stop( void )
{
TWCR = (1 << TWEN) | (1 << TWEA) | (1 << TWINT) | (1 |TWSTO);
while( TWCR & (1 << TWINT) );

#if defined DEBUG
Serial.print( "STO:" );
Serial.println( TWSR, HEX );
#endif
}