I2C: how to address a I2C device among multiple devices

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

I2C: how to address a I2C device among multiple devices

跳至解决方案
1,992 次查看
hunglam
Contributor II

Hello,

I need URGENT help! I have in my circuit 2 i2c-based digitial rheostat with the address 46 and 47. I use Kinetis design studio, ksdk1.2 and processor expert (bare metal). When I communicate with just one of the rheostat, everything works fine; i can set the resistance of the rheostat without any problem. However, when I try to set the resistance of the 2 rheostat consecutively, only the first one works, while the other isn't responsive at all. To mention is that the address of the first rheostat is set by using processor expert. In order to switch to the second rheostat I use the routine I2C_HAL_SetAddress7bit. It doesn't matter, which the rheostat I address first (using processor expert). The first one always does what it should while the 2nd doesn't.

Hope someone can give me an advice.

thanks

 

code:

 

/* ###################################################################

**     Filename    : main.c

**     Project     : mcp4017_test

**     Processor   : MK64FN1M0VLL12

**     Version     : Driver 01.01

**     Compiler    : GNU C Compiler

**     Date/Time   : 2015-11-08, 17:51, # CodeGen: 0

**     Abstract    :

**         Main module.

**         This module contains user's application code.

**     Settings    :

**     Contents    :

**         No public methods

**

** ###################################################################*/

/*!

** @file main.c

** @version 01.01

** @brief

**         Main module.

**         This module contains user's application code.

*/        

/*!

**  @addtogroup main_module main module documentation

**  @{

*/        

/* MODULE main */

 

/* Including needed modules to compile this module/procedure */

#include "Cpu.h"

#include "Events.h"

#include "clockMan1.h"

#include "pin_init.h"

#include "osa1.h"

#include "i2cCom1.h"

#include "DbgCs1.h"

#if CPU_INIT_CONFIG

  #include "Init_Config.h"

#endif

 

/* User includes (#include below this line is not maintained by Processor Expert) */

#include "stdio.h"

/*lint -save  -e970 Disable MISRA rule (6.3) checking. */

int main(void)

/*lint -restore Enable MISRA rule (6.3) checking. */

{

  /* Write your local variable definition here */

uint8_t widerstand=0,

i2c_status_t fehler_i2c;

 

  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/

  PE_low_level_init();

  /*** End of Processor Expert internal initialization.                    ***/

 

  /* Write your code here */

  /* For example: for(;;) { } */

 

OSA_TimeDelay(6000);

for(uint i=0;i<116;i+=2)

{

    widerstand=i;

    fehler_i2c=I2C_DRV_MasterSendDataBlocking(FSL_I2CCOM1,&i2cCom1_MasterConfig0,NULL,0,&widerstand,1U,1000U);

    if(fehler_i2c!=kStatus_I2C_Success)

    {

        printf("I2C transmission error\r\n ");

    }

    printf("widerstand is:   %d\r\n",widerstand);

 

    OSA_TimeDelay(1000);

  

 

    // set 2nd mcp4017

    I2C_HAL_SetAddress7bit(I2C0_BASE_PTR,0X5E); // switch to 2nd mcp

    fehler_i2c=I2C_DRV_MasterSendDataBlocking(FSL_I2CCOM1,&i2cCom1_MasterConfig0,NULL,0,&widerstand,1U,1000U);

    if(fehler_i2c!=kStatus_I2C_Success)

    {

        printf("I2C transmission error\n ");

    }

    OSA_TimeDelay(1000);

 

}

 

  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/

  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/

  #ifdef PEX_RTOS_START

    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */

  #endif

  /*** End of RTOS startup code.  ***/

  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/

  for(;;){}

  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/

} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

 

/* END main */

/*!

** @}

*/

/*

** ###################################################################

**

**     This file was created by Processor Expert 10.5 [05.21]

**     for the Freescale Kinetis series of microcontrollers.

**

** ###################################################################

*/

标签 (1)
1 解答
1,467 次查看
Jorge_Gonzalez
NXP Employee
NXP Employee

Hello hung Iam:

The API I2C_HAL_SetAddress7bit is not useful in this case. That API sets the listening address for slave mode, but in this case the MCU is in Master mode.

Instead you need 2 Master configurations in the fsl_i2c component:

pastedImage_0.png

Then you can call I2C Driver Send or Receive APIs using the corresponding master configuration structure (i2cCom1_MasterConfig0 or i2cCom1_MasterConfig1).

Regards!

Jorge Gonzalez

在原帖中查看解决方案

0 项奖励
回复
2 回复数
1,468 次查看
Jorge_Gonzalez
NXP Employee
NXP Employee

Hello hung Iam:

The API I2C_HAL_SetAddress7bit is not useful in this case. That API sets the listening address for slave mode, but in this case the MCU is in Master mode.

Instead you need 2 Master configurations in the fsl_i2c component:

pastedImage_0.png

Then you can call I2C Driver Send or Receive APIs using the corresponding master configuration structure (i2cCom1_MasterConfig0 or i2cCom1_MasterConfig1).

Regards!

Jorge Gonzalez

0 项奖励
回复
1,467 次查看
hunglam
Contributor II

Hi Jorge,

thanks a lot for the absolutely right advice. You have been a great help!

Hung