Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

EK307 Lab 7 Microcontrollers

Laboratory Goal: Program an eight-bit microcontroller to perform a variety of tasks. Learning Objectives: Digital interfaces, pulse-width modulation, TI MSP430 microcontroller, C programming language. Suggested Tools: MSP430 Launchpad, LEDs, oscilloscope, electric motor.

Background

A microcontroller, or C, is a device constructed from logic circuits that is capable of dynamically manipulating digital information. Microcontrollers are everywhere automobiles, airplanes, robots, computer peripherals, medical implants, kitchen appliances, cell phones, wind turbines, toys, lighting systems any system that requires a local intelligence to react, interact, or control something can make use of a microcontroller. In many products multiple microcontrollers are used for specic tasks, such as in a car where there could be independent microcontrollers to monitor and control the engine, fuel injection, accelerator-pedal sensing, tire-pressure sensing, cruise control, anti-lock breaks, emissions monitoring and control, GPS, and radios or other entertainment systems, to name a few examples. A modern aircraft may contain hundreds of microcontrollers, each of which performs a dierent function.

1.1

Number Representation

Ultimately, the microcontroller will operate on binary numbers represented by 1s and 0s. In the C language, however, numbers can be expressed in dierent ways. They can be written in base 10 as they are in normal writing, and they can also be written using base 16 (hexadecimal or just hex) or base 2 (binary). Hexadecimal numbers are specied by prexing the number with 0x and binary numbers specied by prexing the number with 0b. For example the number 15 can also be expressed in hex as 0x0F, or in binary as 0b00001111. The number 137 = 128+8+1 can be expressed as 0x89 or 0b10001001:
Binary Hex Decimal 0b10001001 = 0x89 = 137 27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1

Number: Carries decimal weight: Binary number/ Register Value Bit:

10001001
8 9

Hex grouping:

When working with individual bits of input and output ports, it is often easier to use the binary or hex representation than the decimal representation. Note that Code Composer allows the 0x notation but does Boston University ECE Page 1/15

EK307

Microcontrollers

Lab 7

P2OUT
7

0 1 x x x x x x

VCC

14

(3.3V) 1

13

1 (3.3V)

(0V) 0

Memory

12

0 (0V)

Port 2

Port 1

(3.3V) 1

4
P1OUT P2OUT

11

(0V) 0

10

(3.3V) 1

6
7

9 1 0 1 1 0 1 0 1 P1OUT
0

1 (3.3V)

(3.3V) 1

0 (0V)

Port 1, cont

Figure 1: The pin of each output port is internally connected to a register. When a bit in the register is 1, that pin is driven with a logic 1 (3.3 V). When the bit is a zero, the corresponding pin is driven with a logic 0 (0 V). The letter X represents a dont care, a bit that does not matter because it does not aect anything. not recognize the 0b notation. The 0b is still useful in illustrating many concepts with binary numbers and planning to use binary values.

1.2

Using Input and Output Ports

In addition to many of the standard C functions, there are many special registers that are used to control the operation of the microcontroller. For example, there is a register, labeled P1OUT, that when written to will change the state of the pins of Port 1 to reect the binary value of the number that it contains. In Figure 1, the P1OUT register contains the binary value 0b10110101, or 181 in decimal. Each pin is driven with 3.3 V or 0 V depending on the state of a bit in the P1OUT register.

1.3

The MSP430 Series of Microcontrollers

The Texas Instruments MSP430 series of microcontrollers are 16-bit (they use 16-bits internally to represent numbers), RISC (Reduced Instruction Set Computing), mixed-signal (analog and digital) microcontrollers that are known for being low power devices. The MSP430G2231 or MSP430G2553 supplied with the TI LaunchPad (Figure 2) is a device balanced between low power performance and ease of use. Details about the device and its use can be found in two documents, the datasheet for the specic chip and the MSP430x2xx Family Users Guide, both of which can be obtained from Texas Instruments or found on the EK307 website. The C on the launch pad will have either fourteen or twenty pins:

Boston University ECE

Page 2/15

EK307

Microcontrollers

Lab 7

Pins labeled VCC and GND are reserved for power (VCC = 3.3 V and Ground = 0 V). The pin labeled (S1) RST is used as a RESET pin, connected by default to VCC via a 1 k resistor. This resistor is already in place on the LaunchPad board. If you were to remove a programmed MSP430 chip from the LaunchPad, for example, to put it in a breadboard, you would need to put this resistor in place yourself. The pin labeled TEST is a programming pin that is used for programming and debugging. Pins labeled P1.0, P1.1, . . . , and P1.7 make up an 8-bit input/output (I/O) port. These pins can be used to either read digital signals from an external circuit, or drive digital signals to be read by and external circuit. Pins labeled P2.0, P2.1, . . . , P2.5, XOUT (which is P2.7), and XIN (which is P2.6) make up a second input/output port. On the 14-pin microcontrollers, like the MSP430G2231, only XOUT and XIN are present. On the 20 pin microcontrollers all eight Port 2 pins are present. A pin-out diagram of the MSP430G2231 DIP chip is shown in Figure 2. These pins are brought out to the edge of the LaunchPad to facilitate connecting them to external circuitry. The 20-pin microcontrollers have a similar pin out, with the rst 6 bits of Port 2 being added on at the bottom.

Figure 2: The MSP430G2231 pin-out and breakout on the LaunchPad. The empty sockets below the chip are used for other MSP430 microcontrollers and can safely be ignored for this lab.

1.4

Connecting the LaunchPad to External Circuitry

The LaunchPad can be connected to external by running wires from the sockets on the edge of the LaunchPad to the external devices. Figure 3 shows two pins being connected to two rows of a breadboard. Note that these sockets do not come installed on the LaunchPad development board. They are contained in a static bag within the LaunchPads box. While it isnt absolutely necessary, it is highly recommended that these sockets be soldered to the LaunchPad development board before attempting to connect it to external circuitry. Soldering irons are available in PHO105 and soldering supplies can be obtained from the window. Soldering irons get very hot and can cause severe burns if not used properly. If you do not know how to solder, ask for help before attempting to do it on your own. Boston University ECE Page 3/15

EK307

Microcontrollers

Lab 7

Figure 3: Connecting the LaunchPad to a breadboard. The LaunchPad has another header (labeled J6) that can be used to supply power to external circuitry. The pin labeled VCC supplies 3.3 V and the pins labeled GND are connected to ground.

J6 Pins

When connecting the MSP430 Cs to devices that use other sources of power, it is important to isolate the C from those power sources. NEVER connect power to the LaunchPad while it is plugged into a computer. When other sources of power are required, only the grounds should be connected together. Applying multiple power sources simultaneously can burn the C, the LaunchPad, and even the computer. Alternatively, the MSP430 chip can be removed and installed in a breadboard. To do this, CAREFULLY remove the chip from the socket on the LaunchPad and install it in the breadboard. The pins are easily bent and broken, which can render the part useless. After installing the MSP430 chip in the breadboard, there are three required connections that must be made: 1. Connect VCC to 3.3 V 2. Connect GND to ground 3. Connect the RST pin to 3.3 V using a resistor. The resistor should be between 1 k and 10 k.

Boston University ECE

Page 4/15

EK307

Microcontrollers

Lab 7

RST Pin (10) connected to VCC through 10k Pin 14 Connected to Ground

Red Rails: 3.3V Blue Rails: Ground Pin 1 Connected to VCC

Connections from Port 1 to external circuitry

When power is applied, the microcontroller will start executing whatever code was last downloaded to it.

1.5

Programming and Running the MSP430 Using Code Composer

Code Composer Studio is a software tool produced by Texas Instruments that facilitates the design and implementation of code for their various lines of microcontrollers, including the MSP430. There is a lot of documentation and support available from the Code Composer Studio product page on TIs website. 1.5.1 Setting Up Code Composer

The rst time you run Code Composer you will need to setup a workspace on your X: drive and create a new project. 1. Under My Computer, nd the X: drive and make a new folder for Code Composer to use as a workspace. You might call it ek307lab, for example. 2. Under the folder you just made, create a nested folder called level0. 3. Open the Code Composer either from the Start menu or from the desktop icon .

4. If you are prompted to select a Default Folder or Workspace, point it to X:\ek307lab. If you fail to set your X: drive folder as the default, the program will not work because you do not have permissions to write to any folders on the C: drive. Note: you MUST access your network space using the X: drive. If you access your network space through any other means (such as through the Network Places) Code Composer will appear to be Boston University ECE Page 5/15

EK307

Microcontrollers

Lab 7

working and then fail to compile your code citing cryptic error messages about UNC paths. 5. Select File New CSS Project and give it a name such as lab6level0 (no spaces or special characters allowed). If you dont see File New CSS Project in the menu, then instead select File New Other... and scroll down to CSS Project in the dialog box that comes up.

1 2 3
X:\ek307lab\level0\

6. When prompted to select a project type, select MSP430 from the drop down menu.

Boston University ECE

Page 6/15

EK307

Microcontrollers

Lab 7

7. Look for the part number of the microcontroller installed in your LaunchPad, and select it in the Device Varient: eld.

8. Create a new C Source File in the project. Make sure it is in the correct project folder and is named appropriately. Note that the .c sux must be present at the end of the lename in order for Code Composer to know that it contains C code.

Right Click

Boston University ECE

Page 7/15

EK307

Microcontrollers

Lab 7

9. Copy and paste the contents of ek307master.c into the le you just created.

3. Right Click, Copy 2. Right Click, Select All

4. Paste code into new le

1. Download Code

You are now ready to work with the LaunchPad and MSP430 microcontroller. 1.5.2 Compiling, Downloading, and Running Your Code

After you have modied, rewritten, or otherwise altered the content of the ek307master.c le, you will need to compile it, download it to the chip, and execute it.

Before the code can be compiled, two modications must be made.


First, identify the part number of microcontroller installed in the LaunchPad, it should be one of the following: MSP430G2231, MSP430G2452 or MSP430G2553. Near the beginning of the code is a section labeled Includes. Uncomment the #include statement that corresponds to your microcontroller. Next, scroll down past the main() function to the Timer A interrupt service routine, TimerA0 ISR. There are two commented #pragma statements. Uncomment the statement that corresponds to the microcontroller installed in the LaunchPad, and delete the other. There should be no lines, blank, commented, or otherwise, between the #pragma statement and the TimerA0 ISR function. Now the code can be compiled and downloaded. 1. Click on Build Active Project (rst time) or Rebuild Active Project (thereafter) from the pulldown menu or tiny blue icons. This action will compile your C-code into the machine code that the MSP430 understands. 2. Click on Debug from the pull-down menu (the little insect icon). This action will load your compiled machine code into the MSP430 chip mounted in the socket of the LaunchPad. Note: The program is stored in Flash memory on the MSP430. This means that you can remove power from the chip without it losing the programming. You can then re-apply power (perhaps from a dierent power source) and the program will start running again from the beginning.

Boston University ECE

Page 8/15

EK307

Microcontrollers

Lab 7

3. Click on Run (green arrow) to execute and test your code on the MSP430 via LaunchPad. 4. Click Stop (red box) to halt execution. Note: It is important to stop the execution of your program on the LaunchPad before you change and recompile your code. Failure to do this can result in error messages about communication with the device being interrupted or the device being busy.

1.5.3

Using the Debugger in Code Composer

Code Composer has many tools to help debug code while it is running on a microcontroller. Two of these are breakpoints and watch variables. A breakpoint can be set anyplace in the code. When the microcontroller reaches that place, it will stop what it is doing and wait for further instructions. This allows the programmer to inspect the contents of memory or CPU registers at various places in the program. Breakpoints can be set by right clicking on a line of code, and selecting New Breakpoint from the context menu, and then selecting the Breakpoint option. A colored dot will appear next to the line number to indicate that a breakpoint has been set there. The breakpoint can be subsequently disabled by right-clicking on the dot and selecting Disable Breakpoint. Breakpoints are listed in the Breakpoint Window.

Boston University ECE

Page 9/15

EK307

Microcontrollers

Lab 7

Right click on a line

Whenever the microcontrollers execution is stopped, manually or by a breakpoint, the contents of a variable that is listed as a watch variable is retrieved and displayed in the watch list. Watch variables can be set by adding the name of the variable to the list in the Watch Window.

This is very useful in conjunction with breakpoints. For example, to examining the counter variable to see if it is incrementing, a breakpoint can be set just before it is incremented, and the counter variable can be added to the watch list. Every time the execution reaches that line of code, the microcontroller will pause and update the display in the watch list. The execution can also be stopped by clicking the pause icon in the debugging window. If either the breakpoint or watch window is not displayed, both can be accessed from the View menu.

Boston University ECE

Page 10/15

EK307

Microcontrollers

Lab 7

More information about using the debugging features of Code Composer can be found in the Users Guide on the Code Composer Studio product page

1.6

Starter Code: ek307master.c

The ek307master.c le contains a working program for the MSP430 microcontroller. When compiled and downloaded, this program will blink the LEDs on the LaunchPad on and o with a period of 2 seconds. This program works by conguring the C to execute a function periodically every 10 ms (this function is called an interrupt service routine or ISR). Every 10 ms the function TimerA0 ISR is executed. By counting the number of times this ISR is executed, longer delays can be implemented. For example, if the function has been called 100 times, then 100 10 ms = 1 s has elapsed. The starter code has 3 sections that will be important for this lab: 1. Initializations This is the section where variables are declared and initialized. Anytime information must be retained, a variable is needed to store the information. For example, in ek307master.c there is a counter that is incremented every time the ISR is executed. The variable that stores that count, counter, is declared and initialized here. 2. Main Function This is where dierent parts of the C are congured. For example, this is where the ISR is setup to execute every 10 ms. This is also where Ports 1 and 2 are congured to be input or output ports. Sometimes variables are initialized here as also. 3. Timer A Interrupt Service Routine This function is executed periodically every 10 ms, and this is where most EK307 code should be placed. The code in this function checks the value of the counter variable, and toggles the state of the output pins if it equals 100. If it doesnt equal 100, then it increments counter and goes back to sleep. When working on levels 1, 2, and 3, you should comment out the lines of code that do this. Boston University ECE Page 11/15

EK307

Microcontrollers

Lab 7

1.7

Pseudocode

Before writing a program for a computer or microcontroller, it is helpful to rst write the program using pseudocode. Pseudocode is a high level, human readable, list of the steps that must be taken by the program. Writing pseudocode is similar to outlining a paper or presentation before actually writing it. Well written pseudocode can translate easily into the executable code used to program devices and can be used as comments in the code to help a reader understand how the code works. For example, the pseudocode for the Hello World! program in Level 0 of this lab might look as follows. In the initialization section: 1. Declare and initialize a counter to 0 In the main() function, set up the interrupts and the output port: 2. Setup the Timer A interrupt to execute a function (the interrupt service routine) at regular, known, 10 ms intervals. 3. Congure Port 1 to be an output port. Then, in the interrupt service routine: 4. Check the value of the counter: (a) If counter == 100, i.e., 1 second has passed: i. Change the state of the output LEDs ii. Reset the counter to 0. (b) Otherwise (the counter does not equal 100, i.e., 1 second has not elapsed) increment the counter. Writing good pseudocode rst not only helps to organize thoughts and ideas for how to implement the program, but it also generally results in cleaner, more readable code that is easier to debug.

2
2.0

Lab Assignment
Level 0: Hello World!
Activity

2.0.1

Hello World is traditionally the rst program a programmer will write when learning a new language. It has various forms depending on the platform for which the program is written. On a computer, this typically takes the form that simply prints the words hello world! to the screen. When working with microcontrollers there usually isnt a screen, so the Hello World program for a C has become a program that blinks an LED on and o. A Hello World program can be found in the EK307 starter code: ek307master.c. Create a new project in Code Composer and copy the contents of the ek307master.c le into it, following the directions in Section 1.5. Boston University ECE Page 12/15

EK307

Microcontrollers

Lab 7

Compile the code and download it onto the microcontroller using the LaunchPad. If this process is successful, click the run button, and the two LEDs on the edge of the LaunchPad should start blinking on and o with each state lasting about 1 second. Set a breakpoint on the line of code where the value of the counter is being tested, add counter to the list of watch variables, and show that the counter increments each time the TimerA0 ISR is executed.

2.1

Level 1: Running LEDs

Note: To continue, you will either need to modify the code from level 0, or you will need to create a new Code Composer project for level 1. You cannot have multiple copies of the starter code in the same project. If you choose to modify the code instead of creating a copy of the project, you should make a copy of the code somewhere else for future reference. 2.1.1 Activity

Connect four LEDs to the lowest four pins of port 1 (P1.0-P1.3). Modify the level 0 code to light up the rst LED, then the second, then the third, then the fourth. When each LED turns on, the others should turn o. This sequence is illustrated by the following diagram.
LED0 P1.0 LED1 P1.1 LED2 P1.2 LED3 P1.3

For an extra challenge, you could try to make the LED sequence go in both directions so that the LED appears to move back and forth, from left to right and then back to the left, instead of only moving in one direction. Such a circuit/program might be used to control events and make them happen in sequence, or as a cool eect for the eyes of evil robots bent on destroying all of humanity. 2.1.2 Sign-o

Demonstrate to the lab instructor that the four LEDs light up in the correct sequence. In your notebook: 1. Write a paragraph describing how your code works and include the pseudocode for your program. 2. Include the code from your interrupt service routine, and make a note of any other changes you may have made to the base code.

2.2
2.2.1

Level 2: Motor Speed Controller


Activity

For this level, you will need a DC motor. If you dont have one in your kit, you can sign out a DC motor from the Instrument Window in PHO105. Connect your motor using the following circuit. The 2N2222 Boston University ECE Page 13/15

EK307

Microcontrollers

Lab 7

transistor, found in your lab kit, operates as a simple on/o switch that either connects the motor between VDC and GND (when XOUT=1) or disconnects the motor from VDC (when XOUT = 0).

VDC = +12 V
MSP430 G2231

Motor 1 k XOUT
B C E

Four pins of Port 1 (inputs)

2N2222

EBC

When you build this circuit, you must connect the GROUND from the motor power supply to the GROUND of the 2231. That should be the ONLY connection between the 2231 and the 12V power supply. DO NOT connect 12 V to the VCC of the 2231. The speed of the motor will depend on the average value of the voltage applied to it. In this case you can alter the voltage using a technique known as pulse-width modulation. If the digital signal XOUT sent to the 2N2222 transistor has a regular periodicity Ttot but a variable on time Ton , as illustrated below, the duty cycle, dened by D = Ton /Ttot , will be the fraction of time that the signal is high. In such a case, the average value of the voltage applied to the motor will be (D)(VDC ). (The duty cycle is sometimes expressed as a percentage of on time: D = Ton /Ttot 100%).
VDC Average DC value = (Ton/T)VDC

...
Ton Ttot Time

Port 1 Bits 0001 0010 0100 1000

Duty Cycle 25% 50% 75% 100%

Write a program that will set the motor to dierent speeds (at least four) depending on the high/low status of the Port 1 inputs. For example, your settings might yield the results shown in the table above. A reasonable value for Ttot might be 128 ms, but for the motor provided, Ttot can be anything under about half a second. The ration of Ton /Ttot is what matters, not the exact value of Ttot . Using the PicoScope, show that the duty cycle of your XOUT signal complies with the requirements of the above table (or similar of you elect to use more than 4 bits of Port 1). 2.2.2 Questions

For each speed setting, record the current drawn by the motor. Why is it necessary to use a transistor and a separate power supply to drive the motor? 2.2.3 Sign-o

Demonstrate to the lab instructor that the duty cycle of the signal driving the motor matches the specication for each of the four cases, and that this causes the motor to spin at four dierent speeds. In your notebook: 1. Record a circuit diagram showing your circuit. Boston University ECE Page 14/15

EK307

Microcontrollers

Lab 7

2. Write a paragraph describing how your code works and include the pseudocode for your program. 3. Write answers to the questions for this level. 4. Print, annotate, and tape or staple into your notebook a waveform from the oscilloscope showing the duty cycle of the driving waveform for each case. 5. Include the code from your interrupt service routine, and make a note of any other changes you may have made to the base code. 6. Think of 2 examples of how microcontrollers in general could be used in your branch of engineering, and write a couple of sentences explaining each.

2.3
2.3.1

Level 3: Digital Electronic Synthesizer


Activity

Design and build a small digital electronic synthesizer. The synthesizer should have eight input keys (push button momentary switches or wires from a pin to power or ground) and a single output. The output should produce a square wave with a frequency that depends on which of the keys is pressed as indicated in Table 1. The output should be used to drive a loudspeaker. The output should only produce sound when a key is depressed. If a second key is depressed while a sound is already being produced, it should be ignored. Key 0 1 2 3 4 5 6 7 Frequency [Hz] 262 294 330 349 392 440 494 523

Table 1: Frequencies to produce when a particular key is pressed. 2.3.2 Sign-o

Demonstrate your circuit for the lab instructor. In your notebook: 1. Write a paragraph describing how your code works and include the pseudocode for your program. 2. Print, annotate, and tape or staple into your notebook four waveforms from the oscilloscope showing the waveforms generated when buttons 0, 2, 4, or 7 are pressed. 3. Include the code from your interrupt service routine, and make a note of any other changes you may have made to the base code.

Last revised: April 5, 2013

Boston University ECE

Page 15/15

You might also like