ManchesterCode

Swordfish

// encode...
function Encode(pValue as byte) as word
   dim Index as byte
   Result = 0
   for index = 0 to 7
      if pValue.0 = 0 then
         Result.14 = 1
      else
         Result.15 = 1
      endif
      if Index < 7 then
	      Result = Result >> 2
      endif
      pValue = pValue >> 1
   next
end function

// decode...
function Decode(pEncodedValue as word, byref pDecodedValue as byte) as boolean
   dim Index as byte
   dim DecodedValue as byte   
   Result = true

   for Index = 0 to 7
      if pEncodedValue.1 = 0 and pEncodedValue.0 = 1 then
         DecodedValue.7 = 0
      elseif pEncodedValue.1 = 1 and pEncodedValue.0 = 0 then
	     DecodedValue.7 = 1
	  else
	     Result = false
		 exit
      endif

      pEncodedValue = pEncodedValue >> 2
      if Index < 7 then
         DecodedValue = DecodedValue >> 1
      endif
    next
    pDecodedValue = DecodedValue
end function

dim Value as byte
dim Result as word
dim Timer as TMR1L.AsWord

Timer = 0  
PIE1.0 = 1
T1CON.0 = 1
Result = Encode(210)
Decode(Result, Value)
T1CON.0 = 0

// output results...
end

CCS C

// encode....
int16 Encode(int8 pValue)
{
   int8 Index;
   int16 Result;

   result = 0;
   for(Index=0;Index<8;Index++)
   {
      if (bit_test(pValue,0) == 0)
         bit_set(Result,14);
      else
         bit_set(Result,15);

      if (Index < 7)
         Result = Result >> 2;

      pValue = pValue >> 1;
   } 
   return(Result);
} 

// decode...
int1 Decode(int16 pEncodedValue, int8 *pDecodedValue)
{
   int8 Index;
   int8 DecodedValue;

   for(Index=0;Index<8;Index++)
   {
      if ((bit_test(pEncodedValue,1) == 0) && (bit_test(pEncodedValue,0) == 1))
         bit_clear(DecodedValue,7);
      else if ((bit_test(pEncodedValue,1) == 1) && (bit_test(pEncodedValue,0) == 0)) 
         bit_set(DecodedValue,7);
      else   
         return(0);

      pEncodedValue = pEncodedValue >> 2;
      if (Index < 7)
         DecodedValue = DecodedValue >> 1;   
   }
   *pDecodedValue = DecodedValue;
   return(1);
}

#byte PIE1 = 0x0F9D
#byte T1CON = 0x0FCD
#bit TMR1IE = PIE1.0
#bit TMR1ON = T1CON.0

void main() {
   int8 Value;
   int16 Result;

   set_timer1(0);
   TMR1IE = 1;
   TMR1ON = 1;
   Result = Encode(210);
   Decode(Result, &Value);
   TMR1ON = 0;

   // output results...
}

MikroElectronika BASIC

' Encode...
sub function Encode(dim pValue as byte) as word
   dim Index as byte
   Result = 0
   for index = 0 to 7
      if pValue.0 = 0 then
         Result.14 = 1
      else
         Result.15 = 1
      end if
      if Index < 7 then
	      Result = Result >> 2
     end if
      pValue = pValue >> 1
   next index
end sub

' Decode
sub function Decode(dim pEncodedValue as word, dim byref pDecodedValue as byte) as boolean
   dim Index as byte
   dim DecodedValue as byte
   Result = true

   for Index = 0 to 7
      if (pEncodedValue.1 = 0) and (pEncodedValue.0 = 1) then
         DecodedValue.7 = 0
      else
         if (pEncodedValue.1 = 1) and (pEncodedValue.0 = 0) then
	          DecodedValue.7 = 1
	       else
	          Result = false
		        exit
        end if
      end if
      pEncodedValue = pEncodedValue >> 2
      if Index < 7 then
         DecodedValue = DecodedValue >> 1
       end if
    next Index
    pDecodedValue = DecodedValue
end sub

main:
   dim TMR1 as word absolute 0x0FCE
   dim Value as byte
   dim Result as word

   TMR1 = 0
   SetBit(PIE1,0)
   SetBit(T1CON,0)
   Result = Encode(210)
   Decode(Result, Value)
   ClearBit(T1CON,0)

   ' output results...
end.

PROTON BASIC

Symbol True = 1
Symbol False = 0

Dim Index As Byte
Dim Value As Byte
Dim EncodedValue As Word
Dim DecodedValue As Byte
Dim Success As Bit
Dim Timer As TMR1L.Word

Timer = 0
PIE1.0 = 1  
T1CON.0 = 1 
Value = 210
GoSub Encode ' pass Value, return in EncodedValue
GoSub Decode ' pass pEncodedValue, return DecodedValue
T1CON.0 = 0 

' output results...

End

' encode...
Encode:
   EncodedValue = 0
   For Index = 0 To 7
      If Value.0 = 0 Then
         EncodedValue.14 = 1
      Else
         EncodedValue.15 = 1
      EndIf
      If Index < 7 Then
	      EncodedValue = EncodedValue >> 2
      EndIf
      Value = Value >> 1
   Next
Return

' decode...
Decode:
   Success = True
   For Index = 0 To 7
      If EncodedValue.1 = 0 And EncodedValue.0 = 1 Then
         DecodedValue.7 = 0
      ElseIf EncodedValue.1 = 1 And EncodedValue.0 = 0 Then
	     DecodedValue.7 = 1
	  Else
	     Success = False
         Return
      EndIf
      EncodedValue = EncodedValue >> 2
      If Index < 7 Then
         DecodedValue = DecodedValue >> 1
      EndIf
    Next
Return

PicBASIC PRO

Symbol True = 1
Symbol False = 0

Index var Byte
Value var Byte
EncodedValue var Word
DecodedValue var Byte
Success var Bit
TimerValue var word

TMR1L = 0
TMR1H = 0
PIE1.0 = 1 
T1CON.0 = 1 
Value = 210
GoSub Encode ' pass Value, return in EncodedValue
GoSub Decode ' pass EncodedValue, return DecodedValue
T1CON.0 = 0 

' output results...

End

' encode...
Encode:
   EncodedValue = 0
   For Index = 0 To 7
      If Value.0 = 0 Then
         EncodedValue.14 = 1
      Else
         EncodedValue.15 = 1
      EndIf
      If Index < 7 Then
	      EncodedValue = EncodedValue >> 2
      EndIf
      Value = Value >> 1
   Next
Return

' decode...
Decode:
   Success = True
   For Index = 0 To 7
      If EncodedValue.1 = 0 And EncodedValue.0 = 1 Then
         DecodedValue.7 = 0
      Else 
         If EncodedValue.1 = 1 And EncodedValue.0 = 0 Then
	        DecodedValue.7 = 1
	     Else
	        Success = False
            Return
         EndIf
      endif   
      EncodedValue = EncodedValue >> 2
      If Index < 7 Then
         DecodedValue = DecodedValue >> 1
      EndIf
    Next
Return