SPI Library
mikroBasic 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 |
sub procedure SPI1_Init() ' for XMEGA family of MCUssub procedure SPIx_Init() |
---|---|
Returns |
Nothing. |
Description |
This routine configures and enables SPI module with the following settings:
For XMEGA family of MCUs change the X in the routine prototype with C, D, E or F. ![]() |
Requires |
MCU must have SPI module. |
Example |
' Initialize the SPI module with default settings SPI1_Init() |
SPI1_Init_Advanced
Prototype |
' for MCUs without alternate SPI ports sub procedure SPI1_Init_Advanced(mode, fcy_div, clock_and_edge as byte) ' for MCUs with alternate SPI portssub procedure SPI1_Init_Advanced(mode, fcy_div, clock_and_edge, alt_pinout as byte) ' for XMEGA family of MCUssub procedure SPIx_Init_Advanced(mode, fcy_div, clock_and_edge as byte) |
||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
Nothing. |
||||||||||||||||||||||||||||||||||||||||
Description |
![]() |
||||||||||||||||||||||||||||||||||||||||
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 |
sub function SPI1_Read(dim buffer as byte) as byte ' for XMEGA family of MCUssub function SPIx_Read(dim buffer as byte) as byte |
---|---|
Returns |
Returns the received data. |
Description |
Reads one byte from the SPI bus. Parameters :
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 |
' read a byte from the SPI bus dim take, dummy1 as byte ... take = SPI1_Read(dummy1) |
SPI1_Write
Prototype |
sub procedure SPI1_Write(dim data_ as byte) ' for XMEGA family of MCUssub procedure SPIx_Write(dim data_ as byte) |
---|---|
Returns |
Nothing. |
Description |
Writes byte via the SPI bus. Parameters :
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 dim data_ as byte ... SPI1_Write(data_) |
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 |
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 :
|
Requires |
SPI module must be initialized before using this function. See SPI1_Init and SPI1_Init_Advanced routines. |
Example |
dim buffer, take as byte ... take = SPI_Read(buffer) |
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 |
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 dim data_ as 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
program SPI ' DAC module connections dim Chip_Select as sbit at PORTB0_bit Chip_Select_Direction as sbit at DDB0_bit ' End DAC module connections dim value as word sub procedure InitMain() 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 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: InitMain() ' Perform main initialization value = 2048 ' When program starts, DAC gives ' the output in the mid-range while TRUE ' Endless loop if ((PINA0_bit) and (value < 4095)) then ' If PA0 button is pressed Inc(value) ' increment value else if ((PINA1_bit) and (value > 0)) then ' If PA1 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 HW connection
What do you think about this topic ? Send us feedback!