Software SPI Library
The mikroC PRO for AVR 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.
The library configures SPI to the master mode, clock = 20kHz, data sampled at the middle of interval, clock idle state low and 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 : |
---|---|---|
extern sfr sbit SoftSpi_SDI; |
Data In line. | sbit SoftSpi_SDI at PINB6_bit; |
extern sfr sbit SoftSpi_SDO; |
Data Out line. | sbit SoftSpi_SDO at PORTB5_bit; |
extern sfr sbit SoftSpi_CLK; |
Clock line. | sbit SoftSpi_CLK at PORTB7_bit; |
extern sfr sbit SoftSpi_SDI_Direction; |
Direction of the Data In pin. | sbit SoftSpi_SDI_Direction at DDB6_bit; |
extern sfr sbit SoftSpi_SDO_Direction; |
Direction of the Data Out pin | sbit SoftSpi_SDO_Direction at DDB5_bit; |
extern sfr sbit SoftSpi_CLK_Direction; |
Direction of the Clock pin. | sbit SoftSpi_CLK_Direction at DDB7_bit; |
Library Routines
Soft_SPI_Init
Prototype |
void Soft_SPI_Init(); |
---|---|
Returns |
Nothing. |
Description |
Configures and initializes the software SPI module. |
Requires |
Global variables:
|
Example |
// Software SPI module connections sbit SoftSpi_CLK at PORTB7_bit; sbit SoftSpi_SDI at PINB6_bit; // Note : Input signal sbit SoftSpi_SDO at PORTB5_bit; sbit SoftSpi_CLK_Direction at DDB7_bit; sbit SoftSpi_SDI_Direction at DDB6_bit; sbit SoftSpi_SDO_Direction at DDB5_bit; // End Software SPI module connections ... Soft_SPI_Init(); // Init Soft_SPI |
Soft_SPI_Read
Prototype |
unsigned short Soft_SPI_Read(char sdata); |
---|---|
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 |
unsigned short data_read; char data_send; ... // 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 |
void Soft_SPI_Write(char sdata); |
---|---|
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.
// DAC module connections sbit Chip_Select at PORTB0_bit; sbit SoftSpi_CLK at PORTB7_bit; sbit SoftSpi_SDI at PINB6_bit; // Note: Input signal sbit SoftSpi_SDO at PORTB5_bit; sbit Chip_Select_Direction at DDB0_bit; sbit SoftSpi_CLK_Direction at DDB7_bit; sbit SoftSpi_SDI_Direction at DDB6_bit; sbit SoftSpi_SDO_Direction at DDB5_bit; // End DAC module connections unsigned int value; void InitMain() { DDA0_bit = 0; // Set PA0 pin as input DDA1_bit = 0; // Set PA1 pin as input Chip_Select = 1; // Deselect DAC Chip_Select_Direction = 1; // Set CS# pin as Output Soft_SPI_Init(); // Initialize Soft_SPI } // DAC increments (0..4095) --> output voltage (0..Vref) void DAC_Output(unsigned int valueDAC) { char temp; Chip_Select = 0; // Select DAC chip // Send High Byte temp = (valueDAC >> 8) & 0x0F; // Store valueDAC[11..8] to temp[3..0] temp |= 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 } void main() { InitMain(); // Perform main initialization value = 2048; // When program starts, DAC gives // the output in the mid-range while (1) { // Endless loop if ((PINA0_bit) && (value < 4095)) { // If PA0 button is pressed value++; // increment value } else { if ((PINA1_bit) && (value > 0)) { // If PA1 button is pressed value--; // decrement value } } DAC_Output(value); // Send value to DAC chip Delay_ms(1); // Slow down key repeat pace } }
What do you think about this topic ? Send us feedback!