Lab 7: LED/Switch I/O
In this lab you have to implement a LED/button/switch I/O problem using a finite state machine (FSM). You will also capture the falling and rising edges of a pushbutton press using an oscilloscope.
Prelab (20)
Wiring and testing new components (10)
If your heartbeat LED and the wire connecting pin 13 and the 3.3 V rail are still disconnected from Lab 6 Task 3, reconnect them.
You will need to add the following parts from your kit to your breadboard:
1 x slide switch
1 x 51 ohm resistor (If your LED is too bright, try a 400-1000 ohm resistor instead. The TA can give you one in lab.)
1 x LED (color doesn't matter)
1 x pushbutton
Some wires (You can use jumper wires initially as you are figuring out how to place components, but if you have jumper wires on your board you will not get prelab credit)
Below is a part of a pin diagram of the PIC. The pins we are interested in for this lab are RB12, RB13, and RB14. These are pins 23-25.
RB14 = Pin 25
RB13 = Pin 24
RB12 = Pin 23
From Microchip DS70000657J
Connect the parts as follows:
RB14 to positive (long) lead of LED
Negative (short) lead of LED to ground
Resistor from RB14 to 3.3 V rail
RB13 to one side of pushbutton
Other side of pushbutton to ground
RB12 to a terminal of the slide switch
Adjacent leg of slide switch to ground
Here is one way to do this:
Overview of entire board. The green LED (and 51 ohm resistor) on the left lets me know the position of my on-off switch. This is optional.
I used a blue LED and a 680 ohm resistor.
You do not have to connect your components exactly like this, but:
Wiring should be neat and flush to the board. You should not have jumpers on your board when you are finished.
Component leads should be trimmed.
The switch and button shouldn't wobble and/or fall off the board when you use them.
To avoid confusing yourself and the TA, if you use a red LED it should not be right next to the heartbeat LED on RB15. Likewise the pushbutton should not be right next to the reset button.
Open the MPLAB project C:\ece3724\pic24_code_examples\chap8\ledsw1.X. Take a look at ledsw1.c. You will be modifying this program to solve your problem.
States from ledsw1.c with descriptions corrected/clarified
Build ledsw1 and copy the hexfile info into BullyCPP. Upload it to your board and test that your new LED, switch and pushbutton work properly.
State Machine Diagram (10)
Based on the last digit of your student ID number, you will be implementing one of the following LED/Switch I/O problems. Be sure to do the right problem or you will receive a 0 for the lab:
Last Digit 1 or 6:
Turn LED on. On press and release, turn LED off, goto 2.
After press and release, blink 2 times, freeze on.
After press and release, if switch = 0 goto (1) else goto next step.
Blink when pressed and held down. After release stop blinking. Another press continue blinking. After another release, goto (5).
Blink rapidly (twice as fast as 4). On press and release, go to (1).
Last Digit 2 or 7:
Turn LED off. On press and release, turn LED on, goto 2.
After press and release, begin blinking.
After press and release, halt blinking. If switch = 1 goto (1), else goto next step.
Turn LED off. On press only, goto 5.
Blink rapidly 5 times while button held down. If button released before all 5 blinks complete, go to (1). If all 5 blinks complete, freeze off, and release remains in this state (freeze off). A subsequent press repeats (5).
Last Digit 3 or 8:
Turn LED off. On press and release, turn LED on, goto 2.
On press, begin blinking. On release, freeze LED on and go to 3.
After press and release, if switch = 1 goto (1), else goto next step.
Blink five times. If all five blinks complete then: if switch = 0, go to 1, else goto next. Any press during the five blinks aborts and goto next step.
Blink rapidly while button held down. On release, goto 1.
Last Digit 4 or 9:
Turn LED on. On press and release, turn LED off, goto 2.
On press and release, blink 2 times, goto 3.
LED off. Blink while button held down. On release, if switch = 0, then goto (1), else goto next.
Blink rapidly. On a press and release goto next.
LED off. On press, turn LED on. On release, goto (1).
Last Digit 5 or 0:
Turn LED off. On press and release, turn LED on, goto 2.
After three press/releases, begin blinking. Another press and release, stops blinking, goto next step.
On press, turn LED on. On release, if switch = 0, goto 1, else goto next.
Blink rapidly. If five blinks are completed, goto 1. Any press and release before this go to step 5.
LED on. Any press and release, goto 1.
Draw a state diagram for your problem. This is a Moore machine where outputs are only dependent on the current state.
Each state should have a name and an output (what the LED is doing).
Put each state in a box or an oval, whichever you like.
Draw an arrow to the initial state.
Draw arrows between states to show the transitions, based on what is happening with the button and/or switch.
Below is an example of a state diagram for ledsw1. Your state diagram will be larger, with perhaps a dozen states:
Your state diagram should be of equal or greater quality to get prelab credit. You may draw with a computer. You may not use MS Paint or the equivalent.
TA Checkoff
Your board is wired with the pushbutton, LED, and switch. You can demonstrate to the TA that these components all work by advancing through the states of ledsw1. (10)
Components are not wired, components are untrimmed, wires are sticking out from the board. One or more components do not work properly when running ledsw1. (0)
The state diagram for your problem is complete, readable, and looks correct at a glance. (10)
Your state diagram looks like you made it in the elevator on the way to the lab. (0)
0.0
Distance Students Only: Take a picture of your new components and your initial state chart (similar to the photos above). Submit them with your report.
Task 1 - LED/Switch/Button State Machine (45)
Note: To blink means to flash on and off. Gradually dimming over the course of several minutes and then slowly reilluminating is not blinking. To blink rapidly means that the flashing of the LED should be visibly faster than when blinking at the normal rate. To press a button means to push the button down and hold it down.
Make a new project called myledsw1, by completing the following steps, all in MPLAB:
Copy ledsw1.X to a new project in the same directory and save it as myledsw1.X.
Save ledsw1.c as myledsw1.c.
Remove ledsw1.c from the myledsw1.X project.
Add myledsw1.c to the myledsw1.X project.
Close the ledsw1.X project. You can leave ledsw1.c open if you want to refer to it, but be careful not to edit it by mistake.
Replace the state names in the enum with the ones from your state diagram.
They should be capitalized.
They can't have spaces.
They all have to end with a comma, even the last one.
They don't have to start with STATE or contain an underscore, but your naming convention should be clear and consistent.
Good.
Bad.
3. Change the state strings
They should be in quotes.
They all end with a comma, even the last one.
The first part of each string should be the state name.
The second part of each string should be the output in that state.
You must have the exact same number of strings as you do states. Check your commas!
4. Now you have to change your switch statement.
There needs to be a case for each state.
The states must be named exactly the same as they are in your enum.
A state may or may not change the output, but it must have a transition to another state.
If a state needs to count the number of blinks, have it print the number of toggles (or number of blinks) on the screen.
If a state blinks indefinitely, it should not print an endless series of numbers to the screen.
Do not forget to put a break statement in each of the cases!
2 toggles = 1 blink
Build your program and upload it to your board. Test it: Go through every state and make sure the name of the state is correct and that the output (LED) is doing what it says on the screen. Make sure your program does what the description of the problem says it is supposed to do. Take a screenshot of BullyCPP showing most/all of your states.
A good screenshot of ledsw1 in action.
TA Checkoff
Your program performs correctly.
Task 2 - Pushbutton Fall and Rise Times (15 points)
VirtualBench Setup (Starkville students)
You will need to make use of a National Instruments VirtualBench oscilloscope for this task. You should not need to download any software. Plug the USB cable into your laptop. If you have only one USB port on your computer, you'll have to use the wall transformer to power your PIC.
Turn on the scope. The VirtualBench icon should appear in File Explorer under "This PC" like when you insert a thumb drive into your computer. Click on the icon to open the software.
Obtain a coaxial cable and place it in the connector on the VirtualBench box labeled channel 1. Make sure the switch on the probe is set to 1x, not 10x.
Click Default in the upper left to reset the settings:
Since you'll be measuring something between 0-3.3 V, you should probably set volts per division between 500 mV and 2 V so you get a nicely-sized waveform that doesn't go off the screen.
Where to set Volts/Div for channel 1.
You'll need to adjust Time/Div to get a waveform that isn't so steep that it is impossible to measure, but isn't so stretched out that it takes up more than one screen width. Here is where to change that:
To get horizontal and vertical cursors at the same time, select Manual and make sure they are both on channel 1:
You need to change Trigger to Edge and Edge Direction to Falling or Rising. Set Level to 1 V or so:
Measuring Fall and Rise Times with VirtualBench (Starkville students)
Connect your scope probe and ground clip to the circuit. (Leave myledsw1 running on your PIC so button presses will do something.)
Press/release the pushbutton until the scope triggers and you capture a waveform.
Your first waveform will probably not be quite right, so adjust the Volts/Div, Time/Div, position of the waveform, and Trigger Settings until you get one that looks like it would be nice to measure. Once you've used the Single Trigger, the trigger icon changes to Stop. Click Single to try again.
Move the horizontal cursors to the top and bottom of the waveform. Find ΔY.
Data below the waveform.
5. Based on Δ Y and the top (100%) and bottom (0%) of your waveform, calculate where 10% and 90% are and move the horizontal cursors to those levels. For example, if the bottom of your waveform is 0 V and the top is 3.3 V, set one cursor to 330 mV and the other to 2.97 V (or as close as you can get them).
6. Move the vertical cursors to the intersection of the horizontal cursors and your waveform. Δ X will be your rise or fall time.
7. You can take a screenshot by clicking the camera icon in the top left corner. Your screenshots should look something like this (but without the current elf):
Falling edge of pushbutton switch.
The following video shows Mrs. Jane Moorhead demonstrating the VirtualBench software to measure the falling edge of a pushbutton switch:
Place a short wire in the ground rail of your board near where the RB13 pushbutton connects to it. Connect this to the ground clip of the scope.
Connect the scope probe to the other side of the RB13 pushbutton.
Connection for the first two measurements
Configure the oscilloscope for single-shot falling edge capture, and capture the falling edge of a button press. Do not expect to see switch bounce when sampling this fast for this short of a time period. Measure the fall time from VDD to GND. Take a screenshot.
2. Configure the scope for single-shot rising edge capture, and capture the rising edge of a button press. (Hold the button down and then let it up.) Measure the rise time from VDD to GND. Take a screenshot.
Remove the scope probe and the wire it is attached to. Place a 51 ohm resistor between the (non-grounded side of the) button and 3.3 V (similar to how the reset button is connected). Connect the scope probe to the resistor as shown below:
3. Repeat the measurement of the rising edge of a button press. The external pullup decreases the total resistance to VDD and should result in a shorter (faster) rise time. The total resistance to VDD is decreased because the external pullup is in parallel with the internal weak pullup, and the external resistor has lower resistance than the weak pullup. Take a screenshot.
TA Checkoff
You have taken all three measurements and have obtained valid results and decent screenshots.
Report
Your report must have a title page.
You must textually describe what all your images are showing the reader, like this webpage does. E.g. "Below is a state diagram, showing the states used when implementing my problem." or "Above: Screenshot showing measurement of falling edge time of button press."
Label the first section Task 1.
Distance Students Only: Include the photos of your board and state diagram from the prelab..
Write a few sentences describing what you did for Task 1.
Include a description of the problem you were attempting to solve. (You can copy/paste or screenshot this from the lab writeup.)
Include a redrawn state diagram. Use the drawing tools in Powerpoint, Word or some other professional drawing program (not MS Paint or the like) to create the diagram – no scanning of hand-drawn diagrams is allowed. Be sure it is correct and accurately depicts the states you used in your program.
Include the screenshot of BullyCPP showing your console output.
Label the second section Task 2.
Write a few sentences describing what you did for Task 2.
Include a neat table or concise list of the times you measured. Don't just write: "The rise and fall times are in the screenshots", or something like that.
Include your three screenshots of the waveforms.
Submit your myledsw1.c file
Remove the default boilerplate header above the asterisks.
Change ledsw1.c to myledsw1.c in the file description
Add a header of your own including name, course number, lab number, lab section number, and filename.
Be sure the indentation and spacing is consistent and neat.
Make sure all comments are applicable to the code you are submitting. You don't need to add any extra comments (though it's fine if you do), but you should not, for example, have a comment that says Delay to make LED blinks visible next to a line of code that says LED1 = 0.
Build your code and run your program again to make sure it still works right.
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:
bad screenshots (not cropped properly, blurry, too small)
text is "lab jargon" unintelligible to an outside reader ("I put the button, LED and switch on." "Here is the screenshots of my task 3 measurements.")
text is phrased as instructions to a second party ("Take current measurements.")
text is copy-pasted from the lab writeup rather than using your own words
text is a literal recap of the activities you performed rather than communicating your results ("First I connected a small piece of wire to the ground clip. Then I inserted it into the ground rail of the breadboard. Next, I attached another small piece of wire to the scope probe...")
blatantly erroneous text ("These screenshots show the amount of current flowing through the pushbutton.")
garbled / confusing / gibberish text ("I put moar ASCII which is 0-E!!")
using vague or incorrect terminology
excessive text (Each subtask requires approximately 1-3 sentences, not paragraphs or pages)
careless use of pagebreaks that leave blank pages, 1-2 lines of text on a page, etc.
general unprofessional appearance
state diagram is ugly (arrows criss-crossing, bizarrely-sized state shapes, didn't use arrows, etc)
code is untidy
code is missing header or contains default header
code has inaccurate comments
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 45 points and the second task is worth 15 points. The following non-exhaustive list of errors will result in losing credit from a task:
missing screenshot/diagram/text
screenshot of the wrong thing
screenshot shows incorrect results
cannot read screenshot
state diagram incorrect
handdrawn state diagram
code won't build
code performs incorrectly
code is convoluted
code uses incorrect methodology (for loop, etc)
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.