Designing with Bitwise Operator Logic

Exploiting the power of digital signals and digital hardware

The following information is intended to introduce AND (&) bitwise logic and clarify concepts introduced in the blog; The Case for Direct Port Manipulation. This example operates a stepper motor driven camera slider with multiple buttons and potentiometer control. The chosen microcontroller has limited I/O spread across many ports, which is handled by assembling the I/O into single digital values.

Overview

Evaluating switch inputs in software often begins with useful if/then statements. That may be fine for one stand-alone input but not so fine with multiple interactive inputs. Covering all the combinations of button presses with if/then statements could generate hundreds of lines of code and a multitude of executions each cycle. Implementing bitwise logic minimizes code and increases cycle speed.

Project Parameters

The Camera Slider project involves five momentary contact SPST switches for selecting direction and speed in the operation of the slider drive motor. The input is converted to SPEED/ENABLE/DIRECTION output. Switch presses are latched in software along with multiple switch press discrimination.

AND Bitwise Operator

The main bitwise operator used for the camera slider is AND, which uses the ampersand (&) symbol in C language programming and uses the truth table in Figure 1.

Figure 1: AND bitwise operator truth table and examples. (Image source: Don Johanneck)

I/O Combination

To build a single value that represents all switches, bits are shifted to the desired place, added together, and compared using the AND operator (See Figure 2). Another value that represents the current state of limit sensors is compared to the switch press value to negate output if a limit is reached.

Figure 2: Camera Slider switch configuration, bit compilation, and use of AND operator. (Image source: Don Johanneck)

In this scenario, if any button or combination of buttons is pushed, buttonValues is not equal to zero, and action is taken in software using the limitValue and lookup tables to determine output exactly the same way as described in the Direct Port Manipulation blog mentioned earlier.

Programming

The program uses a table of output values indexed by the processed input value. The programmer can determine the action that results from any combination of button presses by changing table values, eliminating the need to alter the structure of the program which can be daunting. The table values in Figure 3 for this example represent single, multiple, and opposing button actions without a single IF statement (Figure 4).

Figure 3: Table of unique output values indexed by the input value.

Figure 4: Main code; reading values and determining output.

Motor control is achieved using a timer interrupt service routine that determines the step frequency as well as a “skipping” method that facilitates speed control by setting the STEP pin on the motor control board only when the stepGo variable is equal to or greater than the stepFreq variable (Figure 5). Lower potentiometer values make the routine skip more STEPs.

Figure 5: Motor control interrupt service routine.

Conclusion:

Using bitwise operators such as AND (&) simplifies code and drastically reduces the number of IF statements needed to parse digital inputs. Building the output table is simple and flexible. Using more output bits expands the table quickly and provides a larger number of output options. Each additional output bit requires an additional handler(s) in the main code. Once established, the table is used only once per cycle, with the main code doing the heavy lifting. The interrupt service routine operates steadily in the background updating the STEP pin of the motor controller, but motion does not occur until the main code toggles the enable (ENN) pin of the motor controller.

About this author

Image of Don Johanneck

Don Johanneck, Technical Content Developer at DigiKey, has been with the company since 2014. Recently moving into his current position, he is responsible for writing video descriptions and product content. Don earned his Associate of Applied Science degree in Electronics Technology & Automated Systems from Northland Community & Technical College through the DigiKey scholarship program. He enjoys radio control modeling, vintage machine restoration and tinkering.

More posts by Don Johanneck
 TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum