Software SPI Library
The mikroPascal PRO for 8051 provides routines for implementing Software SPI communication. These routines are hardware independent and can be used with any MCU. The Software SPI Library provides easy communication with other devices via SPI: A/D converters, D/A converters, MAX7219, LTC1290, etc.
- SPI to Master mode
- Clock value = 20 kHz.
- Clock idle state low.
- Data sampled at the middle of interval.
- Data transmitted at low to high edge.
Note: The Software SPI library implements time-based activities, so interrupts need to be disabled when using it.
External dependencies of Software SPI Library
The following variables must be defined in all projects using Software SPI Library: | Description : | Example : |
---|---|---|
var SoftSpi_SDI : sbit; bdata; sfr; external; |
Data In line. | var SoftSpi_SDI: sbit at P0_4_bit; |
var SoftSpi_SDO : sbit; bdata; sfr; external; |
Data Out line. | var SoftSpi_SDO: sbit at P0_5_bit; |
var SoftSpi_CLK : sbit; bdata; sfr; external; |
Clock line. | var SoftSpi_CLK: sbit at P0_3_bit; |
Library Routines
Soft_SPI_Init
Prototype |
procedure Soft_SPI_Init(); |
---|---|
Returns |
Nothing. |
Description |
Configures and initializes the software SPI module. |
Requires |
Global variables:
|
Example |
// soft_spi pinout definition var SoftSpi_SDI : sbit at P0_4_bit; SoftSpi_SDO : sbit at P0_5_bit; SoftSpi_CLK : sbit at P0_3_bit; ... Soft_SPI_Init(); // Init Soft_SPI |
Soft_SPI_Read
Prototype |
function Soft_SPI_Read(sdata: byte): word; |
---|---|
Returns |
Byte received via the SPI bus. |
Description |
This routine performs 3 operations simultaneously. It provides clock for the Software SPI bus, reads a byte and sends a byte. Parameters :
|
Requires |
Soft SPI must be initialized before using this function. See Soft_SPI_Init routine. |
Example |
var data_read : word; data_send : byte; ... // Read a byte and assign it to data_read variable // (data_send byte will be sent via SPI during the Read operation) data_read := Soft_SPI_Read(data_send); |
Soft_SPI_Write
Prototype |
procedure Soft_SPI_Write(sdata: byte); |
---|---|
Returns |
Nothing. |
Description |
This routine sends one byte via the Software SPI bus. Parameters :
|
Requires |
Soft SPI must be initialized before using this function. See Soft_SPI_Init routine. |
Example |
// Write a byte to the Soft SPI bus Soft_SPI_Write(0xAA); |
Library Example
This code demonstrates using library routines for Soft_SPI communication. Also, this example demonstrates working with Microchip's MCP4921 12-bit D/A converter.
program Soft_SPI; var i, received_data : byte; // Port Expander module connections var SPExpanderRST : sbit at P0_6_bit; var SPExpanderCS : sbit at P0_7_bit; // End Port Expander module connections var SoftSpi_CLK : sbit at P0_2_bit; var SoftSpi_SDI : sbit at P0_3_bit; var SoftSpi_SDO : sbit at P0_4_bit; begin i := 0; WDTCN := 0xDE; // Sequence for WDTCN := 0xAD; // disabling the watchdog timer OSCICN := 0x83; // Enable internal oscillator (24.5MHz divided by 1) P0MDOUT := P0MDOUT or %11010101; // Configure P0.7(CS), P0.6(RST), P0.4(MOSI), P0.2(SCK) // and P0.0(TX) pin as push-pull UART1_Init(4800); // Initialize UART module at 4800 bps Delay_ms(100); // Wait for UART module to stabilize UART1_Write_Text('Start'); SPI_Rd_Ptr := @Soft_SPI_Read; // Link Port Expander Library to Software SPI library Expander_Init(0); // Initialize Port Expander Expander_Set_DirectionPortA(0, 0x00); // Set Expander's PORTA to be output Expander_Set_DirectionPortB(0, 0xFF); // Set Expander's PORTB to be input Expander_Set_PullUpsPortB(0, 0xFF); // Set pull-ups to all of the Expander's PORTB pins while TRUE do // Endless loop begin Expander_Write_PortA(0, i); // Write i to expander's PORTA Inc(i); received_data := Expander_Read_PortB(0); // Read expander's PORTB UART1_Write(received_data); // and send it to UART Delay_ms(100); end; end.
What do you think about this topic ? Send us feedback!