lpcware

LPC810 I2C: master write one byte to subaddress

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by bremans on Sun Jan 04 14:14:52 MST 2015
Hi,

I'm a newbie trying out with the LPC810.

I'm trying to use the I2C to turn on an oled display (no specific reason, just a learning experience). After going through the datasheet several times.. I hope somebody can tell me what's missing in my code.

It should write 0xAF to subaddress 0x00, the address of the display is 0x3C. My code is based on the code example from the datasheet.



#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)
#define I2C_MSTCTL_MSTCONTINUE (0x1)
#define I2C_MSTCTL_MSTSTART (0x2)
#define I2C_WSTCTL_MSTSTOP (0x4)

int main ()
{
        //Set clock for SWM
        LPC_SYSCON->SYSAHBCLKCTRL |= 1<<7;

        //Disable special functions
        LPC_SWM->PINENABLE0 |= 0xFF0FFFFFFUL;

        //Set pins for i2c
        LPC_SWM->PINASSIGN7 |= 0x03FFFFFFUL;
        LPC_SWM->PINASSIGN8 |= 0xFFFFFF04UL;

        //Set clock for IOCON
        LPC_SYSCON->SYSAHBCLKCTRL |= 1<<18;

        //Disable Pullups for PIO0_2 and PIO0_3
        LPC_IOCON->PIO0_2 = 0x80;
        LPC_IOCON->PIO0_3 = 0x80;

       //Set clock for i2c
        LPC_SYSCON->SYSAHBCLKCTRL |= 1<<5;

        //Set preset for i2c
        LPC_SYSCON->PRESETCTRL &= ~1<<6;
        LPC_SYSCON->PRESETCTRL |= 1<<6;

        //NVIC enable
        NVIC_EnableIRQ(I2C_IRQn);

        //Set i2c clock DIV
        LPC_I2C->DIV = 0x1;

        //Enable i2c master
        LPC_I2C->CFG = I2C_CFG_MSTEN;

        while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
        if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) == I2C_STAT_MSTST_IDLE){
                //Set address
                LPC_I2C->MSTDAT = (0x3C << 1) | 0;
                //Send startbit
                LPC_I2C->MSTCTL = 0x2;
        }

        while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
        if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) == I2C_STAT_MSTST_TX){
                //Send subaddress
                LPC_I2C->MSTDAT = 0x00;
                //Send continue
                LPC_I2C->MSTCTL = 0x1;
        }

        while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
        if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) == I2C_STAT_MSTST_TX){
                //Send value
                LPC_I2C->MSTDAT = 0xAF;
                //Send continue
                LPC_I2C->MSTCTL = 0x1;
        }

        while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
        if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) == I2C_STAT_MSTST_TX){
                //Send stopbit
                LPC_I2C->MSTCTL = 0x4;
        }

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

Outcomes