269
Simulation
Explanation
Software SPI requires digital I/Os only. Software SPI is achieved using bit-banging technique. For this
demo, a MCP4921 12-bit DAC is used. This DAC communicates with a host micro using SPI bus. Check
the function below. Here from the data bit to the clock signal, everything is controlled by changing the
logic states of digital I/Os. The changes are done in such a way that they do exactly the same thing a
hardware SPI would have done. If we used hardware-based SPI as in the last example, it would have
taken one or two lines of code unlike the long listing given below. This extra coding along with some
hardware limitations reduces SPI clock speed.
void MCP4921_write(unsigned char cmd, unsigned int dac_value)
{
unsigned char s = 16;
unsigned int value = 0;
value = cmd;
value <<= 8;
value |= (dac_value & 0x0FFF);
CS_LOW();