Lab3 Io
Lab3 Io
Introduction
Welcome to our third lab! In this laboratory exercise you will learn the fundamentals of
input/output, timers, and interrupt programming. After finishing this lab, you should be able to
1. Write code for general-purpose input/output (GPIO) ports.
2. Write code to initialize and use simple input/output devices, such as buttons and LEDs.
3. Write code to initialize and use devices with handshaking, such as a programmable timer.
4. Write code to initialize and handle interrupts.
5. Have a general understand for how to read non-trivial technical manuals, data sheets, and
header files.
Preparations
You must do the lab work in advance, except for the Surprise Assignment. The teachers will
examine your skills and knowledge at the lab session.
We recommend that you book a lab-session well in advance, and start preparing at least a week
before the session.
You can ask for help anytime before the lab session. There are several ways to get help; see the
course website for details and alternatives.
During the lab session, the teachers work with examination as well as offering help. Make sure to
state clearly that you want to ask questions, rather than being examined.
Sometimes, our teachers may have to refer help-seekers to other sources of information, so that
other students can be examined.
Examination
Examination is for each assignment. Examining one assignment takes 5–15 minutes. Make sure to
call the teacher immediately when you are done with an assignment.
Please state clearly that you want to be examined, rather than getting help.
The teacher checks that your program behaves correctly, and that your code follows all coding
requirements and is easily readable. In particular, all assembly-language code must conform to the
calling conventions for MIPS32 systems. Compile-time warnings are not acceptable.
The teacher will also ask questions, to check that your knowledge of the design and implementation
of your program is deep, detailed and complete. When you write code, make detailed notes on your
choices of algorithms, data-structures, and implementation details. With these notes, you can
quickly refresh your memory during examination, if some particular detail has slipped your mind.
You must be able to answer all questions. In the unlikely case that some of your answers are
incorrect or incomplete, we follow a specific procedure for re-examination. This procedure is
posted on the course website.
This lab is examined in three parts. You can only be examined on one part at a time.
Part 1 – Assignment 1.
Part 2 – Assignment 2.
Part 3 – Assignment 3 and the Surprise Assignment.
starting value
third time
fourth time
...and so on
#include <stdint.h>
#include <pic32mx.h>
#include "mipslab.h"
In this file, write a C function that returns the status of the toggle-switches on the board, with the
following specification.
Function prototype: int getsw( void );
Parameter: none.
Return value: The four least significant bits of the return value should contain data from switches
SW4, SW3, SW2, and SW1. SW1 corresponds to the least significant bit. All other bits of the return
value must be zero.
Notes: The function getsw will never be called before Port D has been correctly initialized. The
switches SW4 through SW1 are connected to bits 11 through 8 of Port D.
g) In file time4io.c, add a C function that returns the current status of the push-buttons BTN2,
BTN3, and BTN4 with the following specification1.
Function prototype: int getbtns(void);
Parameter: none.
Return value: The 3 least significant bits of the return value must contain current data from push
buttons BTN4, BTN3, and BTN2. BTN2 corresponds to the least significant bit. All other bits of
the return value must be zero.
Notes: The function getbtns will never be called before Port D has been correctly initialized. The
buttons BTN4, BTN3, and BTN2, are connected to bits 7, 6 and 5 of Port D.
h) In file mipslabwork.c, modify the function labwork to also call getbtns. If any button is
pressed, the function getsw must be called, and the value of the variable mytime updated as follows
(see next page).
• Pressing BTN4 copies the value from SW4 through SW1 into the first digit of mytime. If
SW4 through SW1 are set to 0100, time would change from, say, 16:53 into 46:53.
• Pressing BTN3 copies the value from SW4 through SW1 into the second digit of mytime. If
SW4 through SW1 are set to 0100, time would change from, say, 16:53 into 14:53.
• Pressing BTN2 copies the value from SW4 through SW1 into the third digit of mytime. If
SW4 through SW1 are set to 0100, time would change from, say, 16:53 into 16:43.
• Pressing two (or three) buttons simultaneously should update both (or all three) relevant
digits of mytime.
The figures on the next page illustrate this with an example.
1 You may also (optionally) implement this function to support all four push buttons: BTN1, BTN2, BTN3, and
BTN4. This makes the function a bit more complicated because you need to use more than one Port. If you decide to
do this optional and more complicated exercise, please inform your lab assistant about this when you are being
examined on this exercise. Also, please note that you need to update the code for exercise h) accordingly.
If the toggle-switches (SW4 through SW1) have been set to 0100, pressing a button would change
the corresponding digit into a '4'.
14 : 53
4 (0100)
BTN3
In this example, the switches are set to 4 (0100 binary), so the selected digit changes into a '4'.
When running your code on the Uno32 board with Basic IO Shield, verify that you can set all three
digits: minutes (two digits) and seconds (only the tens digit).
Questions for Assignment 1
The following questions aim to help you check that you understand the code well. At the
examination, the teacher may choose to ask questions which are not on this list.
• Test pressing BTN3 and BTN2 at the same time. What happens? Why?
• Three device-registers for input/output control are TRISE, TRISESET, and TRISECLR.
Their functions are related. How? What are the differences?
• In the generated assembly code, in which MIPS register will the return values from
functions getbtns and getsw be placed in. You should be able to answer this question
without debugging the generated assembly code.
• In this exercise, we explained which bits that should be used in Port D and Port E. How can
you find this information in the PIC32 and ChipKIT manuals? Be prepared to demonstrate
how to find this information in the manuals.
Advice: check the lecture slides from lecture 5 for ideas.
In parts g and h, you write code to enable interrupts. All the other parts must be in place before
interrupts are enabled.
g) In file labwork.S, add a function enable_interrupt that executes the ei instruction (and then
returns to the caller). The ei instruction enables interrupts globally.
h) In file mipslabwork.c, add code to the function labinit, to enable interrupts from Timer 2, and
of course to enable interrupts globally. Before enabling interrupts globally, all other initialization
must be complete. Note: IEC(0) is at 0xbf881060, and IPC(2) at 0xbf8810b0.
Version history
1.3: Clarified and condensed questions, and specified the parts explicitly. 2016-01-13.
1.2: Updated assignment 3e. Clarified the meaning of function user_isr. 2015-10-06.
1.1: Updated assignment 1d, specifying that pic32mx.h not should be used, 2015-09-29.
1.0.1: Added KTH logotype, 2015-09-28.
1.0: First released version, 2015-09-28.