lpcware

i2c in LPC810

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 18, 2016 by lpcware
Content originally posted in LPCWare by HammingBird on Mon Apr 04 09:33:43 MST 2016
Hallo,
I am doing a project with LPC810, i2c communication.  I need to configure LPC810 as a Master.  . trying to send 1byte to slave. But cound not get nothing.

even, I did not see any signal(scl,sda) in oscilloscope.

can anyone help me out ?

here is the prog(Although this prog is from internet, almost everything make sence):







#include "LPC8xx.h"

#define I2C_CFG_MSTEN (0x1)
#define I2C_STAT_MSTPENDING (0x1)
#define I2C_STAT_MSTSTATE (0xe)
#define I2C_STAT_MSTST_IDLE (0x0)
#define I2C_STAT_MSTST_RX (0x2)
#define I2C_STAT_MSTST_TX (0x4) // STAT registor
#define I2C_MSTCTL_MSTCONTINUE (0x1)
#define I2C_MSTCTL_MSTSTART (0x2)
#define I2C_WSTCTL_MSTSTOP (0x4)

int main ()
{
        //Set clock for SWM (file:SWM.c)
// normal addr of SYSAHBCLKCTRL = 0x080 = 1<<7
        LPC_SYSCON->SYSAHBCLKCTRL |= 1<<7; ///Set clock for i2c


      //Disable special functions (file:SWM.c)
        LPC_SWM->PINENABLE0 |= 0xFF0FFFFFFUL;

//Set pins for i2c by SWM (file:SWM.c)
        LPC_SWM->PINASSIGN7 |= 0x03FFFFFFUL; //* I2C0_SDA */Pin PIO0_3= pin3
        LPC_SWM->PINASSIGN8 |= 0xFFFFFF04UL; //* I2C0_SCL */Pin PIO0_4= pin2


        //Set clock for IOCON by SWM (file:iocon.c)
        LPC_SYSCON->SYSAHBCLKCTRL |= 1<<18;

//Disable Pullups for PIO0_2 and PIO0_3 (file:iocon.c) 
        LPC_IOCON->PIO0_2 = 0x80; //PIO0_2 (pin 4)
        LPC_IOCON->PIO0_3 = 0x80; //PIO0_3 (pin 3)


//Set clock for i2c (file:swm.c)
// SYSAHBCLKCTRL= 0x0A0- 0x080 =  0x020 -->1<<5
        LPC_SYSCON->SYSAHBCLKCTRL |= 1<<5;

//Set preset for i2c
//PRESETCTRL =0x044-0x004=0x040--> 0x040= 1<<6
LPC_SYSCON->PRESETCTRL &= ~1<<6;  // ~1<<6 = 0111111 = 3f
LPC_SYSCON->PRESETCTRL |= 1<<6;


//NVIC enable
        NVIC_EnableIRQ(I2C_IRQn);


//Set i2c clock DIV--> CLKDIV
        LPC_I2C->DIV = 0x1;
//CLKDIV= 0x0001 --> PCLK is divided by 2 before use by the I2C  function.




//Example Code:Master write one byte to slave

// Configure I2C Master = CFG = 0x001  
        //Enable i2c master
        LPC_I2C->CFG = I2C_CFG_MSTEN; // Master Enable


// Write to the Slave Address 
        while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
        if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) == I2C_STAT_MSTST_IDLE){
               
//Set address(Display/Slave address 0x3c for OLED  )
                LPC_I2C->MSTDAT = (0x3C << 1) | 0;
               

// MSTCTL = 0x22--> 0x2 
//Send start bit
                LPC_I2C->MSTCTL = 0x2; //ex:code= 0x2
        }

//waiting for pending status
        while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
        if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) == I2C_STAT_MSTST_TX) {
               
// Write 8 bit data
//Send sub address //in ex:sub addr = 0xaa 
                LPC_I2C->MSTDAT = 0x00; // sub address 0x00
              
       // Continue Transmission
//  Send continue
                LPC_I2C->MSTCTL = 0x1; //0x21--> 0x1
        }

// Waiting for Pending Status
        while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
        if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) == I2C_STAT_MSTST_TX){
  //I2C_STAT_MSTSTATE (0xe) //I2C_STAT_MSTST_TX =STAT(0x4)

                //Send value to Display
                LPC_I2C->MSTDAT = 0xAF; //Writing “0xAF” to Display
               
//  Continue Transmission
//Send continue
                LPC_I2C->MSTCTL = 0x1; //0x21-> 0x1
        }

while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
        if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) == I2C_STAT_MSTST_TX){
                     //I2C_STAT_MSTSTATE (0xe)//I2C_STAT_MSTST_TX =STAT(0x4)
               

// stop Transmission
//Send stop bit
                LPC_I2C->MSTCTL = 0x4; //0x24 -->0x4 
        }

        while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
        if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) == I2C_STAT_MSTST_IDLE);

}












Outcomes