Hsieh JenYun
/
I2C_Master
a
Fork of I2C_Master by
main.cpp@4:186241a75818, 2018-10-24 (annotated)
- Committer:
- Tanakacool
- Date:
- Wed Oct 24 06:16:00 2018 +0000
- Revision:
- 4:186241a75818
- Parent:
- 3:996d0d74fb11
- Child:
- 5:221ce6ba655c
V3
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 | |
Tanakacool | 2:3d64970eeb96 | 3 | DigitalOut led_red(LED_RED); |
Tanakacool | 2:3d64970eeb96 | 4 | InterruptIn sw2(SW2); |
Tanakacool | 2:3d64970eeb96 | 5 | I2C i2c( PTB1, PTB0); |
mbed_official | 0:f76c26307f9a | 6 | |
Tanakacool | 2:3d64970eeb96 | 7 | Serial serialPort( PTE0, PTE1); |
Tanakacool | 2:3d64970eeb96 | 8 | |
Tanakacool | 4:186241a75818 | 9 | char Rmotobuffer[3]; |
Tanakacool | 4:186241a75818 | 10 | char Lmotobuffer[3]; |
Tanakacool | 3:996d0d74fb11 | 11 | |
Tanakacool | 2:3d64970eeb96 | 12 | int Right_ADDR = 0x14<<1; |
Tanakacool | 3:996d0d74fb11 | 13 | int Left_ADDR = 0x15<<1; |
mbed_official | 0:f76c26307f9a | 14 | |
Tanakacool | 2:3d64970eeb96 | 15 | void sw2_release(void) |
Tanakacool | 2:3d64970eeb96 | 16 | { |
Tanakacool | 2:3d64970eeb96 | 17 | led_red = !led_red; |
Tanakacool | 2:3d64970eeb96 | 18 | serialPort.printf("On-board button SW2 was released.\n"); |
Tanakacool | 2:3d64970eeb96 | 19 | } |
Tanakacool | 2:3d64970eeb96 | 20 | void Driving_Right(void) |
Tanakacool | 2:3d64970eeb96 | 21 | { |
Tanakacool | 2:3d64970eeb96 | 22 | i2c.start(); |
Tanakacool | 2:3d64970eeb96 | 23 | i2c.write(Right_ADDR); |
Tanakacool | 2:3d64970eeb96 | 24 | i2c.write(0); |
Tanakacool | 4:186241a75818 | 25 | i2c.write(Lmotobuffer[1]); |
Tanakacool | 4:186241a75818 | 26 | i2c.write(Lmotobuffer[2]); |
Tanakacool | 2:3d64970eeb96 | 27 | i2c.stop(); |
Tanakacool | 2:3d64970eeb96 | 28 | } |
Tanakacool | 2:3d64970eeb96 | 29 | void Driving_Left(void) |
Tanakacool | 2:3d64970eeb96 | 30 | { |
Tanakacool | 2:3d64970eeb96 | 31 | i2c.start(); |
Tanakacool | 2:3d64970eeb96 | 32 | i2c.write(Left_ADDR); |
Tanakacool | 2:3d64970eeb96 | 33 | i2c.write(0); |
Tanakacool | 4:186241a75818 | 34 | i2c.write(Rmotobuffer[1]); |
Tanakacool | 4:186241a75818 | 35 | i2c.write(Rmotobuffer[2]); |
Tanakacool | 2:3d64970eeb96 | 36 | i2c.stop(); |
Tanakacool | 2:3d64970eeb96 | 37 | } |
Tanakacool | 2:3d64970eeb96 | 38 | |
Tanakacool | 2:3d64970eeb96 | 39 | int main() |
Tanakacool | 2:3d64970eeb96 | 40 | { |
Tanakacool | 2:3d64970eeb96 | 41 | |
Tanakacool | 2:3d64970eeb96 | 42 | sw2.rise(&sw2_release); |
Tanakacool | 2:3d64970eeb96 | 43 | serialPort.baud(115200); |
Tanakacool | 2:3d64970eeb96 | 44 | serialPort.printf("WPI Serial Port Started\n"); |
Tanakacool | 3:996d0d74fb11 | 45 | |
Tanakacool | 3:996d0d74fb11 | 46 | i2c.frequency(100000); |
Tanakacool | 2:3d64970eeb96 | 47 | wait_ms(100); |
Tanakacool | 3:996d0d74fb11 | 48 | |
Tanakacool | 3:996d0d74fb11 | 49 | int motorOutPut=2000; |
Tanakacool | 4:186241a75818 | 50 | Rmotobuffer[0]=0x00; |
Tanakacool | 4:186241a75818 | 51 | Rmotobuffer[1]= motorOutPut>>8; |
Tanakacool | 4:186241a75818 | 52 | Rmotobuffer[2]=motorOutPut; |
Tanakacool | 4:186241a75818 | 53 | Lmotobuffer[0]=0x00; |
Tanakacool | 4:186241a75818 | 54 | Lmotobuffer[1]= motorOutPut>>8; |
Tanakacool | 4:186241a75818 | 55 | Lmotobuffer[2]=motorOutPut; |
Tanakacool | 3:996d0d74fb11 | 56 | |
Tanakacool | 2:3d64970eeb96 | 57 | Driving_Right(); |
Tanakacool | 2:3d64970eeb96 | 58 | wait_ms(100); |
Tanakacool | 2:3d64970eeb96 | 59 | Driving_Left(); |
Tanakacool | 3:996d0d74fb11 | 60 | |
Tanakacool | 2:3d64970eeb96 | 61 | while (true) { |
Tanakacool | 2:3d64970eeb96 | 62 | |
mbed_official | 0:f76c26307f9a | 63 | } |
Tanakacool | 2:3d64970eeb96 | 64 | } |