Mikroelektronika MIKROE-442 Datenbogen
mikroBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
473
SPI_Set_Active
Prototype
sub procedure SPI_Set_Active(dim read_ptr as ^TSpi_Rd_Ptr, dim write_ptr as
^TSpi_Wr_Ptr)
Description Sets the active SPI module which will be used by the SPIx_Read and SPIx_Write routines.
Parameters Parameters:
Parameters Parameters:
-
read_ptr
: SPI1_Read handler
-
write_ptr
: SPI1_Write handler
Returns
Nothing.
Requires
Routine is available only for MCUs with multiple SPI modules.
Used SPI module must be initialized before using this function. See the SPIx_Init and SPIx_Init_
Advanced routines.
Example
SPI_Set_Active(@SPI1_Read, @SPI1_Write) ‘ Sets the SPI1 module active
Notes
Number of SPI modules per MCU differs from chip to chip. Please, read the appropriate datasheet
before utilizing this library.
Library Example
The code demonstrates how to use SPI library functions for communication between SPI2 module of the MCU and
MCP4921 DAC chip.
Copy Code To Clipboard
program SPI
‘ DAC module connections
dim Chip_Select as sbit at LATF0_bit
Chip_Select_Direction as sbit at TRISF0_bit
‘ End DAC module connections
dim value as word
sub procedure InitMain()
TRISB0_bit = 1 ‘ Set RB0 pin as input
TRISB1_bit = 1 ‘ Set RB1 pin as input
Chip_Select = 1 ‘ Deselect DAC
Chip_Select_Direction = 0 ‘ Set CS# pin as Output
SPI1_Init() ‘ Initialize SPI 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