SPI Library
SPI module is available with a number of PIC MCU models. mikroPascal 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 :
- SPI routines require you to specify the module you want to use. To select the desired SPI, simply change the letter
xin the prototype for a number from1to2.
Number of SPI modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library. - Some MCUs have multiple SPI modules. Switching between the SPI modules in the SPI library is done by the SPI_Set_Active function (SPI module has to be previously initialized).
- Some of the MCUs do not support SPIx_Init_Advanced routine. Please, refer to the appropriate datasheet.
- Certain SPI lines on some PIC16 Enhanced Family MCUs can be routed through a alternative pins. These pins are selected by configuring APFCON register and appropriate TRIS register.
Library Routines
Generic Routines
SPIx_Init
| Prototype |
procedure SPIx_Init(); |
|---|---|
| Returns |
Nothing. |
| Description |
This routine configures and enables SPI module with the following settings:
|
| Requires |
You need PIC MCU with hardware integrated SPI. |
| Example |
// Initialize the SPI1 module with default settings SPI1_Init(); |
SPIx_Init_Advanced
| Prototype |
procedure SPIx_Init_Advanced(master, data_sample, clock_idle, transmit_edge : byte); |
||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Returns |
Nothing. |
||||||||||||||||||||||||||||||||||
| Description |
Configures and initializes SPI. SPIx_Init or SPIx_Init_Advanced needs to be called before using other functions of SPI Library. Parameters
|
||||||||||||||||||||||||||||||||||
| Requires |
You need PIC MCU with hardware integrated SPI. |
||||||||||||||||||||||||||||||||||
| Example |
// Set SPI1 module 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 |
function SPIx_Read(buffer : byte) : byte; |
|---|---|
| Returns |
Returns the received data. |
| Description |
Reads one byte from the SPI bus. Parameters :
|
| 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 |
var buffer, take : byte; begin take := SPI1_Read(buffer); end. |
SPIx_Write
| Prototype |
procedure SPIx_Write(data_ : byte); |
|---|---|
| Returns |
Nothing. |
| Description |
Writes byte via the SPI bus. Parameters :
|
| 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 |
SPI1_Write(1); |
SPI_Set_Active
| Prototype |
procedure SPI_Set_Active (read_ptr : ^TSPI_Rd_Ptr; write_ptr : ^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 |
function SPI_Read(buffer : byte) : 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 :
|
| 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 |
var buffer, take : byte; begin take := SPI1_Read(buffer); end. |
SPI_Write
| Prototype |
procedure SPI_Write(data_ : 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 :
|
| 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 |
SPI1_Write(1); |
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
program SPI;
// DAC module connections
var Chip_Select : sbit at RC0_bit;
Chip_Select_Direction : sbit at TRISC0_bit;
// End DAC module connections
var value : word;
procedure InitMain();
begin
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 SPI module
end;
// DAC increments (0..4095) --> output voltage (0..Vref)
procedure DAC_Output( valueDAC : word);
var temp : byte;
begin
Chip_Select := 0; // Select DAC chip
// Send High Byte
temp := word(valueDAC shr 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;
begin
ANSEL := 0;
ANSELH := 0;
InitMain(); // Perform main initialization
value := 2048; // When program starts, DAC gives
// the output in the mid-range
while ( TRUE ) do // Endless loop
begin
if ((RA0_bit) and (value < 4095)) then // If RA0 button is pressed
Inc(value) // increment value
else
begin
if ((RA1_bit) and (value > 0)) then // If RA1 button is pressed
Dec(value); // decrement value
end;
DAC_Output(value); // Send value to DAC chip
Delay_ms(1); // Slow down key repeat pace
end;
end.
HW Connection

SPI HW connection
What do you think about this topic ? Send us feedback!




