3.5. PIC16 C SPI Serial Bus

• SPI system connections

• SPI function set

• SPI test system

The serial peripheral interface master controller uses hardware slave selection to identify a peripheral device with which it wishes to exchange data (refer to Section 1.4 for full details of the signaling protocol). The available set of SPI driver functions are shown in Table 3.7

Table 3.7. SPI Function Set
OperationDescriptionExample
SPI SETUPInitializes SPI serial portsetup_spi(spi_master);
SPI READReceives data byte from SPI portinbyte=spi_read();
SPI WRITESends data byte via SPI portspi_write(outbyte);
SPI TRANSFERSends and receives via SPIinbyte=spi_xfer(outbyte);
SPI RECEIVEDChecks if SPI data receiveddone=spi_data_is_in();

.

The test system has a slave transmitter that reads a binary-coded decimal input from a thumbwheel switch and sends it to the master controller. This resends the code to the slave receiver, which outputs to a BCD display (0–9). Each of three devices needs its own test program to make the system work. The test system hardware is shown in Figure 3.8 and the individual test programs as Listings 3.7, 3.8, and 3.9

Listing 3.7. SPI Slave Transmitter Source Code

// Serial I/O using SPI synchronous link

// Simulation hardware SPIC.DSN, transmitter program attached to U2

#include “16F877A.h”

void main() //*****************************************

{

int sendnum;

setup_spi(spi_slave); // Set SPI slave mode

while(1)

{ sendnuminput_D(); // Get BCD input

spi_write(sendnum); // Send BCD code to master

}

}

Listing 3.8. SPI Slave Controller Source Code

// SPIMASTER.C MPB 20-6-07

// Serial I/O using SPI synchronous link

// Simulation hardware SPIC.DSN, master program, attach to U1

#include “16F877A.h”

void main() //***************************************************

{

int number;

setup_spi(spi_master); // Set SPI master mode

while(1)

{ number=spi_read(); // Read SPI input BCD code

spi_write(number); // Resend BCD code to slave

}

}

Listing 3.9. SPI Slave Receiver Source Code

// SPIRECEIVE.C MPB 20-6-07

// Serial I/O using SPI synchronous link

// Simulation hardware SPI.DSN, receiver program, attach to U3

#include “16F877A.h”

void main() //***************************************

{

int recnum;

setup_spi(spi_slave); // Set SPI slave mode

while(1)

{ recnum=spi_read(); // Read BCD code at SPI port

output_D(recnum); // Display it

}

}

Figure 3.8. SPI Test System Schematic


As seen in the schematic, the slave MCUs are permanently enabled by connecting their slave select inputs to ground. This is possible because there is only one sender on the master input, so there is no potential contention. In a system with more that one slave sender, each would need a separate slave select line, with only one being enabled at a time.

The individual programs were created as separate projects in MPLAB but saved in the same folder, sharing a copy of the MCU header file. The COF files were then attached to the corresponding chip in the simulated hardware.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.18.83