SPI Library
mikroBasic PRO for 8051 provides a library for comfortable with SPI work in Master mode. The 8051 MCU can easily communicate with other devices via SPI: A/D converters, D/A converters, MAX7219, LTC1290, etc.
Library Routines
Generic Routines
Notes:
- SPI routines require you to specify the module you want to use. To select the desired SPI, simply change the letter
x
in the prototype for a number from1
to2
.
Number of SPI modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library. - Some MCUs have multiple SPI modules. Switching between the SPI modules in the SPI library is done by the SPI_Set_Active function (SPI module has to be previously initialized).
- Some of the MCUs do not support SPIx_Init_Advanced routine. Please, refer to the appropriate datasheet.
SPIx_Init
Prototype |
' for ATMEL MCUs
' for Silicon Laboratories MCUs |
---|---|
Returns |
Nothing. |
Description |
This routine configures and enables SPI module with the following settings:
|
Requires |
MCU must have SPI module. |
Example |
' Initialize the SPI module with default settings SPI1_Init() ' Initialize the SPI module with the baud rate of 100000 SPI1_Init(100000) |
SPIx_Init_Advanced
Prototype |
' for ATMEL MCUs
' for Silicon Laboratories MCUs |
||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
Nothing. |
||||||||||||||||||||||||||||||||||||||||
Description |
This routine configures and enables the SPI module with the user defined settings. Parameters :
|
||||||||||||||||||||||||||||||||||||||||
Requires |
MCU must have SPI module. |
||||||||||||||||||||||||||||||||||||||||
Example |
' SPI configuration for ATMEL MCU's: clock = Fosc/4 , clock IDLE state low, data transmitted at low to high clock edge and output mode 8051: SPI1_Init_Advanced(_SPI_FCY_DIV4 or _SPI_CLK_IDLE_LO or _SPI_CLK_IDLE_2_ACTIVE or _SPI_OUTPUT_8051) ' SPI configuration for Silicon Laboratories MCU's: clock = 100000, clock IDLE state low and data transmitted at low to high clock edge: SPI1_Init_Advanced(100000, _SPI_CLK_IDLE_LO or _SPI_CLK_IDLE_2_ACTIVE) |
SPIx_Read
Prototype |
sub function SPIx_Read(dim buffer as byte) as byte |
---|---|
Returns |
Received data. |
Description |
Reads one byte from the SPI bus. Parameters :
|
Requires |
SPI module must be initialized before using this function. See SPIx_Init and SPIx_Init_Advanced routines. |
Example |
' read a byte from the SPI bus dim take, dummy1 as byte ... take = SPI1_Read(dummy1) |
SPIx_Write
Prototype |
sub procedure SPIx_Write(dim data_out as byte) |
---|---|
Returns |
Nothing. |
Description |
Writes byte via the SPI bus. Parameters :
|
Requires |
SPI module must be initialized before using this function. See SPIx_Init and SPIx_Init_Advanced routines. |
Example |
' write a byte to the SPI bus dim data_out as byte ... SPI1_Write(data_out) |
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 |
Routine is available only for MCUs with two SPI modules. Used SPI module must be initialized before using this function. See SPIx_Init and SPIx_Init_Advanced routines.
|
Example |
SPI_Set_Active(@SPI2_Read, @SPI2_Write) ' Sets the SPI2 module active |
SPI_Read
Prototype |
sub function SPI_Read(dim buffer as byte) as 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 |
You need MCU with hardware integrated SPI. SPI must be initialized and communication established before using this function. See SPIx_Init_Advanced or SPIx_Init. |
Example |
' read a byte from the SPI bus dim take, dummy1 as byte ... take = SPI_Read(dummy1) |
SPI_Write
Prototype |
sub procedure SPI_Write(dim data_out as 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 |
You need MCU with hardware integrated SPI. SPI must be initialized and communication established before using this function. See SPIx_Init_Advanced or SPIx_Init. |
Example |
' write a byte to the SPI bus dim data_out as byte ... SPI_Write(data_out) |
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 DAC_Test ' DAC module connections dim DAC_CS as sbit at P3_4_bit ' End DAC module connections dim value as word ' DAC increments (0..4095) --> output voltage (0..Vref) sub procedure DAC_Output(dim valueDAC as word ) dim temp as byte DAC_CS = 0 ' select MCP4921 ' 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 http://ww1.microchip.com/downloads/en/DeviceDoc/21897B.pdf#page=18&zoom=100 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 DAC_CS = 1 ' deselect MCP4921 end sub main: DAC_CS = 1 ' deselect MCP4921 SPI1_Init() ' Initialize SPI module value = 2048 ' When program starts, DAC gives ' the output in the mid-range while TRUE ' Endless loop if ((P0_0_bit = 0) and (value < 4095)) then ' If P0.0 is connected to GND Inc(value) ' increment value else if (( P0_1_bit = 0 ) and (value > 0)) then ' If P0.1 is connected to GND Dec(value) ' decrement value end if end if DAC_Output(value) ' Perform output 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!