AnsweredAssumed Answered

I2C: how to address a I2C device among multiple devices

Question asked by hung lam on Nov 18, 2015
Latest reply on Nov 18, 2015 by hung lam

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.

**

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

*/

Outcomes