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/SPISlave.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_SPISLAVE_H |
hayama | 0:da22b0b4395a | 17 | #define MBED_SPISLAVE_H |
hayama | 0:da22b0b4395a | 18 | |
hayama | 0:da22b0b4395a | 19 | #include "platform.h" |
hayama | 0:da22b0b4395a | 20 | |
hayama | 0:da22b0b4395a | 21 | #if DEVICE_SPISLAVE |
hayama | 0:da22b0b4395a | 22 | |
hayama | 0:da22b0b4395a | 23 | #include "spi_api.h" |
hayama | 0:da22b0b4395a | 24 | |
hayama | 0:da22b0b4395a | 25 | namespace mbed { |
hayama | 0:da22b0b4395a | 26 | |
hayama | 0:da22b0b4395a | 27 | /** A SPI slave, used for communicating with a SPI Master device |
hayama | 0:da22b0b4395a | 28 | * |
hayama | 0:da22b0b4395a | 29 | * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz |
hayama | 0:da22b0b4395a | 30 | * |
hayama | 0:da22b0b4395a | 31 | * Example: |
hayama | 0:da22b0b4395a | 32 | * @code |
hayama | 0:da22b0b4395a | 33 | * // Reply to a SPI master as slave |
hayama | 0:da22b0b4395a | 34 | * |
hayama | 0:da22b0b4395a | 35 | * #include "mbed.h" |
hayama | 0:da22b0b4395a | 36 | * |
hayama | 0:da22b0b4395a | 37 | * SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel |
hayama | 0:da22b0b4395a | 38 | * |
hayama | 0:da22b0b4395a | 39 | * int main() { |
hayama | 0:da22b0b4395a | 40 | * device.reply(0x00); // Prime SPI with first reply |
hayama | 0:da22b0b4395a | 41 | * while(1) { |
hayama | 0:da22b0b4395a | 42 | * if(device.receive()) { |
hayama | 0:da22b0b4395a | 43 | * int v = device.read(); // Read byte from master |
hayama | 0:da22b0b4395a | 44 | * v = (v + 1) % 0x100; // Add one to it, modulo 256 |
hayama | 0:da22b0b4395a | 45 | * device.reply(v); // Make this the next reply |
hayama | 0:da22b0b4395a | 46 | * } |
hayama | 0:da22b0b4395a | 47 | * } |
hayama | 0:da22b0b4395a | 48 | * } |
hayama | 0:da22b0b4395a | 49 | * @endcode |
hayama | 0:da22b0b4395a | 50 | */ |
hayama | 0:da22b0b4395a | 51 | class SPISlave { |
hayama | 0:da22b0b4395a | 52 | |
hayama | 0:da22b0b4395a | 53 | public: |
hayama | 0:da22b0b4395a | 54 | |
hayama | 0:da22b0b4395a | 55 | /** Create a SPI slave connected to the specified pins |
hayama | 0:da22b0b4395a | 56 | * |
hayama | 0:da22b0b4395a | 57 | * Pin Options: |
hayama | 0:da22b0b4395a | 58 | * (5, 6, 7i, 8) or (11, 12, 13, 14) |
hayama | 0:da22b0b4395a | 59 | * |
hayama | 0:da22b0b4395a | 60 | * mosi or miso can be specfied as NC if not used |
hayama | 0:da22b0b4395a | 61 | * |
hayama | 0:da22b0b4395a | 62 | * @param mosi SPI Master Out, Slave In pin |
hayama | 0:da22b0b4395a | 63 | * @param miso SPI Master In, Slave Out pin |
hayama | 0:da22b0b4395a | 64 | * @param sclk SPI Clock pin |
hayama | 0:da22b0b4395a | 65 | * @param ssel SPI chip select pin |
hayama | 0:da22b0b4395a | 66 | * @param name (optional) A string to identify the object |
hayama | 0:da22b0b4395a | 67 | */ |
hayama | 0:da22b0b4395a | 68 | SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel); |
hayama | 0:da22b0b4395a | 69 | |
hayama | 0:da22b0b4395a | 70 | /** Configure the data transmission format |
hayama | 0:da22b0b4395a | 71 | * |
hayama | 0:da22b0b4395a | 72 | * @param bits Number of bits per SPI frame (4 - 16) |
hayama | 0:da22b0b4395a | 73 | * @param mode Clock polarity and phase mode (0 - 3) |
hayama | 0:da22b0b4395a | 74 | * |
hayama | 0:da22b0b4395a | 75 | * @code |
hayama | 0:da22b0b4395a | 76 | * mode | POL PHA |
hayama | 0:da22b0b4395a | 77 | * -----+-------- |
hayama | 0:da22b0b4395a | 78 | * 0 | 0 0 |
hayama | 0:da22b0b4395a | 79 | * 1 | 0 1 |
hayama | 0:da22b0b4395a | 80 | * 2 | 1 0 |
hayama | 0:da22b0b4395a | 81 | * 3 | 1 1 |
hayama | 0:da22b0b4395a | 82 | * @endcode |
hayama | 0:da22b0b4395a | 83 | */ |
hayama | 0:da22b0b4395a | 84 | void format(int bits, int mode = 0); |
hayama | 0:da22b0b4395a | 85 | |
hayama | 0:da22b0b4395a | 86 | /** Set the spi bus clock frequency |
hayama | 0:da22b0b4395a | 87 | * |
hayama | 0:da22b0b4395a | 88 | * @param hz SCLK frequency in hz (default = 1MHz) |
hayama | 0:da22b0b4395a | 89 | */ |
hayama | 0:da22b0b4395a | 90 | void frequency(int hz = 1000000); |
hayama | 0:da22b0b4395a | 91 | |
hayama | 0:da22b0b4395a | 92 | /** Polls the SPI to see if data has been received |
hayama | 0:da22b0b4395a | 93 | * |
hayama | 0:da22b0b4395a | 94 | * @returns |
hayama | 0:da22b0b4395a | 95 | * 0 if no data, |
hayama | 0:da22b0b4395a | 96 | * 1 otherwise |
hayama | 0:da22b0b4395a | 97 | */ |
hayama | 0:da22b0b4395a | 98 | int receive(void); |
hayama | 0:da22b0b4395a | 99 | |
hayama | 0:da22b0b4395a | 100 | /** Retrieve data from receive buffer as slave |
hayama | 0:da22b0b4395a | 101 | * |
hayama | 0:da22b0b4395a | 102 | * @returns |
hayama | 0:da22b0b4395a | 103 | * the data in the receive buffer |
hayama | 0:da22b0b4395a | 104 | */ |
hayama | 0:da22b0b4395a | 105 | int read(void); |
hayama | 0:da22b0b4395a | 106 | |
hayama | 0:da22b0b4395a | 107 | /** Fill the transmission buffer with the value to be written out |
hayama | 0:da22b0b4395a | 108 | * as slave on the next received message from the master. |
hayama | 0:da22b0b4395a | 109 | * |
hayama | 0:da22b0b4395a | 110 | * @param value the data to be transmitted next |
hayama | 0:da22b0b4395a | 111 | */ |
hayama | 0:da22b0b4395a | 112 | void reply(int value); |
hayama | 0:da22b0b4395a | 113 | |
hayama | 0:da22b0b4395a | 114 | protected: |
hayama | 0:da22b0b4395a | 115 | spi_t _spi; |
hayama | 0:da22b0b4395a | 116 | |
hayama | 0:da22b0b4395a | 117 | int _bits; |
hayama | 0:da22b0b4395a | 118 | int _mode; |
hayama | 0:da22b0b4395a | 119 | int _hz; |
hayama | 0:da22b0b4395a | 120 | }; |
hayama | 0:da22b0b4395a | 121 | |
hayama | 0:da22b0b4395a | 122 | } // namespace mbed |
hayama | 0:da22b0b4395a | 123 | |
hayama | 0:da22b0b4395a | 124 | #endif |
hayama | 0:da22b0b4395a | 125 | |
hayama | 0:da22b0b4395a | 126 | #endif |