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 sending the correct address and command. With 0xA0 shifted one bit to the right and last bit is high indicates write as described in the handbook in I2C_SDA.
Here's the code
<<code title=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);
}
<<\code>>
<<code title=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
}
<<\code>>
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 sending the correct address and command. With 0xA0 shifted one bit to the right and last bit is high indicates write as described in the handbook in I2C_SDA.
Here's the code
<<code title=KL25Z master>>
KL25Z_mastervoid 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); } <<\code>>
<<code title=KL05Z slave>>
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
} <<\code>>