Software SPI Library
The mikroBasic PRO for ARM 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
Stellaris
The following variables must be defined in all projects using Software SPI Library: | Description : | Example : |
---|---|---|
dim SoftSpi_SDI as sbit sfr external |
Data In line. | dim SoftSpi_SDI as sbit at GPIO_PORTJ_DATA4_bit |
dim SoftSpi_SDO as sbit sfr external |
Data Out line. | dim SoftSpi_SDO as sbit at GPIO_PORTJ_DATA5_bit |
dim SoftSpi_CLK as sbit sfr external |
Clock line. | dim SoftSpi_CLK as sbit at GPIO_PORTJ_DATA3_bit |
dim SoftSpi_SDI_Direction as sbit sfr external |
Direction of the Data In pin. | dim SoftSpi_SDI_Direction as sbit at GPIO_PORTJ_DIR4_bit |
dim SoftSpi_SDO_Direction as sbit sfr external |
Direction of the Data Out pin | dim SoftSpi_SDO_Direction as sbit at GPIO_PORTJ_DIR5_bit |
dim SoftSpi_CLK_Direction as sbit sfr external |
Direction of the Clock pin. | dim SoftSpi_CLK_Direction as sbit at GPIO_PORTJ_DIR3_bit |
STM32
The following variables must be defined in all projects using Software SPI Library: | Description : | Example : |
---|---|---|
dim SoftSpi_SDI as sbit sfr external |
Data In line. | dim SoftSpi_SDI as sbit at GPIOD_ODR.B4 |
dim SoftSpi_SDO as sbit sfr external |
Data Out line. | dim SoftSpi_SDO as sbit at GPIOD_ODR.B5 |
dim SoftSpi_CLK as sbit sfr external |
Clock line. | dim SoftSpi_CLK as sbit at GPIOD_ODR.B3 |
Library Routines
Soft_SPI_Init
Prototype |
sub procedure Soft_SPI_Init() |
---|---|
Description |
Routine initializes the software SPI module. |
Parameters |
None. |
Returns |
Nothing. |
Requires |
External dependencies of the library from the top of the page must be defined before using this function. |
Example |
Stellaris' DAC module connections dim Chip_Select as sbit at GPIO_PORTJ_DATA1_bit SoftSpi_CLK as sbit at GPIO_PORTJ_DATA3_bit SoftSpi_SDI as sbit at GPIO_PORTJ_DATA4_bit SoftSpi_SDO as sbit at GPIO_PORTJ_DATA5_bit dim Chip_Select_Direction as sbit at GPIO_PORTJ_DIR1_bit SoftSpi_CLK_Direction as sbit at GPIO_PORTJ_DIR3_bit SoftSpi_SDI_Direction as sbit at GPIO_PORTJ_DIR4_bit SoftSpi_SDO_Direction as sbit at GPIO_PORTJ_DIR5_bit ' End DAC module connections ... Soft_SPI_Init() ' Init Soft_SPI STM32' DAC module connections dim Chip_Select as sbit at GPIOD_ODR.B1 SoftSpi_CLK as sbit at GPIOD_ODR.B3 SoftSpi_SDI as sbit at GPIOD_ODR.B4 SoftSpi_SDO as sbit at GPIOD_ODR.B5 ' End DAC module connections ... Soft_SPI_Init() ' Init Soft_SPI |
Notes |
None. |
Soft_SPI_Read
Prototype |
sub function Soft_SPI_Read(dim data_ as byte) as byte |
---|---|
Description |
This routine performs 3 operations simultaneously. It provides clock for the Software SPI bus, reads a byte and sends a byte. |
Parameters |
|
Returns |
Byte received via the SPI bus. |
Requires |
Soft SPI must be initialized before using this function. See Soft_SPI_Init routine. |
Example |
dim data_read, 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) |
Notes |
None. |
Soft_SPI_Write
Prototype |
sub procedure Soft_SPI_Write(dim data_ as byte) |
---|---|
Description |
This routine sends one byte via the Software SPI bus. |
Parameters |
|
Returns |
Nothing. |
Requires |
Soft SPI must be initialized before using this function. See Soft_SPI_Init. |
Example |
' Write a byte to the Soft SPI bus Soft_SPI_Write($AA) |
Notes |
None. |
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.
Stellaris
program Soft_SPI ' DAC module connections dim Chip_Select as sbit at GPIO_PORTJ_DATA1_bit SoftSpi_CLK as sbit at GPIO_PORTJ_DATA3_bit SoftSpi_SDI as sbit at GPIO_PORTJ_DATA4_bit SoftSpi_SDO as sbit at GPIO_PORTJ_DATA5_bit dim Chip_Select_Direction as sbit at GPIO_PORTJ_DIR1_bit SoftSpi_CLK_Direction as sbit at GPIO_PORTJ_DIR3_bit SoftSpi_SDI_Direction as sbit at GPIO_PORTJ_DIR4_bit SoftSpi_SDO_Direction as sbit at GPIO_PORTJ_DIR5_bit ' End DAC module connections dim value as word sub procedure InitMain() GPIO_Digital_Input(@GPIO_PORTJ_DATA_BITS, _GPIO_PINMASK_6 or _GPIO_PINMASK_7) ' Set PJ6 and PJ7 as digital input GPIO_Digital_Output(@GPIO_PORTJ_DATA_BITS, _GPIO_PINMASK_1) ' Set PJ1 as digital output Chip_Select = 1 ' Deselect DAC Soft_Spi_Init() ' Initialize Soft_SPI end sub ' DAC increments (0..4095) --> output voltage (0..Vref) sub procedure DAC_Output(dim valueDAC as word) dim temp as byte volatile Chip_Select = 0 ' Select DAC chip ' Send High Byte temp = word(valueDAC >> 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 sub main: InitMain() ' Perform main initialization value = 2048 ' When program starts, DAC gives ' the output in the mid-range while (TRUE) ' Endless loop if ((GPIO_PORTJ_DATA7_bit) and (value < 4095)) then ' If PJ7 button is pressed Inc(value) ' increment value else if ((GPIO_PORTJ_DATA6_bit) and (value > 0)) then ' If PJ6 button is pressed Dec(value) ' decrement value end if end if DAC_Output(value) ' Send value to DAC chip Delay_ms(1) ' Slow down key repeat pace wend end.
STM32
program Soft_SPI ' DAC module connections dim Chip_Select as sbit at GPIOD_ODR.B1 SoftSpi_CLK as sbit at GPIOD_ODR.B3 SoftSpi_SDI as sbit at GPIOD_ODR.B4 SoftSpi_SDO as sbit at GPIOD_ODR.B5 ' End DAC module connections dim value as word sub procedure InitMain() GPIO_Digital_Input(@GPIOC_BASE, _GPIO_PINMASK_6 or _GPIO_PINMASK_7) ' Set PC6 and PC7 as digital input GPIO_Digital_Output(@GPIOD_BASE, _GPIO_PINMASK_1) ' Set PD1 as digital output Chip_Select = 1 ' Deselect DAC Soft_Spi_Init() ' Initialize Soft_SPI end sub ' DAC increments (0..4095) --> output voltage (0..Vref) sub procedure DAC_Output(dim valueDAC as word) dim temp as byte volatile Chip_Select = 0 ' Select DAC chip ' Send High Byte temp = word(valueDAC >> 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 sub main: InitMain() ' Perform main initialization value = 2048 ' When program starts, DAC gives ' the output in the mid-range while (TRUE) ' Endless loop if ((GPIOC_IDR.B7) and (value < 4095)) then ' If PC7 button is pressed Inc(value) ' increment value else if ((GPIOC_IDR.B6) and (value > 0)) then ' If PC6 button is pressed Dec(value) ' decrement value end if end if DAC_Output(value) ' Send value to DAC chip Delay_ms(1) ' Slow down key repeat pace wend end.
What do you think about this topic ? Send us feedback!