User manual

Table Of Contents
438
mikoC PRO for PIC32
MikroElektronika
SPI_Set_Active
Prototype
void SPI_Set_Active(unsigned (*read_ptr)(unsigned), void(*write_ptr)
(unsigned));
Description Sets the active SPI module which will be used by the SPIx_Read and SPIx_Write routines.
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
// DAC module connections
sbit Chip_Select at LATF0_bit;
sbit Chip_Select_Direction at TRISF0_bit;
// End DAC module connections
unsigned int value;
void 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
SPI2_Init(); // Initialize SPI2 module
}
// DAC increments (0..4095) --> output voltage (0..Vref)
void DAC_Output(unsigned int valueDAC) {
char temp;
Chip_Select = 0; // Select DAC chip
// Send High Byte
temp = (valueDAC >> 8) & 0x0F; // Store valueDAC[11..8] to temp[3..0]
temp |= 0x30; // Dene DAC setting, see MCP4921 datasheet
SPI2_Write(temp); // Send high byte via SPI