4 years, 7 months ago.

connection issues with i2C KL25Z as master and KL05Z as slave

Hi,

I tried to use I2C to connect KL25Z as master and KL05Z as slave, but it seems like the KL25Z master is not writing the correct data to the bus. It'd be great if anyone notice an error in the code. When I print out the slave.receive() from the slave KL05Z, it always shows 0, so I guess it does not get any command from the master. Also I checked the oscilloscope, the waveform seems to be not sending the correct address and command, although I'm not sure.

Here's the code

KL25Z_master

//KL25Z_master
#include "mbed.h"
#include <I2C.h>
//define pin outs
Serial pc(USBTX, USBRX);
I2C i2c(I2C_SDA, I2C_SCL);



void i2C_master();
//i2C parameters
const int slave_addr = 0xA0 << 1;




int main()
{   

    i2c.frequency(100000);
    while(1) {
        i2C_master();
        }
}

char cmd[1];
void i2C_master()
{
     cmd[0] = 0x01;
    i2c.write(slave_addr,cmd,1);
    // read and write takes the 8-bit version of the address.
    // set up configuration register (at 0x01)
    wait(0.5);
    i2c.read(slave_addr, cmd, 2);
    
    float tmp = (((cmd[0]<<8)|cmd[1]));
    printf("Temp = %s\n", cmd);
}

KL05Z slave

//KL05Z slave
#include "mbed.h"



//define pinouts
I2CSlave slave(PTB4, PTB3);
Serial pc(USBTX, USBRX);
DigitalOut green(LED_GREEN);
DigitalOut blue(LED_BLUE);
DigitalOut red(LED_RED);




void i2C_slave();
//i2C parameters
const int addr = 0xA0 ;



int main()
{   
    slave.frequency(100000);
    green = 1;
    blue = 1;
    red = 1;
    
    slave.address(addr);
    while(1) {    
        i2C_slave();      
    }
}


char buf[10];
char msg[] = {12345};


void i2C_slave()
{
   

       int i = slave.receive();
       switch (i) {
        case I2CSlave::ReadAddressed:
            //slave.write(msg, strlen(msg) + 1); // Includes null char
            green = 0;
            wait(0.5);
            break;
        case I2CSlave::WriteGeneral:
            blue = 0;
            wait(0.5);
            slave.read(buf, 10);
            printf("Read G: %s\n", buf);

            break;
        case I2CSlave::WriteAddressed:
            red = 0;
            wait(0.5);
            slave.read(buf, 10);
            printf("Read A: %s\n", buf);
               break;
       }
       pc.printf("%i",slave.receive());
       for(int i = 0; i < 10; i++) buf[i] = 0;    // Clear buffer

}
  1. include "mbed.h"/media/uploads/conrad2001/i2c.png

1 Answer

4 years, 7 months ago.

Hi Conrad,

Refer to user manual of KL05Z

http://cache.freescale.com/files/32bit/doc/ref_manual/KL05P48M48SF1RM.pdf

It uses 7-bit i2c address, that means you should set i2c_address to const int slave_addr = 0xA0;.

Regards, Desmond

thank you for the reply, I changed the address and the i2c_slave1.receive() only returns 3, which is I2CSlave::WriteAddressed: no matter what the master sends to the slave I'd really appreciate to know what is going on in the I2C here's my updated code

KL25Z master

#include "mbed.h"


namespace master{
//I2C setup
I2C i2c(PTC9, PTC8);
Serial pc(USBTX, USBRX);
}

int main()
{   const int slave_addr = 0x0A ;
    char cmd[1];
    master::pc.baud(9600);

    master::i2c.frequency(100000);

    while(1) {
        master::i2c.start();
        cmd[0] = 0x01;
        master::i2c.write(slave_addr,cmd,1,true);
        master::i2c.stop();
        cmd[0] = 0x02;
        master::i2c.write(slave_addr,cmd,1);
        master::i2c.stop();
        wait(2);
        cmd[0] = 0x03;
        master::i2c.write(slave_addr,cmd,1);
        
        master::i2c.stop();
    }

}

KL05Z slave

#include "mbed.h"


namespace slave1
{

Serial pc(USBTX, USBRX);



DigitalOut green(LED_GREEN);
DigitalOut red(LED_RED);
DigitalOut blue(LED_BLUE);

}

I2CSlave i2c_slave1(PTB4, PTB3);



//I2C setup

int main()
{   const int slave_addr = 0x0A;
    slave1::red = 1;
    slave1::green = 1;
    slave1::blue = 1;
    i2c_slave1.frequency(100000); //Set the clock frequency
    char buf[1];
    char msg[1];
    msg[0] = 0x00;
    i2c_slave1.address(slave_addr);
    while (1) {
        int i = i2c_slave1.receive();
        switch (i) {
            case I2CSlave::ReadAddressed:
                slave1::green = 0;
                wait(1);
                break;
            case I2CSlave::WriteGeneral:
                slave1::blue = 0;
                wait(1);
                break;
            case I2CSlave::WriteAddressed:
                slave1::red = 0;
                wait(1);
                break;
        }
        slave1::pc.printf("%i",i2c_slave1.receive());
        for(int i = 0; i < 10; i++) buf[i] = 0;    // Clear buffer
        
    }
    buf[0] = 0;    // Clear buffer
    i2c_slave1.stop();
}

i2c_slave1.receive(); only returns 3 no matter what the master sends /media/uploads/conrad2001/3.png

posted by CONRAD CHAN 03 Sep 2019