Pages: [1]   Go Down
Author Topic: Help debug a function (with comments and output)  (Read 390 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all.

I am working on creating some error detection for comm over an rf link.  There are two functions in the code below.  One checks to see if there are an odd number of 1's in byte to be sent.  If there are, it sets the MSB of the byte to a one for a parity. (only ASCII will be sent, so the MSB should always be a 0.)

The second function is for the receiver side to check to see if a) the 7 bits of the byte needed a parity and b) if its there if needed.  I dont know if i am just tired or what, but for some reason i cannot get the checkParity function to return the proper, true or false value.  However, the other parts seem to be functioning right (i.e. determining if the parity is needed and is there.)

I tried to include comments and an easily readable printout of what is happening at each part.  Any help is much appreciated.

Thank you

Code:
byte buff[8] =  "abcdefg"; //This is the buffer we are working with.
byte buff2[8] =  "abcdefg"; //This is the same buffer, just for printing results to the screen.



void setup()
{
  Serial.begin(9600);
}

void loop()
{

  addParity(buff); //Add parity to the MSB of each ascii byte if needed
  for(int k = 0; k < sizeof(buff) - 1; k++) //display
  {
    //This shows the binary of each byte with the parity bit if it should be added
    Serial.print("The binary of ");
    Serial.print((char)buff2[k]);
    Serial.print(" with parity is ");
    Serial.print(buff[k], BIN);
    checkParity(buff[k]);
  
  }

  delay(5000);
}

void addParity(byte array[])
//Inputs: an 8 element byte array
//Outputs: None
//This function takes an array of characters, and adds a parity bit if needed to the MSB
{
 
  for(int j = 0; j < sizeof(buff) - 1; j++) //for each character in the buffer
  {
    //taken from http://graphics.stanford.edu/~seander/bithacks.html
    boolean parity =   (((array[j] * 0x0101010101010101ULL) & 0x8040201008040201ULL) % 0x1FF) & 1;
    if(parity == true)
    {
      //if the ascii character requires a parity, flip the MSB (all ascii have MSB of 0 to start)
      bitSet(array[j],7);
    
    }

  }
}

boolean checkParity(byte tehbyte)
//Inputs: an 8 bit unsigned byte
//Outputs: True if byte requires a parity bit and has one
//This function takes a single 8 bit unsigned byet (an ascii character), checks to see if a parity bit should have been added, and ch
//cks to see if its there
{  
  byte temp = tehbyte & B01111111; //first strip off the parity if it is there
  Serial.print("  7-bit notation is "); //display the origianl binary value
  Serial.print(temp, BIN);
  boolean parity = (((temp * 0x0101010101010101ULL) & 0x8040201008040201ULL) % 0x1FF) & 1;
  if(parity == true)
  {
    //if it was supposed to have a parity, say so
    Serial.print("  and it require a parity.");
  }
  else
  {
    //if not, say so
    Serial.print("  and it didn't require parity.");
  }

  if(tehbyte & B10000000 == B10000000 && parity == true) //did it have a parity and require one?
  {
    Serial.println("  The function returned true.");
    return true;  
  }
  else if(tehbyte & B10000000 == B00000000 && parity == false) //was it missing a parity but didnt require one?
  {      
    Serial.println("  The function returned true.");
    return true;
  }
  else
  {
    Serial.println("  The function returned false."); //would fail if it had one and didnt require it, or vice versa (indicates bit error in transmission)
    return false;
  }
}









Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have resolved the issue.
Logged

Pages: [1]   Go Up
Jump to: