Hsieh JenYun
/
446STM32_PC_I2CS
V1
Fork of 446STM32_Print_PC_I2CS by
main.cpp@3:4d977dd77a4b, 2017-11-10 (annotated)
- Committer:
- Tanakacool
- Date:
- Fri Nov 10 05:08:18 2017 +0000
- Revision:
- 3:4d977dd77a4b
- Parent:
- 2:764981c589b6
v2
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:4d977dd77a4b | 6 | void boxslave(); |
Tanakacool | 2:764981c589b6 | 7 | |
Tanakacool | 2:764981c589b6 | 8 | int main() { |
Tanakacool | 0:8be7c91879df | 9 | char buffer[128]; |
Tanakacool | 3:4d977dd77a4b | 10 | slave.address(0xA0); |
Tanakacool | 2:764981c589b6 | 11 | |
Tanakacool | 2:764981c589b6 | 12 | while(1){ |
Tanakacool | 3:4d977dd77a4b | 13 | boxslave(); |
Tanakacool | 3:4d977dd77a4b | 14 | |
Tanakacool | 0:8be7c91879df | 15 | if(pc.readable()) { |
Tanakacool | 3:4d977dd77a4b | 16 | //slave.stop(); |
Tanakacool | 0:8be7c91879df | 17 | pc.gets(buffer, 128); |
Tanakacool | 0:8be7c91879df | 18 | pc.printf("I got '%s'\n", buffer); |
Tanakacool | 3:4d977dd77a4b | 19 | //slave.stop(); |
Tanakacool | 0:8be7c91879df | 20 | } |
Tanakacool | 3:4d977dd77a4b | 21 | } |
Tanakacool | 3:4d977dd77a4b | 22 | } |
Tanakacool | 3:4d977dd77a4b | 23 | void boxslave(){ |
Tanakacool | 3:4d977dd77a4b | 24 | char buf[20]; |
Tanakacool | 3:4d977dd77a4b | 25 | char msg[] = "Hi Master!"; |
Tanakacool | 3:4d977dd77a4b | 26 | char all_buf[]="i'm here!"; |
Tanakacool | 3:4d977dd77a4b | 27 | int i = slave.receive(); |
Tanakacool | 2:764981c589b6 | 28 | switch (i) { |
Tanakacool | 0:8be7c91879df | 29 | case I2CSlave::ReadAddressed: |
Tanakacool | 0:8be7c91879df | 30 | if(!slave.write(msg, strlen(msg) + 1)) // Includes null char |
Tanakacool | 0:8be7c91879df | 31 | slave.stop(); |
Tanakacool | 0:8be7c91879df | 32 | printf("ReadAddressed\n"); |
Tanakacool | 0:8be7c91879df | 33 | //slave.stop(); |
Tanakacool | 0:8be7c91879df | 34 | break; |
Tanakacool | 0:8be7c91879df | 35 | case I2CSlave::WriteGeneral: |
Tanakacool | 0:8be7c91879df | 36 | if(!slave.write(all_buf, strlen(all_buf) + 1)) // Includes null char |
Tanakacool | 0:8be7c91879df | 37 | slave.stop(); |
Tanakacool | 0:8be7c91879df | 38 | printf("i've received the command...\n"); |
Tanakacool | 0:8be7c91879df | 39 | break; |
Tanakacool | 0:8be7c91879df | 40 | case I2CSlave::WriteAddressed: |
Tanakacool | 0:8be7c91879df | 41 | slave.read(buf, 20); |
Tanakacool | 0:8be7c91879df | 42 | printf("master write to A: %s\n", buf); |
Tanakacool | 0:8be7c91879df | 43 | break; |
Tanakacool | 0:8be7c91879df | 44 | } |
Tanakacool | 0:8be7c91879df | 45 | for(int i = 0; i < 10; i++) { |
Tanakacool | 0:8be7c91879df | 46 | buf[i] = 0; // Clear buffer |
Tanakacool | 0:8be7c91879df | 47 | } |
Tanakacool | 0:8be7c91879df | 48 | } |