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