mbed robotracer for education.
Robotracer (line follower robot ) using stepper motor.
/media/uploads/hayama/mbedrobotracer-manual-english.pdf
/media/uploads/hayama/mbedrobotracer-manual-japanese.pdf
/media/uploads/hayama/eagle-design-robotracer.zip
movie -> https://www.youtube.com/watch?v=INwun8gSds4
(for competition->) https://www.youtube.com/watch?v=l_gP2pUt4w0
mbed/I2C.h@0:da22b0b4395a, 2013-06-19 (annotated)
- Committer:
- hayama
- Date:
- Wed Jun 19 10:00:41 2013 +0000
- Revision:
- 0:da22b0b4395a
mbed robotracer for education ver 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hayama | 0:da22b0b4395a | 1 | /* mbed Microcontroller Library |
hayama | 0:da22b0b4395a | 2 | * Copyright (c) 2006-2013 ARM Limited |
hayama | 0:da22b0b4395a | 3 | * |
hayama | 0:da22b0b4395a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
hayama | 0:da22b0b4395a | 5 | * you may not use this file except in compliance with the License. |
hayama | 0:da22b0b4395a | 6 | * You may obtain a copy of the License at |
hayama | 0:da22b0b4395a | 7 | * |
hayama | 0:da22b0b4395a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
hayama | 0:da22b0b4395a | 9 | * |
hayama | 0:da22b0b4395a | 10 | * Unless required by applicable law or agreed to in writing, software |
hayama | 0:da22b0b4395a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
hayama | 0:da22b0b4395a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
hayama | 0:da22b0b4395a | 13 | * See the License for the specific language governing permissions and |
hayama | 0:da22b0b4395a | 14 | * limitations under the License. |
hayama | 0:da22b0b4395a | 15 | */ |
hayama | 0:da22b0b4395a | 16 | #ifndef MBED_I2C_H |
hayama | 0:da22b0b4395a | 17 | #define MBED_I2C_H |
hayama | 0:da22b0b4395a | 18 | |
hayama | 0:da22b0b4395a | 19 | #include "platform.h" |
hayama | 0:da22b0b4395a | 20 | |
hayama | 0:da22b0b4395a | 21 | #if DEVICE_I2C |
hayama | 0:da22b0b4395a | 22 | |
hayama | 0:da22b0b4395a | 23 | #include "i2c_api.h" |
hayama | 0:da22b0b4395a | 24 | |
hayama | 0:da22b0b4395a | 25 | namespace mbed { |
hayama | 0:da22b0b4395a | 26 | |
hayama | 0:da22b0b4395a | 27 | /** An I2C Master, used for communicating with I2C slave devices |
hayama | 0:da22b0b4395a | 28 | * |
hayama | 0:da22b0b4395a | 29 | * Example: |
hayama | 0:da22b0b4395a | 30 | * @code |
hayama | 0:da22b0b4395a | 31 | * // Read from I2C slave at address 0x62 |
hayama | 0:da22b0b4395a | 32 | * |
hayama | 0:da22b0b4395a | 33 | * #include "mbed.h" |
hayama | 0:da22b0b4395a | 34 | * |
hayama | 0:da22b0b4395a | 35 | * I2C i2c(p28, p27); |
hayama | 0:da22b0b4395a | 36 | * |
hayama | 0:da22b0b4395a | 37 | * int main() { |
hayama | 0:da22b0b4395a | 38 | * int address = 0x62; |
hayama | 0:da22b0b4395a | 39 | * char data[2]; |
hayama | 0:da22b0b4395a | 40 | * i2c.read(address, data, 2); |
hayama | 0:da22b0b4395a | 41 | * } |
hayama | 0:da22b0b4395a | 42 | * @endcode |
hayama | 0:da22b0b4395a | 43 | */ |
hayama | 0:da22b0b4395a | 44 | class I2C { |
hayama | 0:da22b0b4395a | 45 | |
hayama | 0:da22b0b4395a | 46 | public: |
hayama | 0:da22b0b4395a | 47 | enum RxStatus { |
hayama | 0:da22b0b4395a | 48 | NoData, |
hayama | 0:da22b0b4395a | 49 | MasterGeneralCall, |
hayama | 0:da22b0b4395a | 50 | MasterWrite, |
hayama | 0:da22b0b4395a | 51 | MasterRead |
hayama | 0:da22b0b4395a | 52 | }; |
hayama | 0:da22b0b4395a | 53 | |
hayama | 0:da22b0b4395a | 54 | enum Acknowledge { |
hayama | 0:da22b0b4395a | 55 | NoACK = 0, |
hayama | 0:da22b0b4395a | 56 | ACK = 1 |
hayama | 0:da22b0b4395a | 57 | }; |
hayama | 0:da22b0b4395a | 58 | |
hayama | 0:da22b0b4395a | 59 | /** Create an I2C Master interface, connected to the specified pins |
hayama | 0:da22b0b4395a | 60 | * |
hayama | 0:da22b0b4395a | 61 | * @param sda I2C data line pin |
hayama | 0:da22b0b4395a | 62 | * @param scl I2C clock line pin |
hayama | 0:da22b0b4395a | 63 | */ |
hayama | 0:da22b0b4395a | 64 | I2C(PinName sda, PinName scl); |
hayama | 0:da22b0b4395a | 65 | |
hayama | 0:da22b0b4395a | 66 | /** Set the frequency of the I2C interface |
hayama | 0:da22b0b4395a | 67 | * |
hayama | 0:da22b0b4395a | 68 | * @param hz The bus frequency in hertz |
hayama | 0:da22b0b4395a | 69 | */ |
hayama | 0:da22b0b4395a | 70 | void frequency(int hz); |
hayama | 0:da22b0b4395a | 71 | |
hayama | 0:da22b0b4395a | 72 | /** Read from an I2C slave |
hayama | 0:da22b0b4395a | 73 | * |
hayama | 0:da22b0b4395a | 74 | * Performs a complete read transaction. The bottom bit of |
hayama | 0:da22b0b4395a | 75 | * the address is forced to 1 to indicate a read. |
hayama | 0:da22b0b4395a | 76 | * |
hayama | 0:da22b0b4395a | 77 | * @param address 8-bit I2C slave address [ addr | 1 ] |
hayama | 0:da22b0b4395a | 78 | * @param data Pointer to the byte-array to read data in to |
hayama | 0:da22b0b4395a | 79 | * @param length Number of bytes to read |
hayama | 0:da22b0b4395a | 80 | * @param repeated Repeated start, true - don't send stop at end |
hayama | 0:da22b0b4395a | 81 | * |
hayama | 0:da22b0b4395a | 82 | * @returns |
hayama | 0:da22b0b4395a | 83 | * 0 on success (ack), |
hayama | 0:da22b0b4395a | 84 | * non-0 on failure (nack) |
hayama | 0:da22b0b4395a | 85 | */ |
hayama | 0:da22b0b4395a | 86 | int read(int address, char *data, int length, bool repeated = false); |
hayama | 0:da22b0b4395a | 87 | |
hayama | 0:da22b0b4395a | 88 | /** Read a single byte from the I2C bus |
hayama | 0:da22b0b4395a | 89 | * |
hayama | 0:da22b0b4395a | 90 | * @param ack indicates if the byte is to be acknowledged (1 = acknowledge) |
hayama | 0:da22b0b4395a | 91 | * |
hayama | 0:da22b0b4395a | 92 | * @returns |
hayama | 0:da22b0b4395a | 93 | * the byte read |
hayama | 0:da22b0b4395a | 94 | */ |
hayama | 0:da22b0b4395a | 95 | int read(int ack); |
hayama | 0:da22b0b4395a | 96 | |
hayama | 0:da22b0b4395a | 97 | /** Write to an I2C slave |
hayama | 0:da22b0b4395a | 98 | * |
hayama | 0:da22b0b4395a | 99 | * Performs a complete write transaction. The bottom bit of |
hayama | 0:da22b0b4395a | 100 | * the address is forced to 0 to indicate a write. |
hayama | 0:da22b0b4395a | 101 | * |
hayama | 0:da22b0b4395a | 102 | * @param address 8-bit I2C slave address [ addr | 0 ] |
hayama | 0:da22b0b4395a | 103 | * @param data Pointer to the byte-array data to send |
hayama | 0:da22b0b4395a | 104 | * @param length Number of bytes to send |
hayama | 0:da22b0b4395a | 105 | * @param repeated Repeated start, true - do not send stop at end |
hayama | 0:da22b0b4395a | 106 | * |
hayama | 0:da22b0b4395a | 107 | * @returns |
hayama | 0:da22b0b4395a | 108 | * 0 on success (ack), |
hayama | 0:da22b0b4395a | 109 | * non-0 on failure (nack) |
hayama | 0:da22b0b4395a | 110 | */ |
hayama | 0:da22b0b4395a | 111 | int write(int address, const char *data, int length, bool repeated = false); |
hayama | 0:da22b0b4395a | 112 | |
hayama | 0:da22b0b4395a | 113 | /** Write single byte out on the I2C bus |
hayama | 0:da22b0b4395a | 114 | * |
hayama | 0:da22b0b4395a | 115 | * @param data data to write out on bus |
hayama | 0:da22b0b4395a | 116 | * |
hayama | 0:da22b0b4395a | 117 | * @returns |
hayama | 0:da22b0b4395a | 118 | * '1' if an ACK was received, |
hayama | 0:da22b0b4395a | 119 | * '0' otherwise |
hayama | 0:da22b0b4395a | 120 | */ |
hayama | 0:da22b0b4395a | 121 | int write(int data); |
hayama | 0:da22b0b4395a | 122 | |
hayama | 0:da22b0b4395a | 123 | /** Creates a start condition on the I2C bus |
hayama | 0:da22b0b4395a | 124 | */ |
hayama | 0:da22b0b4395a | 125 | |
hayama | 0:da22b0b4395a | 126 | void start(void); |
hayama | 0:da22b0b4395a | 127 | |
hayama | 0:da22b0b4395a | 128 | /** Creates a stop condition on the I2C bus |
hayama | 0:da22b0b4395a | 129 | */ |
hayama | 0:da22b0b4395a | 130 | void stop(void); |
hayama | 0:da22b0b4395a | 131 | |
hayama | 0:da22b0b4395a | 132 | protected: |
hayama | 0:da22b0b4395a | 133 | void aquire(); |
hayama | 0:da22b0b4395a | 134 | |
hayama | 0:da22b0b4395a | 135 | i2c_t _i2c; |
hayama | 0:da22b0b4395a | 136 | static I2C *_owner; |
hayama | 0:da22b0b4395a | 137 | int _hz; |
hayama | 0:da22b0b4395a | 138 | }; |
hayama | 0:da22b0b4395a | 139 | |
hayama | 0:da22b0b4395a | 140 | } // namespace mbed |
hayama | 0:da22b0b4395a | 141 | |
hayama | 0:da22b0b4395a | 142 | #endif |
hayama | 0:da22b0b4395a | 143 | |
hayama | 0:da22b0b4395a | 144 | #endif |