SPI Communication with the SN74HC595 Shift Register using Timers

Objectives

To develop and demonstrate individual proficiency in:

  1. Using SPI communication to control LEDs connected to a shift register

  2. Displaying binary numbers on LEDs connected to a shift register, updated periodically by a timer-driven interrupt.

Resources

Parts Needed

Item Quantity
Breadboard 1
PIC18F47Q10 Curiosity Nano -or- 1
PIC18F47Q10 DIP IC
8-bit serial-in, parallel-out shift register 1
LEDs 8
Current-limiting resistors for LEDs 8
Jumper wires Many

Background

Critical Information and Concepts Importance and Source
a. What a shift register is, why you would want to use one, and how it works Needed to conceptually understand this assignment and figure out which connections to make. Can be found in Scherz & Monk Section 12.8, and also in The Shift Register: Explained video.
b. How to wire a SN74HC595N serial in, parallel out shift register Needed to create serially-addressable LEDs. See the SN74HC595N datasheet for wiring information
c. How the SN74HC595N works See Section 9.1 and Table 1 of the SN74HC595N datasheet for information on how the shift register clock and storage register clock work.
d. How the SPI peripheral works in the PIC18F47Q10 Needed to understand how to wire and write SPI code for the PIC. Can be found in Section 27.2 of the PIC18F47Q10 Datasheet
e. Which pins on the PIC18F47Q10 can be used for SPI Needed to understand how to wire a SPI device to the PIC. The default locations for SPI communication can be found in Table 4 of the PIC18F47Q10 Datasheet

Steps

In this assignment, you may use either the PIC18F47Q10 Curiosity Nano or a PIC18F47Q10 DIP IC connected to your breadboard.

  1. Place the Curiosity Nano board as far to one edge of the breadboard as possible such that the micro USB port is accessible from the edge of the board. Connect 3.3V and GND from the Nano to the power rails of the breadboard.

  2. Place the SN74HC595N shift register near the Curiosity on the breadboard and wire it to 3.3V power and ground.

  3. Place 8 LEDs on the breadboard near the shift register.

  4. Connect the 8 output pins of the shift register to appropriately-sized current-limiting resistors connected to each of the 8 LEDs.

  5. Connect the serial clear pin of the shift register to 3.3V. This prevents the shift register from resetting due to electrical noise.

  6. Connect the output enable pin of the shift register to ground. This enables the register outputs so they can be seen on the LEDs.

  7. Connect the SPI 1 output pin on the Curiosity to the serial input pin on the shift register. (Hint: See Table 4 in the PIC18F47Q10 datasheet).

  8. Connect the SPI 1 serial clock pin on the Curiosity to the shift register clock pin on the shift register. (Hint: See Table 4 in the PIC18F47Q10 datasheet and Table 1 of the ).

  9. Connect the storage register clock pin on the shift register to a GPIO digital output pin on the same port as the other SPI 1 pins above.

Part 1: Displaying a Moving Pattern on LEDs

  1. Launch MPLAB® X and create a new project for your PIC IC. Note: Please remember that this is an individual assignment. The only way to learn to code independently is to work through coding problems and figure them out.

    1. Configure your system clock to use the HFINTOSC at 4 MHz with a clock divider of 4.

    2. In the Pin Manager, make sure that the Package matches the package of the PIC you are using.

    3. Add a SPI interface on serial port module 1. In the Registers tab, change SSPEN (serial port enable) to enabled. Leave all other settings default.

    4. In the Pin Manager, configure the SCK, SDO, and RCLK pins to match how you wired your breadboard. Make sure none of the pins are set to analog, that all pins are correctly set as inputs or outputs accordingly.

    5. Generate your MCC code. Then go to spi.h and identity the function that opens the SPI port along with the struct that contains the default operating mode. This is required to enable the SPI port.

      Please use MCC Classic. We can support it better, it has more advanced configuration options, but is also still relatively easy to use.

  2. Write code to do the following:

    1. Prior to the while() loop add the function to open the SPI port in its default configuration.

    2. Set the storage register clock pin to low (meaning that the storage registers will not yet store the values in the shift register).

    3. Write an 8-bit byte to illuminate some of the LEDs.

      (See spi1.h for the API function to do this)

    4. Wait 1 ms for the send operation to complete.

    5. Set the storage register clock pin to high (this low to high transition will cause the storage registers to store the current values in the shift register and output them to the LEDs).

    6. Wait 500 ms before the next operation.

  3. Compile, test, and (if necessary) debug the above code and hardware until you are able to light up LEDs connected to the shift register.

    If this step doesn’t work, debug by first checking all connections against the data sheet, looking for error messages in the IDE, and analyzing your code. If you’re still stuck, see the teaching team for help.

  4. Modify the above code to display a pattern where a single LED “chases” from one end of the row to the other, and then reverses direction. See video example.


Part 2

proceed to Part 2 of this tutorial…