Manchester Code Library

The mikroPascal PRO for PIC32 provides a library for handling Manchester coded signals. The Manchester code is a code in which data and clock signals are combined to form a single self-synchronizing data stream; each encoded bit contains a transition at the midpoint of a bit period, the direction of transition determines whether the bit is 0 or 1; the second half is the true bit value and the first half is the complement of the true bit value (as shown in the figure below).

Manchester signal format

  Important :

External dependencies of Manchester Code Library

The following variables must be defined in all projects using Manchester Code Library: Description: Example:
var MANRXPIN : sbit; sfr; atomic; external; Receive line. var MANRXPIN : sbit at RF0_bit;
var MANTXPIN : sbit; sfr; atomic; external; Transmit line. var MANTXPIN : sbit at LATF1_bit;
var MANRXPIN_Direction : sbit; sfr; atomic; external; Direction of the Receive pin. var MANRXPIN_Direction : sbit at TRISF0_bit;
var MANTXPIN_Direction : sbit; sfr; atomic; external; Direction of the Transmit pin. var MANTXPIN_Direction : sbit at TRISF1_bit;

Library Routines

The following routines are for the internal use by compiler only:

Man_Receive_Init

Prototype

function Man_Receive_Init() : word;

Description

The function configures Receiver pin. After that, the function performs synchronization procedure in order to retrieve baud rate out of the incoming signal.

Parameters

None.

Returns

  • 0 - if initialization and synchronization were successful.
  • 1 - upon unsuccessful synchronization.
  • 255 - upon user abort.

Requires Global variables :
  • MANRXPIN : Receive line
  • MANRXPIN_Direction : Direction of the receive pin
must be defined before using this function.
Example
// Initialize Receiver
var MANRXPIN : sbit at RF0_bit;
var MANRXPIN_Direction : sbit at TRISF0_bit;
...
Man_Receive_Init();
Notes

In case of multiple persistent errors on reception, the user should call this routine once again or Man_Synchro routine to enable synchronization.

Man_Receive

Prototype

function Man_Receive(var error : word) : byte;

Description

The function extracts one byte from incoming signal.

Parameters
  • error: error flag. If signal format does not match the expected, the error flag will be set to non-zero.
Returns

A byte read from the incoming signal.

Requires

To use this function, the user must prepare the MCU for receiving. See Man_Receive_Init routines.

Example
var data_, error : word;
...
error := 0;
data_ := 0;
data_ := Man_Receive(error);
if (error <> 0) then
begin 
	// error handling 
end;
Notes

None.

Man_Send_Init

Prototype

procedure Man_Send_Init();

Description

The function configures Transmitter pin.

Parameters

None.

Returns

Nothing.

Requires Global variables :
  • MANTXPIN : Transmit line
  • MANTXPIN_Direction : Direction of the transmit pin
must be defined before using this function.
Example
// Initialize Transmitter:
var MANTXPIN : sbit at LATF1_bit;
var MANTXPIN_Direction : sbit at TRISF1_bit;
...
Man_Send_Init();
Notes

None.

Man_Send

Prototype

procedure Man_Send(tr_data : byte);

Description

Sends one byte.

Parameters
  • tr_data: data to be sent
Returns

Nothing.

Requires

To use this function, the user must prepare the MCU for sending. See Man_Send_Init routine.

Example
var msg : byte;
...
Man_Send(msg);
Notes

Baud rate used is 500 bps.

Man_Synchro

Prototype

function Man_Synchro(): word;

Description

Measures half of the manchester bit length with 10us resolution.

Parameters

None.

Returns
  • 0 - if synchronization was not successful.
  • Half of the manchester bit length, given in multiples of 10us - upon successful synchronization.
Requires

To use this function, you must first prepare the MCU for receiving. See Man_Receive_Init.

Example
var man__half_bit_len : word;
...
man__half_bit_len := Man_Synchro();
Notes

None.

Man_Break

Prototype

procedure Man_Break();

Description

Man_Receive is blocking routine and it can block the program flow. Call this routine from interrupt to unblock the program execution. This mechanism is similar to WDT.

Parameters

None.

Returns

Nothing.

Requires

Nothing.

Example
var data1, error, counter : byte;

procedure Timer1Int(); org IVT_ADDR_T1INTERRUPT;
begin
  counter := 0;
  if (counter >= 20) then
    begin    
      Man_Break();
      counter := 0;                // reset counter
    end
  else
    Inc(counter);                  // increment counter

  T1IF_bit := 0;                 // Clear Timer1 overflow interrupt flag
end;

begin

  ...

  if (Man_Receive_Init() = 0)
    begin
    ...
    end;

  ...

  // try Man_Receive with blocking prevention mechanism
  IPC0   := IPC0 or 0x1000;       // Interrupt priority level = 1 
  T1IE_bit := 1;                 // Enable Timer1 interrupts
  T1CON := 0x8030;               // Timer1 ON, internal clock FCY, prescaler 1:256
  
  data1 := Man_Receive(@error);
  T1IE_bit := 0;                 // Disable Timer1 interrupts
end.
Notes

Interrupts should be disabled before using Manchester routines again (see note at the top of this page).

Library Example

The following code is code for the Manchester receiver, it shows how to use the Manchester Library for receiving data:

Copy Code To ClipboardCopy Code To Clipboard
program Manchester_Receiver;

// LCD module connections
var LCD_RS : sbit  at LATB2_bit;
var LCD_EN : sbit  at LATB3_bit;
var LCD_D4 : sbit  at LATB4_bit;
var LCD_D5 : sbit  at LATB5_bit;
var LCD_D6 : sbit  at LATB6_bit;
var LCD_D7 : sbit  at LATB7_bit;

var LCD_RS_Direction : sbit at TRISB2_bit;
var LCD_EN_Direction : sbit at TRISB3_bit;
var LCD_D4_Direction : sbit at TRISB4_bit;
var LCD_D5_Direction : sbit at TRISB5_bit;
var LCD_D6_Direction : sbit at TRISB6_bit;
var LCD_D7_Direction : sbit at TRISB7_bit;
// End LCD module connections


// Manchester module connections
var MANRXPIN : sbit at RF0_bit;
    MANRXPIN_Direction : sbit at TRISF0_bit;
    MANTXPIN : sbit at LATF1_bit;
    MANTXPIN_Direction : sbit at TRISF1_bit;
// End Manchester module connections


var error : word;
    ErrorCount, chr_counter, byte_rcvd : byte;

  begin

  ErrorCount := 0;
  chr_counter := 0;
  CHECON := 0x32;
  AD1PCFG := 0xFFFF;                          // Configure AN pins as digital I/O

  Lcd_Init();                                // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                       // Clear LCD display

  Man_Receive_Init();                        // Initialize Receiver

  while TRUE do                              // Endless loop
    begin
      Lcd_Cmd(_LCD_FIRST_ROW);               // Move cursor to the 1st row

      while TRUE do                          // Wait for the "start" byte
        begin
          byte_rcvd := Man_Receive(error);   // Attempt byte receive
          if (byte_rcvd = 0x0B) then         // "Start" byte, see Transmitter example
             break;                          // We got the starting sequence
          if (error <> 0) then               // Exit so we do not loop forever
            break;
        end;

      repeat
        begin
          byte_rcvd := Man_Receive(error);   // Attempt byte receive
          if (error <> 0) then               // If error occured
            begin
              Lcd_Chr_CP('?');               // Write question mark on LCD
              Inc(ErrorCount);               // Update error counter
              if (ErrorCount > 20) then      // In case of multiple errors
                begin
                  Man_Synchro();             // Try to synchronize again
                  //Man_Receive_Init();      // Alternative, try to Initialize Receiver again
                  ErrorCount := 0;           // Reset error counter
                end;
            end

          else                               // No error occured
            begin
              if (byte_rcvd <> 0x0E) then    // If "End" byte was received(see Transmitter example)
                begin                        // do not write anymore received byte on LCD
                  Lcd_Chr_CP(byte_rcvd);     // else write character on LCD
                  Inc(chr_counter);          // Counts how many chars have been written on LCD
                  if (chr_counter = 25) then // If there were more then 25 characters
                    begin                    // synchronization is off
                      Lcd_Cmd(_LCD_CLEAR);   // Clear the LCD of garbled communication
                      Man_Synchro();         // Try to synchronize again
                    end;
                end
              else
                chr_counter := 0;            // reset chr_counter
            end;
          Delay_ms(25);
        end;
      until (byte_rcvd = 0x0E);
    end;                                     // If "End" byte was received exit do loop
end.

The following code is code for the Manchester transmitter, it shows how to use the Manchester Library for transmitting data:

Copy Code To ClipboardCopy Code To Clipboard
program Manchester_Transmitter;

// Manchester module connections
var MANRXPIN : sbit at RF0_bit;
    MANRXPIN_Direction : sbit at TRISF0_bit;
    MANTXPIN : sbit at LATF1_bit;
    MANTXPIN_Direction : sbit at TRISF1_bit;
// End Manchester module connections

var index, character : byte;
    s1 : array[17] of char;

  begin
    s1 := 'mikroElektronika';
    CHECON := 0x32;
    AD1PCFG := 0xFFFF;                // Configure AN pins as digital I/O

    Man_Send_Init();                 // Initialize transmitter

    while TRUE do                    // Endless loop
      begin
        Man_Send(0x0B);              // Send "start" byte
        Delay_ms(100);               // Wait for a while

        character := s1[0];          // Take first char from string
        index := 0;                  // Initialize index variable
        while (character <> 0) do    // String ends with zero
          begin
            Man_Send(character);     // Send character
            Delay_ms(90);            // Wait for a while
            Inc(index);              // Increment index variable
            character := s1[index];  // Take next char from string
          end;
        Man_Send(0x0E);              // Send "end" byte
        Delay_ms(1000);
      end;
  end.

Connection Example

Simple Transmitter connection

Simple Transmitter connection


Simple Receiver connection

Simple Receiver connection

Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code