V1

Fork of 446STM32_Print_PC_I2CS by Hsieh JenYun

Committer:
Tanakacool
Date:
Wed Nov 01 07:19:20 2017 +0000
Revision:
0:8be7c91879df
Child:
1:51fb8c086992
PCI2C

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Tanakacool 0:8be7c91879df 1 #include "mbed.h"
Tanakacool 0:8be7c91879df 2
Tanakacool 0:8be7c91879df 3 DigitalOut myled(LED1);
Tanakacool 0:8be7c91879df 4 Serial pc(USBTX, USBRX);
Tanakacool 0:8be7c91879df 5 I2CSlave slave(PC_9, PA_8);
Tanakacool 0:8be7c91879df 6
Tanakacool 0:8be7c91879df 7 int main() {
Tanakacool 0:8be7c91879df 8 char buffer[128];
Tanakacool 0:8be7c91879df 9 char all_buf[]="i'm here!";
Tanakacool 0:8be7c91879df 10 char buf[20];
Tanakacool 0:8be7c91879df 11 char msg[] = "Hi Master!";
Tanakacool 0:8be7c91879df 12 slave.address(0xA0);
Tanakacool 0:8be7c91879df 13
Tanakacool 0:8be7c91879df 14 while(1){
Tanakacool 0:8be7c91879df 15 if(pc.readable()) {
Tanakacool 0:8be7c91879df 16 slave.stop();
Tanakacool 0:8be7c91879df 17 pc.gets(buffer, 128);
Tanakacool 0:8be7c91879df 18 pc.printf("I got '%s'\n", buffer);
Tanakacool 0:8be7c91879df 19 }
Tanakacool 0:8be7c91879df 20 int i = slave.receive();
Tanakacool 0:8be7c91879df 21 switch (i) {
Tanakacool 0:8be7c91879df 22 case I2CSlave::ReadAddressed:
Tanakacool 0:8be7c91879df 23 if(!slave.write(msg, strlen(msg) + 1)) // Includes null char
Tanakacool 0:8be7c91879df 24 slave.stop();
Tanakacool 0:8be7c91879df 25 printf("ReadAddressed\n");
Tanakacool 0:8be7c91879df 26 //slave.stop();
Tanakacool 0:8be7c91879df 27 break;
Tanakacool 0:8be7c91879df 28 case I2CSlave::WriteGeneral:
Tanakacool 0:8be7c91879df 29 if(!slave.write(all_buf, strlen(all_buf) + 1)) // Includes null char
Tanakacool 0:8be7c91879df 30 slave.stop();
Tanakacool 0:8be7c91879df 31 printf("i've received the command...\n");
Tanakacool 0:8be7c91879df 32 break;
Tanakacool 0:8be7c91879df 33 case I2CSlave::WriteAddressed:
Tanakacool 0:8be7c91879df 34 slave.read(buf, 20);
Tanakacool 0:8be7c91879df 35 //slave.stop();
Tanakacool 0:8be7c91879df 36 printf("master write to A: %s\n", buf);
Tanakacool 0:8be7c91879df 37 break;
Tanakacool 0:8be7c91879df 38 }
Tanakacool 0:8be7c91879df 39 for(int i = 0; i < 10; i++) {
Tanakacool 0:8be7c91879df 40 buf[i] = 0; // Clear buffer
Tanakacool 0:8be7c91879df 41 }
Tanakacool 0:8be7c91879df 42 }
Tanakacool 0:8be7c91879df 43 }