AnsweredAssumed Answered

FRDM KL25Z I2C don't working

Question asked by wojciech22a on Jul 8, 2015
Latest reply on Jul 15, 2015 by Jorge_Gonzalez

Hi. I'm trying to write a program that will be transmitting one byte of data from the accelerometer via UART. Unfortunately this not working. By UART all the time is sent a value of "3A", an address "slave" accelerometer.

Bez tytułu.png

Up - UART terminal

I2C I initiated with the file header h, using KDS 2.0.

Bez tytułu2.png

Program:

for(;;){

   unsigned char Kp;

   I2C0_C1 = I2C0_C1 | (1 << 7);//Enable I2C

  I2C0_C1 = I2C0_C1 | (1 << 4);//TX mode

  I2C0_C1 = I2C0_C1 | (1 << 5);//start

 

    I2C0_D=0x3A;//slave adress of accelerometer - Write

 

 

   while ( !(I2C0_S & (1)) );//Wait for ACK from accelerometer

 

    I2C0_D=0x01;//Sending accelerometer register adress

 

 

  while ( !(I2C0_S & (1)) );//Wait for ACK from accelerometer

 

 

    I2C0_C1 = I2C0_C1 | (1 << 2);//Rstart

 

     I2C0_D=0x3B;//slave adress of accelerometer - Read

  while ( !(I2C0_S & (1)) );//Wait for ACK from accelerometer

    I2C0_C1 = I2C0_C1 & ~(1 << 4);//clear TX

 

 

 

 

    I2C0_C1 = I2C0_C1 | (1 << 3);//set NACK

    Kp=I2C0_D;//receive data from I2C

  I2C0_C1 = I2C0_C1 & ~(1 << 5);//set Stop

  I2C0_C1 = I2C0_C1 & ~(1 << 7);//Disable I2C

 

     Transmisja(Kp);//Transmit byte value via UART

  }

Please help.

 

EDIT. When I changed the program (i add "while ( !(I2C0_S & (1<<7)) )"):

for(;;){

   unsigned char Kp;

   I2C0_C1 = I2C0_C1 | (1 << 7);//Enable I2C

  I2C0_C1 = I2C0_C1 | (1 << 4);//TX mode

  I2C0_C1 = I2C0_C1 | (1 << 5);//start

    I2C0_D=0x3A;//slave adress of accelerometer - Write

while ( !(I2C0_S & (1<<7)) );//Wait for end of trans.

   while ( !(I2C0_S & (1)) );//Wait for ACK from accelerometer

    I2C0_D=0x01;//Sending accelerometer register adress

while ( !(I2C0_S & (1<<7)) );//Wait for end of trans.

  while ( !(I2C0_S & (1)) );//Wait for ACK from accelerometer

    I2C0_C1 = I2C0_C1 | (1 << 2);//Rstart

     I2C0_D=0x3B;//slave adress of accelerometer - Read

while ( !(I2C0_S & (1<<7)) );//Wait for end of trans.

  while ( !(I2C0_S & (1)) );//Wait for ACK from accelerometer

    I2C0_C1 = I2C0_C1 & ~(1 << 4);//clear TX

    I2C0_C1 = I2C0_C1 | (1 << 3);//set NACK

    Kp=I2C0_D;//receive data from I2C

  I2C0_C1 = I2C0_C1 & ~(1 << 5);//set Stop

  I2C0_C1 = I2C0_C1 & ~(1 << 7);//Disable I2C

 

     Transmisja(Kp);//Transmit byte value via UART

  }

Program send 00 once..and stop working

Outcomes