Bit-banging

From Wikipedia, the free encyclopedia

Bit-banging is a technique in embedded systems for example to use serial communications without the use of dedicated hardware such as a UART or shift register, instead using software to emulate their behavior. 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.

With a few extra components, video signals can be output from digital pins.

Although it is not referred to as bit-banging, software-defined radio is an extreme extension of the same idea.

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

There are some problems with bit-banging. More power is normally consumed in the software emulation process than in dedicated hardware. Another problem is that the microcontroller is busy most of the time looking at samples or sending a sample to the pin, instead of performing other tasks. Yet another potential problem is that the signal produced normally has more jitter or glitches, especially when the microcontroller moves on to perform other tasks. However, if the bit-banging software is hardware interrupt-driven by the signal, this may be of minor importance.

[edit] C code example

int8 const bitMask8[]={
   0b10000000,
   0b01000000,
   0b00100000,
   0b00010000,
   0b00001000,
   0b00000100,
   0b00000010,
   0b00000001
};

// This will send data in bit7~0, updating the clock each bit
void send_8bit_serial_data(unsigned int8 data)
{
   int8 x;

   output_high(SD_CS);     // lets select the device

   // lets go thru 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_high(SD_CS);     // lets DE-select the device
}

[edit] External links

In other languages