Hsieh JenYun
/
I2C_Master
a
Fork of I2C_Master by
Diff: main.cpp
- Revision:
- 2:3d64970eeb96
- Parent:
- 1:6926cb0de2b0
- Child:
- 3:996d0d74fb11
--- a/main.cpp Fri Oct 20 05:06:15 2017 +0000 +++ b/main.cpp Wed Oct 24 06:03:27 2018 +0000 @@ -1,53 +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 +DigitalOut led_red(LED_RED); +InterruptIn sw2(SW2); +I2C i2c( PTB1, PTB0); -I2C Master(PC_9, PA_8); -Serial pc(USBTX,USBRX); -const int addr = 0xA0; +Serial serialPort( PTE0, PTE1); + +int Right_ADDR = 0x14<<1; +int Left_ADDR = 0x15<<1; -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 - } +void sw2_release(void) +{ + led_red = !led_red; + serialPort.printf("On-board button SW2 was released.\n"); +} +void Driving_Right(void) +{ + i2c.start(); + i2c.write(Right_ADDR); + i2c.write(0); + i2c.write(0xe2); + i2c.write(0); + i2c.stop(); +} +void Driving_Left(void) +{ + i2c.start(); + i2c.write(Left_ADDR); + i2c.write(0); + i2c.write(0xe2); + i2c.write(0); + i2c.stop(); +} + +int main() +{ + + sw2.rise(&sw2_release); + serialPort.baud(115200); + serialPort.printf("WPI Serial Port Started\n"); + + i2c.frequency(100000); + wait_ms(100); + + Driving_Right(); + wait_ms(100); + Driving_Left(); + + while (true) { + } -} \ No newline at end of file +}