Created
December 17, 2015 11:19
-
-
Save shirish47/d21b896570a8fccbd9c3 to your computer and use it in GitHub Desktop.
This is simple implementation of Gray to binary and Binary to Gray for a single Byte of Data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uint8_t Binary2Gray(uint8_t data) | |
{ | |
int n_data=(data>>1); | |
n_data=(data ^ n_data); | |
return n_data; | |
} | |
uint8_t Gray2Binary(uint8_t data) | |
{ | |
byte b[8]={0,0,0,0,0,0,0,0}; | |
int8_t i=0; | |
for(i=0;i<8;i++) | |
{ | |
b[i]=(data & (1<<i))>>i; | |
} | |
uint8_t n_data=0; | |
n_data= (b[7]<<7); | |
for(i=6;i>=0;i--) | |
{ | |
if( (b[i+1]+b[i])==0 || (b[i+1]+b[i])==2 ) | |
{ | |
n_data|=(0<<i); | |
}else if( (b[i+1]+b[i])==1 || (b[i+1]+b[i])==3) | |
{ | |
n_data|=(1<<i); | |
} | |
} | |
return n_data; | |
} | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(4800); | |
Serial.println(" Gray binary conversion test"); | |
uint8_t i=0; | |
uint8_t gc=0; | |
for (i=0;i<=20;i++) | |
{ | |
Serial.print("GRAY CODE of BINARY ");Serial.print(i,BIN);Serial.print(" is "); | |
gc=Binary2Gray(i); | |
Serial.println(gc,BIN); | |
} | |
gc=0x00; | |
for (i=0;i<=20;i++) | |
{ | |
Serial.print("BINARY of GRAY CODE ");Serial.print(i,BIN);Serial.print(" is "); | |
gc=Gray2Binary(i); | |
Serial.println(gc,BIN); | |
} | |
} | |
void loop() { | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment