slave

Dependencies:   mbed

Committer:
maner1256
Date:
Mon Dec 17 18:04:08 2018 +0000
Revision:
0:516fb708ec42
slave

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maner1256 0:516fb708ec42 1 #include <mbed.h>
maner1256 0:516fb708ec42 2
maner1256 0:516fb708ec42 3 I2CSlave slave(D14, D15);
maner1256 0:516fb708ec42 4 Serial pc(USBTX, USBRX);
maner1256 0:516fb708ec42 5 DigitalIn mybutton(USER_BUTTON);
maner1256 0:516fb708ec42 6 #define ADDRESS 0x50
maner1256 0:516fb708ec42 7 int main() {
maner1256 0:516fb708ec42 8 pc.baud(57600);
maner1256 0:516fb708ec42 9 pc.printf("Starting slave device at address:0x%02x\r\n", ADDRESS);
maner1256 0:516fb708ec42 10 slave.frequency(400000); //Set the clock frequency
maner1256 0:516fb708ec42 11 slave.address(ADDRESS);
maner1256 0:516fb708ec42 12 char buf[6];
maner1256 0:516fb708ec42 13 //char msg[1];
maner1256 0:516fb708ec42 14 //msg[0] = 0x00;
maner1256 0:516fb708ec42 15 int j = 1;
maner1256 0:516fb708ec42 16
maner1256 0:516fb708ec42 17 while (1) {
maner1256 0:516fb708ec42 18
maner1256 0:516fb708ec42 19 int i = slave.receive();
maner1256 0:516fb708ec42 20 if(i > 0)pc.printf("rcv:%d\r\n", i);
maner1256 0:516fb708ec42 21
maner1256 0:516fb708ec42 22 switch (i) {
maner1256 0:516fb708ec42 23 case I2CSlave::ReadAddressed:
maner1256 0:516fb708ec42 24 pc.printf("I2CSlave::ReadAddressed\r\n");
maner1256 0:516fb708ec42 25 //slave.write(msg, strlen(msg) + 1); // Includes null char
maner1256 0:516fb708ec42 26 break;
maner1256 0:516fb708ec42 27 case I2CSlave::WriteAddressed:
maner1256 0:516fb708ec42 28 pc.printf("I2CSlave::WriteAddressed\r\n");
maner1256 0:516fb708ec42 29
maner1256 0:516fb708ec42 30 slave.read(buf, 2);
maner1256 0:516fb708ec42 31 /*for(int ik=0;ik<7;ik++)
maner1256 0:516fb708ec42 32 {
maner1256 0:516fb708ec42 33 buf[ik]=0x00;
maner1256 0:516fb708ec42 34 }*/
maner1256 0:516fb708ec42 35 printf("Read A: 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x \r\n", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
maner1256 0:516fb708ec42 36 break;
maner1256 0:516fb708ec42 37
maner1256 0:516fb708ec42 38
maner1256 0:516fb708ec42 39 }
maner1256 0:516fb708ec42 40 if(mybutton == 0) {
maner1256 0:516fb708ec42 41
maner1256 0:516fb708ec42 42
maner1256 0:516fb708ec42 43 buf[0] = 0; // Clear buffer
maner1256 0:516fb708ec42 44 buf[1] = 0;
maner1256 0:516fb708ec42 45 buf[2] = 0;
maner1256 0:516fb708ec42 46 buf[3] = 0;
maner1256 0:516fb708ec42 47 buf[4] = 0;
maner1256 0:516fb708ec42 48 buf[5] = 0;
maner1256 0:516fb708ec42 49
maner1256 0:516fb708ec42 50 }
maner1256 0:516fb708ec42 51 //msg[0] = j;
maner1256 0:516fb708ec42 52 j++;
maner1256 0:516fb708ec42 53
maner1256 0:516fb708ec42 54 }
maner1256 0:516fb708ec42 55 }