Problem with I2C communication

General discussion on mikroBasic PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
loojade
Posts: 35
Joined: 04 Oct 2008 03:03

Problem with I2C communication

#1 Post by loojade » 07 Sep 2012 13:55

Hello everybody,

I'm developing a system who need to save some datas on external eeprom by I2C protocol and at the same time use the UART to comunicate to PC (by alternate pin). I'm using dsPIC30F4012 and EEPROM 24LC1025.

1° question is: Can I use UART and I2C at same time?
2° question is (if the 1° question was "YES"): why my program stop after when I try to write via I2C? The code is the following:

Code: Select all


 dim address_H, address_L as byte
 dim data_to_eeprom as byte

  I2C1_Init(400000)      ' initialize I2C communication
  I2C1_Start()           ' issue I2C start signal
  I2C1_Write(0xA0)       ' send byte via I2C  (device address + W)
  I2C1_Write(address_H)          ' send byte (address of EEPROM location)
  I2C1_Write(address_L)          ' send byte (address of EEPROM location)
  I2C1_Write(data_to_eeprom)       ' send data (data to be written)
  I2C1_Stop()            ' issue I2C stop signal




I'm using the Timer3 to create a wave (so I can cheek the stability of system by a oscilloscope). When the program arrive to the line "I2C1_Write(0xA0) " he stop.

Anyone can help

Really thanks


Diego

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: Problem with I2C communication

#2 Post by janko.kaljevic » 10 Sep 2012 12:38

Hello,

In this case you should use different pins for UART and I2C.
On your controller there is option to use alternate pins for UART.

Just please notice that UART library will initialize default UART pins (ALTIO = 0).
So in order to set UART to alternate pins you should initialize it, set proper value of ALTIO pin (1), and set TRIS bits of alternate pins.
After this you should initialize I2C and everything will be fine.

Also the other reason why I2C freezes could be the wrong address of I2C slave module.

Best regards.

loojade
Posts: 35
Joined: 04 Oct 2008 03:03

Re: Problem with I2C communication

#3 Post by loojade » 10 Sep 2012 13:22

janko.kaljevic wrote:Hello,

In this case you should use different pins for UART and I2C.
On your controller there is option to use alternate pins for UART.

Just please notice that UART library will initialize default UART pins (ALTIO = 0).
So in order to set UART to alternate pins you should initialize it, set proper value of ALTIO pin (1), and set TRIS bits of alternate pins.
After this you should initialize I2C and everything will be fine.

Also the other reason why I2C freezes could be the wrong address of I2C slave module.

Best regards.
Thanks Janko,

So what do you mean about TRIS? pins used for UART must be Input and pins used for I2C as Output?

loojade
Posts: 35
Joined: 04 Oct 2008 03:03

Re: Problem with I2C communication

#4 Post by loojade » 10 Sep 2012 15:34

Hello Janko,


I post the code I used but it still does not work...

Code: Select all

...

'----------------- INIZIO impostazioni porte IO -------------------------------------

  ADPCFG = 0xFFFF                          ' Configure AN pins as digital I/O

  TRISB = 0x00                             ' Set PORTB as output (error signalization)

  PORTB = 0                                ' No error

  LATB = 0             ' Set PORTB to zero
  LATC = 0             ' Set PORTC to zero
  LATD = 0             ' Set PORTD to zero
  'LATF = 0             ' Set PORTF to zero

  TRISB.0 = %1
  TRISB.1 = %1
  TRISB.2 = %1
  TRISB.3 = %1
  TRISB.4 = %1
  TRISB.5 = %1

  'utilizzati per la porta seriale
  TRISC.13 = %1
  TRISC.14 = %1


  TRISD.0 = %1
  TRISD.1 = %1
  TRISE.8 = %1


  TRISE.0 = %0
  TRISE.1 = %0
  TRISE.2 = %0
  TRISE.3 = %0
  TRISE.4 = %1
  TRISE.5 = %0

  
  'imposto porta seriale
  TRISC.13 = %1
  TRISC.14 = %1
  UART1_Init(57600)
  delay_ms(100)


  'imposto la I2C
  I2C1_Init(400000)
  U1MODE.ALTIO = 1 'utilizzo i pin alternativi
  TRISF.2 = 0
  TRISF.3 = 0
  
' I use PortF.2 and PortF.3 as I2C and PortC.13 and PortC.14 as alternate Uart
'Please note that the Uart on the alternative pins works perfectly

'----------------- FINE impostazioni porte IO -------------------------------------

...

             'prova lettura eeprom esterna
             if porte.4=1 then
                valore_da_scrivere_eeprom = 87

                I2C1_Start()
                I2C1_Write(0xA0)
                I2C1_Write(0x00)
                I2C1_Write(0x00)
                I2C1_Write(0x75)
                I2C1_Stop()
                
                 I2C1_Start()
                I2C1_Write(0xA0)
                I2C1_Write(0x00)
                I2C1_Write(0x00)
                I2C1_Restart()
                I2C1_Write(0xA1)
                byte_appoggio = I2C1_Read(1)
                if byte_appoggio = 0x75 then
                   porte.2 = 1
                else
                    porte.2 = 0
                end if
             end if

' my 24FC1025 use A0 and A1 to ground and A2 to 5v so The command byte should be 10100000 (for write) and '10100001 (for read)
...
At first write to I2C the program stop.
I'm using also Timer1-2-3 and INT0-1-2 and CN7, Could this Interrupt create some problems???

Thanks a lot

loojade
Posts: 35
Joined: 04 Oct 2008 03:03

Re: Problem with I2C communication

#5 Post by loojade » 11 Sep 2012 10:37

I tried to force pin PORTF.2 and PORTF.3 to High (disabling UART and I2C) but for some reason this pins does not go high. It look strange! (in addition I working on a EASYdsPIC4A... not on strange board. There is any reason why this can happen?


Thanks

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: Problem with I2C communication

#6 Post by janko.kaljevic » 11 Sep 2012 12:33

Hello,

In this case you should try to test only I2C module.
Try to establish successful connection.

Timer interrupt will not interfere with I2C, and this is not the cause of this.

I2C_Write is blocking call, so if there is no 2C slave connected it will freeze.

Also it will freeze if the slave address is wrong.

Best regards.

Post Reply

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