Hello
Bit banging simply means using a general purpose I/O port for SPI (or I2C
or other serial bus) emulation. Here is an example of SPI implementation in
pseudo code (part of it only, example for sending a byte in SPI).
We will use the following conventions:
MISO_LOW (Master In Slave Out Low). This is the input from the CPU point
of view, from device to processor.
MISO_HIGH : same but high
MOSI_LOW and MOSI_HIGH (same as above but with Master Out Slave In).
This is the output, from processor to device.
Similarily, we'll have CS_LOW and CS_HIGH for select or deselect,
and CLK_LOW, CLK_HIGH for the clock.
Usually, when you want to send a byte or multiple bytes to a SPI device, you
have first to select it (usually drive CS low). Therefore CS_LOW in our pseudo
code. Then you have to send every bit, in many case MSB first.
And then we will clock evey bit from the CPU.
We will also define the following array:
Code:
const uint8 BIT8[] = {
0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
};
Sending a byte MSB first will be:
Code:
void BitBangSendByte(uint8 val) {
uint8 i;
CS_LOW();
for(i = 0 ; i < 8 ; ++i) {
if(val & BIT8[i]) MOSI_HIGH();
else MOSI_LOW();
CLK_LOW();
CLK_HIGH();
}
CS_HIGH();
}
With this method, you can communicate with a SPI device even if the
processor you are using doesn't have a SPI bus.
Dora