Thursday, August 25, 2011

Task 11 - bit-ANDing function

/*
 * Project:    BinaryANDing_621task11
 * Author:     Jane-Maree Howard
 * Date:         Wednesday 21/09/2011; modified Friday 07/10/2011
 * Platform:   Arduino 22
 * Purpose:    To demonstrate a function to bitwise AND a byte with B01
 -                   and output the result to the screen.
 -                   Tests in the main loop().
 * Operation: Inputs a byte via Serial comm & 

 -                   function taking a byte as parameter bitwise-ANDs it
 -                   with value B01
 -         Arduino Library: LIB03_BinaryAND - function BinaryAND(byte,byte)

 -                                    LIB04_KeypressInput  
 -             Declare:          byte bInbyte; int iKeyPress;
 -             Setup():          Serial comms @ 9600 baud; print heading              
 -             Loop():           Input a byte via Serial comm & output to SM as BIN;

 -                                   bitwise-AND byte with value B01 & output result to SM.                
 */
int     iKeyPress = 0;     // for incoming serial data 
byte    bInbyte;

void setup()  
{
  Serial.begin(9600);      //Serial @ 9600baud
  // print headings
  Serial.println("KEY-PRESS  BIN\t    AND b01"); 
}//end setup()

void loop()                    

  // send data only when you receive data
  iKeyPress = KeypressInput();       // LIB04_KeypressInput
  if (iKeyPress !=0)
  {
    Serial.print(iKeyPress, BYTE);   // print as ASCII character
    Serial.print("\t   ");           // print tabs
    bInbyte  = iKeyPress;            //
    Serial.print(bInbyte, BIN);      // print resultant byte as binary
    Serial.print("  ");              // print tab
    bInbyte  = BinaryAND(bInbyte, B01);   // LIB03_BinaryAND
    Serial.println(bInbyte, BIN);         // ..print resultant byte as binary
   }//if()  
}//end loop()
//END


At right is the Serial Monitor output showing what happens to a byte that is bitwise-ANDed with the value B01.

Only the LSB appears, as leading zeroes are ignored.



/*
 * Project:    LIB03_BinaryAND
 * Author:     Jane-Maree Howard
 * Date:         Friday 07/10/2011
 * Platform:   Arduino 22
 * Purpose:    To make a library function for bitwise ANDing 2 bytes
 * Operation: bitwise ANDs 2 parameter bytes & returns a third byte
 -              Declare:     NONE - MUST BE DECLARED IN CONJOINING SKETCH(ES)
 -              Setup():     NONE - MUST BE USED ONLY IN CONJOINING SKETCH(ES)
 -                              (used  only in testing)
 -              Procedure(): byte BinaryAND(byte, byte);     
 -              Loop():      NONE - MUST BE USED ONLY IN CONJOINING SKETCH(ES)
 -                              (used  only in testing)            
 */
byte BinaryAND(byte bB1, byte bB2)
{
  /*  bitwise ANDs 2 Bytes together & returns a separate Byte
      CALLED IN MAIN SKETCH  */
  byte bOutByte  = bB1 & bB2;
  return bOutByte;
}//BinaryAdd()
//END


/*
 * Project:     LIB04_KeypressInput
 * Author:     Jane-Maree Howard
 * Date:         Saturday 08/10/2011
 * Platform:   Arduino 22
 * Purpose:    To make a library function for inputting a key-press via Serial comm link
 * Operation:  inputs a key-press & returns an Integer
 -             Declare:     NONE - MUST BE DECLARED IN CONJOINING SKETCH(ES)
 -             Setup():     NONE - MUST BE USED ONLY IN CONJOINING SKETCH(ES)
 -                               (used  only in testing)
 -             Procedure(): int KeypressInput();  no parameters   
 -             Loop():      NONE - MUST BE USED ONLY IN CONJOINING SKETCH(ES)
 -                              (used  only in testing)            
 */
int KeypressInput()
{
  /*  inputs a keypress via the Serial comms link
      & returns a non-zero Integer only if there is an input
      CALLED IN MAIN SKETCH  */
  int iKey = 0;
  // send data only when you receive data
  if (Serial.available() > 0)
    iKey = Serial.read();
  return iKey;
}//KeypressInput()
//END
 

1 comment:

  1. yes i know bitwiseAND is hardly worth a Library entry but the modularity helps me think..

    ReplyDelete