Microcontroller Basics
Presented by IEEE of Texas
A&M
About This Workshop
Based on the MSP430G2553 microcontroller by
Texas Instruments
Powerpoint and code available at
ieeetamu.org/mcc/microbasics
Workshop Reference available at
ieeetamu.org/mcc/wsref
Footnotes refer to User Guide (UG), Datasheet,
or Workshop Reference (WSRef)
Example: UG 1.2.3, DS 4.5.6, WSRef 7.8.9
Import code projects into Code
Composer
Follow instructions in the Workshop Reference
or at ieeetamu.org/mcc/importing
Code: ieeetamu.org/mcc/microbasics
Projects to import:
Blinking LEDs 1 (Open and run blinking_leds_1.c)
Blinking LEDs 2
Polling
Interrupts
WSRef 1.2
Extremely Useful
Documents
User's Guide MSP430x2xx Family
ieeetamu.org/msp430ug
All general MSP430 information
Ex: MSP430 Architecture, Instruction Set, Registers,
clocks, timers, module types and functionality
MSP430G2x53 Datasheet
ieeetamu.org/msp430ds
Information specific to individual set of chips
Ex: List of included modules, pin-outs, memory size
4
Topics Covered
Using Code Composer Studio
The MSP430
Input and Output
Interrupts vs Polling
USING CODE COMPOSER
STUDIO
Projects
The Code Composer
Code
Files
Window
Errors/Warnings and
Console
Debugging a Program
Click Debug (bug icon)
or F11
After debug mode
starts, click Resume
(play button) or F8
Pause program using
Suspend
When program is
paused, code can be
run one line at a time
Hover over variables
to see current values
WSRef 1.3
Breakpoints
Right click next to
a line of code
and click toggle
breakpoint
Code will stop
executing when
breakpoint is hit
Step through code
or press Resume
to continue
WSRef 1.3
THE MSP430
The MSP430 Family
Ultra-low power (<100A/MHz)
Multiple Low Power Modes
16-bit, RISC-based CPU
Hardware modules (UART, SPI, I2C)
for communicating with peripherals
or PC
Interrupts and timer modules
Analog to Digital Converter (ADC)
11
MSP430G2553
CPU runs at up to 16 MHz
512 Bytes RAM (for stack/heap)
16 kB Flash Memory (for program
data)
8 channel, 10-bit ADC
Temperature Sensor
12
Registers - The hardware-software
link
All configuration is done using
registers
Controls how microcontroller behaves
In software, register names are
defined in header file
# in clu d e "m sp430g2553.h"
Written in all caps: P1OUT, WDTCTL, IFG2
13
The Header File - msp430g2553.h
Only works with one specific MSP430
model
Other MSP430s have different features
Defines register names
Defines helpful constants
Also in all caps: BIT0, WDTHOLD, UCA0RXIE
Increases readability:
Clear:
WDTCTL = WDTPW + WDTHOLD;
Confusing:
WDTCTL = 0x5A80;
14
The Watchdog Timer
Resets microcontroller in case of
malfunction
Must be continually reset before elapsing
Allows a system to recover autonomously
Useful for applications in remote and/or
dangerous locations such as space
Turn off when not needed
WDCTL = WDTPW + WDTHOLD;
UG Ch. 10
15
INPUT AND OUTPUT
Open blinking_leds_1.c in the project
Blinking LEDs 1 - Microcontroller Basics
Input and Output
Microcontrollers can set and read voltages
on its GPIO pins depending on configuration
of registers
In software, 0 = ground and 1 = Vcc (~3.3V)
The MSP430G2253 has 16 GPIOs
Port 1: P1.0 through P1.7
Port 2: P2.0 through P2.7
Pins are labeled on Launchpad
Each bit (0 through 7) in PxOUT, PxDIR, PxIN,
etc. correspond to a GPIO pin
UG Ch. 8, WSRef 2.2
17
P1DIR and P1OUT
These two registers are used to turn
the LED on and off
P1DIR sets direction of each pin on
port 1
0 = input, 1 = output
P1OUT sets what is being output on
each port 1 pin (if set to output)
0 = ground, 1 = Vcc
WSRef 2.2
18
Blinking LEDs 1
Run the code!
void m ain(void ) {
W D TCTL = W D TPW + W D TH O LD ;
P1D IR = BIT0;
w h ile (1) {
P1O U T ^ = BIT0;
__delay_cycles(250000);
}
}
19
Bitwise Operators
x means dont care (replaceable with 0
or 1)
Operatio
n
Example
Resu
Useful for
lt
OR
xxxx | 0010
xx1x
Setting bits to 1
AND
w/NOT
xxxx & ~0010 xx0x
Setting bits to 0
(clearing)
XOR
1xx0 ^ 1001
Toggling bits (0 to 1 and
1 to 0)
0xx1
Notice that the bits in the first
operand change only where there are
1s in the second operand
WSRef 3.2
20
BITx and Shorthand
Operators
Consta Value
nt
BIT0
000000
01
BIT1
000000
10
P1OUT ^= BIT0; is equivalent
to
BIT2
P1OUT = P1OUT ^ BIT0;
...
000001
00
Same for other operators: P1OUT
BIT6 |= 010000
00
BIT0;
BIT7
100000
00
is the same as P1OUT = P1OUT |
BIT0;
Action Example
P1OUT
Changing multiple
bits
Set
P1OUT |= BIT1;
xxxxxx1x
P1OUT |= BIT4 +
BIT5;
Clear
P1OUT &=
~BIT2;
xxxxx0xx
P1OUT &= ~(BIT5 +
BIT6);
Toggl
e
P1OUT ^= BIT3;
Allows individual bits to be changed
WSRef 2.3.1, 3.5
xxxx?xxx
P1OUT ^= BIT6 +
BIT7;
21
End of main() and
__delay_cycles()
Microcontroller programs should not
end
Use an infinite loop: w h ile (1) {/*code*/}
Enter a low power mode:
_BIS_SR(LPM0_bits);
__delay_cycles() delays code
execution
Length of time depends on MSP430
clock rate!
22
Ex: 1 million cycles @ 1MHz = 1 second
Blinking LEDs 1 - Exercises
to try
Make the green LED blink instead of red
Green LED is on P1.6
Make both LEDs blink at once
Red and Green should turn on and off together
Make sure both are on or off before infinite loop
Make the LEDs alternate (one on at a time)
Exactly one LED should be on at all times
Make an LED turn on and off for different
durations
On state should be longer/shorter than off
Try using |= and &= ~
23
Blinking the green LED
# in clu d e "m sp430g2553.h"
void m ain(void ) {
W D TCTL = W D TPW + W D TH O LD ;
P1D IR = B IT6; // green LED is on P1.6
w h ile (1) {
P1O U T ^ = B IT6 ;
__delay_cycles(250000);
}
}
24
Blinking both LEDs at Once
# in clu d e "m sp430g2553.h"
void m ain(void ) {
W D TCTL = W D TPW + W D TH O LD ;
P1D IR = B IT0 + B IT6 ;
P 1O U T = 0; // start both LED s off
w h ile (1) {
P1O U T ^ = B IT0 + B IT6 ;
__delay_cycles(250000);
}
}
25
Alternating Both LEDs
# in clu d e "m sp430g2553.h"
void m ain(void ) {
W D TCTL = W D TPW + W D TH O LD ;
P1D IR = B IT0 + B IT6 ;
P 1O U T = B IT0; // start only red on
w h ile (1) {
P1O U T ^ = B IT0 + B IT6 ;
__delay_cycles(250000);
}
}
26
Separate duration for on
and off
# in clu d e "m sp430g2553.h"
void m ain(void ) {
W D TCTL = W D TPW + W D TH O LD ;
P1D IR = BIT0;
w h ile (1) {
P 1O U T |= B IT0; // red LED on
__delay_cycles(250000);
P 1O U T & = ~ B IT0; // red LED off
__d elay_cycles(500000);
}
}
27
BLINKING LEDS 2
Open blinking_leds_2.c in the project
Blinking LEDs 2 Microcontroller Basics
Launchpad Setup
Download code to
MSP
Disconnect MSP!
LED leg to P1.1
Resistor leg to
P1.2
Reconnect MSP
and verify LED is
blinking
29
Pins Output Real Voltages!
P1.2 is always set to ground
P1.1 toggles between ground
and Vcc, which turns the LED
on and off
All normal voltage and current
rules apply to circuit connected
between P1.1 and P1.2
Current flows out of P1.1 and into
P1.2
P1D IR = BIT1 + BIT2;
P1O U T = 0;
w h ile (1) {
P1O U T ^ = BIT1;
__delay_cycles(250000);
}
30
Exercise to try
Without changing the circuit, make
the LED blink by toggling P1.2
31
Make LED blink by toggling
P1.2
void m ain (void ) {
W D TC TL = W D TPW + W D TH O LD ;
P1D IR = BIT1 + BIT2;
P1O U T = B IT1;
w h ile (1)
P1O U T ^ = B IT2;
__delay_cycles(250000);
}
}
32
POLLING
Open polling.c in the project Polling Microcontroller
Basics
Launchpad setup
Disconnect the MSP!
Vcc to P1.4
Gnd to P1.5
Connecting P1.4,
red LED will blink
Connecting P1.5,
green LED will turn
on
Having both
connected causes
red LED to blink
slowly
34
Pullup/pulldown Resistors
Default voltage for unconnected
input pin
Input
pin
Prevents voltage from floating
between states
Pullup resistor pulls voltage up to Vcc
Pulldown resistor pulls voltage down
to ground
Takes input voltage when pin is
connected
Useful for switches and buttons
WSRef 4.2
Input
pin
35
Configuring pullup/down
resistors
PxREN enables pullup/down resistors on
pins
When enabled, PxOUT decides direction
0 = down, 1 = up
void m ain(void ) {
W D TCTL = W D TPW + W D TH O LD ;
P1D IR = BIT0 + BIT6;
P1O U T = 0;
P1REN |= BIT4 + BIT5;
P1O U T |= BIT5;
36
P1IN
Use P1IN to read inputs on port 1
pins
0 = ground, 1 = Vcc
WSRef 2.2
37
The Polling Loop
Constantly loops, checking conditions over and over again
Very inefficient
w h ile (1) {
if (P1IN & BIT4) { // checks if P1.4 is Vcc. P1IN & BIT4
P1O U T ^ = BIT0; // results in either 010000 or 000000
}
// w hich evaluates to true and false
if (!(P1IN & BIT5)) { // checks if P1.5 is ground
P1O U T |= BIT6;
__delay_cycles(250000);
} else {
P1O U T & = ~ BIT6;
}
__delay_cycles(250000);
}
38
INTERRUPTS
Open interrupts.c in the project Interrupts
Microcontroller Basics
Launchpad Setup
Same as polling
example
Vcc to P1.4
Gnd to P1.5
Connecting
P1.4, red LED
will toggle
Connecting
P1.5, green LED
will blink 4
times
40
Interrupts
Interrupts are handled by the
microcontroller
Only runs code when interrupt is
triggered
Interrupts pause currently running
code to run their own code
Interrupts have different priorities
More important interrupts run before
others
UG 2.2
41
Interrupt Configuration
void m ain (void ) {
W D TCTL = W D TPW + W D TH O LD ;
P1D IR = BIT0 + BIT6;
P1O U T = 0;
P1REN |= BIT4 + BIT5;
P1O U T |= BIT5;
P1IE |= BIT4 + BIT5;
P1IES = BIT5;
P1IFG & = ~ (BIT4 + BIT5);
_BIS_SR(LPM 4_bits + G IE);
}
42
Interrupt Handler
# p rag m a vector= PO RT1_VECTO R
// handles interrupts on port 1
__in terru p t void P ort_1(void ) {
if (P1IFG & BIT4) {
// handles P1.4 interrupt
P1O U T ^ = BIT0;
_delay_cycles(100000);
P1IFG & = ~ BIT4;
} else if (P1IFG & BIT5) {
// handles P1.5 interrupt
in t i;
for (i= 0; i< 4; i+ + ) {
P1O U T |= BIT6;
_delay_cycles(100000);
P1O U T & = ~ BIT6;
_delay_cycles(100000);
}
P1IFG & = ~ BIT5;
}
}
43