Let's Make Robots!

Problem with I2C mode of 16f877a

I am having problem with I2C mode of 16f877a. I wrote a code for I2C ranging mode of the SRF02 sensors. At first I thought the problem was with the code, but then I debugged the circuit a little; the code is as below:

int main(){

TRISB = 0x00; //Port B is output
TRISD = 0x00; //Port D is output

//Get the range
  while(1)
  {
I2C_Init(2000000);
I2C_Start(void); // send start sequence
I2C_Wr(0xE0); // SRF02 I2C address with R/W bit clear
I2C_Wr(0x00); // SRF02 command register address
I2C_Wr(0x81); // command to start ranging in cm
I2C_Stop(void); // send stop sequence

delay_ms(70); //delay for 70ms.

I2C_Start(void); // send start sequence
I2C_Wr(0xE0); // SRF02 I2C address with R/W bit clear
I2C_Wr(0x02); // send internal adress of high byte.

I2C_Repeated_Start(void); // send a restart sequence
I2C_Wr(0xE1); // SRF02 I2C address with R/W bit set

PORTB=I2C_Rd(1); // get the high byte of the range and send acknowledge.
PORTC=I2C_Rd(0); // get low byte of the range - note we don't acknowledge the last byte.

I2C_Stop(void); // send stop sequence
 }

return 0;
}
 

This code would normally first of all make a PIC generate a clock on the CLK pin of the PIC (I think this is what I2C_Init() command does). But I observed on Scope that there is no clock on this pin. I only observed 5V dc. I heard that 16f877a PICs have problems with i2c, and now I observe that it simply does not generate clock. 

PS: I also wrote MIKROC's example simple code to the PIC to eliminate my mistakes, just to check the presence of the clock. Still, I could not see it. My final conclusion is: 16f877a does not generate clock in I2C mode.=> no i2c operation is possible?

Does anyone has similar problem? Should I change the Microcontroller?  

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
It is OK, the problem is solved. my compiler had a problem I think. I tried to compile the code on another compiler it worked!
Yes, clock is  RC3/SCK/SCL pin of 16f877a. Oscillator configuration (under this I assume the oscillator selected when programming the PIC) is HS.  I have tried 100KHz as well, but still no clock, only DC level signal. And about the interference with I2C pins: I made PORTD=I2C_Rd(0) instead of  PORTC=I2C_Rd(0), still the same. Also by debugging I observed that the process stops on the operation I2C_Wr(0xE0), I think it is not able to write (most probably because of absence of clock).
TeleFox's picture

By 'CLK' do you mean RC3/SCK/SCL? What is your oscillator configuration? What are you using to measure the I2C clock frequency? 2MHz is pretty fast, have you tried using a slower clock option?
Make sure your PORTB=I2C_Rd(1); and PORTC=I2C_Rd(0); commands aren't interfering with the I2C pins.