Bit banging
Under bit banging refers to a technique by means of software and I / O a hardware ¯ lines interface emulates , which is usually implemented with a specific peripheral device. Both the serial and the parallel interface can be used on a PC . In microcontrollers one uses the I / O pins.
The bit-banging method can be useful if a specific interface is not available in hardware, e.g. B. no standard PC has an SPI , or if a resource is already occupied in microcontrollers . Bit banging is particularly often used to save costs by replacing relatively expensive peripheral components.
A variety of interfaces can be emulated by bit banging. Here are some examples:
- SPI , synchronous serial interface
- UART , asynchronous serial interface - is then also referred to as "software UART"
- 1-wire , single-wire interface
- LC display (e.g. HD44780 )
- I²C , synchronous serial interface
- Decoding of line codes for TV remote controls
- Digital-to-analog converter , using PWM and RC filter element
Different methods are used depending on the complexity of the interface protocol. The simplest way is polling . The processor queries changes to the I / O lines as often as it can. If a certain time behavior has to be observed, waiting loops or timer functions are used. The use of the interrupt lines further reduces the processor load. The direct timer outputs are often used specifically to generate a PWM signal. It is tricky to upgrade a simpler interface to a complex protocol.
Disadvantages of bit banging are the high processor load, the increased software effort and usually strong jitter in the timing. Some interfaces, such as SPI, are immune to this, while others, such as UART, have strict time requirements.
Program example in C
The following fragment in the C programming language shows the transmission part of a synchronous serial interface (SPI) using bit banging. The I / O pins are designated as SD_CS (Chip Select), SD_DI (Data) and SD_CLK (Clock).
// transmit byte serially, MSB first
void send_8bit_serial_data(unsigned char data)
{
int i;
// select device
output_high(SD_CS);
// send bits 7..0
for (i = 0; i < 8; i++)
{
// consider leftmost bit
// set line high if bit is 1, low if bit is 0
if (data & 0x80)
output_high(SD_DI);
else
output_low(SD_DI);
// pulse clock to indicate that bit value should be read
output_low(SD_CLK);
output_high(SD_CLK);
// shift byte left so next bit will be leftmost
data <<= 1;
}
// deselect device
output_low(SD_CS);
}
Web links
- Definition in the elektronik.net lexicon
- Herbert Valerio Riedel: UART: A Software Implementation Approach . ( Memento from July 22, 2012 in the Internet Archive ) Diploma thesis on the subject, Vienna University of Technology .
- mikrocontroller.net - DCF77 radio clock