Software SPI Library

The mikroBasic 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 :
dim SoftSpi_SDI as sbit bdata sfr external Data In line. dim SoftSpi_SDI as sbit at P0_4_bit
dim SoftSpi_SDO as sbit bdata sfr external Data Out line. dim SoftSpi_SDO as sbit at P0_5_bit
dim SoftSpi_CLK as sbit bdata sfr external Clock line. dim SoftSpi_CLK as sbit at P0_3_bit

Library Routines

Soft_SPI_Init

Prototype

sub procedure 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
dim SoftSpi_SDI as sbit at P0_4_bit
    SoftSpi_SDO as sbit at P0_5_bit
    SoftSpi_CLK as sbit at P0_3_bit
...
Soft_SPI_Init() ' Init Soft_SPI

Soft_SPI_Read

Prototype

sub function Soft_SPI_Read(dim sdata as byte) as byte

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
dim data_read as byte
    data_send as 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

sub procedure Soft_SPI_Write(dim sdata as byte)

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 Microchip's MCP4921 12-bit D/A converter.

program Soft_SPI

dim i as byte

' Port Expander module connections
dim SPExpanderRST as sbit at P1_0_bit
dim SPExpanderCS  as sbit at P1_1_bit
' End Port Expander module connections

dim SoftSpi_CLK as sbit at P1_7_bit
dim SoftSpi_SDI as sbit at P1_6_bit
dim SoftSpi_SDO as sbit at P1_5_bit

main:
  i = 0
  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 TRUE                                 ' Endless loop
    Expander_Write_PortA(0, i)               ' Write i to expander's PORTA
    Inc(i)
    P2 = Expander_Read_PortB(0)              ' Read expander's PORTB and write it to PORT0
    Delay_ms(100)
  wend
end.
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