Can't get I2C working

General discussion on mikroC PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
Centroid
Posts: 1
Joined: 11 Dec 2019 11:41

Can't get I2C working

#1 Post by Centroid » 11 Dec 2019 12:46

Hi

I recently purchased mikroeC for PIC24 and tried it for the first time at the weekend with some success.

The thing I can't get working is I2C, I've used some examples but don't get anything on the clock or data lines, I'm monitoring these with a logic analyser.

I'm using a 24FJ64GA004 on my own board programmed with a microProg, I can get I2C working with another compiler with the same setup but not with mikroeC though I have blinked a led and got the UART and interrupts working.

Things just grind to a halt at the I2C commands.

Any ideas, am I missing something simple, it is my first time?

Code:

Code: Select all

void main() {

//OSCCON = 0b1111111111110000;
OSCCON = 0b0000000000000000;


AD1PCFG = 0xFFFF;        // Set All pins as digital

//Timer 1 Setup
  IPC0   = IPC0 | 0x1000;     // interrupt priority level = 1
  T1IF_bit = 0;               // clear T1IF
  T1IE_bit = 1;               // enable Timer1 interrupts
  T1CON = 0x8020;             // Timer1 ON, internal clock FCY, prescaler 1:64
  PR1   = 42500;              // Set interval





  TRISA = 0;                // Initialize PORTA as output
  TRISB = 0;                // Initialize PORTB as output
  TRISC = 0;                // Initialize PORTC as output

  LATA = 0;                 // Set PORTA to zero
  LATB = 0;                 // Set PORTB to zero
  LATC = 0;                 // Set PORTC to zero

  
  Unlock_IOLOCK();
  PPS_Mapping(24, _OUTPUT, _U1TX);
  Lock_IOLOCK();
  UART1_Init(57600);          // initialise UART1
  I2C1_Init(100000);         // initialize I2C1
  Delay_100ms();
  


  while(1) {
  PORTB.F3 = ~ PORTB.F3;     //toggle led on portB.3

  UART_Write_Text("It works!   ");

 I2C1_Start(); // issue I2C start signal
 I2C1_Write(0xA0); // send byte via I2C (command to 24cO2)
 I2C1_Write(0x02); // send byte (address of EEPROM location)
 I2C1_Write(0xF4); // send data (data to be written)
 I2C1_Stop();




 Delay_ms(1000);
  }
}

User avatar
jovana.medakovic
mikroElektronika team
Posts: 986
Joined: 18 Dec 2018 10:36

Re: Can't get I2C working

#2 Post by jovana.medakovic » 24 Dec 2019 16:34

Hello,

Which pins you are using for I2C? Can you send me a schematic where I can see how you connect EEPROM chip with MCU?

Kind regards,
Jovana

Post Reply

Return to “mikroC PRO for dsPIC30/33 and PIC24 General”