SPI Library

SPI module is available with a number of PIC MCU models. mikroBasic PRO for PIC provides a library for initializing Slave mode and comfortable work with Master mode. PIC can easily communicate with other devices via SPI: A/D converters, D/A converters, MAX7219, LTC1290, etc. You need PIC MCU with hardware integrated SPI (for example, PIC16F877).

  Important :

Library Routines

Generic Routines

SPIx_Init

Prototype

sub procedure SPIx_Init()

Returns

Nothing.

Description

Configures and initializes SPI with default settings. SPIx_Init_Advanced or SPIx_Init needs to be called before using other functions from SPI Library.

Default settings are:

  • master mode
  • clock Fosc/4
  • clock idle state low
  • data transimtted on low to high edge
  • input data sampled at the middle of interval
Requires

You need PIC MCU with hardware integrated SPI.

Example
' Initialize the SPI module with default settings
SPI1_Init()

SPIx_Init_Advanced

Prototype

sub procedure SPIx_Init_Advanced(dim master_slav, data_sample, clock_idle, transmit_edge as byte)

Returns

Nothing.

Description

Configures and initializes SPI. SPIx_Init_Advanced or SPIx_Init needs to be called before using other functions of SPI Library.

Parameters mode, data_sample and clock_idle configure the SPI module, and can have the following values:

    Description Predefined library const
    SPI work mode:
    Master clock = Fosc/4 _SPI_MASTER_OSC_DIV4
    Master clock = Fosc/16 _SPI_MASTER_OSC_DIV16
    Master clock = Fosc/64 _SPI_MASTER_OSC_DIV64
    Master clock source TMR2 _SPI_MASTER_TMR2
    Slave select enabled _SPI_SLAVE_SS_ENABLE
    Slave select disabled _SPI_SLAVE_SS_DIS
    Data sampling interval:
    Input data sampled in middle of interval _SPI_DATA_SAMPLE_MIDDLE
    Input data sampled at the end of interval _SPI_DATA_SAMPLE_END
    SPI clock idle state:
    Clock idle HIGH _SPI_CLK_IDLE_HIGH
    Clock idle LOW _SPI_CLK_IDLE_LOW
    Transmit edge:
    Data transmit on low to high edge _SPI_LOW_2_HIGH
    Data transmit on high to low edge _SPI_HIGH_2_LOW

Requires

MCU must have SPI module.

Example
' Set SPI to master mode, clock = Fosc/4, data sampled at the middle of interval, clock idle state low and data transmitted at low to high edge:
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH)

SPIx_Read

Prototype

sub function SPIx_Read(dim buffer as byte) as byte

Returns

Returns the received data.

Description

Reads one byte from the SPI bus.

Parameters :

  • buffer: dummy data for clock generation (see device Datasheet for SPI modules implementation details)

Requires

You need PIC MCU with hardware integrated SPI.

SPI must be initialized and communication established before using this function. See SPIx_Init_Advanced or SPIx_Init.

Example
' read a byte from the SPI bus 
dim take, dummy1 as byte
...
take = SPI1_Read(dummy1)

SPIx_Write

Prototype

sub procedure SPIx_Write(dim wrdata as byte)

Returns

Nothing.

Description

Writes byte via the SPI bus.

Parameters :

  • wrdata: data to be sent

Requires

You need PIC MCU with hardware integrated SPI.

SPI must be initialized and communication established before using this function. See SPIx_Init_Advanced or SPIx_Init.

Example
' write a byte to the SPI bus
dim buffer as byte
...
SPI1_Write(buffer)

SPI_Set_Active

Prototype

sub procedure SPI_Set_Active(dim read_ptr as ^TSpi_Rd_Ptr, dim write_ptr as ^TSpi_Wr_Ptr)

Returns

Nothing.

Description

Sets the active SPI module which will be used by the SPI routines.

Parameters :

Requires

Routine is available only for MCUs with two SPI modules.

Used SPI module must be initialized before using this function. See the SPIx_Init, SPIx_Init_Advanced

Example
SPI_Set_Active(@SPI2_Read, @SPI2_Write) ' Sets the SPI2 module active 

SPI_Read

Prototype

sub function SPI_Read(dim buffer as byte) as byte

Returns

Returns the received data.

Description

Reads one byte from the SPI bus.

This is a generic routine which uses the active SPI module previously activated by the SPI_Set_Active routine.

Parameters :

  • buffer: dummy data for clock generation (see device Datasheet for SPI modules implementation details)

Requires

You need PIC MCU with hardware integrated SPI.

SPI must be initialized and communication established before using this function. See SPIx_Init_Advanced or SPIx_Init.

Example
' read a byte from the SPI bus 
dim take, dummy1 as byte
...
take = SPI_Read(dummy1)

SPI_Write

Prototype

sub procedure SPI_Write(dim wrdata as byte)

Returns

Nothing.

Description

Writes byte via the SPI bus.

This is a generic routine which uses the active SPI module previously activated by the SPI_Set_Active routine.

Parameters :

  • wrdata: data to be sent

Requires

You need PIC MCU with hardware integrated SPI.

SPI must be initialized and communication established before using this function. See SPIx_Init_Advanced or SPIx_Init.

Example
' write a byte to the SPI bus
dim buffer as byte
...
SPI_Write(buffer)

Library Example

The code demonstrates how to use SPI library functions for communication between SPI module of the MCU and Microchip's MCP4921 12-bit D/A converter

Copy Code To ClipboardCopy Code To Clipboard
program SPI

' DAC module connections
dim Chip_Select as sbit at RC0_bit
    Chip_Select_Direction as sbit at TRISC0_bit
' End DAC module connections

dim value as word

sub procedure InitMain()
  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
  SPI1_Init()                            ' Initialize SPI1 module
end sub

' DAC increments (0..4095) --> output voltage (0..Vref)
sub procedure DAC_Output(dim valueDAC as word)
dim temp as byte
  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
  SPI1_Write(temp)                       ' Send high byte via SPI

  ' Send Low Byte
  temp = valueDAC                        ' Store valueDAC[7..0] to temp[7..0]
  SPI1_Write(temp)                       ' Send low byte via SPI

  Chip_Select = 1                        ' Deselect DAC chip
end sub

main:
  ANSEL = 0
  ANSELH = 0
  InitMain()                             ' Perform main initialization
  value = 2048                           ' When program starts, DAC gives
                                         '   the output in the mid-range
  while TRUE                             ' Endless loop
    if ((RA0_bit) and (value < 4095)) then  ' If RA0 button is pressed
      Inc(value)                            '   increment value
    else
      if ((RA1_bit) and (value > 0)) then   ' If RA1 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.

HW Connection

SPI connection

SPI HW connection

Copyright (c) 2002-2012 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