Software SPI Library

The mikroC PRO for 8051 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.

Library configuration:

Note: The Software SPI library implements time-based activities, so interrupts need to be disabled when using it.

External dependencies of Software SPI Library

The following variables must be defined in all projects using Software SPI Library: Description : Example :
extern sfr sbit bdata SoftSpi_SDI; Data In line. sbit SoftSpi_SDI at P1_6_bit;
extern sfr sbit bdata SoftSpi_SDO; Data Out line. sbit SoftSpi_SDO at P1_5_bit;
extern sfr sbit bdata SoftSpi_CLK; Clock line. sbit SoftSpi_CLK at P1_7_bit;

Library Routines

Soft_SPI_Init

Prototype

void Soft_SPI_Init();

Returns

Nothing.

Description

Configures and initializes the software SPI module.

Requires

Global variables:

  • SoftSpi_SDI: Data in line
  • SoftSpi_SDO: Data out line
  • SoftSpi_CLK: Data clock line
must be defined before using this function.

Example
// soft_spi pinout definition
sbit SoftSpi_SDI   at P1_6_bit;
sbit SoftSpi_SDO   at P1_5_bit;
sbit SoftSpi_CLK   at P1_7_bit;
...
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 :

  • sdata: data to be sent.

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 :

  • sdata: data to be sent.

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 Port Expander MCP23S17.

// Port Expander module connections
sbit  SPExpanderRST at P1_0_bit;
sbit  SPExpanderCS  at P1_1_bit;
// End Port Expander module connections

// Software SPI connections
sbit SoftSpi_CLK at P1_7_bit;
sbit SoftSpi_SDI at P1_6_bit;
sbit SoftSpi_SDO at P1_5_bit;
// End Software SPI connections

char (*SPI_Rd_Ptr)(char);                // Pointer to SPI_Read function
                                         //   used by Port Expander Library
unsigned char i = 0;

void main() {

  SPI_Rd_Ptr = Soft_SPI_Read;            // Link Port Expander Library to Software SPI library

  Expander_Init(0);                      // Initialize Port Expander

  Expander_Set_DirectionPortA(0, 0x00);  // Set Expander's PORTA to be output

  Expander_Set_DirectionPortB(0,0xFF);   // Set Expander's PORTB to be input
  Expander_Set_PullUpsPortB(0,0xFF);     // Set pull-ups to all of the Expander's PORTB pins

  while(1) {                             // Endless loop
    Expander_Write_PortA(0, i++);        // Write i to expander's PORTA
    P2 = Expander_Read_PortB(0);         // Read expander's PORTB and write it to PORT2 LEDs
    Delay_ms(100);
  }
  
}
Copyright (c) 2002-2013 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code