Manchester code (phase encoding) library.

Dependents:   Manchester_Transmitter Manchester_Receiver

Manchester code (phase encoding) library

It implements Manchester code according to both IEEE 802.3 and G.E. Thomas' conventions.

  • A '0' is expressed by a high-to-low transition, a '1' by low-to-high transition in the IEEE 802.3 convention. The reverse is true in the G.E. Thomas' convention.
  • The transitions which signify '0' or '1' occur at the midpoint of a period.
  • Transitions at the start of a period are overhead and don't signify data.
  • Least significant bit is sent first
  • There are synchronization pulses (the number can be set) at the begin of transmission

    Select a convention to be used by commenting or uncommenting the line below in the Manchester.h header file.

Manchester.h

#define G_E_THOMAS 1

The IEEE 802.3 convention is used by default.

A Manchester encoded message (using G.E. Thomas' convention), with one sync pulse in the preamble, carrying four bytes:

/media/uploads/hudakz/manchester01.png

ACKNOWLEDGEMENT: The code in this library was based on this article published by Robert Guastella.

Import programManchester_Transmitter

Manchester transmitter demo.


Import programManchester_Receiver

Manchester receiver demo.

NOTE: To perform a simple test (without radio modules) connect the txPin on transmitter board to the rxPin on the receiver board and make sure that grounds are also connected one another.

Committer:
hudakz
Date:
Sun May 21 19:17:28 2017 +0000
Revision:
5:3b2c7e9fda3f
Parent:
2:de778df5892c
Child:
6:7454ad91f714
Added IEEE 802.3 convention.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:d5c75b0e5708 1 /*
hudakz 0:d5c75b0e5708 2 ******************************************************************************
hudakz 0:d5c75b0e5708 3 * @file Manchester.h
hudakz 0:d5c75b0e5708 4 * @author Zoltan Hudak
hudakz 2:de778df5892c 5 * @version
hudakz 0:d5c75b0e5708 6 * @date 16-May-2017
hudakz 1:11292d238e50 7 * @brief Manchester code for mbed
hudakz 0:d5c75b0e5708 8 ******************************************************************************
hudakz 0:d5c75b0e5708 9 * @attention
hudakz 0:d5c75b0e5708 10 *
hudakz 0:d5c75b0e5708 11 * <h2><center>&copy; COPYRIGHT(c) 2017 Zoltan Hudak <hudakz@outlook.com>
hudakz 0:d5c75b0e5708 12 *
hudakz 0:d5c75b0e5708 13 * All rights reserved.
hudakz 0:d5c75b0e5708 14
hudakz 0:d5c75b0e5708 15 This program is free software: you can redistribute it and/or modify
hudakz 0:d5c75b0e5708 16 it under the terms of the GNU General Public License as published by
hudakz 0:d5c75b0e5708 17 the Free Software Foundation, either version 3 of the License, or
hudakz 0:d5c75b0e5708 18 (at your option) any later version.
hudakz 0:d5c75b0e5708 19
hudakz 0:d5c75b0e5708 20 This program is distributed in the hope that it will be useful,
hudakz 0:d5c75b0e5708 21 but WITHOUT ANY WARRANTY; without even the implied warranty of
hudakz 0:d5c75b0e5708 22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
hudakz 0:d5c75b0e5708 23 GNU General Public License for more details.
hudakz 0:d5c75b0e5708 24
hudakz 0:d5c75b0e5708 25 You should have received a copy of the GNU General Public License
hudakz 0:d5c75b0e5708 26 along with this program. If not, see <http://www.gnu.org/licenses/>.
hudakz 0:d5c75b0e5708 27 */
hudakz 2:de778df5892c 28
hudakz 5:3b2c7e9fda3f 29 /*
hudakz 5:3b2c7e9fda3f 30 This library implements Manchester code according to both IEEE 802.3
hudakz 5:3b2c7e9fda3f 31 and G.E. Thomas' convention.
hudakz 5:3b2c7e9fda3f 32 • A '0' is expressed by a high-to-low transition, a '1' by low-to-high transition
hudakz 5:3b2c7e9fda3f 33 in the IEEE 802.3 convention. The reverse is true in the G.E. Thomas' convention.
hudakz 5:3b2c7e9fda3f 34 • The transitions which signify 0 or 1 occur at the midpoint of a period.
hudakz 5:3b2c7e9fda3f 35 • Transitions at the start of a period are overhead and don't signify data.
hudakz 5:3b2c7e9fda3f 36 • Least significant bit is sent first
hudakz 5:3b2c7e9fda3f 37 • There is one synchronization pulse at the begin of transmission
hudakz 5:3b2c7e9fda3f 38
hudakz 5:3b2c7e9fda3f 39 The IEEE 802.3 convention is used by default.
hudakz 5:3b2c7e9fda3f 40 Select a convention to be used by commenting or uncommenting
hudakz 5:3b2c7e9fda3f 41 the line "#define G_E_THOMAS 1" below.
hudakz 5:3b2c7e9fda3f 42 */
hudakz 5:3b2c7e9fda3f 43
hudakz 0:d5c75b0e5708 44 #ifndef MANCHESTER_H
hudakz 0:d5c75b0e5708 45 #define MANCHESTER_H
hudakz 0:d5c75b0e5708 46
hudakz 0:d5c75b0e5708 47 #include "mbed.h"
hudakz 0:d5c75b0e5708 48 #include "ManchesterMsg.h"
hudakz 0:d5c75b0e5708 49
hudakz 5:3b2c7e9fda3f 50
hudakz 5:3b2c7e9fda3f 51 //Uncomment the following line to use G.E.Thomas' convention
hudakz 5:3b2c7e9fda3f 52 //#define G_E_THOMAS 1
hudakz 5:3b2c7e9fda3f 53
hudakz 0:d5c75b0e5708 54 class Manchester
hudakz 0:d5c75b0e5708 55 {
hudakz 0:d5c75b0e5708 56 enum State
hudakz 0:d5c75b0e5708 57 {
hudakz 0:d5c75b0e5708 58 IDLE,
hudakz 0:d5c75b0e5708 59 SYNCH_START,
hudakz 0:d5c75b0e5708 60 SYNCH_NEXT,
hudakz 0:d5c75b0e5708 61 SYNCH_END,
hudakz 0:d5c75b0e5708 62 SETUP,
hudakz 0:d5c75b0e5708 63 TRANSITION,
hudakz 0:d5c75b0e5708 64 COMPLETE,
hudakz 0:d5c75b0e5708 65 LISTEN,
hudakz 0:d5c75b0e5708 66 DECODE,
hudakz 0:d5c75b0e5708 67 ERROR
hudakz 0:d5c75b0e5708 68 };
hudakz 2:de778df5892c 69
hudakz 0:d5c75b0e5708 70 public:
hudakz 0:d5c75b0e5708 71 Manchester
hudakz 0:d5c75b0e5708 72 (
hudakz 1:11292d238e50 73 PinName txPin, /* transmitter pin name */
hudakz 1:11292d238e50 74 PinName rxPin, /* receiver pin name */
hudakz 5:3b2c7e9fda3f 75 uint32_t speed = 1200, /* speed in bits per second */
hudakz 5:3b2c7e9fda3f 76 uint8_t tol = 25 /* pulse width tolerance (+/-) in % */
hudakz 0:d5c75b0e5708 77 );
hudakz 2:de778df5892c 78 ~Manchester(void) { }
hudakz 0:d5c75b0e5708 79 void transmit(ManchesterMsg& msg);
hudakz 0:d5c75b0e5708 80 bool receive(ManchesterMsg& msg);
hudakz 2:de778df5892c 81
hudakz 0:d5c75b0e5708 82 private:
hudakz 0:d5c75b0e5708 83 DigitalOut _tx; // transmitter line
hudakz 0:d5c75b0e5708 84 InterruptIn _rx; // receiver line
hudakz 0:d5c75b0e5708 85 Ticker _txTicker; // transmitter ticker
hudakz 2:de778df5892c 86 Timeout _timeout; // timeout (watchdog)
hudakz 0:d5c75b0e5708 87 uint32_t _midBitTime; // mid-bit time [us]
hudakz 1:11292d238e50 88 uint32_t _minPulseWidth; // minimum pulse width [us]
hudakz 1:11292d238e50 89 uint32_t _maxPulseWidth; // maximum pulse width [us]
hudakz 0:d5c75b0e5708 90 State _state; // state
hudakz 0:d5c75b0e5708 91 char* _data; // data array
hudakz 2:de778df5892c 92 size_t _len; // data length in bytes
hudakz 2:de778df5892c 93 size_t _maxLen; // maximum length
hudakz 2:de778df5892c 94
hudakz 0:d5c75b0e5708 95 void transmission(void);
hudakz 0:d5c75b0e5708 96 void reception(void);
hudakz 2:de778df5892c 97 void txTimeout(void);
hudakz 0:d5c75b0e5708 98 void rxTimeout(void);
hudakz 0:d5c75b0e5708 99 };
hudakz 0:d5c75b0e5708 100 #endif // MANCHESTER_H
hudakz 0:d5c75b0e5708 101
hudakz 2:de778df5892c 102