SPI Library

mikroPascal PRO for AVR provides a library for comfortable work with SPI module in Master mode. The AVR MCU can easily communicate with other devices via SPI: A/D converters, D/A converters, MAX7219, LTC1290, etc.

Library Routines

Generic Routines

SPI1_Init

Prototype

procedure SPI1_Init();

// for XMEGA family of MCUs

procedure SPIx_Init();

Returns

Nothing.

Description

This routine configures and enables SPI module with the following settings:

  • master mode
  • 8 bit data transfer
  • most significant bit sent first
  • serial clock low when idle
  • data sampled on leading edge
  • serial clock = fosc/4

For XMEGA family of MCUs change the X in the routine prototype with C, D, E or F.

  Note : Bear in mind when using MCU's with alternate SPI ports, that this routine will activate standard SPI port. Please, consult the appropriate datasheet.
Requires

MCU must have SPI module.

Example
// Initialize the SPI1 module with default settings
SPI1_Init();

SPI1_Init_Advanced

Prototype // for MCUs without alternate SPI ports

procedure SPI1_Init_Advanced(mode : byte; fcy_div : byte; clock_and_edge : byte);

// for MCUs with alternate SPI ports

procedure SPI1_Init_Advanced(mode : byte; fcy_div : byte; clock_and_edge : byte; alt_pinout : byte);

// for XMEGA family of MCUs

procedure SPIx_Init_Advanced(mode : byte; fcy_div : byte; clock_and_edge : byte);

Returns

Nothing.

Description
  • Configures and initializes SPI. SPI1_Init_Advanced or SPI1_Init needs to be called before using other functions of SPI Library.
  • For XMEGA family of MCUs change the X in the routine prototype with C, D, E or F.
  • Parameters mode, fcy_div, clock_and_edge and alt_port determine the work mode for SPI, and can have the following values:
    • Description Predefined library const
      SPI mode constants:
      Master mode _SPI_MASTER
      Slave mode _SPI_SLAVE
      Clock rate select constants:
      Sck = Fosc/4, Master mode _SPI_FCY_DIV4
      Sck = Fosc/16, Master mode _SPI_FCY_DIV16
      Sck = Fosc/64, Master mode _SPI_FCY_DIV64
      Sck = Fosc/128, Master mode _SPI_FCY_DIV128
      Sck = Fosc/2, Master mode _SPI_FCY_DIV2
      Sck = Fosc/8, Master mode _SPI_FCY_DIV8
      Sck = Fosc/32, Master mode _SPI_FCY_DIV32
      SPI clock polarity and phase constants:
      Clock idle level is low, sample on rising edge _SPI_CLK_LO_LEADING
      Clock idle level is low, sample on falling edge _SPI_CLK_LO_TRAILING
      Clock idle level is high, sample on rising edge _SPI_CLK_HI_LEADING
      Clock idle level is high, sample on falling edge _SPI_CLK_HI_TRAILING
      Alternate SPI port settings:
      Set standard SPI port as active _SPI_STD_PINOUT
      Set alternate SPI port as active _SPI_ALT_PINOUT
      Note :
    • Some SPI clock speeds are not supported by all AVR MCUs and these are: Fosc/2, Fosc/8, Fosc/32. Please consult appropriate datasheet.
    • For alternate SPI port function and use, please consult appropriate datasheet.
Requires

MCU must have SPI module.

Example
// Set SPI to the Master Mode, clock = Fosc/32 , clock idle level is high, data sampled on falling edge:
SPI1_Init_Advanced(_SPI_MASTER, _SPI_FCY_DIV32, _SPI_CLK_HI_TRAILING);

// Set SPI to the Master Mode, clock = Fosc/4 , clock idle level is low, data sampled on rising edge, alternate SPI port selected:
SPI1_Init_Advanced(_SPI_MASTER, _SPI_FCY_DIV4, _SPI_CLK_LO_LEADING, _SPI_ALT_PINOUT);

SPI1_Read

Prototype

function SPI1_Read(buffer : byte) : byte;

// for XMEGA family of MCUs

function SPIx_Read(buffer : byte) : 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)

For XMEGA family of MCUs change the X in the routine prototype with C, D, E or F.

Requires

SPI module must be initialized before using this function. See SPI1_Init and SPI1_Init_Advanced routines.

Example
var buffer, take : byte;

begin
  take := SPI1_Read(buffer);
end.

SPI1_Write

Prototype

procedure SPI1_Write(data_ : byte);

// for XMEGA family of MCUs

procedure SPIx_Write(data_ : byte);

Returns

Nothing.

Description

Writes byte via the SPI bus.

Parameters :

  • data_: data to be sent

For XMEGA family of MCUs change the X in the routine prototype with C, D, E or F.

Requires

SPI module must be initialized before using this function. See SPI1_Init and SPI1_Init_Advanced routines.

Example
// write a byte to the SPI bus
var data_ : byte;
...
SPI1_Write(data_);

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

Used SPI module must be initialized before using this function. See the SPI1_Init, SPI1_Init_Advanced

Example
SPI_Set_Active(@SPI1_Read, @SPI1_Write); // Sets the SPI1 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 :

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

SPI module must be initialized before using this function. See SPI1_Init and SPI1_Init_Advanced routines.

Example
var buffer, take : byte;

begin
  take := SPI_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 :

  • data_: data to be sent
Requires

SPI module must be initialized before using this function. See SPI1_Init and SPI1_Init_Advanced routines.

Example
// write a byte to the SPI bus
var data_ : byte;
...
SPI_Write(data_);

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
var Chip_Select : sbit at PORTB0_bit;
    Chip_Select_Direction : sbit at DDB0_bit;
// End DAC module connections

var value : word;

procedure InitMain();
  begin
    DDA0_bit := 0;                           // Set PA0 pin as input
    DDA1_bit := 0;                           // Set PA1 pin as input
    Chip_Select := 1;                        // Deselect DAC
    Chip_Select_Direction := 1;              // Set CS# pin as Output
    SPI1_Init();                             // Initialize SPI1 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

  InitMain();                                // Perform main initialization

  value := 2048;                             // When program starts, DAC gives
                                             //   the output in the mid-range
                                          
  while ( TRUE ) do                              // Endless loop
    begin

      if ((PINA0_bit) and (value < 4095)) then   // If PA0 button is pressed
        Inc(value)                               //   increment value
      else
        begin
          if ((PINA1_bit) and (value > 0)) then  // If PA1 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 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