How can I choose I2C module similar to using SPI_Set_Active?

General discussion on mikroBasic PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
calixarene
Posts: 17
Joined: 03 Apr 2012 06:31

How can I choose I2C module similar to using SPI_Set_Active?

#1 Post by calixarene » 23 May 2012 04:57

I am developing a moudle for a I2C sensor so that I can easily reused it in different I2C module (not swithcing at run time). Unlike SPI, there is no active I2C function to use. Is there any way to do this? I would like to know whether the following code will work in the design time.

Code: Select all


' Uncomment the appropriate flag for communication Channel:
' Set the I2C channel software
' 1,2,3 corresponding to hardware module
 #DEFINE I2C_1
 '#DEFINE I2C_2
 '#DEFINE I2C_3
 '#DEFINE SoftI2C
 #IFDEF I2C_1 then
        symbol I2C_Init = I2C1_Init
        symbol I2C_Start = I2C1_Start
        symbol I2C_Restart = I2C1_Restart
        symbol I2C_Is_Idle = I2C1_Is_Idle
        symbol I2C_Read = I2C1_Read
        symbol I2C_Write =  I2C1_Write
        symbol I2C_Stop = I2C1_Stop
  #else
    #ifdef I2C_2 then
        symbol I2C_Init = I2C2_Init
        symbol I2C_Start = I2C2_Start
        symbol I2C_Restart = I2C2_Restart
        symbol I2C_Is_Idle = I2C2_Is_Idle
        symbol I2C_Read = I2C2_Read
        symbol I2C_Write =  I2C2_Write
        symbol I2C_Stop = I2C2_Stop
    #else
      #ifdef I2c_3 then
        symbol I2C_Init = I2C3_Init
        symbol I2C_Start = I2C3_Start
        symbol I2C_Restart = I2C3_Restart
        symbol I2C_Is_Idle = I2C3_Is_Idle
        symbol I2C_Read = I2C3_Read
        symbol I2C_Write =  I2C3_Write
        symbol I2C_Stop = I2C3_Stop
      '#else 'SoftI2C
'        symbol I2C_Init = Soft_I2C_Init
'        symbol I2C_Start = Soft_I2C_Start
'        symbol I2C_Read = Soft_I2C_Read
'        symbol I2C_Write = Soft_I2C_Write
'        symbol I2C_Stop = Soft_I2C_Stop
      #endif
    #endif
  #endif
Any suggestion?
Thanks.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: How can I choose I2C module similar to using SPI_Set_Act

#2 Post by filip » 23 May 2012 12:12

Hi,

I have tried the code below for I2C_1 and apparently it works :

Code: Select all

program I2C_Simple

 #DEFINE I2C_1
 '#DEFINE I2C_2

 #IFDEF I2C_1 then
  symbol I2C_Init = I2C1_Init
  symbol I2C_Start = I2C1_Start
  symbol I2C_Restart = I2C1_Repeated_Start
  symbol I2C_Is_Idle = I2C1_Is_Idle
  symbol I2C_Read = I2C1_Rd
  symbol I2C_Write =  I2C1_Wr
  symbol I2C_Stop = I2C1_Stop
#endif

#IFDEF I2C_2 then
  symbol I2C_Init = I2C2_Init
  symbol I2C_Start = I2C2_Start
  symbol I2C_Restart = I2C2_Repeated_Start
  symbol I2C_Is_Idle = I2C2_Is_Idle
  symbol I2C_Read = I2C2_Rd
  symbol I2C_Write =  I2C2_Wr
  symbol I2C_Stop = I2C2_Stop
#endif

main:
  ANSELB = 0             ' Configure PORTB pins as digital
  ANSELC = 0             ' Configure PORTC pins as digital
  TRISB = 0              ' Configure PORTB as output
  LATB = 0               ' Clear PORTB

  I2C_Init(100000)      ' initialize I2C communication
  I2C_Start()           ' issue I2C start signal
  I2C_Write(0xA2)          ' send byte via I2C (device address + W)
  I2C_Write(2)             ' send byte (address of EEPROM location)
  I2C_Write(0xFF)          ' send data (data to be written)
  I2C_Stop()            ' issue I2C stop signal

  Delay_100ms()

  I2C_Start()           ' issue I2C start signal
  I2C_Write(0xA2)          ' send byte via I2C  (device address + W)
  I2C_Write(2)             ' send byte (data address)
  I2C_Restart()          ' issue I2C signal repeated start
  I2C_Write(0xA3)          ' send byte (device address + R)
  LATB = I2C_Read(0)      ' Read the data (NO acknowledge)
  I2C_Stop()            ' issue I2C stop signal
end.
It should work with the I2C_2 too.

Regards,
Filip.

Post Reply

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