The Bit Bang way

Reusable software is an issue that still attracts much attention among developers. In fact, the concern with the development of such software is not a new proposal, although concentrated efforts seems to be recent. Designing components rather than monolithic artifacts is the main product of these efforts. Undoubtedly, we've been learned so much with hardware industry, since  its component-based development is from decades ago. Although we have much to do to make reusable software better, nowdays we can find a significant range of solutions with a good level of maturity on the market.


Verifying of a MTWS (Miniature Threaded Wire Store), programmable component that store the firmware of a MCU used in the TXE telephone system. Each MTWS
held 500 programming steps.

Like any rational being, I support for more advances in this area. The point is that the idea that there is already a component ready for anything you can imagine has taken over half the world. Half a world that now faces a little more work, a little more thinking, as painful. Definitely, a hardware or software component is not a magic wand. Well, it all depends of the context's problem. Although in some cases the component seems to fit perfectly in context, it may acts as restricted when solves part of your problem and lacking on some other requirement or, no less often, acts as complex as trying to solve the world's problem and the problem in context is much simpler than that. In the latter case, especially when it comes to hardware, we're talking about higher costs.

In hardware development, the use of reusable software in physical components replacement may be a great alternative when you wish a lower cost in manufacturing. With a quick search and a little luck, you can find some library or platform for your microcontroller/processor that adapts well to your project; otherwise it may be very interesting develop the solution by yourself, and perhaps, start to build your own reusable library. Of course this is only feasible if in relationship between the cost of engineering and manufacturing the most important is the lower cost of manufacture.

One of the best allies of this perspective of low-cost hardware is the old Bit Bang (or Bit Banging) technique. Using this technique it's possible to enable our microcontroller to communicate with protocols as I2C, UART, SPI, USB, ethernet, among others, without the need of a specific hardware to do so. All done by software.

Bit Bang is a technique that use I/O pins of microcontroller or other programmable device to emulate by software a protocol that would normally be implemented by dedicated hardware, controlling all the required behavior, including timing and synchronization. The technique is most used in serial lines, but any sequence of more or less recurrent activities performed by the software upon I/O pins, such as control a parallel LCD, for example, can be regarded as a Bit Bang since we don't have a specific hardware for it.

The code below shows a simple way to send a byte to a serial line controlled by clock. The bits are sent from most significant to least significant. Instantiating a practical situation, we could use this code for bit banging shift registers like 74HC595. But this is just a sample of what the Bit Bang technique is capable of .

for(bit = 0; bit < 8; bit++) {

  /* Put the bit value on pin */    
  if(byte & 0x80) {      
     setDataPin();    
  } else {
    clearDataPin();
  }

  /* Send the clock rise */    
  riseClock();

  /* Wait the needed time to make right clock frequency */    
  delay();

  /* Send the clock fall */    
  fallClock();

  /* Shift the next bit to send */    
  byte <<= 1;

}

 

Individual bit handling (banging it!)

Working individually with bits distributed along bytes is not always allowed by the processor instruction set or some high level language available. Typically, the instructions can only handle complete words in size corresponding to data bus size. Another case occurs when it's most desirable to hanlde n parts of the whole in a single instruction, than use n instructions to handle the n parts. All these situations are almost inherent in Bit Bang; it's time like these we must know how to use logical operators and Boolean logic.

Using AND, OR and XOR  logical operators, we can manipulate individual bits (one or more bits) without affecting others bits in the word. The most common operations are: read, set, clear, and toggle the bit value. From here, I assume that you know well the truth table of cited operators. Let's look at each operation, with a 8 bits data bus example.

Reading the bit value:
To determine the bit value, first make a mask with all bits cleared (logic 0),
except the bit at the same bit position that we want to check, which must be
set (logic 1). Then apply the mask to the byte using the AND operation.

   Example: Check bit 4 value at B
      MASK = 0001 0000b
      V = B AND MASK
     
      If V value is zero, this means that the value of bit 4 at B is zero too (0 and 1 = 0).
      Any other value in V indicates that the bit 4 at B is 1.
 

Clearing the bit:
The clearing of bit also use the bitwise AND. The mask is the complement
of the example shown in reading.

   Example: Clear both bit 1 and bit 4 at B
      MASK = 1110 1101b
      B = B AND MASK

      Applying the AND operation, we found that bit 1 and bit 4 are cleared (x AND 0 = 0)
      and all other bits remain unchanged (x AND 1 = x).
 

Setting the bit:
To setting the bit, the mask should contain 1 at the position that we want
to force the set and all other bits must be cleared. Next, we apply the OR operation.

   Example: Set bit 5 at B
      MASK = 0010 0000b
      B = B OR MASK

      Applying the bitwise OR, we'll see that bit 5 is set (x OR 1 = 1)
      and the remaining bits are unchanged (x OR 0 = x).
 

Toggling the bit:
When we want to toggle the bit value, we use the properties of the logical
XOR operator. The mask follows the same principle used on the bit setting example.

   Example: Toggle bit 5 at B
      MASK = 0010 0000b
      B = B XOR MASK

      Applying the bitwise XOR, bit 5 now is inverted (x XOR 1 = ~x) - if it was 1
      then it becomes zero and vice versa - and all others bits are unchanged (x XOR 0 = x).
 

In addition to logical operations, we can also rely on bit shift operations, which are nothing more than moving all bits of the word to left or right. Although there are other ways of handling bit, such as arithmetic, for example, logical operations shown here are most often enough, and faster too.

This is the Bit Bang technique: few resources, a lot of imagination.
 

Some projects using Bit Bang:

- 16FUSB - A USB 1.1 implementation for PIC16F628/648
- Bitbang VGA from an SD card slot
- Introduction to FTDI bitbang mode
- Bitbang IR Remote
 

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.