This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

bit bang for I2C recieve function...



Ok im using a MSPF5659 and originally had alot of trouble getting i2c to work properly.  now a driver for some led's i have will never work with i2c because the chip is not designed to ack back....   well through help on the forums i got an i2c bit banged function that worked great for the led driver.  i now need a receive function and was wondering if anyone had any pointers..   the pins are port mapped and i was going to just switch the port mapping to gpio whenever i did the led driver but its been decided above me to just do it all bit banged.  i know i can use my normal function as far as sending the address but then have to re direct the gpio pins as inputs and read bit by bit  into bytes and store the data into an array...   any ideas

this is what i have to transmit out

//function to transmit one byte in master transmit
void TransmitByte (unsigned char Value)
{
unsigned char c;
unsigned int i;


// Add 9th bit
i = ((unsigned int)Value<<1) +1;

for (c=9; c>0; c--)
{
// Set SCL low
I2C_OUT &= ~USCL;

// Set bit state
if ((i & 0x0100) != 0) {I2C_OUT |= USDA;} else {I2C_OUT &= ~USDA;}
// Rotate bits
i <<= 1;
// Set SCL high
I2C_OUT |= USCL;

}

// Exit with SCL low
I2C_OUT &= ~USCL;
}
//function to bitbang start, stop, and bytes over i2c
void MasterTransmit (unsigned char Address, unsigned char* Data, unsigned int Number)
{


unsigned int i;
unsigned char* p;

// Set START condition
I2C_OUT |= USCL + USDA;
I2C_OUT &= ~USDA;

// Transmit address
// Strip bit 0
TransmitByte((Address<<1) & 0xFE);

// Transmit data
p = Data;
for (i=Number; i>0; i--) {TransmitByte(*p++);}

// Set STOP condition
I2C_OUT &= ~USDA;
I2C_OUT |= USCL;
I2C_OUT |= USDA;
}

  • Hello,
    For reading a byte i do this way.

    #define SDA_LOW P8DIR |= BIT5
    #define SDA_HIGH P8DIR &= ~BIT5
    #define SCL_LOW P8DIR |= BIT6
    #define SCL_HIGH P8DIR &= ~BIT6



    for (id=0; id != 8; ++ id) { __delay_cycles(50); SCL_HIGH; __delay_cycles(50); I2c.rx_data[0] <<= 1; if ((P8IN & BIT5) != 0) I2c.rx_data[0] |= 0X01; else I2c.rx_data[0] &= ~0X01; // __delay_cycles(50); SCL_LOW; }

**Attention** This is a public forum