I2CSlave
使用 I2C Slave 与 I2C Master 通信。
同步级别:不受保护。
I2CSlave 类参考
公共类型 | |
enum | RxStatus { NoData = 0, ReadAddressed = 1, WriteGeneral = 2, WriteAddressed = 3 } |
公共成员函数 | |
I2CSlave (PinName sda, PinName scl) | |
void | frequency (int hz) |
int | receive (void) |
int | read (char *data, int length) |
int | read (void) |
int | write (const char *data, int length) |
int | write (int data) |
void | address (int address) |
void | stop (void) |
受保护的属性 | |
i2c_t | _i2c |
I2CSlave 示例
尝试此示例以查看 I2C 响应器的工作原理。
#include <mbed.h>
I2CSlave slave(p9, p10);
int main() {
char buf[10];
char msg[] = "Slave!";
slave.address(0xA0);
while (1) {
int i = slave.receive();
switch (i) {
case I2CSlave::ReadAddressed:
slave.write(msg, strlen(msg) + 1); // Includes null char
break;
case I2CSlave::WriteGeneral:
slave.read(buf, 10);
printf("Read G: %s\n", buf);
break;
case I2CSlave::WriteAddressed:
slave.read(buf, 10);
printf("Read A: %s\n", buf);
break;
}
for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer
}
}