Microcontroller Basics
Microcontroller Basics
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
Projects
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
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
13
UG Ch. 10
15
17
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
Setting bits to 0
(clearing)
XOR
1xx0 ^ 1001
0xx1
20
Consta Value
nt
BIT0
000000
01
BIT1
000000
10
000001
00
Action Example
P1OUT
Changing multiple
bits
Set
P1OUT |= BIT1;
xxxxxx1x
P1OUT |= BIT4 +
BIT5;
Clear
P1OUT &=
~BIT2;
xxxxx0xx
Toggl
e
P1OUT ^= BIT3;
xxxx?xxx
P1OUT ^= BIT6 +
BIT7;
21
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
Exercise to try
Without changing the circuit, make
the LED blink by toggling P1.2
31
POLLING
Open polling.c in the project Polling Microcontroller
Basics
Launchpad setup
Pullup/pulldown Resistors
Default voltage for unconnected
input pin
Input
pin
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
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
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