yuto kawamura
/
George_Slave_BOTHMOVE
right and left move at the same time
KondoServoLibrary/SerialHalfDuplex_HM/SerialHalfDuplex.h@13:678870d8f851, 2019-05-15 (annotated)
- Committer:
- yuto17320508
- Date:
- Wed May 15 10:19:13 2019 +0000
- Revision:
- 13:678870d8f851
- Parent:
- 0:411ab20ce87d
l
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eri | 0:411ab20ce87d | 1 | /* mbed Microcontroller Library - SerialHalfDuplex |
eri | 0:411ab20ce87d | 2 | * Copyright (c) 2010-2011 ARM Limited. All rights reserved. |
eri | 0:411ab20ce87d | 3 | */ |
eri | 0:411ab20ce87d | 4 | #ifndef MBED_SERIALHALFDUPLEX_H |
eri | 0:411ab20ce87d | 5 | #define MBED_SERIALHALFDUPLEX_H |
eri | 0:411ab20ce87d | 6 | |
eri | 0:411ab20ce87d | 7 | #include "platform.h" |
eri | 0:411ab20ce87d | 8 | |
eri | 0:411ab20ce87d | 9 | #if DEVICE_SERIAL |
eri | 0:411ab20ce87d | 10 | |
eri | 0:411ab20ce87d | 11 | #include "Serial.h" |
eri | 0:411ab20ce87d | 12 | |
eri | 0:411ab20ce87d | 13 | namespace mbed { |
eri | 0:411ab20ce87d | 14 | |
eri | 0:411ab20ce87d | 15 | /** A serial port (UART) for communication with other devices using |
eri | 0:411ab20ce87d | 16 | * Half-Duplex, allowing transmit and receive on a single |
eri | 0:411ab20ce87d | 17 | * shared transmit and receive line. Only one end should be transmitting |
eri | 0:411ab20ce87d | 18 | * at a time. |
eri | 0:411ab20ce87d | 19 | * |
eri | 0:411ab20ce87d | 20 | * Both the tx and rx pin should be defined, and wired together. |
eri | 0:411ab20ce87d | 21 | * This is in addition to them being wired to the other serial |
eri | 0:411ab20ce87d | 22 | * device to allow both read and write functions to operate. |
eri | 0:411ab20ce87d | 23 | * |
eri | 0:411ab20ce87d | 24 | * For Simplex and Full-Duplex Serial communication, see Serial() |
eri | 0:411ab20ce87d | 25 | * |
eri | 0:411ab20ce87d | 26 | * Example: |
eri | 0:411ab20ce87d | 27 | * @code |
eri | 0:411ab20ce87d | 28 | * // Send a byte to a second HalfDuplex device, and read the response |
eri | 0:411ab20ce87d | 29 | * |
eri | 0:411ab20ce87d | 30 | * #include "mbed.h" |
eri | 0:411ab20ce87d | 31 | * |
eri | 0:411ab20ce87d | 32 | * // p9 and p10 should be wired together to form "a" |
eri | 0:411ab20ce87d | 33 | * // p28 and p27 should be wired together to form "b" |
eri | 0:411ab20ce87d | 34 | * // p9/p10 should be wired to p28/p27 as the Half Duplex connection |
eri | 0:411ab20ce87d | 35 | * |
eri | 0:411ab20ce87d | 36 | * SerialHalfDuplex a(p9, p10); |
eri | 0:411ab20ce87d | 37 | * SerialHalfDuplex b(p28, p27); |
eri | 0:411ab20ce87d | 38 | * |
eri | 0:411ab20ce87d | 39 | * void b_rx() { // second device response |
eri | 0:411ab20ce87d | 40 | * b.putc(b.getc() + 4); |
eri | 0:411ab20ce87d | 41 | * } |
eri | 0:411ab20ce87d | 42 | * |
eri | 0:411ab20ce87d | 43 | * int main() { |
eri | 0:411ab20ce87d | 44 | * b.attach(&b_rx); |
eri | 0:411ab20ce87d | 45 | * for (int c = 'A'; c < 'Z'; c++) { |
eri | 0:411ab20ce87d | 46 | * a.putc(c); |
eri | 0:411ab20ce87d | 47 | * printf("sent [%c]\n", c); |
eri | 0:411ab20ce87d | 48 | * wait(0.5); // b should respond |
eri | 0:411ab20ce87d | 49 | * if (a.readable()) { |
eri | 0:411ab20ce87d | 50 | * printf("received [%c]\n", a.getc()); |
eri | 0:411ab20ce87d | 51 | * } |
eri | 0:411ab20ce87d | 52 | * } |
eri | 0:411ab20ce87d | 53 | * } |
eri | 0:411ab20ce87d | 54 | * @endcode |
eri | 0:411ab20ce87d | 55 | */ |
eri | 0:411ab20ce87d | 56 | class SerialHalfDuplex : public Serial { |
eri | 0:411ab20ce87d | 57 | |
eri | 0:411ab20ce87d | 58 | public: |
eri | 0:411ab20ce87d | 59 | |
eri | 0:411ab20ce87d | 60 | /** Create a half-duplex serial port, connected to the specified transmit |
eri | 0:411ab20ce87d | 61 | * and receive pins. |
eri | 0:411ab20ce87d | 62 | * |
eri | 0:411ab20ce87d | 63 | * These pins should be wired together, as well as to the target device |
eri | 0:411ab20ce87d | 64 | * |
eri | 0:411ab20ce87d | 65 | * @param tx Transmit pin |
eri | 0:411ab20ce87d | 66 | * @param rx Receive pin |
eri | 0:411ab20ce87d | 67 | * |
eri | 0:411ab20ce87d | 68 | * @note |
eri | 0:411ab20ce87d | 69 | * Either tx or rx may be specified as NC if unused |
eri | 0:411ab20ce87d | 70 | */ |
eri | 0:411ab20ce87d | 71 | SerialHalfDuplex(PinName tx=p13, PinName rx=p14, const char *name=NULL); |
eri | 0:411ab20ce87d | 72 | |
eri | 0:411ab20ce87d | 73 | protected: |
eri | 0:411ab20ce87d | 74 | |
eri | 0:411ab20ce87d | 75 | virtual int _putc(int c); |
eri | 0:411ab20ce87d | 76 | virtual int _getc(void); |
eri | 0:411ab20ce87d | 77 | |
eri | 0:411ab20ce87d | 78 | }; // End class SerialHalfDuplex |
eri | 0:411ab20ce87d | 79 | |
eri | 0:411ab20ce87d | 80 | } // End namespace |
eri | 0:411ab20ce87d | 81 | |
eri | 0:411ab20ce87d | 82 | #endif |
eri | 0:411ab20ce87d | 83 | |
eri | 0:411ab20ce87d | 84 | #endif |