This is a example how to write and read with an I2C IO expander. Check the tutorial page for more information about I2C. Note that there are two versions of the PCF8574: the normal version and the A version. The important difference between those two is the static address.

Datasheet: http://www.nxp.com/documents/data_sheet/PCF8574.pdf


I2C WRITE

#include "lpc17xx.h"
#include "lpc_types.h"
#include "i2c.h"
#include "timer.h"

int main (void)
{
    TimerInit(0,1000);                                                               //Initialize timer 0

    int portused = 1;                                                                 //Declare used channel as var

    I2C1Init(0)                                                                          //Initialize I2C channel 1
    I2CWriteLength[portused] = 2;                                           //Only 2 bytes will be written
    I2CReadLength[portused] = 0;                                           //No read bytes
    I2CMasterBuffer[portused][0] = 0b01001110;                    //Copy address + write bit to masterbuffer pos. 0

    while(1)
    {
        I2CMasterBuffer[portused][1] = 0b00000000;               //Copy 0 to masterbuffer pos. 1
        I2CEngine(portused);                                                    //Start I2C transmission
        delayMs(0,500);

        I2CMasterBuffer[portused][1] = 0b11111111;                 //Copy 255 to masterbuffer pos. 1
        I2CEngine(portused);                                                    //Start I2C transmission
        delayMs(0,500);
    }

}

I2C READ


#include "lpc17xx.h"
#include "lpc_types.h"
#include "i2c.h"

int main (void)
{
    int variable = 0;                                                                 //Declare variable for temporarily storage
    int portused = 1;                                                                //Declare used channel as var

    I2C1Init(0);                                                                        //Initialize I2C channel 1
    I2CWriteLength[portused] = 1;                                          //Only the address byte will be written
    I2CReadLength[portused] = 1;                                          //Read for 1 time
    I2CMasterBuffer[portused][0] = 0b01001111;                    //Copy address + read bit to masterbuffer pos. 0

    while(1)
    {
        I2CEngine(portused);                                                    //Start I2C transmission
        variable = I2CSlaveBuffer[portused][0];                        //Read value from I2C incoming buffer
    }

}



Copyright 2011. Joomla 1.7 templates - Joomla template maker. The Simplecortex is developed by BRC-Electronics