Software SPI Library
The mikroPacal PRO for PIC provides routines for implementing Software SPI communication. These routines are hardware independent and can be used with any MCU. The Software SPI Library provides easy communication with other devices via SPI: A/D converters, D/A converters, MAX7219, LTC1290, etc.
- SPI to Master mode
- Clock value = 20 kHz.
- Data sampled at the middle of interval.
- Clock idle state low.
- Data sampled at the middle of interval.
- Data transmitted at low to high edge.

External dependencies of Software SPI Library
The following variables must be defined in all projects using Software SPI Library: | Description : | Example : |
---|---|---|
var SoftSpi_SDI : sbit; sfr; external; |
Data In line. | var SoftSpi_SDI : sbit at RC4_bit; |
var SoftSpi_SDO : sbit; sfr; external; |
Data Out line. | var SoftSpi_SDO : sbit at RC5_bit; |
var SoftSpi_CLK : sbit; sfr; external; |
Clock line. | var SoftSpi_CLK : sbit at RC3_bit; |
var SoftSpi_SDI_Direction : sbit; sfr; external; |
Direction of the Data In pin. | var SoftSpi_SDI_Direction : sbit at TRISC4_bit; |
var SoftSpi_SDO_Direction : sbit; sfr; external; |
Direction of the Data Out pin | var SoftSpi_SDO_Direction : sbit at TRISC5_bit; |
var SoftSpi_CLK_Direction : sbit; sfr; external; |
Direction of the Clock pin. | var SoftSpi_CLK_Direction : sbit at TRISC3_bit; |
Library Routines
Soft_SPI_Init
Prototype |
procedure Soft_SPI_Init(); |
---|---|
Returns |
Nothing. |
Description |
Configures and initializes the software SPI module. |
Requires |
Global variables:
|
Example |
// Software SPI module connections var SoftSpi_SDI : sbit at RC4_bit; var SoftSpi_SDO : sbit at RC5_bit; var SoftSpi_CLK : sbit at RC3_bit; var SoftSpi_SDI_Direction : sbit at TRISC4_bit; var SoftSpi_SDO_Direction : sbit at TRISC5_bit; var SoftSpi_CLK_Direction : sbit at TRISC3_bit; // End Software SPI module connections ... Soft_SPI_Init(); // Init Soft_SPI |
Soft_SPI_Read
Prototype |
function Soft_SPI_Read(sdata: byte): word; |
---|---|
Returns |
Byte received via the SPI bus. |
Description |
This routine performs 3 operations simultaneously. It provides clock for the Software SPI bus, reads a byte and sends a byte. Parameters :
|
Requires |
Soft SPI must be initialized before using this function. See Soft_SPI_Init routine. |
Example |
var data_read : word; data_send : byte; ... // Read a byte and assign it to data_read variable // (data_send byte will be sent via SPI during the Read operation) data_read := Soft_SPI_Read(data_send); |
Soft_SPI_Write
Prototype |
procedure Soft_SPI_Write(sdata: byte); |
---|---|
Returns |
Nothing. |
Description |
This routine sends one byte via the Software SPI bus. Parameters :
|
Requires |
Soft SPI must be initialized before using this function. See Soft_SPI_Init routine. |
Example |
// Write a byte to the Soft SPI bus Soft_SPI_Write(0xAA); |
Library Example
This code demonstrates using library routines for Soft_SPI communication. Also, this example demonstrates working with Microchip's MCP4921 12-bit D/A converter.
program Soft_SPI; // DAC module connections var Chip_Select : sbit at RC0_bit; SoftSpi_CLK : sbit at RC3_bit; SoftSpi_SDI : sbit at RC4_bit; // Note: Input signal SoftSpi_SDO : sbit at RC5_bit; var Chip_Select_Direction : sbit at TRISC0_bit; SoftSpi_CLK_Direction : sbit at TRISC3_bit; SoftSpi_SDI_Direction : sbit at TRISC4_bit; SoftSpi_SDO_Direction : sbit at TRISC5_bit; // End DAC module connections var value : word; procedure InitMain(); begin TRISA0_bit := 1; // Set RA0 pin as input TRISA1_bit := 1; // Set RA1 pin as input Chip_Select := 1; // Deselect DAC Chip_Select_Direction := 0; // Set CS# pin as Output Soft_Spi_Init(); // Initialize Soft_SPI end; // DAC increments (0..4095) --> output voltage (0..Vref) procedure DAC_Output( valueDAC : word); var temp : byte; volatile; begin Chip_Select := 0; // Select DAC chip // Send High Byte temp := word(valueDAC shr 8) and 0x0F; // Store valueDAC[11..8] to temp[3..0] temp := temp or 0x30; // Define DAC setting, see MCP4921 datasheet Soft_SPI_Write(temp); // Send high byte via Soft SPI // Send Low Byte temp := valueDAC; // Store valueDAC[7..0] to temp[7..0] Soft_SPI_Write(temp); // Send low byte via Soft SPI Chip_Select := 1; // Deselect DAC chip end; begin ANSEL := 0; ANSELH := 0; C1ON_bit := 0; // Disable comparators C2ON_bit := 0; InitMain(); // Perform main initialization value := 2048; // When program starts, DAC gives // the output in the mid-range while (TRUE) do // Endless loop begin if ((RA0_bit) and (value < 4095)) then // If PA0 button is pressed Inc(value) // increment value else begin if ((RA1_bit) and (value > 0)) then // If PA1 button is pressed Dec(value); // decrement value end; DAC_Output(value); // Send value to DAC chip Delay_ms(1); // Slow down key repeat pace end; end.
What do you think about this topic ? Send us feedback!