I2C on Mega16 simulation - TWINT not being set by hardware

Discussion in 'Embedded Systems and Microcontrollers' started by Merlin_M, Jan 20, 2015.

  1. Merlin_M

    Thread Starter New Member

    Dec 12, 2009
    1
    0
    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 (Text):
    1.  
    2. inline void i2cWaitForComplete(void)
    3. {
    4.     // wait for i2c interface to complete operation
    5.     while((TWCR & (1<<TWINT)) == 0);
    6. }
    7.  
    8. u08 i2cMasterSendNI(u08 deviceAddr, u08 length, u08* data)
    9. {
    10.     u08 retval = I2C_OK;
    11.  
    12.     // disable TWI interrupt
    13.     //cbi(TWCR, TWIE);
    14.  
    15.     // send start condition
    16.     i2cSendStart();
    17.     uartSendByte(0xFF);
    18.     i2cWaitForComplete();
    19.     uartSendByte(0xFE);
    20.  
    21.     // send device address with write
    22.     i2cSendByte( deviceAddr & 0xFE );
    23.     uartSendByte(0xFD);
    24.     i2cWaitForComplete();
    25.     uartSendByte(0xFC);
    26.     //uartSendByte(TWSR);
    27.  
    28.  
    29.  
    30.     // check if device is present and live
    31.     if( inb(TWSR) == TW_MT_SLA_ACK)
    32.     {
    33.         // send data
    34.         for (int i = 0;i < length; i++)
    35.         {
    36.             i2cSendByte(data[J][J]);
    37.              TWCR |= (1<<TWINT);
    38.             uartSendByte(TWSR);
    39.             i2cWaitForComplete();
    40.            
    41.         }
    42.     }
    43.    
    44.    
    45.     // transmit stop condition
    46.     // leave with TWEA on for slave receiving
    47.     i2cSendStop();
    48.     while( !(inb(TWCR) & BV(TWSTO)) );
    49.  
    50.     // enable TWI interrupt
    51.     //sbi(TWCR, TWIE);
    52.  
    53.     return retval;
    54. }
    55.  


    Please assist me. I am seeing flames.
     
    • sims.jpg
      sims.jpg
      File size:
      168.4 KB
      Views:
      12
    Last edited by a moderator: Jan 20, 2015
Loading...