Thursday, August 25, 2011

Task 14 - Arbitrary byte serial monitor output bit-by-bit

/*
 * Project:    bitwiseByteOutput_task14
 * Author:     Jane-Maree Howard
 * Date:         Wednesday 24/08/2011; modified Sunday 09/10/2011
 * Platform:   Arduino 22
 * Purpose:    To output the bits, one by one, to the
  =                  serial monitor(SM) screen from an arbitrary byte.
 * Operation:  Inputs an arbitrary byte, which is right-shifted 0 to 7 places
 -             successively, bit-wise ANDed with value B01, & each resultant bit is
 -             separately outputted to the SM
 -             Arduino Libraries: LIB04_KeypressInput; LIB05_BitSelect;
 -             Declare: Function bitwiseByte(bByte,iShifts); int iKeyPress; byte bInbyte;
 -             Setup(): Serial @ 9600 baud; headings             
 -             Loop():  Input keypress as arbitrary byte; output byte as BINary;
 -                      output each bit separately
 */
int     iKeyPress = 0;       // for incoming serial data 
byte    bInbyte;
void setup()  
{
  Serial.begin(9600);        // Serial @ 9600 baud
  // print headings
  Serial.println("KEY-PRESS\tBIN\tLSB..................MSB");  
}//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\t");                    // print tabs
    bInbyte  = iKeyPress;      //
    Serial.print(bInbyte, BIN);         // print resultant byte as binary
    Serial.print("\t");                      // print tab
    for (int j=0; j<8; j++)                 // now select each bit separately
    {
      bInbyte  = iKeyPress;
      bInbyte  = BitSelect(bInbyte, j);     // LIB05_BitSelect
      Serial.print(bInbyte, BIN);             // ..print each bit..
      Serial.print("  ");                           // ..& 2 spaces
    }//for()
    Serial.println(); 
  }//if()  
}//end loop()
//END


As can be seen, the BIN column entries
are 'mirror-imaged' by the block labelled LSB............MSB since we start with the Least Significant Bit.  Leading zeroes omitted in the BIN column give the output a slightly unbalanced look.

 LIB04_KeypressInput
 see Task 13

 /*
 * Project:    LIB05_BitSelect
 * Author:     Jane-Maree Howard
 * Date:         Sunday 09/10/2011
 * Platform:   Arduino 22
 * Purpose:    To make a library    function for selecting a given bit         from an arbitrary byte
 * Operation:  Bitwise ANDs arbitrary byte with value B01
 -                    after being right-shifted a given number of places
 -             Declare:     NONE - MUST BE DECLARED IN CONJOINING SKETCH(ES)
 -             Setup():     NONE
 -             Procedure(): byte BitSelect(byte, byte);     
 -             Loop():      NONE          
 */
byte BitSelect(byte bByte, byte bShift)
{
  /*  bitwise ANDs parameter bByte with value B01
      after being right-shifted bShift places
      & returns a separate Byte
      CALLED IN MAIN SKETCH  */
  bByte      = bByte >> bShift;
  bByte      = bByte & B01;
  return bByte;
}//BitSelect()
//END

2 comments:

  1. slightly less elegant due to my Arduino Library modularity, in that the input is re-entered to the 'Add...'ed function each time a bit-shift is required.
    i discovered this when it didn't work the first time & that was my work-around..

    simpler would've been to keep working on the same byte & shift only 1 place..

    ReplyDelete
  2. arrggh! that's enough for the time being..

    besides, it's my Dad's birthday today..

    ReplyDelete