This action might not be possible to undo. Are you sure you want to continue?
• 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.
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 speciﬁc 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 diﬀerent function.
Ultimately, the microcontroller will operate on binary numbers represented by 1s and 0s. In the C language, however, numbers can be expressed in diﬀerent 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 speciﬁed by preﬁxing the number with ‘0x’ and binary numbers speciﬁed by preﬁxing 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:
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
1.3V) 1 7 8 0 (0V) Port 1. labeled P1OUT.3 V or 0 V depending on the state of a bit in the P1OUT register. When a bit in the register is 1. The MSP430G2231 or MSP430G2553 supplied with the TI LaunchPad (Figure 2) is a device balanced between low power performance and ease of use. 1.EK307 Microcontrollers Lab 7 1 P2OUT 7 0 1 x x x x x x VCC 0 14 (3. RISC (Reduced Instruction Set Computing).3V) 1 6 7 9 1 0 1 1 0 1 0 1 P1OUT 0 1 (3. the corresponding pin is driven with a logic 0 (0 V). that when written to will change the state of the pins of Port 1 to reﬂect the binary value of the number that it contains.2 Using Input and Output Ports In addition to many of the standard C functions. the datasheet for the speciﬁc chip and the MSP430x2xx Family User’s Guide.3 V).3V) 1 2 13 1 (3. a bit that does not matter because it does not aﬀect anything.3 The MSP430 Series of Microcontrollers The Texas Instruments MSP430 series of microcontrollers are 16-bit (they use 16-bits internally to represent numbers). that pin is driven with a logic 1 (3. Details about the device and its use can be found in two documents. In Figure 1.3V) (3. The letter X represents a “don’t care”. there is a register. the P1OUT register contains the binary value 0b10110101. The 0b is still useful in illustrating many concepts with binary numbers and planning to use binary values. there are many special registers that are used to control the operation of the microcontroller. When the bit is a zero. not recognize the 0b notation. both of which can be obtained from Texas Instruments or found on the EK307 website.3V) 1 4 P1OUT P2OUT 11 (0V) 0 5 10 (3.3V) (0V) 0 3 Memory 12 0 (0V) Port 2 Port 1 (3. The µC on the launch pad will have either fourteen or twenty pins: Boston University ECE Page 2/15 . mixed-signal (analog and digital) microcontrollers that are known for being low power devices. or 181 in decimal. For example. Each pin is driven with 3. con’t Figure 1: The pin of each output port is internally connected to a register.
Boston University ECE Page 3/15 . Soldering irons get very hot and can cause severe burns if not used properly.7). Figure 3 shows two pins being connected to two rows of a breadboard. .7 make up an 8-bit input/output (I/O) port.1. . ask for help before attempting to do it on your own. you would need to put this resistor in place yourself. These pins can be used to either read digital signals from an external circuit. P2. This resistor is already in place on the LaunchPad board. • The pin labeled TEST is a programming pin that is used for programming and debugging. The empty sockets below the chip are used for other MSP430 microcontrollers and can safely be ignored for this lab. If you do not know how to solder.0.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. only XOUT and XIN are present. On the 20 pin microcontrollers all eight Port 2 pins are present. . • Pins labeled P1.1. While it isn’t absolutely necessary. The 20-pin microcontrollers have a similar pin out. with the ﬁrst 6 bits of Port 2 being added on at the bottom. like the MSP430G2231.6) make up a second input/output port. P2. . XOUT (which is P2. and XIN (which is P2. . Figure 2: The MSP430G2231 pin-out and breakout on the LaunchPad. Soldering irons are available in PHO105 and soldering supplies can be obtained from the window. 1. it is highly recommended that these sockets be soldered to the LaunchPad development board before attempting to connect it to external circuitry. These pins are brought out to the edge of the LaunchPad to facilitate connecting them to external circuitry. . They are contained in a static bag within the LaunchPad’s box. . A pin-out diagram of the MSP430G2231 DIP chip is shown in Figure 2. If you were to remove a programmed MSP430 chip from the LaunchPad.5. .EK307 Microcontrollers Lab 7 • Pins labeled VCC and GND are reserved for power (VCC = 3. • The pin labeled (S1) RST is used as a RESET pin. • Pins labeled P2. P1. Note that these sockets do not come installed on the LaunchPad development board. or drive digital signals to be read by and external circuit. for example. connected by default to VCC via a 1 kΩ resistor. and P1.3 V and Ground = 0 V). to put it in a breadboard.0. On the 14-pin microcontrollers.
CAREFULLY remove the chip from the socket on the LaunchPad and install it in the breadboard. When other sources of power are required. Applying multiple power sources simultaneously can burn the µC. Alternatively. To do this. the LaunchPad. it is important to isolate the µC from those power sources.EK307 Microcontrollers Lab 7 Figure 3: Connecting the LaunchPad to a breadboard. there are three required connections that must be made: 1. NEVER connect power to the LaunchPad while it is plugged into a computer.3 V using a resistor. which can render the part useless. Connect GND to ground 3. The pins are easily bent and broken. The resistor should be between 1 kΩ and 10 kΩ. and even the computer. The pin labeled VCC supplies 3. J6 Pins When connecting the MSP430 µCs to devices that use other sources of power. Connect the RST pin to 3.3 V 2. Boston University ECE Page 4/15 . only the grounds should be connected together.3 V and the pins labeled GND are connected to ground. After installing the MSP430 chip in the breadboard. The LaunchPad has another header (labeled J6) that can be used to supply power to external circuitry. the MSP430 chip can be removed and installed in a breadboard. Connect VCC to 3.
Under the folder you just made. Under “My Computer”. create a nested folder called level0.3V Blue Rails: Ground Pin 1 Connected to VCC Connections from Port 1 to external circuitry When power is applied. 3. point it to X:\ek307lab.EK307 Microcontrollers Lab 7 RST Pin (10) connected to VCC through 10k Pin 14 Connected to Ground Red Rails: 3. 4. Open the Code Composer either from the Start menu or from the desktop icon . Note: you MUST access your network space using the X: drive. There is a lot of documentation and support available from the Code Composer Studio product page on TI’s website. 1. If you are prompted to select a Default Folder or Workspace.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.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. You might call it ek307lab. 2. 1. 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 . the microcontroller will start executing whatever code was last downloaded to it. If you fail to set your X: drive folder as the default. including the MSP430. ﬁnd the X: drive and make a new folder for Code Composer to use as a workspace. for example. 1. the program will not work because you do not have permissions to write to any folders on the C: drive.
Select File → New → CSS Project and give it a name such as lab6level0 (no spaces or special characters allowed).EK307 Microcontrollers Lab 7 working and then fail to compile your code citing cryptic error messages about UNC paths.. 1 2 3 X:\ek307lab\level0\ 6. then instead select File → New → Other. 5. and scroll down to CSS Project in the dialog box that comes up. When prompted to select a project type. If you don’t see File → New → CSS Project in the menu.. Boston University ECE Page 6/15 . select MSP430 from the drop down menu.
Right Click Boston University ECE Page 7/15 . 8. Make sure it is in the correct project folder and is named appropriately. Create a new C Source File in the project. and select it in the “Device Varient:” ﬁeld.EK307 Microcontrollers Lab 7 7. Look for the part number of the microcontroller installed in your LaunchPad. Note that the .c suﬃx must be present at the end of the ﬁlename in order for Code Composer to know that it contains C code.
EK307 Microcontrollers Lab 7 9. 1. commented. Copy and paste the contents of ek307master. Before the code can be compiled. Select All 4. Next. TimerA0 ISR.c ﬁle.5. Right Click.2 Compiling. between the #pragma statement and the TimerA0 ISR function. Note: The program is stored in Flash memory on the MSP430. two modiﬁcations must be made. This means that you can remove power from the chip without it losing the programming. blank. This action will compile your C-code into the machine code that the MSP430 understands. scroll down past the main() function to the Timer A interrupt service routine. Copy 2. or otherwise altered the content of the ek307master.c into the ﬁle you just created. Downloading. identify the part number of microcontroller installed in the LaunchPad. Click on “Debug” from the pull-down menu (the little insect icon). You can then re-apply power (perhaps from a diﬀerent power source) and the program will start running again from the beginning. and Running Your Code After you have modiﬁed. Right Click. Uncomment the statement that corresponds to the microcontroller installed in the LaunchPad. 3. 1. Uncomment the #include statement that corresponds to your microcontroller. Paste code into new ﬁle 1. you will need to compile it. Now the code can be compiled and downloaded. There are two commented #pragma statements. Click on “Build Active Project” (ﬁrst time) or “Rebuild Active Project” (thereafter) from the pulldown menu or tiny blue icons. or otherwise. Download Code You are now ready to work with the LaunchPad and MSP430 microcontroller. rewritten. First. There should be no lines. and execute it. 2. download it to the chip. MSP430G2452 or MSP430G2553. Near the beginning of the code is a section labeled Includes. it should be one of the following: MSP430G2231. This action will load your compiled machine code into the MSP430 chip mounted in the socket of the LaunchPad. Boston University ECE Page 8/15 . and delete the other.
5. The breakpoint can be subsequently disabled by right-clicking on the dot and selecting “Disable Breakpoint”. Boston University ECE Page 9/15 . Two of these are breakpoints and watch variables. Note: It is important to stop the execution of your program on the LaunchPad before you change and recompile your code. 1. Breakpoints are listed in the Breakpoint Window. Failure to do this can result in error messages about communication with the device being interrupted or the device being busy. A colored dot will appear next to the line number to indicate that a breakpoint has been set there. Breakpoints can be set by right clicking on a line of code. When the microcontroller reaches that place. and then selecting the “Breakpoint” option. Click on “Run” (green arrow) to execute and test your code on the MSP430 via LaunchPad. A breakpoint can be set anyplace in the code.EK307 Microcontrollers Lab 7 3. 4. it will stop what it is doing and wait for further instructions. and selecting “New Breakpoint” from the context menu.3 Using the Debugger in Code Composer Code Composer has many tools to help debug code while it is running on a microcontroller. Click “Stop” (red box) to halt execution. This allows the programmer to inspect the contents of memory or CPU registers at various places in the program.
a breakpoint can be set just before it is incremented. This is very useful in conjunction with breakpoints. Watch variables can be set by adding the name of the variable to the list in the Watch Window. Every time the execution reaches that line of code. both can be accessed from the View menu. to examining the counter variable to see if it is incrementing. and the counter variable can be added to the watch list. the microcontroller will pause and update the display in the watch list.EK307 Microcontrollers Lab 7 Right click on a line Whenever the microcontroller’s 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. The execution can also be stopped by clicking the pause icon in the debugging window. Boston University ECE Page 10/15 . For example. If either the breakpoint or watch window is not displayed.
this program will blink the LEDs on the LaunchPad on and oﬀ with a period of 2 seconds. If it doesn’t equal 100. Main Function This is where diﬀerent parts of the µC are conﬁgured. For example. 2.6 Starter Code: ek307master. 2. The variable that stores that count.EK307 Microcontrollers Lab 7 More information about using the debugging features of Code Composer can be found in the User’s Guide on the Code Composer Studio product page 1. When working on levels 1. and 3. Anytime information must be retained. if the function has been called 100 times. By counting the number of times this ISR is executed. For example. this is where the ISR is setup to execute every 10 ms. Boston University ECE Page 11/15 . Timer A Interrupt Service Routine This function is executed periodically every 10 ms. is declared and initialized here. counter. and this is where most EK307 code should be placed. in ek307master. This is also where Ports 1 and 2 are conﬁgured to be input or output ports. you should comment out the lines of code that do this. then 100 × 10 ms = 1 s has elapsed. then it increments counter and goes back to sleep. The code in this function checks the value of the counter variable. a variable is needed to store the information. 3. longer delays can be implemented.c The ek307master. For example. Initializations This is the section where variables are declared and initialized. The starter code has 3 sections that will be important for this lab: 1.c ﬁle contains a working program for the MSP430 microcontroller. Every 10 ms the function TimerA0 ISR is executed.c there is a counter that is incremented every time the ISR is executed. When compiled and downloaded. Sometimes variables are initialized here as also. and toggles the state of the output pins if it equals 100. This program works by conﬁguring the µC to execute a function periodically every 10 ms (this function is called an interrupt service routine or ISR).
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. Boston University ECE Page 12/15 . so the Hello World program for a µC has become a program that blinks an LED on and oﬀ. Conﬁgure Port 1 to be an output port. In the initialization section: 1.EK307 Microcontrollers Lab 7 1. On a computer. i. Create a new project in Code Composer and copy the contents of the ek307master.1 Hello World is traditionally the ﬁrst program a programmer will write when learning a new language. (b) Otherwise (the counter does not equal 100. 1 second has not elapsed) increment the counter. following the directions in Section 1. Then.0. human readable.c.5.0 Lab Assignment Level 0: Hello World! Activity 2. in the interrupt service routine: 4. list of the steps that must be taken by the program. i. 2 2. Declare and initialize a counter to 0 In the main() function. known. 3.7 Pseudocode Before writing a program for a computer or microcontroller. Pseudocode is a high level. A Hello World program can be found in the EK307 starter code: ek307master. When working with microcontrollers there usually isn’t a screen. more readable code that is easier to debug.c ﬁle into it. Writing good pseudocode ﬁrst not only helps to organize thoughts and ideas for how to implement the program. the pseudocode for the Hello World! program in Level 0 of this lab might look as follows. set up the interrupts and the output port: 2. For example.e. Reset the counter to 0. It has various forms depending on the platform for which the program is written. but it also generally results in cleaner. Writing pseudocode is similar to outlining a paper or presentation before actually writing it. Setup the Timer A interrupt to execute a function (the interrupt service routine) at regular... 1 second has passed: i. Change the state of the output LEDs ii. it is helpful to ﬁrst write the program using pseudocode.e. 10 ms intervals. Check the value of the counter: (a) If counter == 100. this typically takes the form that simply prints the words “hello world!” to the screen.
you will either need to modify the code from level 0.3 For an extra challenge.1 Level 1: Running LEDs Note: To continue. Set a breakpoint on the line of code where the value of the counter is being tested. LED0 P1.1.0 LED1 P1. instead of only moving in one direction. 2. You cannot have multiple copies of the starter code in the same project. Connect your motor using the following circuit. then the fourth. and the two LEDs on the edge of the LaunchPad should start blinking on and oﬀ with each state lasting about 1 second. If this process is successful. 2. Write a paragraph describing how your code works and include the pseudocode for your program. you will need a DC motor.1 LED2 P1. add counter to the list of watch variables.EK307 Microcontrollers Lab 7 Compile the code and download it onto the microcontroller using the LaunchPad. This sequence is illustrated by the following diagram.2 Sign-oﬀ Demonstrate to the lab instructor that the four LEDs light up in the correct sequence.0-P1.2 2. Such a circuit/program might be used to control events and make them happen in sequence. you should make a copy of the code somewhere else for future reference. If you choose to modify the code instead of creating a copy of the project.2 LED3 P1. you could try to make the LED sequence go in both directions so that the LED appears to move back and forth. The 2N2222 Boston University ECE Page 13/15 .1 Level 2: Motor Speed Controller Activity For this level. 2. or you will need to create a new Code Composer project for level 1. then the third. Include the code from your interrupt service routine. 2. from left to right and then back to the left. and make a note of any other changes you may have made to the base code. In your notebook: 1. Modify the level 0 code to light up the ﬁrst LED.3). then the second. click the run button. or as a cool eﬀect for the eyes of evil robots bent on destroying all of humanity. 2. When each LED turns on.1 Activity Connect four LEDs to the lowest four pins of port 1 (P1.2. the others should turn oﬀ. and show that the counter increments each time the TimerA0 ISR is executed. you can sign out a DC motor from the Instrument Window in PHO105. If you don’t have one in your kit.1.
In your notebook: 1. Ttot can be anything under about half a second. For example. If the digital signal XOUT sent to the 2N2222 transistor has a regular periodicity Ttot but a variable “on time” Ton . 2. A reasonable value for Ttot might be 128 ms. not the exact value of Ttot . Boston University ECE Page 14/15 . Why is it necessary to use a transistor and a separate power supply to drive the motor? 2. DO NOT connect 12 V to the VCC of the 2231. 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). your settings might yield the results shown in the table above. (The duty cycle is sometimes expressed as a percentage of on time: D = Ton /Ttot × 100%). VDC Average DC value = (Ton/T)VDC . found in your lab kit.EK307 Microcontrollers Lab 7 transistor. the average value of the voltage applied to the motor will be (D)(VDC ). Record a circuit diagram showing your circuit..2 Questions For each speed setting.2. record the current drawn by the motor. deﬁned by D = Ton /Ttot . In such a case. and that this causes the motor to spin at four diﬀerent speeds. as illustrated below. Using the PicoScope. but for the motor provided. 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). In this case you can alter the voltage using a technique known as pulse-width modulation. you must connect the GROUND from the motor power supply to the GROUND of the 2231. 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 diﬀerent speeds (at least four) depending on the high/low status of the Port 1 inputs.3 Sign-oﬀ Demonstrate to the lab instructor that the duty cycle of the signal driving the motor matches the speciﬁcation for each of the four cases. That should be the ONLY connection between the 2231 and the 12V power supply. the “duty cycle”.2.. 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. The speed of the motor will depend on the average value of the voltage applied to it. The ration of Ton /Ttot is what matters. will be the fraction of time that the signal is high.
5. 2013 Boston University ECE Page 15/15 . and write a couple of sentences explaining each. 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. 4. it should be ignored.2 Sign-oﬀ Demonstrate your circuit for the lab instructor. Write a paragraph describing how your code works and include the pseudocode for your program. and tape or staple into your notebook a waveform from the oscilloscope showing the duty cycle of the driving waveform for each case.1 Level 3: Digital Electronic Synthesizer Activity Design and build a small digital electronic synthesizer. Last revised: April 5.EK307 Microcontrollers Lab 7 2. Write answers to the questions for this level. 2. 3. In your notebook: 1. 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. The output should only produce sound when a key is depressed. annotate.3. 4. Print. Include the code from your interrupt service routine. or 7 are pressed. 6. and make a note of any other changes you may have made to the base code. 2. Include the code from your interrupt service routine. and tape or staple into your notebook four waveforms from the oscilloscope showing the waveforms generated when buttons 0. Print. If a second key is depressed while a sound is already being produced. 2.3 2. Write a paragraph describing how your code works and include the pseudocode for your program.3. 2. The synthesizer should have eight input “keys” (push button momentary switches or wires from a pin to power or ground) and a single output. annotate. and make a note of any other changes you may have made to the base code. 3. Think of 2 examples of how microcontrollers in general could be used in your branch of engineering.