11 years, 4 months ago.

LPC1768 vs KL25Z I2C

Hi all,

I'm trying to establish a I2C communication between ADP1048 (digital PFC) and KL25Z. I'm able to write registers, but when trying to read them, I allways get 0xff. But that's not the question. Today I tried to connect on the KL25Z something I know it was working on my LPC1768 mbed - just to make sure that the i2c communication works. That is the DS1631 temperature sensor. I tried both Mbeds (LPC1768 and KL25Z) On LPC1768 I get the temperature, on the KL25Z I get 0xff. The KL25Z board is brand new, allso the ADP1048 was connected to KL25Z using only 12V auxillary power, the I2C communication is isolated with ADuM2250, powered by 3.3V on the low side, so there is no way, that the I2C port on KL25Z was damaged by overvoltage. I allso have 2k2 pull ups on SCL & SDA to 3.3V. And here comes the question. What is the difference in i2c library between LPC1768 and KL25Z?

Sorry for my poor english. I hope I can get some answers.

Reards,

Gorazd Rosbach

1 Answer

11 years, 4 months ago.

Are you sure you have not mixed up the SDA and SCL pins. The initial enclosed pinout drawing for the KL25Z had mistakes: J2 pin 18 (PTE0) is SDA, J2 pin 20 (PTE1) is SCL. Obviously you also need a common ground.

Accepted Answer

Yes, PTE0 is SDA and PTE1 is SCL. I use 3.3 and GND for DS1631 and 3.3V and GND for ADP1048 with the ADuM2250 isolator. I allso tried the PTC9 and PTC8 pins, but with same results. Btw for the DS1631 I use the code provided by Stijn Sontrop. I just excluded the LCD and used the onboard serial port instead.

RGDS

OK, today I tested the code on another (that is brand new) KL25Z mbed board with same results. Is there any one willing, to test the code below?

/*********************************
**Read DS1631 Temperature Sensor**
******Made by Stijn Sontrop*******
**********22 Dec 2010*************
**********************************/

#include "mbed.h"

///DS1631 Address:
//Formation: 1001 A2 A1 A0 R/W' 1001 011_0/1 
//Write: Make R/W' 0
#define DS1631WRITEADDRESS 0x96
//Read: Make R/W' 1
#define DS1631READADDRESS 0x97


I2C i2c(PTE0, PTE1); //SDA & SCL from I2C
Serial pc(USBTX,USBRX);

typedef struct { //Struct to contain read values
    char msb;
    char lsb;
} temperature;

temperature readds1631(void) {
    temperature readval;
    i2c.start();
    i2c.write(DS1631ADDRESS); //writeadress
    i2c.write(0xAA); //Read the temperature
    i2c.stop();
    wait_ms(1);
    i2c.start();
    i2c.write(DS1631ADDRESS); //readadress
    readval.msb = i2c.read(1); //Read the MSB (and do an ACK)
    readval.lsb = i2c.read(0); //Read the LSB (and don't do an ACK)
    i2c.stop();
    readval.msb = readval.msb - 2; //Apparently you need to do -2 to get the right value
    return readval; //Return the value
}

int main() {
      
    pc.baud(115200);
    temperature readval;
    i2c.start();
    i2c.write(DS1631ADDRESS); //writeaddress
    i2c.write(0xAC); //config reg
    i2c.write(0x02); //config
    i2c.stop();
    
    //Start conversion:
    i2c.start();
    i2c.write(DS1631WRITEADDRESS);
    i2c.write(0x51); //start conversion
    i2c.stop();
    while(1) {
        readval = readds1631(); //Call the readds1631 function
       
        pc.printf("Temp: %d LSB:%d \r\n", readval.msb, readval.lsb);
        wait(0.5); 
    }
}

So, I tested the code on LPC1768 and it works, on 2 KL25Z it only returns 253 for Temp.

posted by Gorazd Rosbach 14 Apr 2014

Reading back values 255 usually means that the slave is not responding due to mixed up I2C wires, wrong address, missing GND or powersupply etc. However in this case I think there may be some issues with the I2C byte write methods on the KL25Z. You may want to try the blockwrite operations instead. They work in my TextLCD and other driver code for both the LPC1768 and KL25Z..

/*********************************
**Read DS1631 Temperature Sensor**
******Made by Stijn Sontrop*******
**********22 Dec 2010*************
**********************************/
 
#include "mbed.h"
 
///DS1631 Address:
//Formation: 1001 A2 A1 A0 R/W' 1001 011_0/1 
//Write: Make R/W' 0
#define DS1631WRITEADDRESS 0x96
//Read: Make R/W' 1
#define DS1631READADDRESS 0x97

#define DS1631_ADDRESS 0x96
 
 
I2C i2c(PTE0, PTE1); //SDA & SCL from I2C
Serial pc(USBTX,USBRX);
 
typedef struct { //Struct to contain read values
    char msb;
    char lsb;
} temperature;
 
temperature readds1631(void) {
    temperature readval;
    char data[2];

    data[0]=0xAA;
    i2c.write(DS1631_ADDRESS, data, 1); //Read the temperature

    wait_ms(1);

    i2c.read(DS1631_ADDRESS, data, 2); //Read the temperature
    readval.msb = data[0]; //Read the MSB
    readval.lsb = data[1];  //Read the LSB

    readval.msb = readval.msb - 2; //Apparently you need to do -2 to get the right value
    return readval; //Return the value
}
 
int main() {
    char data[2];
    temperature readval;
      
    pc.baud(115200);

    data[0]=0xAC;  //config reg
    data[1]=0x02;  //config
    i2c.write(DS1631_ADDRESS, data, 2); //Write the config

    
    //Start conversion:
    data[0]=0x51;  //start conversion
    i2c.write(DS1631_ADDRESS, data, 1); //start conversion


    while(1) {
        readval = readds1631(); //Call the readds1631 function
       
        pc.printf("Temp: %d LSB:%d \r\n", readval.msb, readval.lsb);
        wait(0.5); 
    }
}

posted by Wim Huiskamp 16 Apr 2014

Thank you. It works. Now, I have to try to establish the communication with my ADP...

posted by Gorazd Rosbach 21 Apr 2014