# I2C: BitBanging Read/Write Issue with Pic16f877A ( OSC = 11.0592MHz )

Referring [This Link] (I2C: Unable to Read Multiple Byte using Bit-Banging method).

I created an I2C BitBanging library for Pic16f877A. But I am unable to Read/Write the EEPROM.

My Pin Configuration are :

SCK : RB0
SDATA_OUT : RB1
SDATA_IN : RB2


Rest code remains the same.

Possibly, The issue could be of the delay function as I am using OSC = 11.0592 MHZ with Pic16f877A. but tried increasing the delay by 2 times. Issue remains the same.

Also,

#pragma config  FOSC    =   HS


Can any one help me with Possible issues and way to rectify the issue.

Note: Code remains the same as the link says. Only Pin and its required registers are changed for Pic16f877A.

• Have you looked at the SCL and SDA lines with a scope? – tcrosley Oct 7 '15 at 12:34
• I haven't read the datasheet for that EEPROM but isn't ~11MHz a bit fast for I2C? – Roger Rowland Oct 7 '15 at 12:56
• @RogerRowland, thanks for your suggestion. As you suggested, I checked the maximum frequency for I2C is 400KHz. I can't change the Oscillator, so what would be the better way to calculate the delay ?? – skg Oct 7 '15 at 13:14
• @skg Just insert delays into your bit banging code. – Roger Rowland Oct 7 '15 at 13:18
• @RogerRowland, I am already using delay in my code. Just I need to increase the delay time. But How much , that i need to calculate. – skg Oct 7 '15 at 13:46

## 2 Answers

If all the code is exactly the same you have a mistake here :

#define Crystal_Value       8          // MHz


you should replace this line by:

#define Crystal_Value       11          // MHz


at the condition 11MHz is really the value of your system oscillator and not the only the one of your crystal oscillator.

• thanks for your response. I already did this. " #define Crystal_Value 11.0592 //MHz " – skg Oct 8 '15 at 4:22
• Is 11MHz the frequency of your crystal or the frequency of your system? Some key word defines it – damien Oct 8 '15 at 8:38
• Its Crystal Frequency – skg Oct 8 '15 at 13:10

Finally I caught my mistake.

Link provide just works fine. It was my mistake. While modifying the functions call, I deleted initI2C() function call. Which was causing the issue.

Also, Crystal value should be

#define Crystal_Value       12          // MHz


Thanks everyone for your valuable suggestions, that helped me improving my code.