SPI Remappable Library


mikroC PRO for PIC provides a library for the SPI Remappable Module 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.

  Note : Before using this library, make sure that Peripheral Pin Select Library and SPI Library are checked in the Library Manager, and that appropriate pins were mapped.

Library Dependency Tree

SPI Remappable Library Dependency Tree

Library Routines

SPI_Remappable_Init

Prototype

void SPI_Remappable_Init();

// for MCUs with multiple SPI modules
void SPIx_Remappable_Init();
Returns

Nothing.

Description

Configures and initializes SPI Remappable module with default settings. This function needs to be called before using other functions from SPI Remappable 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
  Note :
  • Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
  • To select the desired SPI module, simply change the letter x in the prototype for the appropriate module number.
Requires

You'll need PIC MCU with hardware integrated SPI and remappable feature.

Example
// Initialize the SPI Remappable module with default settings
SPI_Remappable_Init();

SPI_Remappable_Init_Advanced

Prototype

void SPI_Remappable_Init_Advanced(unsigned short master_slav, unsigned short data_sample, unsigned short clock_idle, unsigned short transmit_edge);

// for MCUs with multiple SPI modules
void SPIx_Remappable_Init_Advanced(unsigned short master_slav, unsigned short data_sample, unsigned short clock_idle, unsigned short transmit_edge);
Returns

Nothing.

Description

Configures and initializes SPI Remappable module. This function needs to be called before using other functions of SPI Library.

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

    Description Predefined library const
    SPI work mode:
    Master clock = Fosc/4 _SPI_REMAPPABLE_MASTER_OSC_DIV4
    Master clock = Fosc/16 _SPI_REMAPPABLE_MASTER_OSC_DIV16
    Master clock = Fosc/64 _SPI_REMAPPABLE_MASTER_OSC_DIV64
    Master clock source TMR2 _SPI_REMAPPABLE_MASTER_TMR2
    Slave select enabled _SPI_REMAPPABLE_SLAVE_SS_ENABLE
    Slave select disabled _SPI_REMAPPABLE_SLAVE_SS_DIS
    Data sampling interval:
    Input data sampled in middle of interval _SPI_REMAPPABLE_DATA_SAMPLE_MIDDLE
    Input data sampled at the end of interval _SPI_REMAPPABLE_DATA_SAMPLE_END
    SPI clock idle state:
    Clock idle HIGH _SPI_REMAPPABLE_CLK_IDLE_HIGH
    Clock idle LOW _SPI_REMAPPABLE_CLK_IDLE_LOW
    Transmit edge:
    Data is valid on low to high edge _SPI_REMAPPABLE_LOW_2_HIGH
    Data is valid on high to low edge _SPI_REMAPPABLE_HIGH_2_LOW

  Note :
  • Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
  • To select the desired SPI module, simply change the letter x in the prototype for the appropriate module number.
Requires

You need PIC MCU with hardware integrated SPI and remappable feature.

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:
SPI_Remappable_Init_Advanced(_SPI_REMAPPABLE_MASTER_OSC_DIV4, _SPI_REMAPPABLE_DATA_SAMPLE_MIDDLE, _SPI_REMAPPABLE_CLK_IDLE_LOW, _SPI_REMAPPABLE_LOW_2_HIGH);

SPI_Remappable_Read

Prototype

unsigned short SPI_Remappable_Read(unsigned short buffer);

// for MCUs with multiple SPI modules
unsigned short SPIx_Remappable_Read(unsigned short buffer);

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)

  Note :
  • Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
  • To select the desired SPI module, simply change the letter x in the prototype for the appropriate module number.
Requires

You need PIC MCU with hardware integrated SPI and remappable feature.

SPI must be initialized and communication established before using this function. See SPI_Remappable_Init_Advanced or SPI_Remappable_Init.

Example
short take, buffer;
...
take = SPI_Remappable_Read(buffer);

SPI_Remappable_Write

Prototype

void SPI_Remappable_Write(unsigned short data_);

// for MCUs with multiple SPI modules
void SPIx_Remappable_Write(unsigned short data_);

Returns

Nothing.

Description

Writes byte data to SSPBUF, and immediately starts the transmission.

  Note :
  • Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
  • To select the desired SPI module, simply change the letter x in the prototype for the appropriate module number.
Requires

You need PIC MCU with hardware integrated SPI and remappable feature.

SPI must be initialized and communication established before using this function. See SPI_Remappable_Init_Advanced or SPI_Remappable_Init.

Example
SPI_Remappable_Write(1);
Copyright (c) 2002-2019 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