V1

Fork of 446STM32_Print_PC_I2CS by Hsieh JenYun

Committer:
Tanakacool
Date:
Mon Nov 06 08:23:22 2017 +0000
Revision:
2:764981c589b6
Parent:
1:51fb8c086992
Child:
3:4d977dd77a4b
PCI2CS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Tanakacool 2:764981c589b6 1 #include "mbed.h"
Tanakacool 2:764981c589b6 2
Tanakacool 1:51fb8c086992 3 DigitalOut myled(LED1);
Tanakacool 1:51fb8c086992 4 Serial pc(USBTX, USBRX);
Tanakacool 2:764981c589b6 5 I2CSlave slave(PC_9, PA_8);
Tanakacool 2:764981c589b6 6
Tanakacool 2:764981c589b6 7 int main() {
Tanakacool 0:8be7c91879df 8 char buffer[128];
Tanakacool 2:764981c589b6 9 char all_buf[]="i'm here!";
Tanakacool 2:764981c589b6 10 char buf[20];
Tanakacool 2:764981c589b6 11 char msg[] = "Hi Master!";
Tanakacool 2:764981c589b6 12 slave.address(0xA0);
Tanakacool 2:764981c589b6 13
Tanakacool 2:764981c589b6 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 2:764981c589b6 20 int i = slave.receive();
Tanakacool 2:764981c589b6 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 2:764981c589b6 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 2:764981c589b6 42 }
Tanakacool 0:8be7c91879df 43 }