go_away

Author Topic: Manchester Encoding Code  (Read 2361 times)

0 Members and 1 Guest are viewing this topic.

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Manchester Encoding Code
« on: September 15, 2009, 09:20:43 PM »
Manchester encoding : http://en.wikipedia.org/wiki/Manchester_code
Idea is that when you do RF, you don't want to send bits that are unbalanced or dont have an equal number of 1s and 0s 0000001 and 11111111 are unbalanced, and so is 00001111 because its not really balanced). Manchester encoding takes one byte and makes it into two bytes which have a pretty much balanced bits.  I've done experiments with this, and I can definitely agree that it works!
Example: You want to transmit decimal 35 ( binary 00100011 ). Manchester encoded that gives you two bytes ; decimal 89(binary 01011001) and decimal 90(binary 01011010).
I've been using RF a lot lately at my company , Narobo , and wrote up some code to do the Manchester encoding for you. Best of all, my code doesnt have any pesky bitwise ORs and bit shifters ( which are a pain in the butt to follow when reading code)
Code: [Select]
Syntax:  int_variable = Encode_MSB( number )     // most significant bits ( the 4 bits to the leftmost of the number)
int_variable = Encode_LSB( number )  // least significant bits ( the 4 bits all the way to the right of the number)

I'll comment and post some code on decoding Manchester-encoded bits too.
Enjoy!



Code: [Select]
unsigned char Encode_MSB(unsigned char original_number) {
int encoded=0;

if( (original_number - 128) >= 0 ) {  // if there is a 1 by the 7th bit, 128
original_number = original_number - 128;
encoded = encoded + 128; // set the 7th bit, 128, of the Encoded MSB as 1
}

else { // if there is a 0 by the 7th bit, 128
encoded = encoded + 64; // set the 6th bit, 64, of the Encoded MSB as 1
}

if( (original_number - 64) >= 0 ) {  // if there is a 1 by the 6th bit, 64
original_number = original_number - 64;
encoded = encoded + 32; // set the 5th bit, 32, of the Encoded MSB as 1
}

else {  // if there is a 0 by the 6th bit, 64
encoded = encoded + 16; // set the 4th bit, 16, of the Encoded MSB as 1
}

if( (original_number - 32) >= 0 ) {  // if there is a 1 by the 5th bit, 32
original_number = original_number - 32;
encoded = encoded + 8; // set the 3th bit, 8, of the Encoded MSB as 1
}

else { // if there is a 0 by the 5th bit, 32
encoded = encoded + 4; // set the 2th bit, 4, of the Encoded MSB as 1
}

if( (original_number - 16) >= 0 ) {  // if there is a 1 by the 4th bit, 16
original_number = original_number - 16;
encoded = encoded + 2; // set the 1th bit, 2, of the Encoded MSB as 1
}

else { // if there is a 0 by the 4th bit, 16
encoded = encoded + 1; // set the 0th bit, 0, of the Encoded MSB as 1
}


return encoded;
}


unsigned char Encode_LSB(unsigned char original_number) {
int encoded=0;

if( (original_number - 128) >= 0 ) {  // if there is a 1 by the 7th bit, 128
original_number = original_number - 128;
}

if( (original_number - 64) >= 0 ) {  // if there is a 1 by the 6th bit, 64
original_number = original_number - 64;
}


if( (original_number - 32) >= 0 ) {  // if there is a 1 by the 5th bit, 32  1
original_number = original_number - 32;
}


if( (original_number - 16) >= 0 ) {  // if there is a 1 by the 4th bit, 16
original_number = original_number - 16;
}

//// --- NOW ITS DOWN TO THE LAST 4 BITS -----

if( (original_number - 8) >= 0 ) {  // if there is a 1 by the 3rd bit, 8
original_number = original_number - 8;
encoded = encoded + 128; // set the 7th bit, 128, of the Encoded LSB as 1    
}

else { // if there is a 0 by the 3rd bit, 8
encoded = encoded + 64; // set the 6th bit, 64, of the Encoded LSB as 1  
}

if( (original_number - 4) >= 0 ) {  // if there is a 1 by the 2nd bit, 4          
original_number = original_number - 4;
encoded = encoded + 32; // set the 5th bit, 32, of the Encoded LSB as 1
}

else {  // if there is a 0 by the 2nd bit, 4
encoded = encoded + 16; // set the 4th bit, 16, of the Encoded LSB as 1    
}

if( (original_number - 2) >= 0 ) {  // if there is a 1 by the 1st bit, 2          
original_number = original_number - 2;
encoded = encoded + 8; // set the 3th bit, 8, of the Encoded LSB as 1
}

else { // if there is a 0 by the 1st bit, 2
encoded = encoded + 4; // set the 2nd bit, 4, of the Encoded LSB as 1    
}

if( (original_number - 1) >= 0 ) {  // if there is a 1 by the 1st bit, 0
original_number = original_number - 1;
encoded = encoded + 2; // set the 1st bit, 2, of the Encoded LSB as 1
}

else { // if there is a 0 by the 0th bit, 0
encoded = encoded + 1; // set the 0th bit, 0, of the Encoded LSB as 1
}


return encoded;
}


« Last Edit: September 15, 2009, 09:21:54 PM by airman00 »
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Manchester Encoding Code
« Reply #1 on: September 16, 2009, 07:21:27 PM »
Wrote up the decoding code and commented it. I'll do testing later tonight.

Code: [Select]
unsigned char Decode_MSB(unsigned char encoded_number) {
int decoded =0;

// care about 7th, 5th,3rd,and 1st bits

if( (encoded_number - 128) >= 0 ) {  // if there is a 1 by the 7th bit, - 128
encoded_number = encoded_number - 128;
decoded = decoded + 128; // set the 7th bit, 128, of the Encoded MSB as 1
}

else {  // there was a 0 by the 7th bit, so obviously 6th one is a 1 , - 64
encoded_number = encoded_number - 64;
}

if( (encoded_number - 32) >= 0 ) {  // if there is a 1 by the 5th bit, - 32
encoded_number = encoded_number - 32;
decoded = decoded + 64; // set the 6th bit, 64, of the Encoded MSB as 1
}

else {  // there was a 0 by the 7th bit, so obviously 4th one is a 1 , - 16
encoded_number = encoded_number - 16;
}


if( (encoded_number - 8) >= 0 ) {  // if there is a 1 by the 3th bit, - 8
encoded_number = encoded_number - 8;
decoded = decoded + 32; // set the 5th bit, 32, of the Encoded MSB as 1
}

else {  // there was a 0 by the 7th bit, so obviously 2th one is a 1 , - 4
encoded_number = encoded_number - 4;
}

if( (encoded_number - 2) >= 0 ) {  // if there is a 1 by the 1sth bit, - 2
encoded_number = encoded_number - 2;
decoded = decoded + 16; // set the 4th bit, 32, of the Encoded MSB as 1
}

else {  // there was a 0 by the 7th bit, so obviously 0th one is a 1 , - 1
encoded_number = encoded_number - 1;
}

return decoded;
}

unsigned char Decode_LSB(unsigned char encoded_number) {
int decoded =0;

// care about 7th, 5th,3rd,and 1st bits

if( (encoded_number - 128) >= 0 ) {  // if there is a 1 by the 7th bit, - 128
encoded_number = encoded_number - 128;
decoded = decoded + 8; // set the 3rd bit, 16, of the Encoded MSB as 1
}

else {  // there was a 0 by the 7th bit, so obviously 6th one is a 1 , - 64
encoded_number = encoded_number - 64;
}

if( (encoded_number - 32) >= 0 ) {  // if there is a 1 by the 5th bit, - 32
encoded_number = encoded_number - 32;
decoded = decoded + 4; // set the 2nd bit, 8, of the Encoded MSB as 1
}

else {  // there was a 0 by the 7th bit, so obviously 4th one is a 1 , - 16
encoded_number = encoded_number - 16;
}


if( (encoded_number - 8) >= 0 ) {  // if there is a 1 by the 3th bit, - 8
encoded_number = encoded_number - 8;
decoded = decoded + 2; // set the 1st bit, 32, of the Encoded MSB as 1
}

else {  // there was a 0 by the 7th bit, so obviously 2th one is a 1 , - 4
encoded_number = encoded_number - 4;
}

if( (encoded_number - 2) >= 0 ) {  // if there is a 1 by the 1sth bit, - 2
encoded_number = encoded_number - 2;
decoded = decoded + 1; // set the 0th bit, 32, of the Encoded MSB as 1
}

else {  // there was a 0 by the 7th bit, so obviously 0th one is a 1 , - 1
encoded_number = encoded_number - 1;
}

return decoded;
}

Syntax: Decode_MSB( number ) 
Decode_LSB( number )

So for example lets take the decimal number 35.

On the transmitter device you would encode by doing this
Code: [Select]
char_original = 35;
encoded_MSB = Encode_MSB(char_original);
encoded_LSB = Encode_LSB(char_original);

Then you transmit your bytes over UART or whatever you do. I use UART so I do
Code: [Select]
uartSendByte(encoded_MSB);
uartSendByte(encoded_LSB);

Now on the receiver end you do this
Code: [Select]
encoded_MSB  = uartGetByte();
encoded_LSB = uartGetByte()
original_value = Decode_MSB(encoded_MSB) + Decode_LSB(encoded_LSB);
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: Manchester Encoding Code
« Reply #2 on: September 20, 2009, 07:19:49 PM »
Hmmmm I always wondered how RF stuff handled constant 1's or 0's . . .

So its simple. A 0 becomes a 01, and a 1 becomes a 10.

Well I guess not that simple, as the UART isn't designed for it . . . thanks for your code! (I probably will never use it, but its still very useful)

I guess the disadvantage would be your baud rate is halved . . .

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Manchester Encoding Code
« Reply #3 on: September 20, 2009, 07:32:33 PM »
I've done test with manchester encoding on and with it off - there is a definite increase of range and reliability when its encoded.
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

 


Get Your Ad Here

data_list