I2C on Mega16 simulation - TWINT not being set by hardware

Thread Starter

Merlin_M

Joined Dec 12, 2009
1
Hi All,

I am simulating I2C Proteus 8 before flashing to the code to the hardware and testing it. I ran the code on AVRStudio 5 and still the code gets stuck in that loop because TWINT is not set by hardware.

The code below waits for TWINT to be set before proceeding. It works after sending the start bit and the slave address + w. But after the command byte, TWINT isnt being set. So my code gets stuck in the "i2cWaitForComplete" loop.

What am I doing wrong?

Please note this is a modified PROCYON library and the uartsendbyte are for debugging purposes.

Code:
inline void i2cWaitForComplete(void)
{
    // wait for i2c interface to complete operation
    while((TWCR & (1<<TWINT)) == 0);
}

u08 i2cMasterSendNI(u08 deviceAddr, u08 length, u08* data)
{
    u08 retval = I2C_OK;

    // disable TWI interrupt
    //cbi(TWCR, TWIE);

    // send start condition
    i2cSendStart();
    uartSendByte(0xFF);
    i2cWaitForComplete();
    uartSendByte(0xFE);
 
    // send device address with write
    i2cSendByte( deviceAddr & 0xFE );
    uartSendByte(0xFD);
    i2cWaitForComplete();
    uartSendByte(0xFC);
    //uartSendByte(TWSR);
 
 
 
    // check if device is present and live
    if( inb(TWSR) == TW_MT_SLA_ACK)
    {
        // send data
        for (int i = 0;i < length; i++)
        {
            i2cSendByte(data[J][J]);
             TWCR |= (1<<TWINT);
            uartSendByte(TWSR);
            i2cWaitForComplete();
           
        }
    }
   
   
    // transmit stop condition
    // leave with TWEA on for slave receiving
    i2cSendStop();
    while( !(inb(TWCR) & BV(TWSTO)) );

    // enable TWI interrupt
    //sbi(TWCR, TWIE);

    return retval;
}


Please assist me. I am seeing flames.
 

Attachments

Last edited by a moderator:
Top