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.
If your LED(s) 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 the following additional parts:
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 red LED (color doesn't matter)
1 x pushbutton
Some wires (If you have jumper wires on your board you will not get prelab credit)
Download the file lab7.zip from Canvas > Labs S2025 > Lab 7. It contains two files, ledsw2.c and lab7.c. Unzip it and put the files in C:\ece3724\pic24_code_examples\chap08.
ledsw2.c is a simple state machine that you can use as an example. It is essentially the same as the file ledsw1.c which is already in the chap08 folder.
lab7.c is what you will be working on in Task 1, and is the file you will be submitting.
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 rail
Resistor from RB14 to Vcc 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:
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.
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\chap08\ledsw1.X.
Add the files (Projects > Source Files > Add Existing Item) ledsw2.c and lab7.c.
Include ledsw2.c and exclude the other source files. Build ledsw1 (F11) and copy the hexfile info into BullyCPP. Upload it to your board and test that your new LED, switch and pushbutton work properly.
To see what "properly" means, read the code.
It doesn't matter how you connected your switch or which direction is which, just as long as one way performs the SW code and the other way does the !SW code.
Based your lab section, 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:
Section 07:
LED is 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 step.
Blink rapidly. On a press and release goto next step.
LED off. On press, turn LED on. On release, goto #1.
Section 03:
LED is 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. On press and release, go to #1.
Section 04:
LED is 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 step. Any press during the five blinks - abort and goto next step.
Blink rapidly while button held down. On release, goto #1.
Section 05:
LED is off. On press and release, turn LED on, goto #2.
After press and release, begin blinking.
After press and release, halt blinking. If select input = 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.
Section 06:
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 input select = 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.
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.c. 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.
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 ledsw2. (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 ledsw2. (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.
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 2x faster than when blinking at the normal rate. In Micro, "press a button" means to push the button down and hold it down, as opposed to "pressing and releasing" which is what normal people would do.
Include lab7.c and exclude ledsw1.c. Build your project and upload it to your board. It should run, but now do nothing except print "RELEASED_1 - LED is ____" in Bully.
Fill out the header with your name and etc. Copy-paste what your program is supposed to do from the description up above:
The next part of the program configures the LED, switch, and button. Don't change it for this lab, but if you ever need to add a component to your board, this is how it's done:
The next part of the program is where you put your state names and descriptions. You can either:
fill this out completely now, using the diagram you made for prelab, or
write the name and description of one state, and write the code for it, then go back and do the next state and etc.
Your states:
all have to have different names.
can't have spaces (use underscores instead).
have to end with a comma, except for the last one.
don't have to start with STATE, contain an underscore, or be capitalized but your naming convention should be clear and consistent. Don't name states things like "b1" and "R" just to save typing.
It's probably best to use state names like RELEASED_1 and PRESSED_2 for most of your states like ledsw2 does, rather than BLINKING_RAPIDLY and LED_ON_1, but it's up to you.
You need have a description for each state. It must contain the actual state name, followed by a hyphen, followed by what is going on in that state.
The first one is done for you (except for the blank), but if you want to rename it, go ahead.
If you want to see how something is done, look at ledsw2.c and just copy a similar case. Some guidelines:
For regular blinking use 250 ms, for rapid blinking 125 ms.
If the code calls for a specific number of blinks you must count toggles (or blinks) in Bully. If it just says "blinking" do not output an endless series of numbers.
A blink is 2 toggles.
Clear the blink counter in the state(s) right before blink counting states.
Use if statements, not while loops. This will make Lab 8 easier for you.
You need to have the same number of states and state descriptions or your program will perform strangely. Check that you have commas between the states and state descriptions.
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.
Your program performs correctly.
Take a screenshot of your BullyCPP console showing your program functioning properly. Try to capture all the states.
Example screenshot showing states of ledsw2.c. Note that you don't need the whole BullyCPP window - including it will probably make your screenshot hard to read.
You will use 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 either obtain a USB hub or 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:
Connect your scope probe and ground clip to the circuit. (Leave lab7.c 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.
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
1 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.
3a. 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:
3b. 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.
You have taken all three measurements and have obtained valid results and decent screenshots.
Screenshot/measurement of rise time
Screenshot/measurement of fall time
Screenshot/measurement of rise time with 51 ohm resistor
Your report must have a title page.
Write a sentence or two about what you did for the lab.
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 directions or your code.)
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. Do not even use a drawing program to freehand sketch the arrows. Be sure your diagram 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. We do not care about voltages, just the 3 times.
Include your three screenshots of the waveforms.
Submit your lab7.c file
Be sure it has the header and the description of the problem on top.
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.
The code you submit must work right and follow proper coding practices. If you somehow got checked off because the TA forgot to flip the switch, or didn't notice that your program blinks 10 times instead of 5, it's your responsibility to fix it before you submit your report. If you made any changes to your code, build your project again and upload it to your board to make sure it still works right.
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
using vague or incorrect terminology (C++, matlab, "a c-Code")
excessive text (Each task 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, a header on one page & content on the next, 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
didn't measure 90/10% right
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.