Hsieh JenYun
/
I2C_Master
a
Fork of I2C_Master by
main.cpp@1:6926cb0de2b0, 2017-10-20 (annotated)
- Committer:
- cathy101
- Date:
- Fri Oct 20 05:06:15 2017 +0000
- Revision:
- 1:6926cb0de2b0
- Parent:
- 0:f76c26307f9a
- Child:
- 2:3d64970eeb96
v1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:f76c26307f9a | 1 | #include "mbed.h" |
cathy101 | 1:6926cb0de2b0 | 2 | |
mbed_official | 0:f76c26307f9a | 3 | |
cathy101 | 1:6926cb0de2b0 | 4 | #include "hal/i2c_api.h" |
cathy101 | 1:6926cb0de2b0 | 5 | #include "platform/SingletonPtr.h" |
cathy101 | 1:6926cb0de2b0 | 6 | #include "platform/PlatformMutex.h" |
cathy101 | 1:6926cb0de2b0 | 7 | //#include "platform/NonCopyable.h" |
cathy101 | 1:6926cb0de2b0 | 8 | |
cathy101 | 1:6926cb0de2b0 | 9 | #if DEVICE_I2C_ASYNCH |
cathy101 | 1:6926cb0de2b0 | 10 | #include "platform/CThunk.h" |
cathy101 | 1:6926cb0de2b0 | 11 | #include "hal/dma_api.h" |
cathy101 | 1:6926cb0de2b0 | 12 | #include "platform/FunctionPointer.h" |
cathy101 | 1:6926cb0de2b0 | 13 | #endif |
mbed_official | 0:f76c26307f9a | 14 | // Read temperature from LM75BD |
mbed_official | 0:f76c26307f9a | 15 | |
cathy101 | 1:6926cb0de2b0 | 16 | I2C Master(PC_9, PA_8); |
cathy101 | 1:6926cb0de2b0 | 17 | Serial pc(USBTX,USBRX); |
cathy101 | 1:6926cb0de2b0 | 18 | const int addr = 0xA0; |
mbed_official | 0:f76c26307f9a | 19 | |
mbed_official | 0:f76c26307f9a | 20 | int main() { |
mbed_official | 0:f76c26307f9a | 21 | char cmd[2]; |
cathy101 | 1:6926cb0de2b0 | 22 | char buffer[20]; |
cathy101 | 1:6926cb0de2b0 | 23 | char slave_buffer[20]; |
cathy101 | 1:6926cb0de2b0 | 24 | char msg[] = "Hi 0xA0"; |
cathy101 | 1:6926cb0de2b0 | 25 | char msg_all[] = "Slave!"; |
mbed_official | 0:f76c26307f9a | 26 | while (1) { |
cathy101 | 1:6926cb0de2b0 | 27 | |
cathy101 | 1:6926cb0de2b0 | 28 | if(pc.readable()) { |
cathy101 | 1:6926cb0de2b0 | 29 | pc.gets(buffer, sizeof(buffer)); |
cathy101 | 1:6926cb0de2b0 | 30 | pc.printf("buffer : %s\n",buffer); |
cathy101 | 1:6926cb0de2b0 | 31 | if(buffer[0]!='\0'|| buffer[0]!='\r'|| buffer[0]!='\n'||buffer!='\0'){ |
cathy101 | 1:6926cb0de2b0 | 32 | int i=atoi(buffer); |
cathy101 | 1:6926cb0de2b0 | 33 | switch (i) { |
cathy101 | 1:6926cb0de2b0 | 34 | case I2C::MasterGeneralCall: |
cathy101 | 1:6926cb0de2b0 | 35 | //write(int address, const char *data, int length, bool repeated = false); |
cathy101 | 1:6926cb0de2b0 | 36 | Master.write(addr, msg_all, strlen(msg_all) + 1, false); // Includes null char |
cathy101 | 1:6926cb0de2b0 | 37 | printf("write msg_all G: %s\n", msg_all); |
cathy101 | 1:6926cb0de2b0 | 38 | break; |
cathy101 | 1:6926cb0de2b0 | 39 | case I2C::MasterWrite: |
cathy101 | 1:6926cb0de2b0 | 40 | Master.write(addr, msg, strlen(msg) + 1, false); |
cathy101 | 1:6926cb0de2b0 | 41 | printf("write msg to A: %s\n", msg); |
cathy101 | 1:6926cb0de2b0 | 42 | break; |
cathy101 | 1:6926cb0de2b0 | 43 | case I2C::MasterRead: |
cathy101 | 1:6926cb0de2b0 | 44 | Master.read(addr,slave_buffer, 20); |
cathy101 | 1:6926cb0de2b0 | 45 | printf("Read A: %s\n", slave_buffer); |
cathy101 | 1:6926cb0de2b0 | 46 | break; |
cathy101 | 1:6926cb0de2b0 | 47 | } |
cathy101 | 1:6926cb0de2b0 | 48 | |
cathy101 | 1:6926cb0de2b0 | 49 | } |
cathy101 | 1:6926cb0de2b0 | 50 | for(int i = 0; i < 10; i++) buffer[i] = 0; // Clear buffer |
cathy101 | 1:6926cb0de2b0 | 51 | } |
mbed_official | 0:f76c26307f9a | 52 | } |
mbed_official | 0:f76c26307f9a | 53 | } |