I2C

Discussion to talk about software related topics only.
Post Reply
jojahn
Posts: 3
Joined: Mon Mar 07, 2011 7:09 am

I2C

Post by jojahn »

Hi there,
I just started to use the Netburner. Everything is totally new to me - except C++ etc.....

Who has a program to write data as a master (Netburner) to the I2C bus? I am using MCP23008 from Microchip. I have the communication working, using VB and and the serial port. Now I have to make it work with the Netburner.
The sample codes arenot too good because they try too many things at once. Do I have access to the ACK from the Slave???

Please, help me to "jump start" this endevour.

Thank you very much.
Josh
v8dave
Posts: 333
Joined: Thu Dec 31, 2009 8:31 pm

Re: I2C

Post by v8dave »

Hi Josh,

If you keep it simple it is straight forward enough. Lack of C++ programming is not an issue as you can do all your coding in C on the Netburner. (sorry Tod) ;)

This is the code I used to talk to a MAXIM battery monitor IC

Code: Select all

int ReadBattery()
{
    int i2cStatus = 0;
    BYTE buf[10];
    int Voltage, Current, Temperature;

    i2cStatus = I2CStart( 0x48, I2C_START_WRITE );
    if(i2cStatus > 4)
    {
        I2CInit(0x16);									// Try to reinit the bus
        return 1;
    }
    i2cStatus = I2CSend( 0x0A );
    if(i2cStatus > 4)
    {
        I2CInit(0x16);									// Try to reinit the bus
        return 1;
    }
    i2cStatus = I2CRestart( 0x48, I2C_START_READ );
    if(i2cStatus > 4)
    {
        I2CInit(0x16);									// Try to reinit the bus
        return 1;
    }
    i2cStatus = I2CReadBuf(0x48, buf, 8);
    if(i2cStatus > 4)
    {
        I2CInit(0x16);									// Try to reinit the bus
        return 1;
    }
    I2CStop();
    //
    // Decode the data
    //
    Voltage = ((buf[2] << 8) + buf[3]) >> 5;
    BatteryStatus.Voltage = Voltage * 0.00488;

    Current = ((buf[4] << 8) + buf[5]) & 0x7FFF;	// Ignore the sign bit
    BatteryStatus.Current = Current * 0.00001042;

    Temperature = ((buf[0] << 8) + buf[1]) >> 5;
    BatteryStatus.Temperature = Temperature * 0.125;

    return(0);
}
You will see that if I get an error with the BUS I re-initialise it as I found that on some occasions the bus would hang up. Doing the test and re-init has proven reliable for myself and this hardware.

Hope this shows some insight into how to do it.

Dave...
jojahn
Posts: 3
Joined: Mon Mar 07, 2011 7:09 am

Re: I2C

Post by jojahn »

Thank you for the reply.

Why do you use 0x16 in your code (I2CInit(0x16)) to Initialize the bus?
I checked out fwe data sheets and can not find a relationship to your numbers. What chip did you use - that way I can find the address and registers.
Thank you.
Josh
v8dave
Posts: 333
Joined: Thu Dec 31, 2009 8:31 pm

Re: I2C

Post by v8dave »

Hi Josh,

If you look in i2cmaster.h you will find the comments for i2cinit. The 0x16 for the MOD5234 I am using uses this value to set the bus to 100Kbits/s

Code: Select all

/*----------------------------------------------------------------------------
void I2CInit( BYTE freqdiv = 0x16 );  MCF5270, MCF5234 and MCF5208
void I2CInit( BYTE freqdiv = 0x15 );  MCF5213, MCF5282 and MCF52234

I2C initialization routine

FOR ALL MCF5270, MCF5234 and MCF5208 products:
System Bus Clock which = 150MHZ/2 divided by 'freqdiv' will give you the max baud rate
of the master mode I2C bus. Values of freqdiv are found in a the I2FDR table found in
the I2C section of the MCF5270 user manual provided by freescale.  0x16=768 which
gives a baud rate close to 100Kbits/s is the value set if parameter excluded

FOR ALL MCF5213, MCF5282 and MCF52234 products:
System Bus Clock which = 66MHZ divided by 'freqdiv' will give you the max baud rate
of the master mode I2C bus. Values of freqdiv are found in a the I2FDR table found in
the I2C section of the MCF5282 user manual provided by freescale.  0x15=640 which
gives a baud rate close to 100Kbits/s is the value set if parameter excluded

returns nothing 
*///////////////////////////////////////////////////////////////////////////////////
BY the way, if you don't already know it, you can right click on a function name in Eclipse and then select from the menu, "open declaration" this will then open the header file where the function is defined (usually the header file) and is useful to get additional help.

Dave...
Post Reply