Hsieh JenYun
/
I2C_Master
a
Fork of I2C_Master by
main.cpp
- Committer:
- cathy101
- Date:
- 2017-10-20
- Revision:
- 1:6926cb0de2b0
- Parent:
- 0:f76c26307f9a
- Child:
- 2:3d64970eeb96
File content as of revision 1:6926cb0de2b0:
#include "mbed.h" #include "hal/i2c_api.h" #include "platform/SingletonPtr.h" #include "platform/PlatformMutex.h" //#include "platform/NonCopyable.h" #if DEVICE_I2C_ASYNCH #include "platform/CThunk.h" #include "hal/dma_api.h" #include "platform/FunctionPointer.h" #endif // Read temperature from LM75BD I2C Master(PC_9, PA_8); Serial pc(USBTX,USBRX); const int addr = 0xA0; int main() { char cmd[2]; char buffer[20]; char slave_buffer[20]; char msg[] = "Hi 0xA0"; char msg_all[] = "Slave!"; while (1) { if(pc.readable()) { pc.gets(buffer, sizeof(buffer)); pc.printf("buffer : %s\n",buffer); if(buffer[0]!='\0'|| buffer[0]!='\r'|| buffer[0]!='\n'||buffer!='\0'){ int i=atoi(buffer); switch (i) { case I2C::MasterGeneralCall: //write(int address, const char *data, int length, bool repeated = false); Master.write(addr, msg_all, strlen(msg_all) + 1, false); // Includes null char printf("write msg_all G: %s\n", msg_all); break; case I2C::MasterWrite: Master.write(addr, msg, strlen(msg) + 1, false); printf("write msg to A: %s\n", msg); break; case I2C::MasterRead: Master.read(addr,slave_buffer, 20); printf("Read A: %s\n", slave_buffer); break; } } for(int i = 0; i < 10; i++) buffer[i] = 0; // Clear buffer } } }