Designing with Microcontrollers

parkagendaElectronics - Devices

Nov 2, 2013 (3 years and 7 months ago)

180 views

1


Designing with Micro
controlle
rs

Programming the MSP430


Purpose:


In this activity, students
will use a MSP
430
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 micro
controllers

to simplify

designs,
reduce expenses, and ease modifications.


Objectives:




Learn

about
appropriate
applications
for
microcontroller
s
.



Understand the purpose of
a
compiler.



Compile and
deploy co
de to the MSP430
microcontroller.



Construct breadboard circuits with the MSP430 micro
controller
.



Learn to interface

code with common input and output devices.



Learn to use 7 segment displays.


Equipment:


o

MSP430 LaunchPad Development Tool

o

IAR Kick
start for MSP430

(or Code Composer Studio)

o

DC Power supply

o

Breadboard

o

Wire kit

o

Resistors
:

1k
Ω (2), 47kΩ (2)
, 220Ω resistor
IC

o

Capacitors:
100n
F, 1nF

o

Two different color LEDs

o

Push button (momentary)

o

7 segment display

(Kingbright SA56
-
11EWA)

o

Flat screwdriver

2


Background

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


Micro
controllers

are
programmable digital devices
capable

of performing tasks, processing data,
perform
ing

calculations, and much more.

M
icro
controllers drive

many digital systems
, often called
embedded systems
.

These would range from devices like
pocket calculator
s, cellphones, and MP3
players.

The MSP430

Texas Instruments produces a line of micro
controllers

known as the MSP430

Mixed Signal Processor
.
The MS
P430 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 wil
l 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
int
erfacing with a PC.



Figure
1
: MSP430 Launchpad development board [1].


3


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.


12.


Click
Project


Options…
in the menu bar.


4


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 me
nu 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 Applicat
ion
.


18.


Now that the application is loaded, the circuit is ready to test. Press the left
button

label
ed

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 t
he 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
s
even 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.


5



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 cont
rol 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
-
11E
WA. This device is a common anode typ
e display.
That is, a logic LOW will turn on a segment.

6



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


Figure
5
: Internal layout for the Kingbright SA56
-
11EWA

[
4
]
.




7


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

a
nd 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 MSP4
30 chip.

Set up the DC power supply with 3.3V


6.

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



8


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


9


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 variables

int state = 0;


// Function prototypes

int 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 o
ff



CCTL0 = CCIE; // Turn on timer interrupt


CCR0 = 65535; // 1 blink/sec to start


TACTL = TASSEL_2 | MC_1 | ID_3; // SMCLK, 16
-
bit, /8, upmode



10



_BIS_SR(GIE);

// Turn on glob
al 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 : down

int get_btn(void)

{


int btn = (P1IN & BIT3) >> 3;




return btn;

}


// Waits for button press and release

void wait_btn(void)

{



while (get_btn()) // Wait for button to be pressed


{


}




while (!get_btn()) // Wait for release


{


}

}



11


Blink

Circuit Schematic



Parts




1k
Ω

resistor (2)



47k
Ω

resistor (2)



2 different colored LEDs



1nF capacitor



100nF
capacitor



Push button switch



MSP430G2231 or G2211



12


Appendix B


Counter Circuit

Code for Main.c

#include <msp430g2231.h>


/*


Richard Dennen


Worcester Polytechnic Institute


Dep
artment 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 variables

volatile 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'};


const char segments[] = {0x7e,0x30,0x6d,0x79,0x33,


0x5b,0x5f,0x70,0x7f,0x73,0x00,


0x77,0x1f,0x4e,0x3d,0x4f,0x47,

13



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 prototypes

void 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 nex
t 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); // Conver
t to 7
-
seg format


display(sgmt); // display it




break;


}


}




clk ^= 1;

14


}


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
-
s
egment 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


P1OU
T = (P1OUT&0x80) | segs; // Set the segment values, keep MSB

}


// Gets btn state

// btn = 0 : up

// btn = 1 : down

int get_btn(void)

{

15



int btn = (P1IN & BIT7) >> 7;




return btn;

}


// Waits for button press and release

void 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 display

char char_to_segments(char c)

{


int i;




for (i = 0; i < si
zeof(chars)/sizeof(char); i++)


{


if (c == chars[i])


{


return segments[i];


}


}




return (char)0x7F; // Default failure

}



16


Counter Circuit Schematic





SA56
-
11EWA display





4116R
-
001
-
221 resistor IC

(220

)
*



MSP430G2231

or G2211



Push button



1k
Ω

(㈩Ⱐ㐷4
Ω

(㈩ resist潲s



1nF, 100nF capacitor


* The resistor IC is equivalent to using eight parallel 220


resistors.



17


Appendix C


7 Segment Display Codes

(xabcdefg)


1111110

0x7E


0110000

0x30


1101101

0x6D


1111001

0x79


0110011

0x33


1011011

0x5B


1011111

0x5F


1110000

0x70


1111111

0x7F


1110011

0x73


1110111

0x77


0011111

0x1F


1001111

0x4F


0111101

0x3D


1001111

0x4F


1000111

0x47


1111011

0x79


0110111

0x37


0000110

0x06


0111000

0x38


0001110

0x0E


0010101

0x15


0011101

0x1D


1100111

0x67


0000101

0x05


0001111

0x0F


0111110

0x3E


0111010

0x3A