Lab 8: Interrupts and Timers

This lab has you re-implement the Button/Switch/LED I/O problem from Lab 7. You will still follow the state machine approach, however you will now use  interrupts rather than polling or software delays. In the second task, you will generate a simple repeating waveform using a timer interrupt.

Basics

The folder chap9 of pic24_code_examples contains a number of reinterpretations of the basic ledsw1 state machine problem. 

Examine them and see how they differ from one another. Build  them and load them on your board. Try them out. You should notice that ledsw1_cn does not work properly.

Prelab (20)

Prelab Checkoff

Task 1 - Revised State Machine (20) 

Upload myledsw1_cn_revised to your board. It probably behaves erratically. It may jump through states, freeze up, and/or not blink right. This is what you need to do to get it running properly:

while (PB_PRESSED()) {

do something

}

becomes:

do something

if (PB_RELEASED()) {

stop doing the thing 

}

case PRESSED1:

LED1 = 0;

if (PB_RELEASED()) {

...

do this: 

case RELEASED1:

if (PB_PRESSED()) {

e_state = PRESSED1;

LED1 = 0;

}

break;

case PRESSED1:

if (PB_RELEASED()) {

    ...

If there are multiple transitions into a particular state, you need to edit all of them. Don't worry about blinking - you'll do that next.

TA Checkoff

Your state machine functions properly. Your code contains no software delay functions. The while(1) loop in main contains only IDLE();

Task 2 - Student ID Squarewave (40) 

Your MSU student ID is of the form Y1Y2Y3Y4Y5Y6Y7Y8Y9. Take each digit of your student ID, and add ‘1’ to it. Your task is to generate a waveform on an output pin (you pick the output pin) that looks like the following figure:

So how do you do this? Begin with chap9/squarewave. Copy it to a new project called mysquarewave and use Save As to create a new a source file called mysquarewave.c.

1. Create a state machine that has a state for each digit as well as a state for the 15 ms pulse width. Refer to  myledsw for an example of how to declare states and define them in the switch statement. You do not need the state strings or a function to print state names. Initialize the state machine to the Y5 state.

2. In each case of the switch statement:

PR2 = msToU16Ticks(X, getTimerPrescale(T2CONbits));

where X is the number of milliseconds for that state. 

3. Call your state machine in the Interrupt Service Routine for Timer2.

4. Build your project and upload to your PIC.

You should see "56789X" scrolling indefinitely in BullyCPP.

5. Connect the oscilloscope and examine the waveform generated to make sure it is correct.

6. At 57600 baud, it takes about 180 us to output one character. This will affect the timing of the waveform, so remove or comment out the outChar statements.

7. Capture a screenshot of your waveform showing all the pulses.

8. Measure and record the width of your 6 pulses. 

TA Checkoff

Your waveform displays properly on the scope. Pulse widths are correct.

15.0 ms!

Report

Distance Students: You do not need to include anything from the prelab. This task is worth 30 points.

   Distance students: This task is worth 50 points.

// names of the states 

or

static state_t e_state = state_y5_9; // initial state

Grading

If you do not successfully complete a task and your report fails to mention or glosses over this fact, you will receive a zero for the report portion of your grade as well as for the task. If you attempt to deceive the grader, e.g. by including screenshots not generated by the code you submitted, your instructor will be notified and you will be recommended for an Honor Code violation.

The report is worth 20 points for neatly and coherently presenting your information to a reader. The following non-exhaustive list of errors will result in losing credit from the report portion of the lab grade:

The tasks are worth 60 points. If your report indicates that you did not successfully complete or do not understand a task, you will lose credit, even if you performed it during the lab. The same is true for tasks performed during the prelab. There are two tasks. The first task is worth 20 points and the second task is worth 40 points. The following non-exhaustive list of errors will result in losing credit from a task:

Lab reports that flagrantly violate submission policy (wrong lab, no screenshots, no title page, no text besides headings/labels, mostly blank, code pasted into pdf, paragraphs of lab text pasted in, extremely sloppy/unprofessional, missing code etc.) will not be accepted. The student will receive a zero for the lab and may resubmit with late penalty.

References

[1] B.A. Jones, R. Reese, and JW Bruce, Microcontrollers: From Assembly Language to C Using the PIC24 Family, 2nd ed. Cengage Learning, 2015.