Hello, I am trying to write a Set of Bit Bang I2C Functions to communicate with a SRF08 Range Finder. I found some code by Grant Beattie for the HC11 and have addapted it for the 89LPC932. Now I have the Start, stop, and Write Byte functions working, but the Read function is not working. I have the suggested pull-up resistors and when I run the code the value outputs 255 meaning the data line never falls. This is my code please help.
unsigned char I2cRead(unsigned char count) { unsigned char mask; unsigned char value; unsigned char index = 0; while(count) { mask = 0x80; value = 0x00; while(mask) // Do the 8 data bits... { SclHi(); // Set SCL //and wait for it to go hi. I2cDelay(); if( GetSda() ) // Read the bit value |= mask; // the returned byte. SclLo(); // Bring SCL low again I2cDelay(); mask >>= 1; } Readvalue = value; // Save the read byte. index++; count--; if(count) // If not the last byte, ACK { SdaLo(); // Bring SDA low for ACK. SclHi(); // Clock high. I2cDelay(); SclLo(); // Clock low. SdaHi(); // Release SDA. I2cDelay(); } } SdaHi(); // SDA high for NACK on last byte. SclHi(); // Clock high. I2cDelay(); SclLo(); // Clock low. I2cDelay(); return 0; }
Hey Erik, Looks Like it was a bad Emulator the SCL pin was always high. Put in a New Emulator and it is starting to work. I think It is good now. Thank You for all of your help. Kenny
One more question if you are still there Erik. I have the code architect functions working in that they go to all of the correct interrupts, but I do not receive any data back from case 58: my I2Dat value is always zero. Just wondering if you have seen this before. This is my Init routine.
void i2c_init( unsigned char address, // The 7-bit I2C address to use bit generalcall // 1 = respond to general call, 0 = ignore // general call ) { // set pins to open-drain P1M1 |= 0x0C; P1M2 |= 0x0C; // configure I2C address I2ADR = 0x00; //I2ADR = address << 1; //if (generalcall) //{ // I2ADR |= 0x01; //} // if // configure internal SCL generator //Page 79 of the user manual gives these values for close to 400Kbit/S //SCLH (SCL Duty Cycle Register High Byte) I2SCLH =0x08; //SCLL (SCL Duty Cycle Reister Low Byte) I2SCLL =0x07; // configure I2C interface // use internal SCL generator I2CON = 0x44; // set interrupt priority to 0 IP1 &= ~0x01; IP1H &= ~0x01; // initial status mi2cstatus = I2C_IDLE; // enable interrupt EI2C = 1; EA=1; } // i2c_init
but I do not receive any data back from case 58:
// no ACK for data byte case 0x58