I am trying to make an I2C communication with a device by using PIC18F26K83. I use 8 MHz MCU clock. While trying to set I2C clock these options:

  1. Use 8 MHz MCU clock & HFINTOSC for I2CxCLK register.
  2. Use 8 MHz MCU clock & timer 2 postscaler output while the timer configs were like below:

void InitTimer2(){
  T2CLK =0b00000101; //500 kHz
  T2CON.B3=0;     // Timer 2 PostScaler = 1:2 (500/2 = 250 kHz)
  T2CON.B7  = 1;  // Timer 2 is on
  1. 250 kHz MCU clock & Fosc for I2CxClk register.

  2. 250 kHz MCU clock & HFINTOSC for I2CxClk register.

And only first option worked. However, I believe all four options above should have worked. So my questions are:

  1. Is that a correct way to set timer like above code?
  2. When I say I use 8 MHz MCU clock, does that mean Fosc is 8 Mhz?
  3. When they say I need to have 1 MHz frequency, should I set clock to 1 MHz or 4 MHz? Because in some sites they mentioned that one operation take 4 clock cycles in PIC. So it made me confused actually.
  4. Is the below code a correct way to set the oscillator in PIC?

void Clk_250kHz (){
  NOSC2_BIT =1;
  NOSC1_BIT=1;      // HFINTOSC
  FRQ3_BIT =0;
  FRQ2_BIT=0;       // 1 MHz
  NDIV3_BIT =0;
  NDIV1_BIT=1;      // Divide 4 = 250 kHz.
  • \$\begingroup\$ Could you better define "worked." How are you judging whether it worked or not? What I2C frequency are you trying to achieve? \$\endgroup\$ – vini_i Apr 3 at 17:14
  • \$\begingroup\$ Worked means I could communicate with a device. I want to achive a frequency between 10 Khz and 100 Khz \$\endgroup\$ – Günkut Ağabeyoğlu Apr 4 at 5:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.