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.

Revision:
7:afd0ee36dcd1
Parent:
6:7454ad91f714
Child:
8:c1b5893191fe
diff -r 7454ad91f714 -r afd0ee36dcd1 Manchester.cpp
--- a/Manchester.cpp	Mon May 22 09:35:26 2017 +0000
+++ b/Manchester.cpp	Sun Sep 03 09:04:18 2017 +0000
@@ -27,17 +27,17 @@
  */
 
 /*
-   This library 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 
+   This library 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 is one synchronization pulse at the begin of transmission 
-   
+   • 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 is one synchronization pulse at the begin of transmission
+
    The IEEE 802.3 convention is used by default.
-   Select a convention to be used by commenting or uncommenting 
+   Select a convention to be used by commenting or uncommenting
    the line "#define G_E_THOMAS 1" in the Manchester.h header file.
  */
 
@@ -55,12 +55,12 @@
  */
 Manchester::Manchester
     (
-        PinName     txPin, 
-        PinName     rxPin, 
-        uint32_t    speed, /* = 1200 bps */ 
-        uint8_t     tol    /* = (+/-)25% */ 
-    ) : 
-    _tx(txPin), 
+        PinName     txPin,
+        PinName     rxPin,
+        uint32_t    speed, /* = 1200 bps */
+        uint8_t     tol    /* = (+/-)25% */
+    ) :
+    _tx(txPin),
     _rx(rxPin) {
     _state = IDLE;
     _midBitTime = 1000000 / speed / 2;  // mid-bit time [us]
@@ -131,7 +131,7 @@
         _tx = 1;            // bring line high for end of sych pulse
 #else
         _tx = 0;            // pull line low for end of sych pulse
-#endif      
+#endif
         byteIndex = 0;
         encodeByte = _data[byteIndex];
         bitIndex = 0;
@@ -143,7 +143,7 @@
         _tx = encodeByte & 0x01;    // setup for next bit to transmit
 #else
         _tx = !(encodeByte & 0x01); // setup for next bit to transmit
-#endif      
+#endif
         _state = TRANSITION;
         break;
 
@@ -172,8 +172,8 @@
 #ifdef G_E_THOMAS
         _tx = 1;    // transmission is complete, bring line high
 #else
-        _tx = 0;    // transmission is complete, pull line low       
-#endif      
+        _tx = 0;    // transmission is complete, pull line low
+#endif
         _state = IDLE;
         break;
 
@@ -188,8 +188,8 @@
  * @brief   ISR handling 'transmission timeout'
  * @note    Called when transmitter is stuck.
  *          Signals 'end of transmission' by setting state to IDLE
- * @param   
- * @retval  
+ * @param
+ * @retval
  */
 void Manchester::txTimeout(void) {
     _timeout.detach();
@@ -198,7 +198,7 @@
 
 /**
  * @brief   Receives message
- * @note    Waits until a message is received or 'receive timeout' occured
+ * @note    Waits until a message is received or 'reception timeout' occured
  * @param   msg   Container to store the received message
  * @retval  true  On success
  *          false Otherwise
@@ -210,9 +210,7 @@
     _maxLen = msg.maxLen();
     _state = LISTEN;
 
-    core_util_critical_section_enter();
     _rx.enable_irq();
-    core_util_critical_section_exit();
 
     do {
         core_util_critical_section_enter();
@@ -220,9 +218,7 @@
         core_util_critical_section_exit();
     } while(!rxFinished);
 
-    core_util_critical_section_enter();
     _rx.disable_irq();
-    core_util_critical_section_exit();
 
     if(_state == ERROR) {
         msg.len = 0;
@@ -257,7 +253,7 @@
         if(_rx == 0)
 #else
         if(_rx == 1)
-#endif        
+#endif
             _state = SYNCH_START;
         else
             _state = ERROR;  // It isn't a synch pulse => error
@@ -314,12 +310,12 @@
 }
 
 /**
- * @brief   ISR handling 'receive timeout'
+ * @brief   ISR handling 'reception timeout'
  * @note    Called when receiver line is idle longer than limit.
- *          Signals 'end of transmission' by setting state to IDLE
+ *          Signals 'end of reception' by setting state to IDLE
  *          or 'timeout error' by setting state to ERROR.
- * @param   
- * @retval  
+ * @param
+ * @retval
  */
 void Manchester::rxTimeout(void) {
     _timeout.detach();
@@ -329,9 +325,7 @@
 #else
     if((_state == DECODE) && (_rx == 0))
 #endif
-        _state = IDLE;  // End of transmission
+        _state = IDLE;  // Reception successful
     else
-        _state = ERROR; // Incomplete transmission
+        _state = ERROR; // Reception incomplete
 }
-
-