SPI Communication with the SN74HC595 Shift Register using Timers (Part 2)

Introduction

This tutorial builds off of Part 1. Go Back and start there if you haven’t yet completed it.

Part 2: Displaying a Changing Variable Value on LEDs

  1. Study the following critical information and concepts:
Critical Information and Concepts Importance
a. Read through this tutorial on interrupts in MCC to understand how linking a callback works You will need to create a custom callback to process a timer-generated interrupt.
  1. Launch MPLAB® X and load your project from Part 2 of this assignment. In MPLAB® X, right-click on the project name and select “Copy” from the contextual menu. Then, right-click on the new project and select “Set as main project” from the contextual menu.

Next, you will modify the new project to add a hardware timer with 1-second period and interrupts enabled.

  1. Open MCC and update the following settings:

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

    1. TMRX:

      1. clock: lfintosc

      2. Set timer prescaler and postscaler so that the timer period range is adjusted so that 1000ms is within the range of values that can be specified for the timer period.

      3. Set the timer period to 1000ms

      4. Enable interrupts

    2. System Interrupts Page

      1. Ensure the TMRX and IOC receive interrupts are enabled. Make sure the timer subsystem is at the top.
    3. Generate the MCC configuration and compile the project.

  2. Open TmrX.h

    1. Find and note the name for the function that links the timer subsystem callback to a user-defined function.

    2. Find and note the name for the function that starts the timer.

  3. Modify your existing code in main.c to add the following features:

    1. Add an 8-bit unsigned integer global variable for a counter and initialize it to 0.

    2. Create a new function timer_callback() above main. It should return type void and take no input variables. Add code in the function that increments the counter by 1 every time it is called.

    3. Uncomment the two lines that enable global and peripheral interrupts.

    4. In main(), add code to:

      1. Set the timer interrupt handler to the callback function you wrote using the callback linking function.

      2. Enable interrupts

      3. Start the timer

      4. In the while() loop, write the current value of the counter to the shift register via SPI and then wait 1000 ms.

        If you set up your code correctly, your new callback function should be called once every second and update the shift register once every second.

  4. Compile and download your code to the microcontroller. The code should count up in binary and display the results on your 8 LEDs.