This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

I2C problem with LPC2148

Hey guys, I can't seem to get I2C working with LPC2148. The problem arise when I transfer data. Slave device (I2C debugger in Proteus) doesn't respond. Neither ACK nor NACK. It just get paused. Although when I Write Slave address, Ack is given by Slave.
PCLK is 60MHz.

#define I2EN (1<<6) //Enable/Disable bit
#define STA  (1<<5) //Start Set/Clear bit
#define STO  (1<<4) //Stop bit
#define SI   (1<<3) //Serial Interrupt Flag Clear bit
#define AA   (1<<2) //Assert Acknowledge Set/Clear bit

void I2C0_Init (void) // initialise i2c
{

        I2C0CONCLR = STA|STO|AA|SI;  // clear start, stop, aa and interrupt bit
        I2C0CONSET = I2EN; // set enable bit
}

bool I2C0_WaitForSI (void)  //wait for si flag to set
{
        int timeout = 0;
        while (!(I2C0CONSET&SI))  // while si bit in conset is not set
        {
                timeout++;
                if (timeout>1000) return false;
        }
        return true;
}

void I2C0_SendStart (void)  //send start or r-start
{
        I2C0CONCLR = STA|STO|SI|AA;  //clear start, stop, aa, interrupt bit
        I2C0CONSET = STA; // enable start bit
        I2C0_WaitForSI();  //wait for above condition to complete
}

void I2C0_SendStop (void)
{
        I2C0CONSET = STO;  //set stop bit
}

void I2C0_Tx (unsigned char data)
{
        I2C0DAT = data;  //load data into register
        I2C0CONCLR = STA|SI|STO;  // clear start,interrupt and stop to transfer data
        I2C0_WaitForSI ();  //wait for above condition to complete
}

//unsigned char I2C0_Rx (bool last)
//{
//      if (last) I2C0CONCLR = AA;  //is last address, than clear aa to send nack.
//      else I2C0CONSET = AA;  //otherwise send ack to keep receiving data.
//      I2C0CONCLR = SI; //clear si to receive data.
//      I2C0_WaitForSI ();  //wait for data to be received.
//      return I2C0DAT;
//}

int main ()
{
        PINSEL0 |= (1<<4)|(1<<6)|(0<<5)|(0<<7); //set i2c scl and sda
        PINSEL2 = PINSEL1= 0;

        VPBDIV=0x01;  //pclk is 60Mhz

        I2C0SCLH=300; // 100 khz rate
        I2C0SCLL=300;

        IODIR1 = 0xffffffff;

  unsigned char slave_address = 0xA0;
        unsigned char data = '1';

        I2C0_Init ();

        I2C0_SendStart();

        I2C0_Tx (slave_address);
        I2C0_SendStop();
        I2C0_SendStart ();
        I2C0_Tx (slave_address);
        I2C0_Tx (data);

        I2C0_SendStop();
}