help with code

I am trying to interface the MCP23016 I/O expander with a micro controller

the MCP 23016 16 GPO ports will go to a current controlled chip the BCR421u to provide a constant current to turn on and off a bunch of strings of leds

i am writing the code in c, because that is my micro-controller code

I know so far, that i have to initialize the output and input polarity registers before i can read and write to a register, I also know that before i can read from the GPO register, I need to set the I/O ports to imports and then read back what i wrote to then.

I am having a hard time understanding the data sheet and i am not sure if this code is going to work,

the code:
//----------------------------------------------------------------------------
// This is the program for the lightbar for our senior design project. It will
// will consistes of 6 individual different patterns. each will be controlled
// by a differnt port. When the port is high, the spacific light pattern will
// flash.
//----------------------------------------------------------------------------

#include <m8c.h> // part specific constants and macros
#include “PSoCAPI.h” // PSoC API definitions for all User Modules
#include “stdlib.h”
#include “PSoCGPIOINT.h”
#include <I2CCommon.h>
#include <I2CMstr.h>
#include “ctype.h”

#define GPO 0x00 // Access to GPO ( lights for the rear of the light bar)
#define GP1 0x01 // Access to GP1 (lights for the front of the light bar)
#define OLATO 0x02 // Access to OLATO
#define OLAT1 0x03 // Access to OLAT1
#define IPOLO 0x04 // Access to IPOLO
#define IPOL1 0x05 // Access to IPOL1
#define IODIRO 0x06 // Access to IODIRO
#define IODIR1 0x07 // Access to IODIR1
#define INTCAPO 0x08 // Access to INTCAPO(Read-only)
#define INTXAP1 0x09 // Access to INTCAP1(Read only)
#define IOCONO 0x0a // Access to IOCONO
#define IOCONI1 0x0b // Access to IOCONI1

BYTE I2Caddr; //group two address
char command[9]; // declares and array of eight bits, used to set logic of the leds to one

void Write_To_A_Register(void);
void Read_From_A_Register(void);
void Test_Pin(void);

void main(void)
{
M8C_EnableGInt;
}

void Write_To_A_Register(void)
{
// before we can write to a register, we have to set up then register
// this is done by initializeing the I/O direction bits an inputs or outputs

// initializes IODIRO as all outputs
charcount =1;
I2Caddr=0x40;
command[0]=IODIRO;

I2C_bWriteBytes(I2Caddr,command, charcount, I2C_CompleteXfer);
while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
I2C_ClrWrStatus( );

charcount =9;
I2Caddr=0x40;
command[0]=IODIRO;
command[1]=0x00;
command[2]=0x00;
command[3]=0x00;
command[4]=0x00;
command[5]=0x00;
command[6]=0x00;
command[7]=0x00;
command[8]=0x00;

I2C_bWriteBytes(I2Caddr, command, charcount,I2C_NoStop);
while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
I2C_ClrWrStatus( );

// initializes IODIR1 as outputs;
charcount =1;
I2Caddr=0x40;
command[0]=IODIR1;

I2C_bWriteBytes(I2Caddr,command, charcount, I2C_CompleteXfer);
while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
I2C_ClrWrStatus( );

charcount =9;
I2Caddr=0x40;
command[0]=IODIR1;
command[1]=0x00;
command[2]=0x00;
command[3]=0x00;
command[4]=0x00;
command[5]=0x00;
command[6]=0x00;
command[7]=0x00;
command[8]=0x00;

I2C_bWriteBytes(I2Caddr, command, charcount,I2C_NoStop);
while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
I2C_ClrWrStatus( );

// final step in process is to set the polarity of the input bits
// a one bit inverts the inputs
// we are not using any input polarity so al of ours will be set to zero

// initializes the input plarity as all zeros
charcount =1;
I2Caddr=0x40;
command[0]=IPOLO;

I2C_bWriteBytes(I2Caddr,command, charcount, I2C_CompleteXfer);
while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
I2C_ClrWrStatus( );

charcount =9;
I2Caddr=0x40;
command[0]=IPOLO;
command[1]=0x00;
command[2]=0x00;
command[3]=0x00;
command[4]=0x00;
command[5]=0x00;
command[6]=0x00;
command[7]=0x00;
command[8]=0x00;

I2C_bWriteBytes(I2Caddr, command, charcount,I2C_NoStop);
while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
I2C_ClrWrStatus( );

// initializes inputPolarity
charcount =1;
I2Caddr=0x40;
command[0]=IPOL1;

I2C_bWriteBytes(I2Caddr,command, charcount, I2C_CompleteXfer);
while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
I2C_ClrWrStatus( );

charcount =9;
I2Caddr=0x40;
command[0]=IPOL1;
command[1]=0x00;
command[2]=0x00;
command[3]=0x00;
command[4]=0x00;
command[5]=0x00;
command[6]=0x00;
command[7]=0x00;
command[8]=0x00;

I2C_bWriteBytes(I2Caddr, command, charcount,I2C_NoStop);
while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
I2C_ClrWrStatus( );

// the setup is now compolete and we can write to the regisers
// sets i2C to write to the first 8 port expanders
I2Caddr = 0x40; // slave address of LED Driver1
charcount =1; // characters entered by the user
command[0]=GPO; // writing to the first 8 port expanders

// sends the contents of the data in txBuffer
I2C_bWriteBytes(I2Caddr,command, charcount, I2C_CompleteXfer);
// Wait until the data is transferred
while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
// clear write complete status bit
I2C_ClrWrStatus( );

// set i2c to write to the first port expanders and set bit 0 = to a logic one
charcount=9; // sets the char count to two, data entered from the user
I2Caddr=0x40;
command[0]=GPO;
command[1]=0x01; // makes the first bit a 1
command[2]=0x01;
command[3]=0x01;
command[4]=0x01;
command[5]=0x01;
command[6]=0x01;
command[7]=0x01;
command[8]=0x01;

// sends the contents of the data in the txBuffer
I2C_bWriteBytes(I2Caddr, command, charcount,I2C_NoStop);
// wait untill the data is transferred
while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
// clears the write comple status biyt
I2C_ClrWrStatus( );
}

void Read_From_A_Register(void)
{
// before we can read back from the register, we need to have the GPO ports as inputs

charcount=1;
I2Caddr=0x40;
command[0]=IODIRO;

I2C_bWriteBytes(I2Caddr, command,charcount,I2C_CompleteXfer);
while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
I2C_ClrWrStatus( );

charcount=8;
I2Caddr=0x40;
command[0]=IODIRO;
command[1]=0x01;
command[2]=0x01;
command[3]=0x01;
command[4]=0x01;
command[5]=0x01;
command[6]=0x01;
command[7]=0x01;
command[8]=0x01;

I2C_bWriteBytes(I2Caddr, command, charcount,I2C_NoStop);
while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
I2C_ClrWrStatus( );

// read from the spacific register

charcount=1;
I2Caddr=0x40;
command[0]=GPO;

// sends the group two address, followed by a logic zero to read from
I2C_bWriteBytes(I2Caddr, command,charcount,I2C_CompleteXfer);
// wait untill the data is transferred
while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
// clears the read status
I2C_ClrWrStatus( );

charcount =10;
I2Caddr=0x40;
command[0]=GPO;
command[1]=GP1;
// sends the group two address, followed by a logic one to read from the register
I2C_fReadBytes(I2Caddr, command, charcount,I2C_RepStart);
// waint untill the data is transferred
while(!(I2C_bReadI2CStatus( ) & I2CHW_RD_COMPLETE));
// clears the read status
I2C_ClrRdStatus( );

// have to make the IODIRO pins as outputs again
charcount=1;
I2Caddr=0x40;
command[0]=IODIRO;

I2C_bWriteBytes(I2Caddr, command,charcount,I2C_CompleteXfer);
while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
I2C_ClrWrStatus( );

charcount=8;
I2Caddr=0x40;
command[0]=IODIRO;
command[1]=0x00;
command[2]=0x00;
command[3]=0x00;
command[4]=0x00;
command[5]=0x00;
command[6]=0x00;
command[7]=0x00;
command[8]=0x00;

I2C_bWriteBytes(I2Caddr, command, charcount,I2C_NoStop);
while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
I2C_ClrWrStatus( );
}

// This function rights a one to the least significant bit ofthe IOCONO register
// its purpous is to use the test pin on the chip,and make sure that the chip
// is getting the right clock signal(according to the data sheet this should be approximetly
// 1 megaHertz
void Test_Pin(void)
{
charcount=1; // counts the number typed by the user
I2Caddr=0x40; // group two address
command[0]=IOCONO; // IOCONO register

I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
I2C_ClrWrStatus( );

charcount=2;
command[1]=0x01;

I2C_bWriteBytes(I2Caddr, command, charcount, I2C_NoStop); //write data
while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE)); //wait until data is transmitted
I2C_ClrWrStatus( ); //clear write status

}

Now, with code box:

    //----------------------------------------------------------------------------
// This is the program for the lightbar for our senior design project. It will
// will consistes of 6 individual different patterns. each will be controlled 
// by a differnt port. When the port is high, the spacific light pattern will 
// flash. 
//----------------------------------------------------------------------------

#include <m8c.h>        // part specific constants and macros
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules
#include "stdlib.h"
#include "PSoCGPIOINT.h"
#include <I2CCommon.h>
#include <I2CMstr.h>
#include "ctype.h"

#define GPO     0x00   // Access to GPO ( lights for the rear of the light bar)
#define GP1     0x01   // Access to GP1 (lights for the front of the light bar)
#define OLATO   0x02   // Access to OLATO
#define OLAT1   0x03   // Access to OLAT1
#define IPOLO   0x04   // Access to IPOLO
#define IPOL1   0x05   // Access to IPOL1
#define IODIRO  0x06   // Access to IODIRO
#define IODIR1  0x07   // Access to IODIR1
#define INTCAPO 0x08   // Access to INTCAPO(Read-only)
#define INTXAP1 0x09   // Access to INTCAP1(Read only)
#define IOCONO  0x0a   // Access to IOCONO
#define IOCONI1 0x0b   // Access to IOCONI1

BYTE I2Caddr;         //group two address
char command[9];       // declares and array of eight bits, used to set logic of the leds to one



void Write_To_A_Register(void);
void Read_From_A_Register(void);
void Test_Pin(void);

void main(void)
{
   M8C_EnableGInt;
}
   
void Write_To_A_Register(void)
{
   // before we can write to a register, we have to set up then register
   // this is done by initializeing the I/O direction bits an inputs or outputs
   
   // initializes IODIRO as all outputs
   charcount =1;
   I2Caddr=0x40;
   command[0]=IODIRO;
   
   
   I2C_bWriteBytes(I2Caddr,command, charcount, I2C_CompleteXfer);
   while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
   I2C_ClrWrStatus( );
   
   charcount =9;
   I2Caddr=0x40;
   command[0]=IODIRO;
   command[1]=0x00;
   command[2]=0x00;
   command[3]=0x00;
   command[4]=0x00;
   command[5]=0x00;
   command[6]=0x00;
   command[7]=0x00;
   command[8]=0x00;
   
   
   I2C_bWriteBytes(I2Caddr, command, charcount,I2C_NoStop);
   while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
   I2C_ClrWrStatus( );
   
   // initializes IODIR1 as outputs;
      charcount =1;
   I2Caddr=0x40;
   command[0]=IODIR1;
   
   
   I2C_bWriteBytes(I2Caddr,command, charcount, I2C_CompleteXfer);
   while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
   I2C_ClrWrStatus( );
   
   charcount =9;
   I2Caddr=0x40;
   command[0]=IODIR1;
   command[1]=0x00;
   command[2]=0x00;
   command[3]=0x00;
   command[4]=0x00;
   command[5]=0x00;
   command[6]=0x00;
   command[7]=0x00;
   command[8]=0x00;
   
   
   I2C_bWriteBytes(I2Caddr, command, charcount,I2C_NoStop);
   while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
   I2C_ClrWrStatus( );
   
   // final step in process is to set the polarity of the input bits
   // a one bit inverts the inputs
   // we are not using any input polarity so al of ours will be set to zero
   
   
   // initializes the input plarity as all zeros
   charcount =1;
   I2Caddr=0x40;
   command[0]=IPOLO;
   
   
   I2C_bWriteBytes(I2Caddr,command, charcount, I2C_CompleteXfer);
   while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
   I2C_ClrWrStatus( );
   
   charcount =9;
   I2Caddr=0x40;
   command[0]=IPOLO;
   command[1]=0x00;
   command[2]=0x00;
   command[3]=0x00;
   command[4]=0x00;
   command[5]=0x00;
   command[6]=0x00;
   command[7]=0x00;
   command[8]=0x00;
   
   
   I2C_bWriteBytes(I2Caddr, command, charcount,I2C_NoStop);
   while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
   I2C_ClrWrStatus( );
   
   
   // initializes inputPolarity
   charcount =1;
   I2Caddr=0x40;
   command[0]=IPOL1;
   
   
   I2C_bWriteBytes(I2Caddr,command, charcount, I2C_CompleteXfer);
   while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
   I2C_ClrWrStatus( );
   
   charcount =9;
   I2Caddr=0x40;
   command[0]=IPOL1;
   command[1]=0x00;
   command[2]=0x00;
   command[3]=0x00;
   command[4]=0x00;
   command[5]=0x00;
   command[6]=0x00;
   command[7]=0x00;
   command[8]=0x00;
   
   
   I2C_bWriteBytes(I2Caddr, command, charcount,I2C_NoStop);
   while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
   I2C_ClrWrStatus( );
   
// the setup is now compolete and we can write to the regisers   
// sets i2C to write to the first 8 port expanders
   I2Caddr = 0x40;     // slave address of LED Driver1
   charcount =1;       // characters entered by the user
   command[0]=GPO;     // writing to the first 8 port expanders
      
   // sends the contents of the data in txBuffer
   I2C_bWriteBytes(I2Caddr,command, charcount, I2C_CompleteXfer);
   // Wait until the data is transferred
   while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
   // clear write complete status bit
   I2C_ClrWrStatus( );
   
// set i2c to write to the first port expanders and set bit 0 = to a logic one
   charcount=9;       // sets the char count to two, data entered from the user
   I2Caddr=0x40;
   command[0]=GPO;
   command[1]=0x01;      // makes the first bit a 1
   command[2]=0x01;
   command[3]=0x01;
   command[4]=0x01;
   command[5]=0x01;
   command[6]=0x01;
   command[7]=0x01;
   command[8]=0x01;
   
   // sends the contents of the data in the txBuffer
   I2C_bWriteBytes(I2Caddr, command, charcount,I2C_NoStop);
   // wait untill the data is transferred
   while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
   // clears the write comple status biyt
   I2C_ClrWrStatus( );
}   

void Read_From_A_Register(void)
{
   // before we can read back from the register, we need to have the GPO ports as inputs
   
   charcount=1;
   I2Caddr=0x40;
   command[0]=IODIRO;
   
   I2C_bWriteBytes(I2Caddr, command,charcount,I2C_CompleteXfer);
   while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
   I2C_ClrWrStatus( );
   
   charcount=8;
   I2Caddr=0x40;
   command[0]=IODIRO;
   command[1]=0x01;
   command[2]=0x01;
   command[3]=0x01;
   command[4]=0x01;
   command[5]=0x01;
   command[6]=0x01;
   command[7]=0x01;
   command[8]=0x01;
   

   I2C_bWriteBytes(I2Caddr, command, charcount,I2C_NoStop);
   while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
   I2C_ClrWrStatus( );
   
   // read from the spacific register
   
   charcount=1;
   I2Caddr=0x40;
   command[0]=GPO;
   
   // sends the group two address, followed by a logic zero to read from
   I2C_bWriteBytes(I2Caddr, command,charcount,I2C_CompleteXfer);
   // wait untill the data is transferred
   while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
   // clears the read status
   I2C_ClrWrStatus( );
   
   charcount =10;
   I2Caddr=0x40;
   command[0]=GPO;
   command[1]=GP1;
   // sends the group two address, followed by a logic one to read from the register
   I2C_fReadBytes(I2Caddr, command,  charcount,I2C_RepStart);
   // waint untill the data is transferred
   while(!(I2C_bReadI2CStatus( ) & I2CHW_RD_COMPLETE));
   // clears the read status
   I2C_ClrRdStatus(  );
   
   // have to make the IODIRO pins as outputs again
      charcount=1;
   I2Caddr=0x40;
   command[0]=IODIRO;
   
   I2C_bWriteBytes(I2Caddr, command,charcount,I2C_CompleteXfer);
   while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
   I2C_ClrWrStatus( );
   
   charcount=8;
   I2Caddr=0x40;
   command[0]=IODIRO;
   command[1]=0x00;
   command[2]=0x00;
   command[3]=0x00;
   command[4]=0x00;
   command[5]=0x00;
   command[6]=0x00;
   command[7]=0x00;
   command[8]=0x00;
   

   I2C_bWriteBytes(I2Caddr, command, charcount,I2C_NoStop);
   while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
   I2C_ClrWrStatus( );
}

// This function rights a one to the least significant bit ofthe IOCONO register
// its purpous is to use the test pin on the chip,and make sure that the chip
// is getting the right clock signal(according to the data sheet this should be approximetly
// 1 megaHertz
void Test_Pin(void)
{
   charcount=1;            // counts the number typed by the user
   I2Caddr=0x40;           // group two address
   command[0]=IOCONO;      // IOCONO register 
   
   
   I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);   
   while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));   
   I2C_ClrWrStatus( );
   
   charcount=2;
   command[1]=0x01; 
   
   I2C_bWriteBytes(I2Caddr, command, charcount, I2C_NoStop);   //write data
   while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));   //wait until data is transmitted
   I2C_ClrWrStatus( );   //clear write status
   
}

I can’t see “setup” or “loop”.

Still can't see "setup" or "loop"

The Arduino IDE doesn't want you to define any "main". It wants the two functions loop (witch will be repeated for ever) and setup (witch only will be run once in the beginning.

So put the part of the code that defines things etc. into a function "void setup()" and the things to be repeated into a function "void loop()". I recomand you to look onto this.

I hope this helps, JanD