EENG 383

In Lab 2 - Morse Codes

Requirements

Working in teams of two, read through the following lab activity and perform all the actions prescribed. You do not need to document bullet items. Make a record of your response to numbered items and turn them in a single copy as your teams solution on Canvas using the instructions posted there.

Include the names of both team members at the top of your solutions. Use complete English sentences when answering questions. If the answer to a question is a table or other piece of art (like an oscilloscope trace or a figure), then include a sentence explaining the piece of art. Only include your answers, do not include the question-text unless it is absolutely needed.

Objective

The objective of this lab is to familiarize you with the MPLAB X development environment so that you can perform simulations with and without hardware. This lab will introduce you to two important tools, the oscilloscope and excel spreadsheets. You will use these frequently throughout the term.

External Hardware

Today you will write code that you can interact with through a button (input) and an LED (output). In order to accomplish this you will need understand how these devices are wired and controlled by the PIC.

  1. Open the schematic for the development board and locate the circuit for the the push button labeled "TOP". Determine the voltage (0v or 3.3v) and logic level (logic 0 or logic 1) that pin RA2 will see for the two possible states of the push button listed in the table below.

    You can verify your answers by connecting the bench digital multimeter (DMM) to the RA2 header break-out pin on the development board as follows. Power-up the DMM by pressing the power-on button on the lower left of the DMM. Next plug-in a black cable to the black COM jack on the DMM. Plug-in a red cable to the upper right red jack labeled "VΩHz". Press the white "DC V" button on the DMM. Connect the other end of the black cable to the ground loop on the development board. Connect the other end of the red cable to pin RA2 on the break-out header on the development board (lower right of the PIC 18F25K22). Pressing and releasing the TOP button should change the value displayed on the DMM.

    Button state Voltage at RA2 Logic level at RA2
    Unpressed    
    Pressed    
  2. We will be illuminating the LED at the bottom of the board by connecting a jumper wire from RB5 to either the R, G, or B header pin. In order to illuminate the LED you will need to assert a logic level on a PIC pin. The relationship between the logic level and illumination state of the LED is investigated in the following question.

  3. Open the schematic for the development board and locate the circuit for the the RGB LED labeled "SMT_RGBLED". Determine the voltage (0v or 3.3v) and logic level (logic 0 or logic 1) that the signal connected to R/G/B need to drive the LED into one of its two states (illuminated or off). If you are having problems with this, try the experiment described after the table below.

    LED state Voltage at R/G/B Logic level at R/G/B
    Illuminated    
    Off    
    Verify your answers by connecting a R/G/B header pin to a VCC/GND header pins surrounding the PIC using jumper wire as shown in the photo below.


Firmware Organization

Since today is the first time that most of you will have used the MPLAB X development environment we will first review the basics of how to create and simulate your code before diving into how the code manipulates the I/O pins of the PIC.

Create a MPLab X project

Start by launching MPLabX on your PC. Then select File → New Project.
  1. In your own words, what does the "#asm" directive do? You can find information in section 5.12.2 of the MPLAB® XC8 C Compiler User’s Guide. A link to this document is posted on the main course web page.
  2. Look-up the NOP instruction on page 388 of the PIC18(L)F2X/4XK22 Data Sheet. A link to this document is posted on the main course web page. Describe, in your own words, what this instruction does?
  3. In the inLab02 program, how is LED_PIN defined? What happen when you assign a value to this "variable"?
  4. When line 66, "LED_PIN^=1;" is executes, what behavior do you expect to see from the LED?
  5. Select the Classes tab in the Project Manager window. Expand the inLab02 selection, then the Libraries selection and then the Microchip\xc8\v1.38\include selection. Scroll down and double click on PORTA (NOT PORTAbits_t). Click Yes on the pop-up warning. Record the assembly language definition for PORTA (a couple of lines below the "#define PORTA PORTA" statement - it's the stuff inside the quoted asm statement). Note "equ" stands for equate and is just like a #define.
  6. Look at section 5.4.4. on page 77,78 of the "Microchip PIC18F25K22 Data Sheet". From the information on these pages interpret the meaning of the PORTA definition. Note when the technical documents describe a hexadecimal number they place the letter "h" after the numerical value. In order to solve this problem you will need to reference your answer to problem 7.

Firmware Operation

This program is designed to blink the LED attached to RB5 at 500Hz when the TOP button on the development board is pressed. Its important to note that in order to blink an LED on and off at 500Hz, the LED pin must be toggled 1000 times a second (or once every 1ms). This point will be important in the development of the code.

Before you start the following task, connect pin RB5 to one of the RGB header pins using a jumper wire.

Debugging code on the development board

There are times when you are having difficulty understanding what your program is doing that it will be handy to be able to single step through your code. Since we want the PIC 18F25K22 to be under the control of the MPLab X IDE, we need to the two connected via the PICkit 3. For this reason, when debugging your code through the MPLab X IDE, you must keep the PICkit 3 connected to the development board. While working through the following instructions if you find the instructions unclear, try consulting the figure below for some useful terms.


Identify warnings errors

Let's intentionally create a couple of errors in our program to see how they are identified and resolved. After introducing a change, click on "Clean and Build Project" (hammer and brush icon) in the toolbar. Note that "Clean and Build" is the same as compiling your project. You will need to scroll through the console messages in the Build area at the bottom of the MPLAB X window to answer to find the warning/error messages.
  1. Complete the following table by introducing each change one at a time. After creating an error, fix it before moving to the next row of the table. If a change creates both a warning and an error, note only the error. If a change creates multiple errors, note only the first.
Change Warning or Error Line number of Warning/Error Warning/Error message
Remove the semicolon at the end of line 66.      
Remove one of the "=" in line 62 yielding "while(BUTTON_PIN = 0);"      
Add a call to a non-existent subroutine "pugs();" on line 60      

Running code on the development board

When debugging code on the development board, the PICkit 3 remains attached to the development board so that it can halt the PIC 18F25K22 processor under your control (at a break-point or single stepping). Running your code means that the PIC 18F25K22 will no longer be under the control of the PICkit 3. For this reason, after you download your program to the PIC 18F25K22 (on the development board), you will disconnect the SNAP from the development board. Since the PIC 18K25K22 is a flashed-based part, now every time that the PIC is powered-up, it will run this program.

Observe firmware behavior on oscilloscope

You will be using an oscilloscope to measure the period of RB5. To do this setup your oscilloscope as follows:

Ch1 probe RB5
Ch1 ground clip Dev board ground loop
Horizontal (scale) 1ms
Ch1 (scale) 1V
Trigger mode Auto
Trigger source 1
Trigger slope
Trigger level 1.5V
Make sure to:

  1. Include the screen shot from the waveform save on your oscilloscope.
  2. Using the volts per division information on the oscilloscope screen and the height of the waveform (described in divisions), show your calculation for the amplitude of the waveform on RB5. For example, if your oscilloscope was set to 1v/divisions and the waveform had an amplitude of 4.6 divisions, then your answer would look like:
    Oscilloscope set to 1v/division
    The amplitude of the RB5 waveform is 4.6 divisions
    
    	  1v        
    	-------- *  4.6 divisions = 4.6v
    	division 
    
  3. Using the time per division information on the oscilloscope screen and the duration the waveform (described in divisions), show your calculation for the period of the waveform on RB5 using the same approach as the previous problem.

Firmware Experiments

As given the inLab02.c program does not generate a 500Hz waveform. In this section you are going to determine the exact number of NOP statements needed in the microSecondDelay subroutine function in order to create a precise 1 microsecond delay. To do this you will run the program three times; each time will have a different number of NOP statements in the microSecondDelay function and you are to record the delay created. You will then interpolate this data to determine the exact number of NOPs needed to produce a 1 microsecond delay.

To do this, you will need to create an excel spreadsheet as follows. When complete your spreadsheet should look something like this:


  1. Include the graph "us delay vs. # NOPs"
  2. What is the equation you entered in cell F12 and how many NOPs are required to generate a 1000 us delay?