Bit-Banging

Under bit - banging refers to a technique that emulates a hardware interface via software and I / O lines is typically realized by a specific peripheral device. On a PC, both the serial port and the parallel port can be used. With microcontrollers you use the I / O pins.

There are various reasons for the use of bit - banging technique. A specific interface is not available in hardware, for example, has no standard PC via an SPI. With microcontrollers a resource is often already in use. Especially often used bit Banging the cost savings by replacing the expensive peripherals.

A variety of interfaces can be emulated by bit - banging. Here are some examples:

  • SPI synchronous serial interface
  • UART, asynchronous serial interface - is referred to as a " software UART "
  • 1-Wire, single-wire interface
  • LC display (eg HD44780 )
  • I ² C, synchronous serial interface
  • Decoding the line code for TV remote controls
  • Digital- analog converter by means of PWM, and the RC filter element

According to the complexity of the interface protocol, different methods are used. The simplest way is the polling. The processor scans as often as he can, changes to the I / O lines from. Is a certain time behavior observed, one uses loops or timer functions. The use of interrupt lines further reduces the processor load. Especially for generating a PWM signal are often used to direct timer outputs. Tricky is the appreciation of a simpler interface to a complex protocol.

Disadvantages of bit bangings are the high processor utilization, increased software complexity and usually strong jitter in the timing.

Example program in C

The following fragment of C language of the transmitter part of a synchronous serial interface (SPI) is represented by bit - banging. The I / O pins are marked as SD_CS (Chip Select), SD_DI (Data) and when 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 to 0     for (i = 0; i < 8; i )     {         / / Consider the leftmost bit         / / Set line high if bit is 1, if low bit is 0         if ( data & 0x80)             output_high ( SD_DI );         else             output_low ( SD_DI );           / / Pulse clock to indicate indication did bit value shoulderstand 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 elektronik.net encyclopedia
  • Diploma thesis: UART: A software implementation approach, by Herbert Valerio Riedel, Vienna University of Technology
  • DCF77 radio clock on www.mikrocontroller.net
  • Bus system
  • Emulator
129524
de