Bit-banging is a method of using general-purpose I/O lines to emulate a serial port. Microcontrollers that include serial-port modules like SPI[tm] and I2C[tm] manage all synchronization and timing signals, and this activity is transparent to the user. With bit-banging each write to the port causes a single transition at the port pin. And it’s up to the user, first, to provide the correct number of transitions to obtain the desired waveform and, second, to ensure that the timing requirements (particularly setup and hold times for reading and writing data) are met.When using bit-banging, the only hardware for each output is a latch which can be explicitly set or cleared by software, and the only hardware for each input is an interface to allow software to test whether it is high or low (and typically execute a conditional branch for one state but not the other).
Bit Banging basically uses Software for communication instead of a dedicated hardware. In this method Transmitting is done by alternating the Transmit Pin after specific interval, this time interval depends on the Baud rate(The baud rate is the rate at which information is transferred in a communication channel. In the serial port context, “9600 baud” means that the serial port is capable of transferring a maximum of 9600 bits per second)for serial communication. In same way, Receive function is performed by sampling the pin at regular interval. This is quite a tedious process and puts too much load the Micro controller processing.
- The software emulation process consumes more processing power than does supporting dedicated hardware.
- The microcontroller spends much of its time reading or sending samples to and from the pin, at the expense of other tasks
- The signal produced normally has more jitter or glitches, especially if the processor is also executing other tasks while communicatin
Due to the overhead associated with the number of writes to the port, though the actual port speed might be quite high the actual bit-bang throughput rate is usually very slow. This technique is very inefficient from a software perspective, but it may be acceptable in some applications where the communication overhead is acceptable (for example for doing occasional controlling communication).
The maximum speed which can be achieved with bit-banging will generally be a fraction of what could be achieved with purpose-built hardware, but outside the limitations imposed by processor speed, bit-banging is much more versatile, and may be used in circumstances where general-purpose hardware is not quite suitable and special-purpose hardware would not be cost-effective.
Bit-banged designs are simplest and most robust in circumstances where either the devices being communicated with are waiting on the bit-banging controller to generate all their timing, or where the controller will be allowed to wait, without distraction, for an event to arrive, and where it will be able to do everything it needs to do with that event before any other event arrives that it needs to act upon. They are much less robust in circumstances where a device will need to be able to react to external stimuli within a relatively short time frame, but cannot device 100% of its energy to watching for such stimuli.
Software directly sets and samples the state of pins on the microcontroller, and is responsible for all parameters of the signal: timing, levels, synchronization, etc. In contrast to bit-banging, dedicated hardware (such as a modem, UART, or shift register) handles these parameters and provides a (buffered) data interface in other systems, so software is not required to perform signal demodulation. Bit-banging can be implemented at very low cost, and is used in, for example, embedded systems.
Bit-banging is the only choice available if your hardware doesn’t have a built-in peripheral, or if it’s already used up by other devices, e.g. if the built-in SPI peripheral is used by a high-bandwidth device that you have to continuously communicate with, maybe you bit bang to another SPI device that doesn’t need to be so real-time in your application. Bit-banging allows the same device to use different protocols with minimal or no hardware changes required.