ELEC 220 Lab Course (ESCAPE)
ELEC 220 Lab Course (ESCAPE)
Collection Editor:
Matthew Johnson
ELEC 220 Lab Course (ESCAPE)
Collection Editor:
Matthew Johnson
Authors:
Joseph Cavallaro
Matthew Johnson
Chris Stevenson
Weiwei Wu
Translated By:
Weiwei Wu
Online:
< http://cnx.org/content/col11513/1.1/ >
CONNEXIONS
Rice University, Houston, Texas
This selection and arrangement of content as a collection is copyrighted by Matthew Johnson. It is licensed under
the Creative Commons Attribution 3.0 license (http://creativecommons.org/licenses/by/3.0/).
Collection structure revised: April 7, 2013
PDF generated: April 7, 2013
For copyright and attribution information for the modules contained in this collection, see p. 59.
Table of Contents
1
2
3
4
5
Introduction to Quartus and Circuit Diagram Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
A Quartus Project from Start to Finish: 2 Bit Mux Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Lab 1-1: 4-Bit Mux and all NAND/NOR Mux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
A Student to Student Intro to IDE Programming and CCS4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Lab 3-1 A "Real-World" Microprocessor: Basic MSP430 Assembly from
Roots in LC-3 (ESCAPE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6 Lab 3-2 Digital Input and Lab 3-2 Output with the MSP430 (ESCAPE) . . . . . . . . . . . . . . . . . . 31
7 Lab 4-1 Interrupt Driven Programming in MSP430 Assembly (ESCAPE) . . . . . . . . . . . . . . . . . . 37
8 Lab 4-2 Putting It All Together: An Interrupt Driven MSP430 Project
(ESCAPE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
9 Lab 5-1 C Language Programming through the ADC and the MSP430
(ESCAPE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
10 Lab 5-2 Using C and the ADC for "Real World" Applications with the
MSP430 (ESCAPE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Attributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
iv
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
Chapter 1
Introduction to Quartus and Circuit
Diagram Design1
1.1 Circuit Design in ELEC 220
For the rst lab and rst project of ELEC 220 we will be focusing on the creation of circuit diagrams using
Altera's Quartus II Web Edition
2
. In addition to simulating these circuits on a computer, we will also be
conguring Field-Programmable Gate Arrays (FPGAs) from these diagrams. Our target platform will be
Terasic's DE0 Development and Education Board
3 which uses Altera's Cyclone III FPGA chip.
1.1.1 FPGAs
An FPGA is an integrated circuit, composed of many logic elements, which can be recongured by the user to
reproduce a variety of circuits. Each logic element contains several dierent logic gates and memory elements
which can be used to recreate a wide variety of circuit components. The DE0 board we will be using has over
15,000 logic elements although we will only be using less than 1% of these, even for the calculator project.
Hopefully, after completing this project you will have a better understanding of the power and versatility of
FPGAs.
1.1.2 FPGA Conguration
In order for an FPGA to emulate a desired circuit, it must rst be set to the proper conguration specied
in a data le uploaded to the board. This data le is created, and often uploaded to the board, using FPGA
design software such as Altera's Quartus II. By providing Quartus with information regarding the specic
FPGA to be congured, a Quartus project can be easily replicated on an FPGA, shortening delays between
concept and protype stages in designing circuits.
1.1.3 HDL vs. Schematic Diagrams
There are two ways to specify the intended function of a Quartus project. The more straightforward method
is to simply create a schematic diagram of the desired circuit as though you were drawing it out on paper or
building it on a breadboard. This has the advantage of being very easy to grasp, however, it requires you to
work out the logic for the entire circuit and lay out all the components. The other more abstracted method
is to use a Hardware Description Language (HDL) such as Verilog. Writing using this specication language
1 This content is available online at <http://cnx.org/content/m42303/1.1/>.
2 http://www.altera.com/products/software/quartus-ii/web-edition/qts-we-index.html
3 http://www.terasic.com.tw/cgi-bin/page/archive.pl?Language=English&CategoryNo=56&No=364
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
1
2
CHAPTER 1. INTRODUCTION TO QUARTUS AND CIRCUIT DIAGRAM
DESIGN
allows you to speciy the intended function of the circuit from which Quartus creates an optimized circuit
layout. Although this method does not give you as much individual control over the design, it allows you
to more easily go from concept to end product by tasking your computer with the bulk of the design work.
However, for simplicities sake, we will be using schematic diagrams for the majority of this course, with HDL
les provided to you for use in later designs.
1.2 Course Overview
The rst few labs are designed to give you practice in using Quartus to create schematic diagrams by tasking
you with creating schematics for circuits you are already familiar with. They also provide a brief review of
the inner workings of components which will be used extensively in later designs. We will then move onto
more complex circuits when we introduce the idea of a clock signal as an input to a circuit and design nite
state machines. Finally, this section of the lab will culminate in the design of a simple calculator, though
it's not quite like most simple calculators you may be used to. This will draw on previous lessons on circuit
design, nite state machines, and clock signals while also providing an introduction to simple computers that
will carry on into the next protion of the lab section.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
Chapter 2
A Quartus Project from Start to Finish:
2 Bit Mux Tutorial1
2.1 Building Projects in Quartus
This section is intended to provide an in-depth introduction to creating projects in Quartus, laying out
a circuit diagram, simulating the circuit, and nally using the project to congure an FPGA through an
example project showcasing a 2-bit MUX. Altera has made a very nice tutorial for Quartus as well which
2.
you can nd here
Altera's tutorial is meant for a dierent board than the DE0 we will be using so make
sure to account for that. Also, they have a slightly dierent method for connecting inputs and outputs to
the FPGA. Either method works and you can use whichever one you prefer, however, the method set forth
in this section will likely be more straightforward and user-friendly. Additionally, you can access another
tutorial from within Quartus at any time by clicking on Tutorial in the Help menu.
2.1.1 Starting a Quartus Project
A Quartus project acts as a support structure for a collection of design les. It serves to bring them together
in a common working environment, dene their relationships both within the project to each other and to
the FPGA, and dene common characteristics. All work in Quartus starts with a project.
•
Begin by opening Quartus II Web Edition. A screen titled Getting Started with Quartus II Software
should open from which you can select Create a New Project.
Otherwise select File->New Project
Wizard. Make sure you select this and not simply New, which would instead create a new le.
•
In the working directory eld specify the folder,My_Quartus_Projects for the purpose of this example, to save your project in. While you can make this folder on your U: drive, Quartus will generally
run faster if working on projects in the C: drive. It is recommended to make temporary folder on the
C: drive to put your projects in and transfer them to your U: drive for safe keeping. Note that Quartus
will not create a folder for the project les in this location, it will merely save the les here so make
sure the lowest level folder is somewhere set aside for this particular project. This will make it easier
to locate les in the project and to transfer the project between dierent computers. Finally, enter
the desired name for your project, the nal eld for the top level design le name will ll itself in as
you name the project. It is recommended for simplicities sake that the project and the folder it's in
have the same name. Also note that Quartus will not let you use spaces in your naming, underscores
or dashes are recommended instead.
The name 2_bit_mux will be used for the purposes of this
example.
1 This content is available online at <http://cnx.org/content/m42302/1.3/>.
2 http://cnx.org/content/m42302/latest/ftp://ftp.altera.com/up/pub/Tutorials/DE2/Digital_Logic/tut_quartus_intro_schem.pdf
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
3
CHAPTER 2. A QUARTUS PROJECT FROM START TO FINISH: 2 BIT
MUX TUTORIAL
4
Figure 2.1:
•
Speciying a Project Location and Name
Next you will see the Add Files screen. All of the labs and projects you will be working on will either
have all necessary les included or be started from scratch so we won't be using this feature for now.
It is also possible to add les whenever you open a le or save as and we will want to do this during
this tutorial in order to ensure our project works as expected.
•
After this you will have to specify your target FPGA. The FPGA in the DE0 board we will be using is
a
Cyclone III EP3C16F484C6. You can also nd this information by looking at the specication
printed on the chip itself.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
5
Figure 2.2:
•
Selecting the correct FPGA
The next screen allows you to specify other programs to use with this project in addition to Quartus.
We won't be using any of these so just click next. After reviewing everything on the nal screen to
make sure it's set up as you want it and you're ready to begin laying out your circuit.
2.1.2 Building a Circuit in Quartus
•
Although we specied a name for our top level design le, we still need to create it. Go to File->New
or hit Ctrl+N and select Block Diagram/Schematic File under Design Files. Once it's open go ahead
and Save As, Quartus should automatically give it the same title as the project. Make sure that the
box titled Add le to current project is checked before saving and that the le is being saved into
the project folder.
•
You should now see a grid of dots and just at the top of it a toolbar. This is where most of our work
in Quartus will take place.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
CHAPTER 2. A QUARTUS PROJECT FROM START TO FINISH: 2 BIT
MUX TUTORIAL
6
Figure 2.3:
•
The toolbar and some of the tools which will be frequently used.
In the upper left corner of the window is the project navigator.
Since we only have one le in our
project, there's not much to see here, but if we had more we would be able to easily keep track of the
hierarchy of all the les within the project. Additionally, we can easily open up les associated with
this project by double clicking them within this box.
•
We'll start by adding symbols to our schematic. Normally you would want to rst plan out your circuit
design by using Karnaugh maps to write logical functions for the operation of your circuit, however,
we'll proceed as though this step has already been completed.
•
Click on the place symbol tool to open up the library of available symbols. This can include the default
symbols included with Quartus as well as any user created symbols. Within the Quartus library, the
majority of the symbols we'll be using will come from the primitives folder. Start by nding a two
input AND gate.
You can either navigate to the logic folder under primitives and nd the gate
labeled and2 or simply search for this symbol using the name box below the browser. Note that the
name typed here has to exactly match the symbol name for Quartus to nd it. Before you click okay,
make sure that the box labeled repeat-insert mode is checked as shown below.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
7
Figure 2.4:
•
The Quartus symbol browser
Place two AND gates onto the grid. Although their relative position isn't that important since we can
remotely connect symbols, it always helps to have a neat circuit layout so for now place them relatively
close together. Once you're done, hit escape to exit from placement mode.
•
Continuing on, go back to the symbol browser and select an or2 gate, also located in primitives->logic.
Place one of these gates to the right of your two AND gates.
•
Next we'll add an inverter to implement the select logic for the MUX. In the symbol browser nd the
not gate. Place this close to the input of one of the AND gates. Note that it shouldn't be a problem
at this point, but if you ever nd yourself running out of room on the grid, drag a component to the
edge of the screen to expand the available area.
•
Now we'll add in I/O pins.
This is where signals will enter and leave the schematic.
They can be
connected to other schematics in the project or connected to inputs and outputs on the board, though
we'll dene these connections later. For now, go to the drop-down menu on the Pin Tool and choose
input. Again, you can place your pins anywhere due to remote wiring, but for now, place two pins to
the left of your logic gates for the inputs to the MUX and one above them for the select signal. Go
back to the symbol tool, select output, and place one output pin to the right of your circuit for the
output of the MUX. Right click on your I/O pins and select properties. From here give each pin a
representative name, which will help out in the later I/O assignment phase.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
CHAPTER 2. A QUARTUS PROJECT FROM START TO FINISH: 2 BIT
MUX TUTORIAL
8
Figure 2.5:
•
One possible way to layout your gates and pins on the grid
The nal step will be to connect all our components together. As previously mentioned, we can run
wires directly between components or make wireless connections.
•
To make a wired connection, either select the orthogonal node tool or move your mouse over one of the
ports on a symbol, the pointer should change to the look like the node tool. Then click and drag from
the origin port to the port you wish to reach and release. Be careful not to intersect any other ports
as this will cause them to be joined to the wire, although crossing over other wires will not create a
connection. You can tell if a wire is connected to something by the large dot, the typical indicator of
connections in circuit diagrams. Go ahead and connect up the inputs to the two AND gates so that
they will function as the beginning of a MUX.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
9
Figure 2.6:
•
Possible circuit wiring for the rst stage of the MUX
While this method is fairly straightforward, it has its disadvantages such as possible unintended connections and vast webs of wiring on more complicated circuits.
We can simplify the process with
wireless connections.
•
Unfortunately, you cannot directly name ports. Instead we will connect a small piece of wire to the
ports and name this wire. Any wires on the grid which share the same name are connected together.
•
Begin by placing short bits of wire at all the remaining ports in the circuit. A length of one on the
grid is sucient though a length of two may be easier to work with. Once placed, right click on the
wire and select properties.
•
Under the General tab you can enter a name for the selected wire. As with project names, Quartus
won't allow for spaces so either remove them or use underscores. To connect any other wire with this
named one, simply repeat the procedure. Using this wireless method, connect the remainder of the
MUX together.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
CHAPTER 2. A QUARTUS PROJECT FROM START TO FINISH: 2 BIT
MUX TUTORIAL
10
Figure 2.7:
•
Example wireless connections
The nal step before we get to pin connections is to make sure our circuit is functional. On the left side
of the screen is the Tasks menu where we can nd a variety of commands to create a nished design
le. Eventually we will want to compile our whole design, though for now we can simply go through
the Anaylsis & Synthesis step. Double click on Analysis & Synthesis for Quartus to check over the
circuit for any potential issues such as unconnected ports. If Quartus nds something wrong it will
halt the process and display the error in the message box at the bottom of the screen.
Figure 2.8:
An unsuccessful analysis & synthesis process due to an unconnected port
2.1.3 Dening I/O Connections
•
Once we've successfully performed Analysis & Synthesis we are ready to move on to dening pin
connections.
In order to do this we rst need to know the pin addresses of the input and output
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
11
3 on pages 24-29, pages 27-32
devices on our DE0 board. These can be found in the DE0 User Manual
of the PDF. For now we will only be looking at the switch and LED pin assignments.
•
For each entry in the assignment table, the signal name corresponds to the identier printed on the
board next to the relavent device and the pin name tells us where we should connect to in order to
access that device.
•
To specify these connections we will use the Pin Planner located under Assignments->Pin Planner.
By performing Analysis and Synthesis earlier, we gave Quartus information on how many I/O pins we
had on our circuit diagram and what their names were. Now we just need to connect these with pins
on the board. By putting in the name of a physical pin under the Location column in the Pin Planner,
we tie that point on our board to the specied point on our circuit.
•
Although we'll be using a particular pin layout here, you can setup your pin assignments in whatever
way you feel works best for you. On future labs/projects pin layouts will already be setup so the labbies
and in particular the project graders will be expecting a particular board setup and you should leave
assignments as they are.
•
For this example we'll use the rightmost slider switch, SW[0], for our select signal. Since we can see in
the user manual that SW[0] is tied to PIN_J6, we simply type this, or even just J6 and it will ll in
the name, into the Location column next to the select listing under the Node Name column. We'll
continue in this fashion, assigning data1 to SW[1] at PIN_H5, data2 to SW[2] at PN_H6, and
assigning out to the rightmost LED, LEDG[0] at PIN_J1.
3 http://www.terasic.com.tw/cgi-bin/page/archive_download.pl?Language=English&No=364&FID=0c266381d75ef92a8291c5bbdd5b07eb
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
CHAPTER 2. A QUARTUS PROJECT FROM START TO FINISH: 2 BIT
MUX TUTORIAL
12
Figure 2.9:
•
Example pin assignments in the Pin Planner
Now that we have nished assigning pins, we can go back and run the complete compilation process.
Double click on Compile Design in the Tasks menu of the Project Navigator to run all of the sub tasks.
Inevitably, you will get warnings about some features not being available without a subscription and
there not being a clock, since we didn't need one. Also, you will get critical warnings telling you that a
specic design le is needed for the Timing Analyzer. These extra features are not required and these
warnings can be ignored.
2.1.4 Waveform Simulation
•
Before actually programming the FPGA on the board, it is a good idea to simulate a variety of inputs
to our circuit and check the responses. Although the ability to simulate inputs to circuits was removed
from Quartus II beginning with version 10.0 , these features can still be used with the Altera University
Program Simulator
4.
4 http://www.altera.com/education/univ/software/qsim/unv-qsim.html
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
13
•
Opening the Altera U.P. Simulator should open two windows, the U.P simulator and Qsim.
Qsim, select File->Open Project, and select your .qpf project le for the 2 bit mux.
Go to
Next go to
File->New Simulation Input File to open up the Simulation Waveform Editor.
•
Right click in the white space under the Name heading and select Insert Node or Bus.
window click the Node Finder button.
From this
Finally, click the List button to have the Waveform Editor
import the I/O ports from the project le. Move all of these nodes over to the Selected Nodes box and
return to the Waveform Editor Window which should now list these I/O ports along the left side. By
clicking and dragging the name of a signal you can rearrange the order they are displayed in, useful
for separating the input and output signals. Go ahead and save the waveform le in the project folder
for the 2 bit mux.
Figure 2.10:
•
Simulation Waveform Editor Window
To begin with, all inputs are set to a constant value of 0 and the output is undened since we have not
yet run the simulation. Note the timing intervals displayed along the top. These are not as important
now, but will be very useful once we start building project with clocks.
•
To change the value of an input, click and drag along a waveform to select one or more intervals. Once
selected, you can change the highlighted interval with buttons in the toolbar to set intervals low, high,
undened, opposite of their current value and several other options. For the purposes of testing all
possible input combinations, we can either manually set the intervals or use the Overwrite Clock
button to set up several alternating signals of diering periods.
•
For starters select the entire data1 signal by clicking the name and then click the Overwrite Clock
button. The Waveform Editor should have defaulted to a total time of 1000ns so set the period of this
signal to be 250ns. Select the data2 signal and give it a signal with a period of 500ns and then a signal
with period of 1000ns for select. Over the 1000ns of the simulation, this will test all the possible input
combinations. Once nished save this waveform le and return to Qsim.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
CHAPTER 2. A QUARTUS PROJECT FROM START TO FINISH: 2 BIT
MUX TUTORIAL
14
Figure 2.11:
•
Waveforms to test all input combinations
Go to Assign->Simulation Settings. The Waveform Simulator supports two modes: Functional, where
only the logic of the system is tested and timing is not considered, and Timing, where delays and
other timing constraints are taken into account. In order to perfom Functional simulation you must
rst go to Processing->Generate Simulation Netlist, but for now we'll just do a Timing simulation. In
the Simulation Settings box make sure Timing is selected and then browse for the waveform le you
created.
•
Finally go to Processing->Start Simulation or click the blue arrow over the waveform. The simulator
will run and once nished it will open up waveform window containing your specied input waveforms
and the resulting output. Once you are satised with the results or have made the necessary changes,
we can move to the nal step, programming the board.
Figure 2.12:
Simulation Output
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
15
2.1.5 Programming the Board
•
Now that we are certain our project will function as intended, we can program our FPGA. Make sure
that the DE0 board is plugged into the computer and powered on.
The DE0 oers two modes of
programming: one which retains the program in volatile memory only as long as the board is powered
on and another which stores the program in non-volatile memory to be retrieved when the board is
powered on. For our purposes the volatile memory storage will be sucient. To set the board for this
programming method, make sure the switch next to the 7 segment display is set to RUN.
•
In the Tasks menu below the Analysis & Synthesis and Compile Design commands we used earlier,
click on Program Device. Next to Hardware Setup should be listed USB-Blaster [USB-0]. If not, click
on Hardware Setup and select USB Blaster from the drop down menu. Make sure that Mode is set to
JTAG and that the Program/Congure box next to the .sof le is checked.
•
Once ready click Start and wait for the board to be programmed.
You can see the state of the
programming process in the message bar where it will inform you once it's nished. If you followed
the same structure as the tutorial, SW0 should serve as the select switch with SW1 and SW2 toggling
the two data inputs high or low. With select in a low state, the mux will take the value from SW1 and
in a high state the value from SW2, either of which will be output on LEDG0.
•
This concludes the tutorial on Quartus projects. It should now be a simple matter to create a 4-bit
mux and move on to the rest of the projects.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
16
CHAPTER 2. A QUARTUS PROJECT FROM START TO FINISH: 2 BIT
MUX TUTORIAL
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
Chapter 3
Lab 1-1: 4-Bit Mux and all
NAND/NOR Mux1
3.1 Lab 1-1
3.1.1 4-Bit Mux
For the rst part of Lab 1 you will rst design a 4-bit mux, similar to the 2-bit mux covered in the tutorial
on Quartus projects found here (Chapter 2). An already started Quartus project complete with I/O pins
and pin assignments for the DE0 board can be found on OWL Space. This will provide a helpful starting
point and ensure a common board setup, making it easier for the labbies to check your circuits' functionality.
Download the .zip le and extract it to a temporary working directory on the C: drive, making sure to move
it over to your U: drive before you leave the lab.
3.1.2 NAND or NOR only Mux
Create a new version of your original 4-bit mux using only NAND or only NOR gates. A useful tutorial for
2
NAND/NOR conversion can be found here . Do not use inverters (NOT gate) or gates with inverted inputs
in your design.
1 This content is available online at <http://cnx.org/content/m42304/1.3/>.
2 "An algorithm to implement a boolean function using only NAND's or only
<http://cnx.org/content/m13240/latest/>
NOR's."
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
17
18
CHAPTER 3. LAB 1-1: 4-BIT MUX AND ALL NAND/NOR MUX
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
Chapter 4
A Student to Student Intro to IDE
Programming and CCS41
Firstly, this is by no means a comprehensive guide, but a few basics for students who have not been
exposed to working in an IDE before.
To look more closely at CCS4, see the help docs on ti.com
(http://processors.wiki.ti.com/index.php/Category:Code_Composer_Studio_v4
2)
4.1 What is an IDE:
IDE stands for Integrated Development Environment, and the philosophy behind creating an IDE is to
combine all of the separate tools you would need to write, debug, and deploy code into one consistent
program. Basically, CCS4 allows you to write code (in C, C++, or assembly) and push a single button to
compile, assemble, link, and upload your code to the device (in our case the MSP430). CCS4 also has a
built in debugger that launches when you run in debug mode, interfacing in real time with the hardware
(through JTAG) and allowing you to see if your code does what you think it should do. Ultimately though,
a sophisticated IDE is only a tool that allows you to write clean code more quicklyit will not code for you
and relies on you the programmer to use it and take advantage of its potential.
4.1.1 CCS4 and Eclipse:
CCS4 is TI's embedded specialty version of the eclipse framework.
The eclipse IDE was developed open
source for Java, and you will most likely see it again if you pursue higher level programming courses. Code
Composer takes the framework given by Eclipse and tailors it to TI's embedded processors and the real time
needs of DSP. The things you learn about working in an Eclipse based work environment (or any sophisticated
IDE) should help you eciently write and debug code in the future.
can create dierent
Eclipse is highlycustomizable.
You
perspectives (see control buttons upper right hand corner) with dierent information
views. Check out the view and window menus to explore dierent panes you can use.
4.1.2 Licenses
When you rst open CCS4 on a computer, you will have to add the license server information (if you are a
student using a university network license) or specify the location of the individual license le.
1 This content is available online at <http://cnx.org/content/m37154/1.2/>.
2 http://processors.wiki.ti.com/index.php/Category:Code_Composer_Studio_v4
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
19
CHAPTER 4. A STUDENT TO STUDENT INTRO TO IDE PROGRAMMING
AND CCS4
20
4.1.3 Workspaces and Projects:
When you rst start up CCS4, it will ask you to specify a
workspace. This le directory is where CCS4 will
save all of your raw C and asm les, as well as the compiled and linked executables before uploading them to
the hardware. Inside your workspace, the Eclipse environment divides your les into projects. Each project
has its own independent source les and conguration properties. In general, each lab you will complete for
this class will be setup as a new project. One project at a time can be set as the Active project (by default
it is the most recently created one. You can view and edit les from any project at any time, but pressing
the debug button will compile and load the code for the active project, not necessarily what you think you
are working on!).
4.1.4 Setting up a new project:
To start setting up a new project, go to the New project wizard (le→ new
→
CCS Project). The rst step
nameenter one you like! In the next window, it asks to select a project type. In this
lab we will be using the MSP430, so select it from the drop down menu and click next. (Don't worry about
the build congurations, the defaults are ne). The next window asks about project dependencies. . . in
asks you for a project
other words, does your project need to reference functions and les already in another project. Most likely
for this class you won't have any, so again,
leave this as is and click next. Now you have arrived at the
most important section. This page congures the device specic compiler and assembler. For the Device
MSP430G2231. Lastly, If you are working on one of the earlier labs with
be sure to continue to the next menu and select the "Empty Assembly Only
Project" template. This tells the IDE not to invoke the compiler and skip straight to assembling and
Variant, select our chip, the
only assembly code,
linking. If you forget to set this option, the compiler will throw an error that it cannot nd the required c
function void main() in your assembly code. Don't worry if you mess something up, you can create a
new project and just copy your code straight over.
4.2 The code perspective and writing code
Code Composer supports assembly code, classic C, and C++.
For this class we will focus on assembly
code and standard C. Most of your coding will happen in the coding perspective, a view where the screen
is dominated by a massive text editing window.
Code Composer's editor can be setup in a range from
straight forward wyswig to auto-tabbing, auto-highlighting, and auto-completing. Again, explore the options
(window→preferences) and nd what works best for you and your lab partner.
4.2.1 Writing Assembly:
To write assembly in Code Composer, you rst need to create a new project following the steps above (be
sure to select Empty
(le
→
new
→
Assembly-only Project!). Once you have your empty project, insert a new le
le). When you input the le name, be sure to give it an .asm extension. Now that you
have your freshly created asm le, you can start writing code in the code window (the big blank white space
in the middle of the screen). In assembly mode, code composer parses the column most left as labels, so
any non-label code must be indented at least one tab (and conversely labels cannot be indented). You will
learn more about the specic components required for a functional assembly le in your specic labs, but
in general, you need ve common lines. The rst, .cdecls C, LIST, msp430g2231.h denes all of your
programming constants (such as P1IN, WDTCTL, etc.). The second .text tells the assembler where your
actual code begins. The label RESET goes at the start of your program so the hardware knows where to
begin code execution after a power reset. At the end of your code, you need to leave the memory address of
your reset label. To do this, use the command [.sect .reset] to tell the compiler you are in the reset section,
and then [.word RESET] to place the address of the RESET label into memory.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
21
4.2.2 Writing C:
Code composer really shines writing C and C++. Like in assembly, you will need to create a new project
for your new program. This time leave treat as an assembly-only project unchecked. Now you will create
a new c source le (le→ new
→
source le). When you input the le name this time, be sure to give it
a .c extension. In c mode, you don't have to worry about line spacing or tabbing for the functionality of
the program, just your own sanity and code readability. To include the le you used in the .asm projects
that dened all the hardware constants, put the line #include
msp430g2231.h at the top of your
code. You won't have to worry about the reset vector or anything like thatthe c compiler will take care of
it all for you. The only thing actually required in your c program is the function CODE. . .}
void main() {. . .YOUR
. Other more advanced operations (like interrupts) require special c syntax, but you will cover
that in the specic labs when it comes up.
4.2.3 Debug Mode, Stepping, Breakpoints, and Watches
Debug mode dierentiates an IDE like CCS4 from simpler command line tools.
For better or for worse,
simply pressing the debug button magically translates your source code into a running program on your
attached MSP430.
You will notice that after the debugger nally starts up though, your code will not
actually be running.
This is because the debugger starts in
step mode with the rst line of your code
highlighted. In other words, the hardware is waiting for you to let it execute that one line of code, so your
slow human reexes can process and verify what it can do in a fraction of a second. Stepping through your
code one line at a time helps you nd subtle errors and see exactly where a program goes o track. Yes, as
you can imagine, simply stepping through a real world multi-thousand line program (or the larger programs
you will write later in this course) is inecient and unfeasible.
Breakpoints allow you to tell the debugger
to stop if/when the processor gets to a certain point in your code, letting you run quickly through the code
you trust and only stop at certain problematic sections you want to look into more closely.
You can set
several breakpoints at once, and once the program has broken, you will be able to actively see all register
and memory values and step through line by line just as if you had started step mode at your break point.
Watches are a little bit more abstract and more useful for larger programs, but they allow you to set a
watch on a particular variable (in c) or memory location/register (in asm) and only break the program when
it tries to change that particular value. This can help you nd where exactly where and when a value changes
into an erroneous state.
Using a combination of breakpoints, watches, and careful stepping, you can pick apart any complicated
program to hunt down errors and really understand what goes on during the program's execution.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
22
CHAPTER 4. A STUDENT TO STUDENT INTRO TO IDE PROGRAMMING
AND CCS4
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
Chapter 5
Lab 3-1 A "Real-World" Microprocessor:
Basic MSP430 Assembly from Roots in
LC-3 (ESCAPE)1
5.1 An Intro to the MSP430 from the LC-3
This week you will go over the basic dierences between the MSP430's assembly ISA and the LC-3's, and
learn how to write a basic assembly program for the MSP-430 using TI's Code Composer Studio. You have
two main tasks ahead of you:
1. Following the ESCAPE platform labs 0 and 1, setup and establish communication with an ESCAPE
sensor board. Run the test program to see if you can communicate wirelessly from the computer and
if your sensors are working.
2. Coding in MSP430 Assembly,
implement a Fibonacci sequence calculator. This should be done
with a loop and run innitely.
Debugger.
Step through, explain, and demonstrate the code, using the CCS4
Be sure to view the registers while stepping through the program.
Observe the amount
of CPU cycles each of the instructions takes to complete. Detailed Instructions (Section 5.3: Part I
Assignment Detail)
5.2 Some Background Information
5.2.1 Main Dierences Between MSP430 and LC-3
• The MSP430 has a larger assembly instruction set than the LC-3
MSP430 assembly includes some task specic instructions (Such as
inc
reading the language
Some MSP430 assembly instructions are interpreted instructions (Such as
and
pop
dec)
and
to simplify
push)
Denition 5.1: Interpreted Instructions
An instruction that is decomposed by the assembler into several smaller/ more basic
fundamental instructions.
Example
pop R3
1 This
contains two implicit instructions:
mov @SP, R3
and
add #0x02, SP
content is available online at <http://cnx.org/content/m45850/1.1/>.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
23
CHAPTER 5. LAB 3-1 A "REAL-WORLD" MICROPROCESSOR: BASIC
MSP430 ASSEMBLY FROM ROOTS IN LC-3 (ESCAPE)
24
Math and logical instructions are similar, but do not have a specic destination.
•
MSP430 instructions come in two avors, dual operand and single operand. Neither type has an
explicit destination register, rather, the last operand serves as the destination too.
For Example:
add R4, R5
in MSP430 assembly corresponds to
add R5, R4, R5
in LC-3
warning: Be careful to not overwrite data you wish to keep! If you need to preserve
the values in both operand registers, you will need to save one of them rst using a
mov
instruction.
MSP430 Supports some byte as well as word instructions
•
Some MSP430 instructions allow you to address and write/read from a specic 8 bit byte in
memory instead of the entire 16 bit word. The MSP430 memory has byte level addressability,
but word instructions only operate on even numbered memory addresses (implicitly modifying
the next odd numbered memory byte too). In many cases, especially when working with memory
mapped I/O registers, you may need to operate on one specic byte only. To do so, just add a
.b
onto the end of the assembly instruction
For example:
mov.b #0, &P1DIR
sets 8 bit length P1DIR register to zero without accidentally
modifying the registers around it.
aside: MSP430 assembly species
.b
.w
for executing word length instructions as well as
for bit length instructions. The assembler by default assumes word length, so you
the programmer don't have to explicitly write
conscious that
mov R5, R14
mov.w R5, R14
although you should be
means the same thing.
The MSP430 has 16 CPU registers
•
The MSP430 has twice as many CPU registers as the LC-3. Like in the LC-3 though, some of
the MSP430's registers are reserved for the MSP430 runtime environment. Registers R0-R3 are
reserved (Program Counter, Stack Pointer, Status Register, and a Constant Generation Register
respectively), leaving registers R4 through R15 available for general purpose use as dened by the
programmer.
In your assembly programs you have 12 general purpose registers at your disposal, but you also
must manage and keep track of the additional options.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
25
MSP430 Register Usage Diagram
Figure 5.1
Indirect, relative, and absolute addressing occurs dierently
•
Instead of dierent indirect and direct load and store instructions (LD,
MSP430 uses one versatile
mov
mov
LEA, LDI,
can both read and write from memory it acts like both a load and store. (mov
corresponds to a
LC-3,
mov
ST
while
etc...), the
instruction with dierent operand addressing modes.
mov &0x0200, R4
corresponds to a
LD)
R4, &0x0200
Be careful though, unlike in
does NOT update the condition register.
Dierentiate between the various direct and indirect modes by using special syntax to specify
the type of operand you want. This allows you to mix addressing types (read indirect and store
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
CHAPTER 5. LAB 3-1 A "REAL-WORLD" MICROPROCESSOR: BASIC
MSP430 ASSEMBLY FROM ROOTS IN LC-3 (ESCAPE)
26
direct, etc...) even though everything is in one mov instruction.
*
Direct register access: Rn (where n is the number of a general purpose register) Example:
*
Immediate Values: #x (where X is an immediate numerical value or label) Example:
R4
refers directly to R4
#02h
refers to the literal hex number 2
*
Indirect Access From a Register: @Rn (where n is the number of a general purpose register)
Example:
*
@R6
refers indirectly to the data stored in the memory location in R6
Indirect Oset Access: x(Rn) (where n is the number of a general purpose register and x is
either an literal oset or a label) Example:
0(R7)
refers to the data stored in the location
in memory pointed to by R7
note: This has the same end result as
@R7.
By TI code convention though, @Rn
cannot be used to specify the destination of an operation, so if you wish to store a
result indirectly, you must use the 0(Rn) syntax.
tip: In this example R7 essentially contained the address while the literal oset was
a small number. Oset Access can be very powerful when looked at the other way:
where the literal contains a starting location in memory (potentially a label) and
the register contains a small oset value incremented to access a series of locations
in memory.
MSP430 Addressing Modes
Figure 5.2
You can also perform indirect or relative operand addressing with operations other than loads
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
27
and stores
Example 5.1
add @R4, R5
takes the data stored in the address pointed to by R4 and adds it with
R5, storing the result in R5.
For more information, see the summary chart Figure 5.2 (MSP430 Addressing Modes) or the
2 section 3.3.0 through 3.3.7
comprehensive MSP430 users guide
The MSP430 has two types of memory
•
The MSP430 has both traditional RAM and non-volatile Flash memory. On a power reset, all
values in RAM are cleared, so your program will be stored in Flash. The Flash write process is
fairly involved, so we won't be writing to it in this class during run time (Code Composer will
take care of loading your programs). In a nutshell, your program must store any temporary or
changing values to RAM memory, although it can read your instructions and any preset constants
from ash
Important Memory Locations:
0x0200 : The Lowest Address in RAM
0x0280 : The Highest Address in RAM
0xF800 : The Beginning of Flash Memory
0xFFE0 : The Beginning of the Interrupt Vector Table
MSP430 Memory Map
Figure 5.3
2 See
the le at <http://cnx.org/content/m45850/latest/MSP430 User Guide-slau144e.pdf>
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
CHAPTER 5. LAB 3-1 A "REAL-WORLD" MICROPROCESSOR: BASIC
MSP430 ASSEMBLY FROM ROOTS IN LC-3 (ESCAPE)
28
The MSP430 Uses Memory Mapped I/O Peripherals
·
These devices function independently of the main processor, and use memory mapped registers
to communicate with the program executing on the main CPU.
·
Peripherals free up CPU resources and also allow more usage of low power CPU suspend modes.
You'll learn more about peripherals in Lab 5
MSP430 Architecture Block Diagram
Figure 5.4
5.2.2 Example Code Translations
LC-3 Assembly
LC-3 Pseudocode
AND R4,R5,R6;
BRz R4,Loop;
R4
if
<-
MSP430 Assembly
mov.w R5,R4;
and.w R6,R4;
R5 & R6
R4
branch
==
to
0,
label
tst R4;
jz Loop;
"Loop"
MSP430
Pseudocode
R4
R4
<<-
R5
R4 & R6
load the attributes
of R4 into the SR
jump
to
label
"Loop" if the zero
bit is agged
Table 5.1
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
29
5.2.3 Other Useful Information
The code composer debugger actually runs on the real MSP430 hardware through a JTAG interface.
To
you have to have the launchpad board plugged into the computer.
The debugger controls the CPU's clock (and therefore can monitor it). To see how many clock cycles
something takes, go to Target -> Clock -> Enable, and look in the bottom right corner of the
screen for a small counter with a clock next to it.
debug code,
5.3 Part I Assignment Detail
Your task is to create a simple MSP430 assembly program using CCS4 and the MSP430 launchPad to
calculate a Fibonacci sequence. You do not need to explicitly display the sequence, but rather use the Code
Composer register view tools to watch the sequence progress as you step through your program.
To view the registers in Code Composer Studio v4, rst start a debug session. Once you are in the debug
perspective, you can go to View> Registers to open the register dialog. From there, expand the section
"Core Registers" to see your CPU registers, or the section "Port_1_2" to see the raw data from the input
pins.
Enable the clock cycle monitor (Target>Clock>Enable) and you will see a yellow clock icon at the
very bottom of your screen. This tells you how many actual CPU clock cycles have passed since you enabled
it. Observe the dierent amounts of time that dierent instructions take.
Denition 5.2: The Fibonacci Sequence
The sequence of numbers starting with 0 , 1 in which N= (N-1) + (N-2) 0, 1, 1, 2, 3, 5, 8, 13, 21,
34...
aside: The Fibonacci sequence plays an important role in the natural world. It appears in many
biological sequences, and is fundamentally linked to the famed "golden ratio." For more "fun" info
about Leonardo Fibonacci, see the ever reliable Wikipedia
3
Diagrams courtesy of TI document slau144e "MSP430 User's Guide"
The LC-3 was developed by Yale N. Patt (University of Texas at Austin) and Sanjay J. Patel (University
of Illinois at Urbana-Champaign) and is used in their book Introduction to Computing Systems.
3 http://en.wikipedia.org/wiki/Fibonacci
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
30
CHAPTER 5. LAB 3-1 A "REAL-WORLD" MICROPROCESSOR: BASIC
MSP430 ASSEMBLY FROM ROOTS IN LC-3 (ESCAPE)
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
Chapter 6
Lab 3-2 Digital Input and Lab 3-2
Output with the MSP430 (ESCAPE)1
6.1 Basic Digital I/O in the Real World
In this lab you'll go over the basics of how to setup and use the GPIO on the MSP430. This will allow you to
get data from the outside world, run some processing on it, and then output it again as useful information.
You only have one task this week:
1. Coding in MSP430 assembly,
write a simple I/O echo program. Setup the GPIO pins and poll
the input DIP switches for any changes. Take the input and display it to the output so any changes
are immediately reected. Step through this program to observe how it behaves. Assignment Details
(Section 6.3: Assignment Details)
6.2 Digital I/O Basics
6.2.1 GPIO
Philosophy
•
The MSP430 uses a limited number of GPIO hardware pins that are assignable to several functions depending on your specic model and your program's needs. For example, our version, the MSP430F5637,
can have the pins act as digital output, digital input, or ADC input.
•
The pins are organized into ports, with each port usually one byte (8 bits/pins) wide.
On larger
versions of the processor (dierent format chips with physically many more pins...) you can encounter
several ports. In this lab our MSP430F5637 has 9 dierent ports we will use.
•
You can set each pin's function independently (input or output) by modifying some memory mapped
I/O registers. Since we want to do both, we will assign dierent tasks to dierent pins as needed.
Usage
•
•
The I/O ports are memory mapped into the top of the MSP430 address space.
There are several registers associated with each port. For now, you only need to worry about six (P4IN,
P4OUT, P4DIR, and P9IN, P9OUT, P9DIR).
P9IN
The P9IN register is located in memory, which you can refer to using the C symbol
1 This
content is available online at <http://cnx.org/content/m45851/1.1/>.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
31
&P9IN
CHAPTER 6. LAB 3-2 DIGITAL INPUT AND LAB 3-2 OUTPUT WITH THE
MSP430 (ESCAPE)
32
The register holds the values the MSP430 sees at each associated pin, regardless of the pin direction
setting.
To read the register, it is good practice to use a
mov.b
instruction to avoid accidentally reading
adjacent registers
tip: If you are looking to test or read just the pins set to input, you will have to mask
the P9IN register to zero out the other unwanted/output pins. Reading P1IN reads the
entire port, regardless of pin direction.
P4OUT
The P4OUT register is located in memory, which you can refer to using the C symbol
&P4OUT
If their direction bits in P4DIR are set to output/ "1", the corresponding pins will output the
values set in P1OUT.
If a pin's direction bits are set to input in P4DIR and its resistors are enabled in P4REN, P4OUT
controls the pin's connection to the pull-up resistor. Setting P4REN to "1" enables the pull-up,
while setting it to "0" leaves the input to oat in a high impedance state.
To set P1OUT, use a
you can use an
or.b
mov.b instruction to set several pins at once.
To set individual bits to "1",
instruction with a "1" in the positions you want to set. To clear individual
bits/ set them to zero, use an
and.b
instruction with mostly "1"s except for a "0" for the bits
you want to clear.
P4DIR
The P4DIR register is located in memory, which you can also refer to using the C symbol
&P4DIR
The value of the bits in P4DIR determines whether the MSP430 hardware leaves the pin in a high
impedance state where it responds to external voltage changes (which you can read at P4IN), or in
a low impedance state where the MSP430 drives the output voltage to a certain value determined
by P1OUT.
mov.b
or.b
To set the bit directions all at once, use a
regardless of the others, use an
and.b
or a
instruction, but to change individual bits
Set the corresponding bits to "0" to set pins to input mode, or to "1" to set them
to output mode.
6.2.2 So What?
In this lab we're going to use the MSP430's GPIO pins, combined with some external switches and an LED
display, to build a basic I/O system for our board. Because of how things t together on the board, it makes
sense to use P1.0-P1.3 (the rst three Port_1 GPIO Pins) to read the input switches and P1.4-P1.7 for the
output signals.
Outputs
&P1DIR to "1", and then write
&P1OUT. That means you'll have to shift your data left 4 positions before
Setting up the outputs is easy simply set the upper four bits (bits 4-7) of
the output to the upper four bits of
output, but you should already know a simple technique to do so!
aside: You'll notice that when you change the output, the corresponding input bits also change.
This happens because the input hardware always reads the status of the line, regardless if it is set
to input our output. Changing the
&P1DIR values only connects or disconnects the driving circuitry
built into the MSP430. In advanced applications this can be used to analyze potential faults in the
circuitry outside the chip.
Inputs
Inputs are also "easy," but there are a few hardware concepts you'll need before you understand how they
work!
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
33
6.2.2.1 A Little Bit About Wires
As mentioned briey in class, binary digital logic has two valid states, plus one third mystery state. That
third state, "The High Impedance State," (High-Z) just means that the wire isn't connected to anything.
You've already talked about using so called tri-state buers to negotiate who can talk on a shared bus the
listening components enter the high impedance state, allowing the transmitting component's signal to drive
the bus with no conicts.
aside:
Impedance is a generalized form of the classical Resistance concept.
Impedances can be
real or complex valued, and apply too signals expressed in complex exponential form (whether
constant or variable!). To learn more about impedance, check out Dr. Johnson's sections from
2
the Elec 241 course notes.
A Basic Switch
Figure 6.1
In order to read useful input from your switches, you need them to be "0" in one state, and "1" in
the other.
Yet knowing what you know about the third state, the switch shown above will actually give
a "0"/"1" (depending on what you connect it to) when closed and "High-Z" when open. Because there's
nothing else driving the sensor input besides our switch, the input value will be random when the
switch is open. In digital logic this is called oating, and it is a very very bad thing.
One simple solution is the Pull-Up (or Pull-Down) Resistor. Connecting the oating side of the
switch to a logic level through a large resistor will tie down the oating input when the switch is open, but
won't eect the read value much when the switch is closed.
As you can see, when the switch is closed, the input is shorted to ground and reads zero.
When the switch is open, the pull-up resistor holds the previously oating end at Vcc.
Figure 6.2:
2 "The
Impedance Concept" <http://cnx.org/content/m0024/latest/>
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
CHAPTER 6. LAB 3-2 DIGITAL INPUT AND LAB 3-2 OUTPUT WITH THE
MSP430 (ESCAPE)
34
Pull-Ups in the MSP430
For better or for worse, the MSP430 actually has pull up resistors already built into the chip's hardware.
Conguring them takes several steps, but once setup they provide all the functionality above without the
extra external connections.
∗
∗
∗
&P1DIR
&P1REN to "1")
0-3 of &P1OUT to "1")
Set the Pin Direction for P1.0-P1.3 to input. (Set bits 0-3 of
to "0")
Enable the resistors themselves. (Set bits 0-3 of
Congure the resistors to be pull-up. (Set bits
important: The most confusing part of the whole process is the double function of
cause of the hardware implementation on the MSP430,
&P1OUT
P1OUT.
Be-
controls the outputs as well as the
You will need to ensure that every time you output a
value, you KEEP the lower four bits "1". The easiest way to do this is just by ORing your
connections to the pull up resistors.
raw output with the constant
#0Fh before you write to P1OUT. The MSP430 does not have a specic
bis does the same thing. For more info on bis and its inverse bic,
"or" instruction by name, but
3
see next week's lab.
Figure 6.3: Notice that congured this way, the MSP430 GPIO pin takes the form of the simplied
Pull-Up gure above.
6.2.3 Polling
Philosophy
3 http://cnx.org/content/m45851/latest/
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
35
ˆ
A traditional single threaded polling scheme consists of a main loop that runs continuously. Within
that loop, the processor periodically checks for changes, and if there are none, continues looping. Once
a change is detected, the program moves to a new section of code or calls a new subroutine to deal
with the changes.
ˆ
Polling has advantages and disadvantages it keeps program execution linear and is very easy to code
and implement, but it also is not incredibly responsive.
Since polling only checks values at certain
points in the main run loop, if the loop is long or changes occur quickly, a polling scheme can miss
input data. For now though it will suce.
6.3 Assignment Details
Your task is to code a simple input to output echo program for the MSP430. Your program should consist
of:
•
•
•
•
A setup section that runs once and congures the GPIO pins
A main loop that runs innitely
Code inside your loop to read the state of the GPIO input pins
A separate section of code to write the changes to the output pins and then return to the main loop
tip: You should already know the basics of masking from class, but it becomes very important when
dealing with I/O. Since dierent pins do dierent things in the same port, you the programmer
will have to be careful not to accidentally modify the wrong bits even though your instructions will
operate on the entire register.
All images drawn by Matt Johnson, Rice ECE
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
36
CHAPTER 6. LAB 3-2 DIGITAL INPUT AND LAB 3-2 OUTPUT WITH THE
MSP430 (ESCAPE)
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
Chapter 7
Lab 4-1 Interrupt Driven Programming
in MSP430 Assembly (ESCAPE)1
7.1 MSP430 Interrupts and Subroutines: Your Tasks
This week you will learn more about the philosophy of interrupt driven programming and specically how
interrupts work on the MSP430. To test out your knowledge, you'll write another simple I/O echo program
that builds o the code from the last lab.
1. Coding in MSP430 Assembly,
create an interrupt driven I/O echo program.
The program
should read the values of the input DIP Switches(P9.4-P9.7) when one of the pushbuttons (P3.6 or
P3.7) triggers an interrupt, and then output the read value to the 7 segment display (P4.0-P4.3).
Details (Section 7.3: Interrupt Assignment Detail)
7.2 Background Information
7.2.1 A Few More Instructions
Like you saw in the GPIO Lab, the MSP430 (even though it's a RISC
Reduced Instruction Set Computing
processor) has a fair number of instructions in addition to those you learned for the LC-3.
instructions help programmers simplify code readability and streamline program execution.
You've already seen how the MSP430 uses memory access modiers and the general purpose
The extra
mov instruc-
tion to implement all the functionality of the LC-3's plethora of load and store instructions. Two other very
useful MSP430 instructions are
bis (Bit Set) and bic (Bit Clear).
These instructions take an operand with
"1"s in the bits you wish to set or clear, and then a destination upon which to do the operation. This comes
in handy when you need to modify a few specic conguration bits out of a whole register (like the GIE bit
in the SR for interrupts... see below!). The header le has pre-dened masks you can use with
bic
and
bis
to make bit operations much more readable.
note:
inv,
and
The
or.
bis
and
bic
instructions actually emulate functionality you already had with
and,
∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼
bis op1, op2
corresponds to
or op1, op2
1 This
content is available online at <http://cnx.org/content/m45965/1.2/>.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
37
CHAPTER 7. LAB 4-1 INTERRUPT DRIVEN PROGRAMMING IN MSP430
ASSEMBLY (ESCAPE)
38
∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼∼
bic op1, op2
corresponds to
inv op1
and op1, op2
7.2.1.1 Directives
Assembler and Compiler Directives sound intimidating, but they are nothing more than bits of code intended
for the assembler/compiler itself. Directives allow you to specify how the assembler/compiler handles your
code and how it all nally comes together into the executable binary le.
.cdecls C,LIST, "msp430f5637.h" tells your
.text tells
memory section, and .sect "reset" denes where
The skeleton le has included several directives all along
.asm le to include the c code header aliases from the generic MSP430G2231 conguration le.
the assembler to place your program in the main ash
to start the program after a processor restart.
In this lab, you'll have to use directives to place your ISR vectors into the vector table. (p. 41)
7.2.2 Basic Interrupts
Problems with polling
Continuously polling a pin for input wastes useful CPU cycles and consequently uses more power
The CPU must check the pin often enough to detect a change when trying to catch a rapidly
changing digital signal (a small pulse or transient, etc.), polling may not be sucient.
In conclusion, polling is easy to understand and implement, but is generally inecient.
The solution... interrupts
Interrupts use dedicated hardware to detect input changes or hardware events (button pushes,
timer intervals, etc...)
When a change is detected, the interrupt logic interrupts the CPU execution.
*
The CPU stops what it is doing and calls a special section of code determined beforehand in
the interrupt vector table. This section of code is known as the
Interrupt Service Routine,
or ISR for short.
*
Once the interrupt has been serviced and the ISR is complete, the CPU returns to what it
was doing before.
The way the main program pauses execution and then branches to a new section of code works
in a similar way to the LC3's Traps.
Advantages to Interrupts
Interrupts will catch quickly changing inputs (within reason) that polling might have missed.
The CPU is allowed a level of freedom to multitask without needing to "worry" about explicitly
catching input changes. The CPU can do other tasks safely while waiting for an interrupt to re.
note: Programs can be "interrupt driven," meaning that the program is just a collection
of dierent interrupt service routines for dierent tasks.
*The
CPU is only active while servicing an ISR, allowing it to go into low power
mode between interrupts. Programs that spend a large percentage of their run time
waiting on outside events can be made
much more power ecient.
Basic Interrupt Implementation
Discrete hardware detects interrupt conditions and then triggers the appropriate interrupt in the
CPU if it is high enough priority.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
39
The interrupt vector table maps each interrupt to the memory address of its interrupt service
routine. Like with traps, the CPU rst goes to this table to nd the address of the ISR and then
jumps to the actual ISR code.
CPUs contain several dierent interrupts to handle dierent external events uniquely.
MSP430 Interrupt Call Procedure
Figure 7.1
Interrupts on the MSP430
On the MSP430, there are two types of interrupts: maskable and non-maskable.
Denition 7.1:
Maskable Interrupt
Most interrupts are maskable. Maskable interrupts can be enabled or disabled as a group
by setting the GIE (General
Ineterrupt Enable) bit in the status register. The interrupts
must also be enabled individually, but masking allows delicate code (For example, if you
are running a precisely timed output routine that must execute all at once) to run in a
near interrupt free state by disabling only one bit.
bis.w
Enabling All Maskable Interrupts
#GIE, SR
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
CHAPTER 7. LAB 4-1 INTERRUPT DRIVEN PROGRAMMING IN MSP430
ASSEMBLY (ESCAPE)
40
Denition 7.2:
Non-Maskable Interrupt
Non-Maskable interrupts will trigger an interrupt at any point in code execution they
cannot be enabled or disabled on a line by line basis, and they will execute even if the
processor is "stuck". Non-maskable interrupts mainly deal with recovering from errors
and resets (illegal memory accesses, memory faults, watchdog expiration, or a hardware
reset will trigger non-maskable interrupts).
In the MSP430, GPIO interrupt capability must be enabled at the masking level as
well as the individual pin enable level.
Interrupts should be enabled during the program initialization (before the main code loop or
entering low power mode), but after any initialization steps vital to the ISR
There are four main steps to enabling interrupts on the MSP430's GPIO pins.
*
Enable interrupts on the individual input pin (in this example pin P1.4) using the port's
interrupt enable register.
bis.b #010h, &P1IE
P1IE=
*
Port One Interrupt Enable
Select whether the interrupt triggers on a transition from low->high ("0") or high->low ("1")
using the port's edge select register
bis.b #010h, &P1IES
*
P1IES=Port
One Interrupt Edge Select
Clear the interrupt ag on the pin in the port's interrupt ag register.
bic.b #010h, &P1IFG
P1IFG=Port
One Interrupt FlaG
note: Flags are important. For one, if you forget to clear the ag at the end of your
ISR, you will just trigger another interrupt as soon as you return. Also, all of the
GPIO pins trigger the same port one ISR. If you have multiple interrupt triggering
pins, ags can allow you to determine which pins triggered the interrupt.
*
And lastly, only after all of your other important setup, enable all the maskable interrupts in
the overall CPU status register.
bis.w #GIE, SR
Writing an MSP430 Interrupt Service Routine
The ISR needs to be a section of code outside of the normal main loop.
Your ISR must begin with a label and end with a
Pin1_ISR
reti
instruction.
<YOUR ISR CODE>
bic.b #001h, &P1IFG
reti
At the end of your .asm program, you need to tell the assembler to write the starting address
of your ISR to the correct section of the interrupt vector table. The label at the beginning of
your ISR allows you to nd this address.
aside:
CCS5 uses a separate le to dene dierent sections of your controller's
memory. This extra layer of abstraction makes it easier to port code between different microcontrollers, but means that you the programmer can't write directly to
a specic memory address in your program. To ll your vector table, you'll need to
use the following syntax:
.sect MEMORYSECTION
.word DATATOPLACE/LABEL
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
41
The port one interrupt vector for the MSP430 G2231 is dened as 0xFFDE. If you look in
the le "Lnk_msp430f5637.cmd" (in the le browser for your lab 4 project), you will see
that address 0xFFDE has been assigned to int47. In the second half of the linker le, the
> int47. When you want to write
to the GPIO entry of the interrupt vector table, you need write to code section
"PORT1" in your assembly le. You can do the same for dierent ports as wellin the escape platform you will likely use the push buttons on "PORT3" Setting
the GPIO vector in the interrupt vector table for port3
section PORT1 has been assigned to memory addresses
.sect "PORT3"
.word Pin1_ISR
The .sect instruction
directs the linker to put the code that follows into a specic code
section. (You have been using this all along, just putting your code into the main program
".text" section.)
The
.word
instruction directs the linker to write a word length data value into memory.
For more information on interrupts, see the Interrupt section of TI's Microcontroller and Embedded Systems
2
Laboratory.
7.2.3 Subroutines
Subroutine Basics
Subroutines have a lot in common with interrupt service routines (in fact, many programmers use ISR
interchangably between
Interrupt Sub Routine and interrupt service routine).
Subroutines are sections of code you use repeatedly during a program they allow you to keep repetitive
program sizes smaller by re-using the same code section instead of repeating it everywhere you need
it.
To go to a subroutine, use the
call #SubroutineLabel
instruction.
call
is analogous to triggering
an interrupt. Call works in practice a lot like just jumping to the label, but it also pushes the PC onto
the stack (like an ISR) so you can return to wherever you may have left o (since multiple places in
code can call the same subroutine).
At the end of your subroutine, use a
ret (return) instruction to pop the PC o the stack and go back
reti instruction at the
to the original execution point of the main program. This is analogous to the
end of an ISR.
warning: Calling a subroutine on the MSP430
ONLY saves the PC, not the status register
like an ISR. You can use subroutines to encapsulate complicated logic, and then examine the
conditions afterwords in your main program.
There is a
slight performance trade o when using subroutines from the overhead involved with storing
the PC and moving to a new section in memory, so use them intelligently.
A simple subroutine to demonstrate call and return:
<Your Other Code...>
call #Sub220
<Your Other Other Code...>
Sub220
2 "Interrupts"
add R4, R5
inv R5
ret
<http://cnx.org/content/m12321/latest/>
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
CHAPTER 7. LAB 4-1 INTERRUPT DRIVEN PROGRAMMING IN MSP430
ASSEMBLY (ESCAPE)
42
7.3 Interrupt Assignment Detail
Your task is to create a simple MSP430 assembly program using CCS4 and the MSP430 LaunchPad to
output a stored value to the 7-segment display. Your program should be
interrupt driven, and triggering
an interrupt from one of the pushbuttons (Pin 3.6 or 3.7) should store and output a new output value
corresponding to the state of the DIP Switches on Port9.4-9.7. Changing the switches should
not eect
the output until pushing the button. Your program should consist of:
•
•
•
A setup section that congures the GPIO pins and enables interrupts.
An innite main loop that does nothing (the
No Operation instruction
nop
could come in handy).
An ISR that takes the new inputs and writes them to the output before returning to the main loop.
Interrupt Diagrams Courtesy of TI document slau144e, "MSP430 User's Guide."
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
Chapter 8
Lab 4-2 Putting It All Together: An
Interrupt Driven MSP430 Project
(ESCAPE)1
8.1 A More Complicated Assembly Program
By now you already have all of the tools you need to complete this assignment. Remember what you have
2
3
4
learned about MSP430 assembly language , setting up GPIO , and using interrupts .
1. Coding in MSP430 assembly,
implement an interrupt driven number sequence recorder. You
will use the same input conguration from last week (get data from pins 9.4-9.7 on an interrupt from
pin 3.7), but now will output a readable loop of the last 5 received numbers in order.
Assignment
Details (Section 8.2: Part II Assignment Detail)
8.2 Part II Assignment Detail
Your task is to write an assembly program to display a programmable sequence of 5 numbers on the MSP430
ESCAPE Platform.
You should use ve slots to store the input numbers.
Since our simple setup only has one display, you will have to rotate through each of the ve numbers
after a "short" (in human terms) delay.
Use an ISR to store a new number in the "next" slot. (Next not necessarily meaning what is currently
being displayed). The input should go from slot 1 to 2 to 3... etc. regardless of which slot is currently
being output.
The program should only display a slot after a number has been input into it. You will need to keep
track of which slots have been lled.
1 This content is available online at
2 "A "Real-World" Microprocessor:
<http://cnx.org/content/m46074/1.1/>.
Basic MSP430 Assembly from Roots in LC-3"
<http://cnx.org/content/m37151/latest/>
3 "Digital Input and Output with the MSP430" <http://cnx.org/content/m40643/latest/>
4 "Interrupt Driven Programming in MSP430 Assembly" <http://cnx.org/content/m37217/latest/>
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
43
CHAPTER 8. LAB 4-2 PUTTING IT ALL TOGETHER: AN INTERRUPT
DRIVEN MSP430 PROJECT (ESCAPE)
44
Only grey boxes are output to the display. Also, notice how after lling all ve slots, the
ISR loops back and starts lling from the beginning.
Figure 8.1:
Your program should consist of:
•
•
•
A setup routine that readies all the components of your program.
A main loop that displays the stored numbers one after the other with a readable delay in between.
An ISR that stores each new input number to the appropriate slot.
A Few Hints:
The MSP430 operates at
∼13MHz,
which may seem slow in terms of computers, but is much too fast
for the human eye to process (∼30Hz). You will have to implement a very signicant delay in between
number changes.
One way to generate a naive delay is a long loop which does nothing. You may even need to use a
nested loop depending on how long of a delay you need. Nested Loop Example in C:
int i=0;
int j=0;
for (i=0; i<bigNumber; i=i+1)
{
for(j=bigNumber; j>0; j=j-1)
{
<!--This code will run ixj times-->;
}
}
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
45
You may nd it convenient to put your ve slots in RAM instead of using registers.
You can then
store a memory address in the register, and then increment it or set it as needed. You will need to use
indirect addressing mode though.
mov R4, 0(R15); moves the contents of R4 to the address in R15
mov 0(R15), R4; moves the contents of the address in R15 into R4
mov &0x006300, R4; moves the contents of memory address 0x006300 into R4
Consider where it may be useful to implement parts of your program in subroutines
8.3 Wrapup
Congratulations on completing lab 4! Your program sophistication has dramatically increased. You understand the basics of
interrupt driven programming, and know how to use assembly level subroutines.
data as well as design a responsive I/O interface to the outside world. Keep
You have had to keep track of
up the good work!
Labs based on the original Elec 220 labs maintained by Michael Wu.
Images from original lab documents by Yang Sun. Modied by Matt Johnson.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
46
CHAPTER 8. LAB 4-2 PUTTING IT ALL TOGETHER: AN INTERRUPT
DRIVEN MSP430 PROJECT (ESCAPE)
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
Chapter 9
Lab 5-1 C Language Programming
through the ADC and the MSP430
(ESCAPE)1
9.1 The C Language and Analog Interfacing: Your Task
This lab covers the basic principals behind Analog to Digital Conversion, as well as the basics of programming
in C. You are expected to have some background in C from class, but if you are confused, see this basic
2
reference .
1. Using Code Composer Studio 5, write a C language program turning your ESCAPE Platform into
a simple 10 level light meter. Your program should divide the 0-3.3V input range of the ADC into
IN
GENERAL, DO NOT EXCEED AN INPUT VOLTAGE OF 3.3V (You don't have to worry
10 zones, and then output from a 0 to a 9 on the LED display depending on the input voltage.
about this during this light sensor lab). You will damage your circuits and destroy your MSP430 if
you measure a raw voltage greater than 3.3V though. Assignment Details (Section 9.3: Assignment
Details)
9.2 The ADC and "C" Through a Practical Example
9.2.1 Interfacing with the Analog World: The ADC
Analog to Digital
Converter, and it does exactly what you would expect it to. It samples an external voltage, and then converts
ADC's play an incredibly important role in digital electronics and DSP. ADC stands for
that voltage to a binary number compared to the reference voltage range from Vdd to Vss. (In plain English
terms, the ADC samples what fraction the input is of some maximum allowed reference voltage.) The ADC's
result gets written to a memory mapped register, where the programmer can access it and use it in his or
her code.
An ADC has a
nite voltage range it can safely convert (usually related to its power supply range,
but not always). The precision of the converted sample is related to the number of bits used by the ADC.
More bits means more precision (more nite "slots" into which the innitely variable analog single can
be quantized) and a lower "quantization error." To learn more about error and ADC, see this except from
3
the Introduction to Electrical Engineering course notes .
ADC's also have a
maximum sampling rate
1 This content is available online at <http://cnx.org/content/m46087/1.1/>.
2
<http://cnx.org/content/col10788/latest/>
3 "Amplitude Quantization" <http://cnx.org/content/m0051/latest/>
PROGRAMMING FUNDAMENTALS IN C++
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
47
CHAPTER 9. LAB 5-1 C LANGUAGE PROGRAMMING THROUGH THE
ADC AND THE MSP430 (ESCAPE)
48
specication (how frequently the ADC can make a conversion), but in this course we will be sampling very
low frequency signals, so we won't need to worry about it.
9.2.2 The MSP430 ADC
The MSP430 F5637 has one 16 channel 12 bit 200Khz ADC. ADC channels allow the single ADC to select
between several dierent signals (such as two dierent analog inputs on dierent GPIO pins) like an analog
multiplexer. In the F5637, channels 1-8 are connected to the 8 P6 GPIO pins, and channel 10 is connected
to the chip's internal temperature sensor.
For this lab, we will congure the ADC to use the internal 3.3 Vdd as the reference voltage.
•
•
•
•
A voltage of 3.3V would result in the ADC register holding 1111 1111 1111 (0x0FFF)
A voltage of 0.0V would result in the ADC register reading 00 0000 0000 (0x0000)
A voltage of 1.65V would result in the ADC register reading 0111 1111 1111 (0x07FF)
#Bits ], or .00081 Volts.
The ADC will have a sample resolution of 3.3V/4096 [Voltage Range/2
The ADC is a peripheral device, so it operates independently from the CPU. It has several operation
modes (congured by writing to its control registers).
Denition 9.1: Peripheral
A device that can operate independently from direct CPU support and control. Peripherals often
rely on interrupts to notify the CPU when they have completed some given task or need attention,
and use independent control registers for conguration. The ADC 12 is a peripheral, as well as the
MSP430's UART (serial controller) and timers.
ADC12 Operation Modes
•
Single Sample and Hold the ADC12 will start a conversion when triggered by the CPU. After that
conversion, it will hold the converted value in the ADC12MEMx registers and automatically go into
sleep mode until signaled to begin another conversion. We will mostly use this mode.
•
Sequence of Channels Sample and Hold the ADC12 will convert a series of dierent channels once,
and store the result to ADC12MEM.
•
Repeat Single Channel Mode it will continuously sample on channel, continuously updating the
ADC12MEM register.
•
Repeat Sequence of Channels Mode the ADC will continuously sample through a series of channels.
The MSP430's ADC 12 also has a built in memory controller.
We won't be using it, but it becomes
important when using the repeat modes. The memory controller can automatically write the ADC data into
main memory as conversions nish, bypassing the CPU.
The F5637's ADC can run o of one of several available clock signals of varying speeds. Once a sample
has been captured, it is held ready in the ADC10MEMx registers for a dened number of clock cycles. Since
we are concerned with a low frequency signal, we will want to slow down the ADC12 as much as possible.
(Students who have had Elec241 will notice some fundamental aws in the assumptions made regarding
high-frequency noise, but in practice this has very little eect on the nal results). Even in its slowest mode,
the ADC12 will still sample too quickly, so the use of some kind of moving average will help stabilize its DC
readings.
9.2.3 Controlling the ADC12 in C
9.2.3.1 C Basics
Your C program will be structured similarly to its assembly language counterparts, but with a much dierent
syntax. Like before, the register names are all pre-dened in the
"msp430.h"
header le. To set a register,
now just use an equals sign and set it like any other variable. For example, you will want to disable the
watchdog timer in the rst line of your program.
WDTCTL=WDTPW+WDTHOLD;
The compiler will execute the
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
49
void main(void) function rst.
From that function, you can call any other functions or run any loops that
you would like.
C Skeleton Program
#include
<msp430.h>
//Global Variable Declarations
//Global Function Declarations
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
//Other Setup
}
//Your Program Here
//Can call other helper functions, loops, etc.
return 0;
9.2.3.2 Conguring the ADC12
The ADC12 has two main control registers
ADC10MCTLx (12bit ADC Memory
ADC12CTL0
and
ADC12CTL1,
and 16 memory control registers
CoNtroL 0/1/2... channel 0-15). These registers control all the timing
and conversion aspects of the ADC.
ADC12CTL0
Figure 9.1
In the rst control register (ADC12CTL0), we only need to change two parameters,
•
ADC12SHT0_x12bit
ADC Sample Hold Time a higher value means each sample will be held for
a longer period of time. We want to set this at the max value of
•
ADC12ON12bit
ADC12SHT0_8
ADC ON/OFFsetting this bit to "1" (denoted by the label
ADC, a vital step to performing any conversion!
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
.
ADC12ON) turns on the
CHAPTER 9. LAB 5-1 C LANGUAGE PROGRAMMING THROUGH THE
ADC AND THE MSP430 (ESCAPE)
50
To actually do this in C, just use addition and an equals sign:
ADC12CTL0 = ADC12SHT0_8 + ADC12ON ;
ADC12CTL1
Figure 9.2
In the second control register (ADC12CTL1), we want to set two parameters.
•
aside: Since some of the bit labeling is inconsistent (especially between dierent versions of
a microcontroller), it is always good to examine the header le for a controller to see how its
aliases are dened before using them in your code.
ADC12CTL1 = ADC12SHP + ADC12CONSEQ_0;
Figure 9.3
We also need to set the appropriate input channel to our output memory address in the ADC12MCTL0
register
•
ADC12INCH_x12 bit
ADC Input Channel # this 4 bit section determines which of the possible
input channels the ADC will actually convert and store into the
ADC12MEM0
register.
ADC12MCTL|= ADC12INCH_0;
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
51
Lastly, analog conversion must be enabled on the input ports themselves. These act as gates to prevent
leakage current from owing from a pin set as an output through the ADC to ground a substantial waste
of power. To enable the ADC for your desired GPIO pin, just set the corresponding bit in
Special Function
P6SEL
(Port
6
SELect) to "1".
P6SEL |= BIT#;
For more info about the ADC12's conguration options, see the MSP430 manual starting on page 742.
9.2.3.3 Using the ADC
To read a sample from the ADC, just read from the ADC12MEM register after the sample has completed.
while (ADC12CTL1&ADC12BUSY); // Wait in naive loop for conversion to complete
my_var= ADC12MEM0;
Remember that we have setup the ADC for single conversion and hold, so if you want another value, you
will have to tell it to sample and convert again. You do so by modifying two values in ADC12CTL0:
•
ADC12ENC
12bit ADC Enable Conversion locks in the ADC settings and stabilizes it for conver-
sion.
•
ADC12SC12bit
ADC Start Conversion setting this bit to one actually triggers the ADC's conversion
cycle.
ADC12CTL0 |= ADC12ENC + ADC12SC;
note:
Be sure to use OR equal (|=) so that the conguration bits you set before don't get
overridden.
note: Also, don't forget to congure P3 as usual. You will need to set the pins you wish to use as
outputs in the
P3DIR
register to display a value on the LEDs. You can congure the P3 registers
using aliases and variable assignments just like with the ADC registers.
9.3 Assignment Details
Using Code Composer Studio 5, write a C language program turning your MSP430 ESCAPE Platform into
a simple 10 level voltmeter. Your program should divide the 0-3.3V input range of the ADC into 10 zones,
and then output from a 0 to a 9 on the LED display depending on the input voltage from the light sensor.
Don't worry about a value landing on the boundary between two zones, just deal with it consistently. Test
your volt meter by sampling pin P6.0 and exposing it to dierent light intensities.
Your Program should consist of:
•
•
•
A "void main(void)" function that drives your program
A successful setup routine that properly congures the ADC12
An output routine that loop innitely and successfully re-scales the 4096 ADC possibilities to 10 zones
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
52
CHAPTER 9. LAB 5-1 C LANGUAGE PROGRAMMING THROUGH THE
ADC AND THE MSP430 (ESCAPE)
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
Chapter 10
Lab 5-2 Using C and the ADC for "Real
World" Applications with the MSP430
(ESCAPE)1
10.1 A Real World Situation
On some level, every signal and every interface starts out analog. In this lab you will learn a simple two
point calibration routine and how to use it to get accurate data from the physical world. This assignment
is less about new programming principals and more about applying what you already know. You have one
main task:
1. Using Code Composer Studio 5, write a C language program to drive a precise LUX meter (useful for
photography!). Use the ADC to read the voltage from the light sensor and display back the actual LUX
value. Write an interrupt driven calibration routine for your light meter and an output routine that
will allow you to display at least three output digits. Assignment Details (Section 10.3: Full Range
Light Meter Assignment Details)
10.2 Analog Signals Background
10.2.1 Simple Analog Sensing
The analog voltmeter may seem simple, but the ability to measure and quantify an analog voltage allows
the MSP430 to interface with a whole range of analog sensors. Ultimately, any real world signal starts out
analog, so at the heart of every interface lies some kind of analog system..
Analog sensors use the physical properties of some electronic device (or a system of many devices) to
modify an analog voltage or current signal. The MSP430's ADC allows it to use that signal in computations
(as long as the signal's maximum frequency is less than 100khz- the nyquist sampling frequency of the 200khz
ADC). In simple terms, the ADC can't accurately measure signals that change too quickly for it to see. Not
only that, but the ADC can pickup unwanted distortion from those higher frequency signal components,
making even the low frequency parts inaccurate.
Example 10.1: A simple analog device
A photodiode is just one of many such devices. When kept in reverse voltage bias, the photodiode
allows an amount of current through it proportional to the amount of light shining onto it.
1 This
content is available online at <http://cnx.org/content/m46109/1.1/>.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
53
By
CHAPTER 10. LAB 5-2 USING C AND THE ADC FOR "REAL WORLD"
APPLICATIONS WITH THE MSP430 (ESCAPE)
54
attaching a photodiode in series with a resistor, we can examine the voltage across the resistor to
nd that current (v=i*R), and therefore the relative amount of light!
Denition 10.1: Photodiode
A P-N diode specically constructed to allow a large amount of light to enter the diode's depletion
region.
This excess light generates free electron and hole pairs in the depletion region, allowing
current to ow. Photodiodes are surprisingly linear, meaning the light ux is almost 1:1 proportional
to the amount of current output. To learn more about photodiodes and electronic devices in general,
look forward to ELEC 305.
A Simple Photodiode Circuit
Figure 10.1
10.2.2 Our Simple Circuit
For simplicity, we're going to
use the pre-built light sensor circuit on the ESCAPE platform. It
is a little bit more complicated than the one above. As you continue to take more courses and learn more
about analog electronics, you will be able to design your own analog circuits to capture and condition the
information you want.
In this last lab you'll be using the full repertoire of I/O options available to you. You'll use the ADC to
read an analog voltage, the pushbuttons and interrupts to control a calibration routine, and the 7-segment
display to output a measured number.
10.2.3 Calibration
Like anything in the real world, your sensors and devices won't necessarily work perfectly all the time. Many
analog semiconductor devices are light, temperature, and pressure sensitive, so your actual readings can
vary depending on outside conditions. Also, while sensors are usually manufactured to within pretty tight
tolerances, every sensor has some nite error that you will need to account for to get maximum performance.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
55
Some kind of calibration routine can help alleviate many of these concerns. By calibrating your sensors
against a trusted source, you can correct a lot of the skew caused by outside conditions. Calibration will
also eliminate any steady state error caused by the device itself.
A calibration routine collects a set of known data points and scales the input signal to t the calibration
data. Since we are calibrating a fairly linear device, we only need two data points to determine its operating
curve. To get the most out of the calibration, you should use two data points that are as far from each other
In the case of our light sensor, that will be darkness, and a reference value that is
very bright. You can either download a free smartphone LUX meter to measure a reference source to use
as possible.
to calibrate your board, or use an approximation from the computer monitors in the lab. The monitors are
about 620 lux when showing white at full brightness with the measurement device held within a few inches
of the monitor.
Calibration Routine Pseudocode (NOT IN C SYNTAX)
flag=0;
interrupt function: calibrate()
returns: lowMeasured, highMeasured
{
if flag=0:
set low and set flag=1
if flag=1:
set high
return
}
10.2.4 Interrupts in C
Code Composer Studio has a special way of handling interrupts in C. It uses "compiler directives" (special
instructions to the compiler, assembler, and linker) to specify which functions should go in the interrupt
vector table. ISRs in C are written like any other function, except that they can take and return no values.
This ts with the convention that ISRs don't interfere with or depend on the code around them.
Formatting an Interrupt in Code Composer C
#pragma vector=PORT3_VECTOR
//compiler directive saying that this function should correspond with the port3 interrupt vector
__interrupt void interruptHandle()
{
//your ISR CODE
}
void main()
{
... all setup
__enable_interrupt();
//Enables general maskable interrupts
... the rest of your program
}
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
CHAPTER 10. LAB 5-2 USING C AND THE ADC FOR "REAL WORLD"
APPLICATIONS WITH THE MSP430 (ESCAPE)
56
Other Concerns
Even though interrupts should work in isolation, you often need to get or modify data inside them. This
can be done using volatile global variables. A global variable simply means a variable that any function in
your code can see and modify. Using global variables is generally discouraged (they can be easily abused
and lead to problems if you repeat common variable names), but in this case they allow you to interface
with your ISR. Volatile is a directive that tells the processor to never cache the variable's value.
When
dealing with ISRs, cached variables could lead to problems if the ISR updates the cached data while another
function is using it. Be careful using the volatile keyword excessively though the lack of caching slows down
performance.
10.3 Full Range Light Meter Assignment Details
Using Code Composer Studio 5, write a C language program to drive a simple 3 digit lux meter. Use the
ADC to read the voltage and display back the actual lux value.
Write a calibration routine for your lux
meter and an output routine that will allow you to display each of the output digits.
Your program should include:
•
•
A setup section to setup the GPIO, congure the ADC, and enable interrupts
An ISR that runs the calibration routine and keeps track of what has been calibrated so far (so it will
only run once)
•
A main loop that continuously samples, lters, and scales the ADC input to the output range as
determined by your calibration.
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
GLOSSARY
57
Glossary
I
Interpreted Instructions
(illegal memory accesses, memory faults,
watchdog expiration, or a hardware reset
An instruction that is decomposed by the
will trigger non-maskable interrupts).
assembler into several smaller/ more
basic fundamental instructions.
pop R3 contains two implicit
instructions: mov @SP, R3 and add
#0x02, SP
Example:
P
A device that can operate independently
from direct CPU support and control.
Peripherals often rely on interrupts to
M Maskable Interrupt
notify the CPU when they have
completed some given task or need
Most interrupts are maskable. Maskable
attention, and use independent control
interrupts can be enabled or disabled as a
registers for conguration. The ADC 12
group by setting the GIE (General
is a peripheral, as well as the MSP430's
Ineterrupt Enable) bit in the status
UART (serial controller) and timers.
register. The interrupts must also be
Photodiode
enabled individually, but masking allows
delicate code (For example, if you are
A P-N diode specically constructed to
running a precisely timed output routine
allow a large amount of light to enter the
that must execute all at once) to run in a
diode's depletion region. This excess light
near interrupt free state by disabling only
generates free electron and hole pairs in
Enabling All Maskable
Interrupts
one bit.
bis.w
N
Peripheral
the depletion region, allowing current to
ow. Photodiodes are surprisingly linear,
#GIE, SR
meaning the light ux is almost 1:1
proportional to the amount of current
output. To learn more about photodiodes
Non-Maskable Interrupt
and electronic devices in general, look
Non-Maskable interrupts will trigger an
forward to ELEC 305.
interrupt at any point in code execution
they cannot be enabled or disabled on a
line by line basis, and they will execute
even if the processor is "stuck".
T
The Fibonacci Sequence
The sequence of numbers starting with 0 ,
Non-maskable interrupts mainly deal
1 in which N= (N-1) + (N-2) 0, 1, 1, 2, 3,
with recovering from errors and resets
5, 8, 13, 21, 34...
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
INDEX
58
Index of Keywords and Terms
Keywords are listed by the section with that keyword (page numbers are in parentheses). Keywords
do not necessarily appear in the text of the page. They are merely associated with that section.
apples, Ÿ 1.1 (1)
A
Terms are referenced by the page they appear on.
ADC, Ÿ 9(47), Ÿ 10(53)
Altera, Ÿ 1(1), Ÿ 2(3), Ÿ 3(17)
assembly, Ÿ 5(23)
D
E
M Maskable Interrupt, 39
Memory Mapped, Ÿ 6(31)
mov, Ÿ 5(23)
C programming, Ÿ 10(53)
MSP 430, Ÿ 4(19)
Cavallaro, Ÿ 8(43), Ÿ 10(53)
MSP430, Ÿ 5(23), Ÿ 6(31), Ÿ 7(37), Ÿ 8(43),
CCSv4, Ÿ 4(19)
Ÿ 9(47), Ÿ 10(53)
Code Composer Studio, Ÿ 4(19)
multiplexor, Ÿ 2(3), Ÿ 3(17)
Cycle, Ÿ 8(43)
mux, Ÿ 2(3), Ÿ 3(17)
N
Display, Ÿ 8(43)
NOR, Ÿ 3(17)
P
Peripheral, 48
Ÿ 8(43), Ÿ 9(47), Ÿ 10(53)
photodiode, Ÿ 10(53), 54
escape, Ÿ 5(23), Ÿ 6(31), Ÿ 7(37), Ÿ 10(53)
polling, Ÿ 5(23), Ÿ 6(31)
Escape Platform, Ÿ 7(37)
Project, Ÿ 8(43)
ltering, Ÿ 9(47)
FPGA, Ÿ 1(1), Ÿ 2(3), Ÿ 3(17)
GPIO, Ÿ 6(31), Ÿ 8(43)
Q
R
S
IDE, Ÿ 4(19)
Quartus, Ÿ 1(1), Ÿ 2(3), Ÿ 3(17)
Rice, Ÿ 7(37), Ÿ 9(47)
str, Ÿ 5(23)
Subroutine, Ÿ 7(37)
Interpreted Instructions, 23
Interrupt, Ÿ 7(37), Ÿ 8(43)
L
NAND, Ÿ 3(17)
Non-Maskable Interrupt, 40
ECE, Ÿ 7(37)
ELEC 220, Ÿ 3(17), Ÿ 4(19), Ÿ 6(31), Ÿ 7(37),
G
I
microcontroller, Ÿ 10(53)
C, Ÿ 9(47)
Echo, Ÿ 6(31)
F
apples, 1
loop, Ÿ 5(23)
Assembly language, Ÿ 7(37)
C
Ex.
lab, Ÿ 5(23), Ÿ 7(37)
T
V
The Fibonacci Sequence, 29
Vector Table, Ÿ 7(37)
Voltmeter, Ÿ 9(47)
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
Ex.
ATTRIBUTIONS
59
Attributions
Collection:
ELEC 220 Lab Course (ESCAPE)
Edited by: Matthew Johnson
URL: http://cnx.org/content/col11513/1.1/
License: http://creativecommons.org/licenses/by/3.0/
Module: "Introduction to Quartus and Circuit Diagram Design"
By: Joseph Cavallaro, Chris Stevenson, Matthew Johnson
URL: http://cnx.org/content/m42303/1.1/
Pages: 1-2
Copyright: Joseph Cavallaro, Chris Stevenson, Matthew Johnson
License: http://creativecommons.org/licenses/by/3.0/
Module: "A Quartus Project from Start to Finish: 2 Bit Mux Tutorial"
By: Chris Stevenson, Joseph Cavallaro, Matthew Johnson
URL: http://cnx.org/content/m42302/1.3/
Pages: 3-15
Copyright: Chris Stevenson, Joseph Cavallaro, Matthew Johnson
License: http://creativecommons.org/licenses/by/3.0/
Module: "Lab 1-1: 4-Bit Mux and all NAND/NOR Mux"
By: Chris Stevenson, Joseph Cavallaro
URL: http://cnx.org/content/m42304/1.3/
Page: 17
Copyright: Chris Stevenson, Joseph Cavallaro
License: http://creativecommons.org/licenses/by/3.0/
Module: "A Student to Student Intro to IDE Programming and CCS4"
By: Matthew Johnson, Weiwei Wu
URL: http://cnx.org/content/m37154/1.2/
Pages: 19-21
Copyright: Matthew Johnson, Weiwei Wu
License: http://creativecommons.org/licenses/by/3.0/
Module: "A "Real-World" Microprocessor: Basic MSP430 Assembly from Roots in LC-3 (ESCAPE)"
Used here as:
"Lab 3-1 A "Real-World" Microprocessor:
Basic MSP430 Assembly from Roots in LC-3
(ESCAPE)"
By: Matthew Johnson
URL: http://cnx.org/content/m45850/1.1/
Pages: 23-29
Copyright: Matthew Johnson
License: http://creativecommons.org/licenses/by/3.0/
Based on: A "Real-World" Microprocessor: Basic MSP430 Assembly from Roots in LC-3
By: Matthew Johnson
URL: http://cnx.org/content/m37151/1.5/
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
ATTRIBUTIONS
60
Module: "Digital Input and Output with the MSP430 (ESCAPE)"
Used here as: "Lab 3-2 Digital Input and Lab 3-2 Output with the MSP430 (ESCAPE)"
By: Matthew Johnson
URL: http://cnx.org/content/m45851/1.1/
Pages: 31-35
Copyright: Matthew Johnson
License: http://creativecommons.org/licenses/by/3.0/
Based on: Digital Input and Output with the MSP430
By: Matthew Johnson
URL: http://cnx.org/content/m40643/1.1/
Module: "Interrupt Driven Programming in MSP430 Assembly (ESCAPE)"
Used here as: "Lab 4-1 Interrupt Driven Programming in MSP430 Assembly (ESCAPE)"
By: Matthew Johnson
URL: http://cnx.org/content/m45965/1.2/
Pages: 37-42
Copyright: Matthew Johnson
License: http://creativecommons.org/licenses/by/3.0/
Based on: Interrupt Driven Programming in MSP430 Assembly
By: Matthew Johnson
URL: http://cnx.org/content/m37217/1.2/
Module: "Putting It All Together: An Interrupt Driven MSP430 Project (ESCAPE)"
Used here as: "Lab 4-2 Putting It All Together: An Interrupt Driven MSP430 Project (ESCAPE)"
By: Matthew Johnson
URL: http://cnx.org/content/m46074/1.1/
Pages: 43-45
Copyright: Matthew Johnson
License: http://creativecommons.org/licenses/by/3.0/
Based on: Putting It All Together: An Interrupt Driven MSP430 Project
By: Matthew Johnson
URL: http://cnx.org/content/m40645/1.1/
Module: "C Language Programming through the ADC and the MSP430 (ESCAPE)"
Used here as: "Lab 5-1 C Language Programming through the ADC and the MSP430 (ESCAPE)"
By: Matthew Johnson
URL: http://cnx.org/content/m46087/1.1/
Pages: 47-51
Copyright: Matthew Johnson
License: http://creativecommons.org/licenses/by/3.0/
Based on: C Language Programming through the ADC and the MSP430
By: Matthew Johnson
URL: http://cnx.org/content/m37386/1.3/
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
ATTRIBUTIONS
61
Module: "Using C and the ADC for "Real World" Applications with the MSP430 (ESCAPE)"
Used here as: "Lab 5-2 Using C and the ADC for "Real World" Applications with the MSP430 (ESCAPE)"
By: Matthew Johnson
URL: http://cnx.org/content/m46109/1.1/
Pages: 53-56
Copyright: Matthew Johnson
License: http://creativecommons.org/licenses/by/3.0/
Based on: Using C and the ADC for "Real World" Applications with the MSP430
By: Matthew Johnson
URL: http://cnx.org/content/m40646/1.1/
Available for free at Connexions <http://cnx.org/content/col11513/1.1>
ELEC 220 Lab Course (ESCAPE)
This collection contains the modules for the updated ELEC 220 lab course based on the ESCAPE MSP430
microcontroller development platform.
About Connexions
Since 1999, Connexions has been pioneering a global system where anyone can create course materials and
make them fully accessible and easily reusable free of charge. We are a Web-based authoring, teaching and
learning environment open to anyone interested in education, including students, teachers, professors and
lifelong learners. We connect ideas and facilitate educational communities.
Connexions's modular, interactive courses are in use worldwide by universities, community colleges, K-12
schools, distance learners, and lifelong learners.
Connexions materials are in many languages, including
English, Spanish, Chinese, Japanese, Italian, Vietnamese, French, Portuguese, and Thai. Connexions is part
of an exciting new information distribution system that allows for
Print on Demand Books. Connexions
has partnered with innovative on-demand publisher QOOP to accelerate the delivery of printed course
materials and textbooks into classrooms worldwide at lower prices than traditional academic publishers.
Was this manual useful for you? yes no
Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Download PDF

advertising