EK307 Microcontrollers Lab Background - Boston...
Transcript of EK307 Microcontrollers Lab Background - Boston...
10/17/2017 pg 1 of 13
EK307 – Microcontrollers Lab
Background
The purpose of this lab is to provide you with a basic understanding of microcontrollers. The
microcontroller is a small computational device built on a single integrated-circuit “chip.” The
microcontrollers has become so commonplace that we are often unaware of its presence.
Microcontrollers are found everywhere: in tablets,
computers, automobiles, airplanes, cell phones, toys,
medical implants, kitchen appliances, wind turbines,
solar panels, security systems, lighting systems,
robots, and a host of other devices and systems. A
microcontroller is used whenever localized digital control is required. For example, the keyboard
on your laptop contains its own dedicated microcontroller whose sole function is to interpret
keystrokes and send them to the laptop’s main processor. An optical or touchpad mouse also
contains a microcontroller that monitors movement and button clicks, converting them into a
signal that is then sent to the main computer. Complex products, such as cars or airplanes,
incorporate many microcontrollers, wherein each one performs one specific function. In an
automobile, for example, separate microcontrollers are used for engine monitoring, fuel
injection, accelerator-pedal control, tire-pressure sensing, cruise control, ABS breaking,
emissions control, GPS integration, entertainment system coordination – and that’s just for a
simple family sedan. A modern aircraft contains hundreds of microcontrollers that each perform
different functions.
There are literally hundreds of different kinds of microcontroller chips made by dozens of
manufacturers. In EK307, will use the popular MSP430 microcontroller made by Texas
Instruments to examine various digital operations. In the lab, and MSP430 chip is mounted on
a test and development board called the LaunchPad, where the latter is very similar to the
popular Arduino controller. The LauchPad is designed so that you can program the
microcontroller, and then take the chip out and install it on a breadboard or printed-circuit
board of your choosing. (You may purchase your own LaunchPad if you wish.)
10/17/2017 pg 2 of 13
The MSP430 chip “thinks” in binary machine code (instructions are all sequences of 1 and 0
bits,) but it is programmed in the computer language C using a program called Code
ComposerStudio. This software has been installed on the computers in PHO105, but you may
also download your own personal copy of Code Composer (PC or MAC) from
http://processors.wiki.ti.com/index.php/Download_CCS
Simplified Description of the MSP430-G- 2553
The various chips in the MSP430 family of microcontrollers are distinguished by a letter (e.g.,
“G series”) followed by four numbers. In EK307, we will focus on the MSP430-G2553, a basic
level member of the MPS430 family, in conjunction with the LaunchPad test and development
board (shown below to the right). As is the case for all microcontrollers , the “2553” operates in
a binary world wherein a “high” voltage (between about 3 to 3.3 V) represents a Logic 1, and a
low voltage (about 0 V) represents a Logic Ø.1
The ‘2553” has twenty pins. Of these, two are reserved for power (VCC = 3.3 V) and ground
(GND = 0 V). One pin is used as a RESET pin (connected by default to VCC via a 1-k resistor2),
and one is a programming TEST pin (which we will not use in this class).
Of the remaining sixteen pins, eight comprise an 8-bit input/output (I/O) port labeled P1, and
another eight comprise an 8-bit I/O port labeled P2. The bits of Port 1 are consecutively
numbered P1.0 through P1.7, while those of Port 2 numbered P2.0 through P2.7. A pin-out
diagram of the 2553 DIP chip is shown below to the left. Note the “half-moon” notch which
locates the pin-1/pin-20 end of the chip:
1 In digital systems, the symbol Ø is sometimes used to represent Logic 0, so that it will not be confused with the
letter O. 2 You do not need to worry about connecting the programming-pin resistor, VCC, or ground when using the USB-
connected LaunchPad programmer. These connections are made automatically on the LaunchPad. If you remove a
programmed MSP430 chip from the Launchpad to install in your own breadboard circuit (for example, as part of
your final project), will you need to make these connections on your own.
10/17/2017 pg 3 of 13
The 2553 performs tasks in binary machine language (e.g., “assembly code”), but you can
program it with C-language commands using the Code Composer Studio program which
converts C code into MSP430 machine code via a process called compiling. Code Composer
also allows you to to download compiled machine-code programs to the microcontroller via the
USB connection between the LaunchPad and a PC.
Note: You can take the actual microcontroller chip out of its LaunchPad socket and plug it
directly into your breadboard. The MSP430 must be powered from a 3.3-V source with its
RESET pin tied to VCC. [Important: Do not power the MSP430 with a VCC higher than about 3.6
V or it will be damaged.]
The various pins of the microcontroller are brought to pins on the sides of the Launchpad
development and programming board, as shown in
the diagram to the right. For ease in testing,
debugging, and eventual integration onto your
breadboard, you can use the “rainbow” jumpers that
come with your EK307 Lab Kit to make connections
between the LauchPad and your breadboard. Note
that you can separate the jumper strip into individual
wires if you wish, or you can leave them as a flat
10/17/2017 pg 4 of 13
ribbon.
Programming the MSP430
The MSP430 chip recognizes numerous commands that tell it what to do with digital data
collected from (or sent to) its input/output (I/O) pins. The 2553 can also perform various
programmable logic functions such as AND, OR, if statements, etc. Using C code, each of the
ports is written to or read from using decimal (base-10) numbers to map to individual port pins.
Suppose, for example, that Port 1 is set up so that all of its pins are input bits, where the logic
levels (3.3 V and 0 V) are sent from a circuit on your breadboard. Further suppose that the
program inside the 2553 executes a command to read Port 1 and set the value of an integer
variable n to the data received from the port. The value of n will be determined by the eight
individual bits read from Port 1. The data bits, starting from bit P1.0 (the LSB) and proceeding
to bit P1.7 (the MSB), where each bit carries a weight of increasing powers of 2. Here LSB is
the “least significant bit” (smallest weight) and MSB is the “most significant bit” (largest
weight):
The value of n is determined by adding together the weights of all those bits that are set to Logic
1. Thus, for example, if the MSP430 reads Port 1, and its bits have been set to 1000 1001
By the circuit to which the microcontroller is connected (see figure above), then the value of n
read in from Port 1 will be 128 + 8 + 1 = 137, because a Logic 1 is found in the 128, 8, and 1
positions of the port.
PORT BIT: 7 6 5 4 3 2 1 0
Carries decimal weight: 128 64 32 16 8 4 2 1
LSB MSB 1 0 0 0 1 0 0 1
weight: 128 64 32 16 8 4 2 1
Sum of decimal weights: 128 + 0 + 0 + 0 + 8 + 0 + 0 + 1 = 137
LSB MSB 1 0 0 0 1 0 0 1
10/17/2017 pg 5 of 13
Ports 1 and 2 each have eight pins, hence the minimum and maximum values of n that can be
read from (or sent to) these ports are, respectively, 0 = 0000 0000 and 255 = 1111 1111.
Setup Commands
To program the MSP430, you must write and compile C-code using the Code Composer Studio
program. Various statements tell the MSP430 what to do and how to behave. Many common
commands have been pre-written for you and are explained in more detail detail the program
ek307starter_code .c available on the EK307 lab website: sites.bu.edu/engcourses/ek307 or
Blackboard: learn.bu.edu . Here several examples are provided below, but the starter code has
many more.
DIR Command
The DIR command tells the MSP430 whether a given port pin is to be used for input or output.
Specifically, if a bit in the DIR command is a logic 1, then the corresponding port pin will be set
to be an output. Conversely, if that same bit pin in the DIR command is a logic 0, then the
corresponding port pin is set to be an input. The input or output status of a port can be set on a
pin-by-pin basis and can be changed any time in your program.
Here are some examples:
Command Function
P1DIR = 0 Set all bits of Port 1 so that they receive (input) data
P1DIR = 255 Set all bits of Port 1 so that they send (output) data
P2DIR = 0 Set the two bits of Port 2 so that they receive (input) data
As explained above, the DIR command functions on a bit-by-bit (i.e., pin-by-pin) basis. Thus
some of the pins of a given port can be set to inputs, and others as outputs. For example, the
decimal number 64 corresponds to the binary number 0100 0000. Thus, the command
P1DIR = 64 (equivalent to P1DIR = 0100 0000)
sets Port 1 so that P1. 6 is an output, and all other Port 1 pins are inputs.
10/17/2017 pg 6 of 13
Likewise, the decimal number 65 corresponds to the binary number 0100 0001. Thus, the
command
P1DIR = 65 (equivalent to P1DIR = 0100 0001)
sets Port 1 so that P1. 6 and P1.0 are outputs, and all other Port 1 pins are inputs. Similarly,
P2DIR = 128 (equivalent to P2DIR = 1000 0000)
sets P2.7 to be an output, with all other pins as inputs as an input, and
P2DIR = 64
The either the command:
P2DIR = 128 + 64 (equivalent to P2DIR = 1100 0000)
or the command:
P2DIR = 192 (because 128 + 64 = 192, these statements are the same)
Sets P2.7 and P2.6 as output pins, and the rest of the Port 2 pins as inputs.
PIN Command
The PIN command tells the MSP430 to read data from one of its ports. The data pins are set to
either 1 or 0 (VCC or GND) by the circuit or connections external to the chip (or LaunchPad). In
the following example, suppose that n is a variable declared in the C program:
Command Function
n = P1IN Sets the integer variable n to the bit values applied to Port 1 or Port 2 by an
n = P1IN external circuit connected to Port 1 (e.g., by a circuit on your breadboard).
Only the pins designated as inputs via the PDIR command are read.
Select Command
The SELECT command is unique to the MSP430. It tells the chip to use pins P2.6 and P2.7 as
input output (I/O) pins, rather than as crystal-clock inputs3. The following command must
always be in your program if you want to use Port 2:
Command Function
3 We do not run the MSP430 with a clock crystal. The latter makes its timing ultra-precise in certain applications.
10/17/2017 pg 7 of 13
P2SEL=0 Tells the MSP430 that P2.6 and P2.7 are to be uses as I/O pins.
Procedures in C
While commands tell the MPS430 how to set up its hardware and ports, proceedures tell the
MSP430 how to process data. The file ek307starter_code .c provides many procedures (and
commands) that have been prewritten and preceded by “comment” bars //. To use any line of the
starter code, simply remove the // to make the statement active. You can also modify any lines,
or write your own. Just observe these basic rules:
1. A procedure, which can consist of multiple lines, is enclosed within squirrelly brackets { ….}
2. Every line of code must end with a semicolon ;
3. Any integers that you use in the program must be declared at the start of the program, e.g.
int n; (declares n as an integer whose value you will set later in the program)
int n=0; (declares n as an integer with an initial value of 0 that can be changed later
by the program.)
Conditional Procedures
Two very useful constructs are the logical if and while statements which have the following
syntax:
if (condition) { Do the commands inside the brackets that follow
one time if “condition” is true;}
while (condition) {Do the commands inside the brackets that follow
repeatedly as long as “condition” is true;}
Notes:
In C code, the binary digit 1 is interpreted as a “true” by if and while statements. The digit
0 equals “false”.
For a while procedure, the loop will go on forever if the condition contained in the
brackets { } never changes to “false”,
Example - The following code sequences the integer n from 0 to 5, and then returns it n = 0:
10/17/2017 pg 8 of 13
int n=0; // sets n to an integer with an initial value of 0.
while(1) // Because 1 equates to “true”, the bracketed procedure that
// follows will go on indefinitely.
{ // Here is the bracket that begins the while procedure.
if (n<6) {n = n+1;} // Increments n by 1 if n is less than 6.
if (n==6) {n=0;} // Resets n to 0 if it reaches the value 6.
// Note that an “equals” sign that is part of an if statement is a double “= =”
} // Here is the bracket that ends the while procedure.
Programming and Running the MSP430 Using Code Composer
To setup, compile, and run your code, follow this procedure exactly:
Plug the LaunchPad into the PC using the USB cable provided.
First time only:
1. Log on to your kerberos account on the PC at your lab station.
2. Under “My Computer”, find your X: drive.
3. Make a new folder on the X: drive called “MSP Folder” (or whatever else you choose to
call it).
4. Download ek307starter_code .c from the course website and save it in X:MSP Folder
Open Code Composer
5. From the START menu in Windows, find and open the TI folder, where you
should click on the icon for Code Composer. You may choose to create a
shortcut of this icon on your desktop.
From the Code Composer pull-down menu:
6. Select New CSS Project
7. Give your project a name such as MyLab (or whatever you want.)
8. Follow the prompts and steps, but be sure to choose
a. Project Type: MSP430
b. Output Type: Executable
c. Project Settings: MSP430G2553
10/17/2017 pg 9 of 13
d. Everything else: Choose the default settings
9. IMPORTANT: Set the Default Folder in Code Composer to X:MSP Folder (or whatever
you may have called it) from Step 3.
10. Drag-n-drop the ek307starter_code .c program from your desktop into the MyLab icon on
the left-hand column of Code Composer. This action will cause Code Composer to
generate all the needed auxiliary files.
10/17/2017 pg 10 of 13
Designing and Testing Your Microcontroller Program:
1. Modify, rewrite, or otherwise suitably alter ek307starter_code .c, then save it.
2. Click on “Build Active Project” (first time) or “Rebuild Active Project’ (thereafter) from
the pull-down menu. This action will compile your C-code.
3. Click on the “Debug” icon
from the pull-down menu. This action will load your
compiled machine code into the 2553 chip mounted in the socket on the LaunchPad.
4. Click on “Run” to execute and test your code on the 2553 via LaunchPad. Click “Stop” to
halt execution.
Debug Run
Build Active Project
Stop
10/17/2017 pg 11 of 13
Activities: Learning to Use the MSP430 Microcontroller
Your goal is to experiment with the MSP430 so that you can master its operation. For Level 1,
do an exercise that is labeled as a Level 1 challenge. To get credit for Level 2, do an exercise that
is labeled as a Level 2 challenge.
The MSP430 circuit boards are available for loan at the service window in the lab.
Exercise #1 – LED Bar Graph: (LEVEL 1)
Write a program that does the following:
Set up Port 1 as an output port and Port 2 as an input port.
Read a 3-digit binary input n from Port 2 (e.g., pins P2.0 – P2.2). Using rainbow
jumpers, set the Port 2 pins to either high (3.3 V) or low (0 V) by connecting them to
VCC or GND. Because n will be a 3-bit binary number, the decimal value of n can thus be
a decimal number between 0 and 7 (i.e., n will have eight possible values, depending on
the inputs read by Port2.
Drive the eight pins of Port 1 (P1.0 through P1.7) so that, when a given bit is in the 1
state, it will light an LED that has been connected between the pin and ground. Your
eight LEDs should light up according to the chart shown to the below.
10/17/2017 pg 12 of 13
Exercise #2 – Best of Seven (LEVEL 1)
Configure Port 1 as in input port, and Port 2 as an output port. Connect LEDs (in series with 1-
k current limiting resistors) to any two pins of Port 2. Call these LED1 and LED2. Write a
program that reads the first seven bits of Port 1 (P1.0 through P1.6), then sets the LEDs
connected to Port 2 to indicate the majority status of Port 1. Specifically:
Majority of Port 1 bits high – LED1 on, LED2 off.
Majority of Port 1 bits low – LED1 off, LED2 on.
Exercise #3 – Motor Speed Controller (LEVEL 2)
Connect a motor and a PN2222 transistor (available from the instrument window) using the
circuit shown below. The 2N2222 transistor operates as a binary switch that either connects the
motor between VDC and ground (logic 1 output from the MSP430) or disconnects the motor (logic
0 output from the MSP430).
The speed of the motor will depend on the average dc value of the voltage applied to it. In this
case, we can alter the average DC using the technique of pulse-width modulation.
If the digital signal sent to the 2N2222 transistor has a regular periodicity Ttot but a variable on
time Ton, as illustrated below, the “duty cycle”, defined by D = Ton/Ttot , will be the fraction of
time that the signal is high. In such a case, the average value of voltage will be DVDC. **
** The duty cycle is sometimes expressed as a percentage of on time: Duty Cycle = (Ton/ Ttot) 100%
One pin
of Port 2
(output)
Four pins
of Port 1
(inputs)
MS
P430
G2553
VDC = +12 V
Motor
1 k 2N2222
E B C
C B E
10/17/2017 pg 13 of 13
Write a program that will set the motor to different speeds (at least four) depending on the
high/low status of the Port 1 inputs. For example, your settings might yield the following result:
A reasonable value for T might be 128 ms but, for the motor
provided, it could be anything under about a half second.
Exercise #4 – Morse Code SOS Generator (LEVEL 2)
Write a program that will blink an LED and sound a loudspeaker according to the universally-
known distress signal
Port 1 Bits Duty Cycle 0001 25%
0010 50%
0100 75%
1000 100%
Ttot
Ton
Time
. . .
VDC Average DC value = (Ton/T)VDC
Time
Bit output to LED 1
0
S O S