New lower price for Axon II ($78) and Axon Mote ($58).

0 Members and 1 Guest are viewing this topic.

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)

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;}

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; }

char_original = 35;encoded_MSB = Encode_MSB(char_original);encoded_LSB = Encode_LSB(char_original);

uartSendByte(encoded_MSB);uartSendByte(encoded_LSB);

encoded_MSB = uartGetByte();encoded_LSB = uartGetByte()original_value = Decode_MSB(encoded_MSB) + Decode_LSB(encoded_LSB);