Hsieh JenYun
/
I2C_Master
a
Fork of I2C_Master by
Diff: main.cpp
- Revision:
- 1:6926cb0de2b0
- Parent:
- 0:f76c26307f9a
- Child:
- 2:3d64970eeb96
diff -r f76c26307f9a -r 6926cb0de2b0 main.cpp --- a/main.cpp Thu Feb 14 17:24:48 2013 +0000 +++ b/main.cpp Fri Oct 20 05:06:15 2017 +0000 @@ -1,25 +1,53 @@ #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 i2c(p28, p27); - -const int addr = 0x90; +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) { - cmd[0] = 0x01; - cmd[1] = 0x00; - i2c.write(addr, cmd, 2); - - wait(0.5); - - cmd[0] = 0x00; - i2c.write(addr, cmd, 1); - i2c.read(addr, cmd, 2); - - float tmp = (float((cmd[0]<<8)|cmd[1]) / 256.0); - printf("Temp = %.2f\n", tmp); + + 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 + } } } \ No newline at end of file