The same if SW2 is closed, the master sends a ‘1’ to slave 2 and a ‘0’ to slave 1. If SW1 is closed, the master sends a ‘1’ to slave 1 and a ‘0’ to slave 2. Two switches SW1 and SW2 are connected to PORTB0 and PORTB1 of the master. Let us create a simple project as shown on figure 2 below, it consists of a PIC18F26K20 Master connected with SPI bus to two PIC18F26K20 Slaves.
To learn more, read the article: MPLAB® Code Configurator
The advantage of MCC, it can generate codes not only for PIC18F but for a wide range of PICs including PIC16F and PIC24 series. The MPLAB® Code Configurator (MCC) is a user friendly Graphical User Interface (GUI) plug-in tool for MPLAB® X IDE which generates easy to understand C code that is inserted into an MPLAB® X project, based on the settings peripherals configurations and selections made in the Graphical User Interface (GUI). SPI Configuration with MPLAB Code Configurator on the same PCB as the Master or via a relatively short cable compared to RS-232, RS-485, or CAN-bus
It requires more signal lines due to the fact that each slave must have its own CS line, this will require more wires than other serial communications especially when many slaves are involved.Lower power requirements than I☬ due to less circuitry and no need for pull-ups resistors.Complete protocol flexibility for the bits transferred, this is not limited to 8-bit words and arbitrary choice of message size, content, and purpose.The hardware protocol is simple to implement, the receiver hardware can be a simple shift register. Transceivers are not needed, slaves use the master’s clock and don’t need precision oscillators.SPI can operate at extremely high speeds but generally dictated by the slowest device on the bus. Common bus speeds are in the 1-100Mbits per second range It’s faster than asynchronous serial and I2C allowing large quantities of data to be transferred quickly.Figure 1 below shows an illustration of one master (microcontroller) controlling three slave devices.
There are many devices that support the SPI protocol and can easily communicate with a microcontroller via SPI: A/D converters, D/A converters, SD Cards, Liquide Crystal Displays (LCD), Real Time Clocks like the popular DS1306, serial display drivers like MAX7219, Serial EEPROM like 25LC256, etc.ĭepending on the number of slave devices connected, the master will have one dedicated pin for SS line for each slave. SPI is used for connecting peripherals to each other and to microcontrollers. Figure 1 hows the microcontroller connected to 3 SPI slaves (3 chip select lines). Depending on the number of devices in the bus, we can have more than one chip Select lines, one per device in the bus. This signal must be used when more than one slave exists in a system, but can be optional when only one slave exists in the circuit but it always a good practice to use it. The signal is most often active low, so a low on this line will indicate the SPI is active and communication can take place, in a shift register, this corresponds to the “latch” input, which transfers the received data to the output lines, but a high on the CS pin will signal inactivity. SS (Slave Select)or CS (Chip Select) : This pin allows a master device to indicates to a slave that the master wishes to start an SPI data exchange between that slave device and itself. MISO (Master In slave Out) or SDI (Serial Data In) This pin carries data into a device (from slave to master). MOSI (Master Out Slave In) or SDO (Serial Data Output) This pin carries data out of a device (from master to slave). The SCLK pin of master is connected to SCLK pin of slave. It is generated by the master device and controls when d ata is sent and when it is read. SCLK (Serial Clock): This is the serial clock signal.
SPI is usually called a 4-wire full duplex synchronous serial communication, the functions of the 4 wires are as follow: It allows serial communication between two or more devices at a high speed and is reasonably easy to implement The MSSP can operate either in I☬ or SPI mode. This module is built into many different PICmicro devices.
SPI is implemented in the PIC microcontrollers by a hardware module called the Synchronous Serial Port or the Master Synchronous Serial Port (MSSP). The master initiates a communication and also provides clock pulses to the slave devices. In a system that uses the SPI bus, one device acts as the master and other devices act as slaves. The SPI protocol was initially developed and proposed by Motorola for use in microprocessor and microcontroller based interface applications. SPI stands for Serial Peripheral Interface. Watch the Video Tutorial: Introduction to SPI