24
Designing with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use a MSP430 microcontroller to design simple circuits like those that could be designed with discrete logic components. The intent is to show students the value of using microcontrollers to simplify designs, reduce expenses, and ease modifications. Objectives: Learn about appropriate applications for microcontrollers. Understand the purpose of a compiler. Compile and deploy code to the MSP430 microcontroller. Construct breadboard circuits with the MSP430 microcontroller. Learn to interface code with common input and output devices. Learn to use 7 segment displays. Equipment: 1

Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

Embed Size (px)

Citation preview

Page 1: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

Designing with MicrocontrollersProgramming the MSP430

Purpose:

In this activity, students will use a MSP430 microcontroller to design simple circuits like those that could be designed with discrete logic components. The intent is to show students the value of using microcontrollers to simplify designs, reduce expenses, and ease modifications.

Objectives:

Learn about appropriate applications for microcontrollers. Understand the purpose of a compiler. Compile and deploy code to the MSP430 microcontroller. Construct breadboard circuits with the MSP430 microcontroller. Learn to interface code with common input and output devices. Learn to use 7 segment displays.

Equipment:

o MSP430 LaunchPad Development Toolo IAR Kickstart for MSP430 (or Code Composer Studio)o DC Power supplyo Breadboardo Wire kito Resistors: 1kΩ (2), 47kΩ (2), 220Ω resistor ICo Capacitors: 100nF, 1nFo Two different color LEDso Push button (momentary)o 7 segment display (Kingbright SA56-11EWA)o Flat screwdriver

1

Page 2: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

Background

Transistors and logic gates are the fundamental building blocks of digital logic systems. Computer systems contain substantial numbers of gates and their comprising transistors. Surely, when looking inside a computer case, one does not see millions of wired gates. These gates are manufactured on a single chip, or integrated circuit.

Microcontrollers are programmable digital devices capable of performing tasks, processing data, performing calculations, and much more. Microcontrollers drive many digital systems, often called embedded systems. These would range from devices like pocket calculators, cellphones, and MP3 players.

The MSP430

Texas Instruments produces a line of microcontrollers known as the MSP430—Mixed Signal Processor. The MSP430 line boasts both low price and low power consumption, making it a good choice for the embedded computer system applications. This series is taught here at Worcester Polytechnic Institute in its introductory embedded systems course.

This activity will use the MSP430 LaunchPad, a low-cost beginner’s kit to the MSP430. The LaunchPad typically costs about $5 from electronics suppliers. It includes two MSP430 processors, a development board and programmer, an external clock source, and USB cable for interfacing with a PC.

Figure 1: MSP430 Launchpad development board [1].

2

Page 3: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

1 Developing for the MSP430

1.1 Setting up IAR Kickstart

1. Get a LaunchPad kit with a development board, USB cable, and MSP430 microcontroller integrated circuit (IC) or chip.

2. First, check the pins on the MSP430 chip. Be sure they are PERPENDICULAR to the body of the IC. If not, hold the pins on each side against a flat surface (bench or table) and CAREFULLY roll the chip until the pins are perpendicular. If you have trouble, ask for help.

NOTE: New chips typically have the pins flared out at an angle to provide a spring retention force when picked up by automatic insertion equipment.

3. Carefully insert the chip into the socket. The chip should push in without resistance. If there is, please ask for help.

NOTE: Forcing the chip into the socket can result in damage to the chip and the board.

4. Connect the development board to the PC with the included USB cable.

5. Create a new folder on the PC called Blink.

6. Open IAR Kickstart.

7. Click File – Workspace in the menu bar. Then click Project – Create New Project.

8. A dialog called Create New Project will appear. Click the + beside C and click main. Click OK to continue.

9. In the Save As dialog, browser to the Blink folder and save the file as Blink.ewp.

10. A tab will open entitled main.c. This contains the program code for this project. Delete the default code in the window and type in the code in Appendix A.

11. Click File – Save All in the menu bar or the button on the toolbar. When prompted with Save Workspace As, save as Blink.eww in the Blink folder.

3

Page 4: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

12. Click Project – Options… in the menu bar.

13. Find Category on the left hand side. Selected General Options. Under the Target tab, change Device to MSP430G2231 or MSP430G2211, depending on the chip being used.

14. Under Category, now select Debugger. Change the Driver field to FET Debugger.

15. Copy the code in Appendix A in the file main.c.

16. Click Project – Make in the menu bar or press F7. This will compile the project into binary code for use by the MSP430 processor.

17. When the code is done compiling, the bottom left corner of the window will say Ready. When it does, click Project – Download – Download Active Application.

18. Now that the application is loaded, the circuit is ready to test. Press the left button labeled S2 (P1.3). Observe the operation of the program.

1.2 Breadboarding the MSP430

1. Get a small, flathead screwdriver. Slide the screwdriver under the MSP430 chip and carefully lift up the chip. If the chip does not lift easily, please ask for help.

2. Build the circuit in Appendix A using the programmed MSP430 chip. Set the DC supply to 3.3V.

3. Press the push button on the board. Does the circuit work the same as when on the development board?

2 Interfacing with the 7 Segment Display

The means by which the user can enter and receive data from a system can greatly determine its effectiveness. A smoke detector must provide a warning to the occupants of building and thus has a flashing light and loud alarm. A TV has an array of thousands of pixels, each of which can take on one of millions of colors.

A common output device is the seven segment display. As its name suggests, the device is comprised of seven LED (light emitting diode) segments. The segments can used to form the ten Arabic numerals as well as many other characters, making a number or text display.

4

Page 5: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

Figure 2: Kingbright SA56-11EWA 7-segment display [2].

The LEDs inside share one common point: either cathode or anode. These are referred to as common cathode and common anode, respectively.

Figure 3: Common anode and common cathode [3].

The outputs of a digital circuit can control the voltages on the LEDs, turning them on or off. For common anode, a logic LOW will turn on the segment. Common cathode segments are turned on with a logic HIGH.

This experiment will use the Kingbright SA56-11EWA. This device is a common anode type display. That is, a logic LOW will turn on a segment.

5

Page 6: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

Figure 4: Pin layout for Kingbright SA56-11EWA display.

Figure 5: Internal layout for the Kingbright SA56-11EWA [4].

6

Page 7: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

1. Remove the MSP430 chip from the breadboard and return it to the development. Remember, the chip should push in easily. If it does not, please ask for help.

2. Create a new project called Counter like in Section 1.

3. In main.c, enter the code in Appendix B and compile. Download the application to the chip.

4. Unplug the USB cable (from either end). Carefully remove the MSP430 chip from the socket with a flathead screwdriver. If you have trouble, please ask for help.

5. Build the circuit in Appendix B with MSP430 chip. Set up the DC power supply with 3.3V

6. Observe the operation of the circuit. How does it work?

7

Page 8: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

References

[1] http://www.newark.com/jsp/search/productdetail.jsp?SKU=77R3863&CMP=AFC-GB100000001

[2] http://media.digikey.com/Photos/Kingbright%20Photos/SA56-11EWA.jpg

[3] http://www.maxim-ic.com/app-notes/index.mvp/id/1883

[4] http://pdf1.alldatasheet.com/datasheet-pdf/view/233186/KINGBRIGHT/SA56-11EWA.html

8

Page 9: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

Appendix A — Blink Circuit

Code for Main.c

#include <msp430g2231.h>

/* fSMCLK = 1.048576 MHz P1.0 - LED1 (red) P1.6 - LED2 (green) P1.3 - S2 (button)

Settings: Red 1 blink/sec Red 2 blink/sec Green 1 blink/sec Green 2 blink/sec*/

// Global variablesint state = 0;

// Function prototypesint get_btn(void);void wait_btn(void);

// Controls the timer#pragma vector=TIMERA0_VECTOR__interrupt void Timer_A(void) switch (state) case 0: // Red blink case 1: P1OUT ^= BIT0; // Flip red P1OUT &= ~BIT6; // Turn off green break; case 2: // Green blink case 3: P1OUT ^= BIT6; // Flip green P1OUT &= ~BIT0; // Turn off red break;

int main(void) // ********* Setup code ********** WDTCTL = WDTPW + WDTHOLD; // Turn off Watchdog timer P1SEL &= ~(BIT0 | BIT3 | BIT6); // Set P1.0,3,6 as digital I/O P1DIR = BIT0 | BIT6; // Set P1.0,6 as outputs P1OUT &= ~(BIT0 | BIT6); // Turn the LEDs off

CCTL0 = CCIE; // Turn on timer interrupt

9

Page 10: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

CCR0 = 65535; // 1 blink/sec to start TACTL = TASSEL_2 | MC_1 | ID_3; // SMCLK, 16-bit, /8, upmode _BIS_SR(GIE); // Turn on global interrupts // ********* Start main program body **************** while (1) // "Forever loop" wait_btn(); // Wait for a button press/release state = (++state) % 4; // Cycle to next state switch (state) // Change state case 0: case 2: CCR0 = 65535; break; case 1: case 3: CCR0 = 32768; break; // loop back return 0;

// Gets btn state// btn = 0 : up// btn = 1 : downint get_btn(void) int btn = (P1IN & BIT3) >> 3; return btn;

// Waits for button press and releasevoid wait_btn(void) while (get_btn()) // Wait for button to be pressed while (!get_btn()) // Wait for release

10

Page 11: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

Blink Circuit Schematic

Parts

1kΩ resistor (2) 47kΩ resistor (2) 2 different colored LEDs 1nF capacitor 100nF capacitor Push button switch MSP430G2231 or G2211

11

Page 12: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

Appendix B — Counter Circuit

Code for Main.c

#include <msp430g2231.h>

/* Richard Dennen Worcester Polytechnic Institute Department of Electrical and Computer Engineering Frontiers Program*/

// I/O : ports P1.0-P1.6 -> 7-segment display// P1.0 -> g// P1.6 -> a//// P1.7 -> Push button

/* -----a----- | | f b | | -----g----- | | e c | | -----d-----*/

// Global constants#define SEG_A 0x40#define SEG_B 0x20#define SEG_C 0x10#define SEG_D 0x08#define SEG_E 0x04#define SEG_F 0x02#define SEG_G 0x01

#define MODE_UP 0#define MODE_DOWN 1#define MODE_PATTERN 2#define MODE_MESSAGE 3

// Global variablesvolatile char state = 0;volatile char count = 0;char clk = 0;

const char chars[] = '0','1','2','3','4', '5','6','7','8','9',' ', 'a','b','c','d','e','f', 'g','h','i','j','l','n', 'o','p','r','t','u','y';

12

Page 13: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

const char segments[] = 0x7e,0x30,0x6d,0x79,0x33, 0x5b,0x5f,0x70,0x7f,0x73,0x00, 0x77,0x1f,0x4e,0x3d,0x4f,0x47, 0x79,0x37,0x06,0x38,0x0e,0x15, 0x1d,0x67,0x05,0x0f,0x3e,0x3a;

const char pattern[] = SEG_A,SEG_B,SEG_G,SEG_E,SEG_D,SEG_C,SEG_G,SEG_F;

const char string[] = "ece frontier5 ";

// Function prototypesvoid display_char(char);void display(int);int get_btn(void);void wait_btn(void);char char_to_segments(char);

// Interrupt service routine#pragma vector=TIMERA0_VECTOR__interrupt void Timer_A(void) char c; int sgmt;

if (clk) // Divide clock by 2 switch (state) case MODE_UP: // Counter up count = (count + 1) % 10; // Count to next number display(segments[count]); break; case MODE_DOWN: if ( count == 0 ) // Loop back around if 0 count = 9;

else // Otherwise, keep counting count--;

display(segments[count]); break; case MODE_PATTERN: count = (count + 1) % (sizeof(pattern)/sizeof(char)); display(pattern[count]); break; case MODE_MESSAGE: count++; c = string[count]; if ( !c ) // Check for null terminator count = 0; // Go back to the beginning

c = string[0]; // Grab that segment code sgmt = char_to_segments(c); // Convert to 7-seg format display(sgmt); // display it break;

13

Page 14: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

clk ^= 1;

int main(void) WDTCTL = WDTPW + WDTHOLD; // Stop WDT state = 0; // Make sure these come up in there known state count = 0; clk = 0; P1SEL = 0; // Set all ports for digital I/O P1DIR = 0x7F; // Set P1.0-P1.6 as output, P1.7 as input P1OUT = 0x01; // Zero CCTL0 = CCIE; // Enable timer interrupts CCR0 = 0xFFFF; // Frequency: 2 Hz (later divided by 2) TACTL = TASSEL_2 | MC_1 | ID_3; // SMCLK, /8, up mode

_BIS_SR(GIE); // Enable global interrupts // Main body of the program while (1) wait_btn(); // Wait for a button press state = (state + 1) % 4; // Cycle to next state switch (state) case MODE_UP: CCR0 = 0xFFFF; // 1 Hz (with clock division)

break; case MODE_PATTERN: CCR0 = 8192; // Change frequency to 4 Hz break; case MODE_MESSAGE: CCR0 = 43690; // To about 1.25 Hz count = 0xff; // Make sure it starts on first character break; default: break;

return 0;

// Sets the pins of the MSP430 to drive the 7-segment display// NOTE: The segments are treated as ACTIVE-HIGH in software and converted// to active low for this function to interface with the hardware.void display(int segs) segs = (~segs) & 0x7F; // Invert and clear MSB P1OUT = (P1OUT&0x80) | segs; // Set the segment values, keep MSB

// Gets btn state// btn = 0 : up

14

Page 15: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

// btn = 1 : downint get_btn(void) int btn = (P1IN & BIT7) >> 7; return btn;

// Waits for button press and releasevoid wait_btn(void) while (get_btn()) // Wait for button to be pressed while (!get_btn()) // Wait for release

// Converts the character into a 7-segment displaychar char_to_segments(char c) int i; for (i = 0; i < sizeof(chars)/sizeof(char); i++) if (c == chars[i]) return segments[i]; return (char)0x7F; // Default failure

15

Page 16: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

Counter Circuit Schematic

SA56-11EWA display 4116R-001-221 resistor IC (220Ω)* MSP430G2231 or G2211 Push button 1kΩ (2), 47kΩ (2) resistors

1nF, 100nF capacitor

* The resistor IC is equivalent to using eight parallel 220Ω resistors.

16

Page 17: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

17

Page 18: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

Appendix C — 7 Segment Display Codes (xabcdefg)

11111100x7E

01100000x30

11011010x6D

11110010x79

01100110x33

10110110x5B

10111110x5F

11100000x70

11111110x7F

11100110x73

11101110x77

00111110x1F

10011110x4F

01111010x3D

10011110x4F

10001110x47

11110110x79

01101110x37

00001100x06

01110000x38

00011100x0E

00101010x15

00111010x1D

11001110x67

00001010x05

0001111 0111110 0111010

18

Page 19: Backgroundusers.wpi.edu/~sjbitar/frontiers/Frontiers 2011 MSP430... · Web viewDesigning with Microcontrollers Programming the MSP430 Purpose: In this activity, students will use

0x0F 0x3E 0x3A

19