In contrast, dedicated communication hardware (e.g., UART, SPI, I²C) satisfies protocol requirements which tends to reduce the runtime load on the controlling system – software and its host processor. In particular, some communication hardware provides data buffering to lower the runtime load of the controlling system. There are also peripheral devices dedicated to bit-banging called programmable input/output, combining the flexibility of bit-banging and the low runtime-load of dedicated hardware.
The bit banging method may allow a computer to support a protocol with limited or no hardware changes and therefore bit banging can be a lower cost option since changing software is typically less expensive than changing hardware.
Choosing between bit banging and dedicated communication hardware involves trade-offs between load, performance and reliability on one hand, and availability of hardware on the other. Bit banging consumes more processing resources than using dedicated hardware. The processor spends much of its time controlling data lines which precludes other processing. Also, unless hardware interrupt latency is uniform such as in early models of Atmel PICs, and other guarantees made that are usually found in barrel processor designs such as the CDC 6600 I/O co-processor, bit banging typically results in a lower quality signal – with more jitter and glitches– especially if the processor is performing other tasks simultaneously. However, if the software is interrupt-driven by the signal, the signal quality may be better, especially if control signals such as RTS, CTS, or DCD are available. Bit banging may be the only solution when dedicated communication hardware is not available.
Example
The following C language code example transmits a byte of data on an SPI bus via bit banging.
voidsend_8bit_serial_data(unsignedchardata){// select device (active low)output_low(SD_CS);// send bits 7..0for(inti=0;i<8;i++){// consider leftmost bit// set line high if bit is 1, low if bit is 0if(data&0x80)output_high(SD_DI);elseoutput_low(SD_DI);// pulse the clock state to indicate that bit value should be readoutput_low(SD_CLK);delay();output_high(SD_CLK);// shift byte left so next bit will be leftmostdata<<=1;}// deselect deviceoutput_high(SD_CS);}
See also
1-bit architecture– Computer architecture bit widthPages displaying short descriptions of redirect targets
Bit manipulation– Algorithmically modifying data below the word level
Bit stream– Sequence of binary digitsPages displaying short descriptions of redirect targets