Bit-banging

From Wikipedia, the free encyclopedia

Bit-banging is a technique for serial communications to use software instead of dedicated hardware such as a UART or shift register. A software routine handles the UART transmit function by alternating a pin on the microcontroller by given time intervals. A receiver function is implemented by sampling a pin on the microcontroller by a given time interval. The technique can be applied in very low cost embedded systems.

With a few extra components, video signals can be output from digital pins. (See TV Typewriter).

Although it is often considered to be something of a hack, bit-banging does allow the same device to use different protocols with minimal or no hardware changes required.

There are some problems with bit-banging. More processing power is consumed in the software emulation process than in supporting dedicated hardware. The microcontroller is busy most of the time looking at samples or sending a sample to the pin, instead of performing other tasks. The signal produced normally has more jitter or glitches, if the processor is also executing other tasks while communicating. However, if the bit-banging software is hardware interrupt-driven by the signal, this may be of minor importance.

[edit] C code example

const unsigned char bitMask8[] = {
   0x80,  // binary 10000000
   0x40,  // binary 01000000
   0x20,  // binary 00100000
   0x10,  // binary 00010000
   0x08,  // binary 00001000
   0x04,  // binary 00000100
   0x02,  // binary 00000010
   0x01   // binary 00000001
};
 
// This will send data in bit7~0, updating the clock each bit
void send_8bit_serial_data(unsigned char data)
{
   unsigned char x;
 
   output_high(SD_CS);     // lets select the device
 
   // Loop through all the bits, 7...0
   for(x = 0; x < 8; x++)
   {
       if(data & bitMask8[x])
       {
           output_high(SD_DI);       // we have a bit, make high
       }
       else
       {
           output_low(SD_DI);        // no bit, make low
       }
 
       output_low(SD_CLK);           // update clock
       output_high(SD_CLK);          // update clock
   }
 
   output_low(SD_CS);     // lets DE-select the device
}

[edit] See also

[edit] External links

Languages