Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
88 views

03 - MSP430 Programming PDF

The document discusses programming the MSP430 microcontroller. It focuses on power efficient applications using the MSP430's low power features like multiple operating modes that allow it to wake quickly, compute quickly and sleep deeply to save power. Specific topics covered include MSP430 products and architecture, power efficient applications and power modes, clock management, timers, and coding tips to optimize power usage.

Uploaded by

Chauhan Sakshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

03 - MSP430 Programming PDF

The document discusses programming the MSP430 microcontroller. It focuses on power efficient applications using the MSP430's low power features like multiple operating modes that allow it to wake quickly, compute quickly and sleep deeply to save power. Specific topics covered include MSP430 products and architecture, power efficient applications and power modes, clock management, timers, and coding tips to optimize power usage.

Uploaded by

Chauhan Sakshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

MSP430 Programming

Introduction
If you’ve programmed most any mainstream microcontroller, this module shouldn’t be too much
of a surprise. What may surprise you is just how little power the MSP430 can draw when the
programmer makes some informed decisions. Most microcontrollers operate in a real-time
environment and respond to either interrupts or timers. The MSP430 is ideally suited to just this
programming model. It can wake quickly, compute quickly and sleep deeply to save power and
has a wide selection of highly capable and low power peripherals.

Learning Objectives
• MSP430 Products

• Power Efficient Applications

• Operating Modes

• Clock Management

• Peripherals

• Timer

• Coding Tips

Low Power RF Solutions - MSP430 Programming 3-1


Module Topics

*** always wear sunscreen ***

3-2 Low Power RF Solutions - MSP430 Programming


Module Topics

Module Topics
MSP430 Programming.............................................................................................................................. 3-1

Module Topics......................................................................................................................................... 3-3


MSP430 Products and Architecture........................................................................................................ 3-5
Power Efficient Applications and Power Modes..................................................................................... 3-6
Clock Management ................................................................................................................................. 3-7
Hints and Tips......................................................................................................................................... 3-8
Timers ....................................................................................................................................................3-11
Summary ................................................................................................................................................3-13
Lab3 – MSP430 Programming ..............................................................................................................3-15
Description: .......................................................................................................................................3-15
Hardware list: ....................................................................................................................................3-16
Software list:......................................................................................................................................3-16
Procedure...........................................................................................................................................3-17

Low Power RF Solutions - MSP430 Programming 3-3


Module Topics

*** I’ve seen an MSP430 run on a couple of grapes ***

3-4 Low Power RF Solutions - MSP430 Programming


MSP430 Products and Architecture

MSP430 Products and Architecture


MSP430 Products
Device
Production
5xx-Next Gen
• 25 MIPS
Sampling • 32-256 KB F5xx
• USB-RF F563x
Development USB
F261x
Future F241x F5xx RF
F23x-F24x F543x
2xx-Catalog
Performance

F23x0
• 16 MIPS
F22xx
• 1-120KB
F21x1 • 500nA Stand By
F21x2 F15x-F16x
F20xx
F13x-F14x F471x7 CG461x
F12xx
1xx-Catalog F/C11xx F43x FG461xx
• 8 MIPS F44x
• 1-60KB F47x4
Fx42x
Fx42x0
F/C41x F = Flash
C = Custom ROM

Integration

Architecture …
2

Ultra-Low-Power Architecture

Multiple operating modes


Zero-power
ƒ 0.1uA power down
ƒ 0.7uA standby mode
brown-out/reset
ƒ 250uA / 1MIPS circuit (BOR)

50nA pin leakage

Modern CPU
Minimum cycles per task

Intelligent peripherals
Instant-on stable high-speed clock

Applications …
3

Low Power RF Solutions - MSP430 Programming 3-5


Power Efficient Applications and Power Modes

Power Efficient Applications and Power Modes


Design Power-Efficient Applications
‹ Power-efficient MSP430 apps:
Š Minimize the instantaneous current draw
Š Maximize the time spent in low-power modes
‹ The MSP430 is inherently low-power, but your design has a big
impact on power efficiency
‹ Proper low-power design techniques make the difference

32768 MSP430 Always-on


250uA
ACL K
lo w- pow er p eriph erals

On demand
MCL K
DCO
CPU and p eriph erals 1uA

Operating modes …
4

Operating Modes

CPU
CPUOff
Off Active
Active Off
DCO Off
DCO
DCOon
on DCOonon All
All
ACLK
ACLKonon ACLK
ACLKonon Clocks
45-65uA 300-500uA <6us ClocksOff
Off
45-65uA 300-500uA 0.1uA
0.1uA

LPM0 <1-6us LPM4


ƒ RAM/SFR
retained
LPM3
ƒ RTC function Stand-by
Stand-by
ƒ LCD driver DCO
DCOoff
off
ƒ RAM/SFR ACLK
ACLKon on
retained 1.0uA
1.0uA
Specific values vary
by device

Clock Management …
5

3-6 Low Power RF Solutions - MSP430 Programming


Clock Management

Clock Management
Clock Management: 1xx, 2xx, 4xx

LFXT1CLK ACLK
OSCOFF

12pF LF XT CPUOFF
12pF

LFXT1 Oscillator MCLK

VCC
SCG0
SCG1
DCO

Digitally Controlled DCOCLK SMCLK


Oscillator

Instant on clock …
6

Instant-On Clock

Interrupt

DCO

‹ Near instant response to events, even from sleep modes


‹ As a result, MSP430 can spend more time asleep

Operating modes …
7

Low Power RF Solutions - MSP430 Programming 3-7


Hints and Tips

Hints and Tips


Using MSP430 Operating Modes
‹ Maximize time spent in low-power modes
Š Set up interrupt handling and then go to
sleep
‹ Use ACLK for peripherals
Š Allows use of LPM3 instead of LPM0
‹ Only activate peripherals while used,
disable when finished

Effects of temperature …
8

Effects of Vcc / MCLK / Temperature


‹ Power draw increases with…
Š Vcc
Š CPU clock speed (MCLK)
Š Temperature
‹ Slowing MCLK reduces instantaneous
power, but usually increases active duty
cycle
Š Power savings nullified – best to use default
MCLK (or increase it if required for
application performance)

Unused I/Os …
9

3-8 Low Power RF Solutions - MSP430 Programming


Hints and Tips

Configuring Unused I/Os


‹ Port I/Os should be…
Š Driven as outputs
Š Be driven at Vcc/ground by an external device
Š Have a pull-up/down resistor

External devices …
10

Power Manage External Devices

TLV2760 MSP430 "1uA OPA" MSP430


SD Px.x
Sensor Sensor
ADC ADC

0.01uA = Shutdown 1uA = Quiescent


20uA = Active 1uA = Active
--------------------------- -----------------------
0.06uA = Average 1uA = Average

‹ External op amp with shutdown can be


20x lower total power

Move functions to peripherals …


11

Low Power RF Solutions - MSP430 Programming 3-9


Hints and Tips

Move Functions to Peripherals

‹ Peripherals use less current than CPU


‹ Delegating to them allows CPU to shut down,
saving system power
‹ “Intelligent” peripherals are more capable,
providing more opportunity for CPU shutoff
‹ Use DMA for repetitive data handling rather
than CPU load/store

Reduce Cycles …
12

Reduce Cycles
‹ CPU active time is a direct function of how many cycles
need to be executed
‹ Reducing cycles is key to maximizing the use of low-
power modes
‹ Many ways to do this, but an important one is interrupt-
driven coding
UART
RX
9600 baud TX

//
// Polling
Polling UART
UART Receive //
For(;;)
Receive // UART
UART Receive
Receive Interrupt
Interrupt
For(;;) #pragma
#pragma vector=UART_VECTOR
vector=UART_VECTOR
{{ __interrupt void rx (void)
while __interrupt void rx (void)
while (!(IFG2&URXIFG0));
(!(IFG2&URXIFG0)); {{
TXBUF0
TXBUF0 == RXBUF0; TXBUF0
}}
RXBUF0; TXBUF0 == RXBUF0;
RXBUF0;
}}

100% CPU Load 0.1% CPU Load

Timer_A …
13

3 - 10 Low Power RF Solutions - MSP430 Programming


Timers

Timers
Timer_A

‹ Asynchronous TACLK
ACLK
16-bit Timer
TAR
Count
Mode
16-Bit timer/counter SMCLK
INCLK Set
‹ Continuous, TAIFG
up-down,
up count modes CCR0

‹ Multiple
CCR1
capture/compare
registers
CCR2
‹ PWM outputs
CCI2A Capture
TACCR2
‹ Interrupt vector CCI2B Mode
register for fast GND
VCC Compararator 2
decoding CCI
‹ Can trigger DMA SCCI Y A Set
transfer EN
CCIFG2

‹ On all MSP430s Output


Unit2

Counting Modes …
14

Timer_A Counting Modes

Stop/Halt Continuous
Timer is halted Timer continuously counts up

0FFFFh

0h

Up Up/Down
Timer counts between 0 and CCR0 Timer counts between 0 and CCR0 and 0
0FFFFh 0FFFFh UP/DOWN Mode

CCR 0
CCR0

0h 0h

CCR – Count Compare Register


Interrupts …
15

Low Power RF Solutions - MSP430 Programming 3 - 11


Timers

Timer_A Interrupts
The Timer_A Capture/Comparison Register 0 Interrupt Flag
(TACCR0) generates a single interrupt vector:

TACCR0 CCIFG TIMERA0_VECTOR

No handler required

TACCR1, 2 and TA interrupt flags are prioritized and combined


using the Timer_A Interrupt Vector Register (TAIV) into another
interrupt vector

TACCR1 CCIFG

TACCR2 CCIFG TAIV TIMERA1_VECTOR

TAIFG

Your code must contain a handler to determine which Timer_A1


interrupt triggered
TAIV …
16

TAIV Handler Example


Source TAIV Contents
TAIV No interrupt pending 0
0 0 0 0 0 0 0 0 0 0 0 x x x x 0 TACCR1 CCIFG 02h
15 0 TACCR2 CCIFG 04h
Reserved 06h
Reserved 08h
TAIFG 0Ah
Reserved 0Ch
Reserved 0Eh

#pragma
#pragma vector
vector == TIMERA1_VECTOR
TIMERA1_VECTOR
__interrupt
__interrupt voidvoid TIMERA1_ISR(void)
TIMERA1_ISR(void)
{{
switch( __even_in_range(TAIV,10))
switch(__even_in_range(TAIV,10))
{{
case
case 22 :: //
// TACCR1
TACCR1 CCIFG
CCIFG
P1OUT
P1OUT ^=^= 0x04;
0x04; break;
break;
case 4
case 4 : : // TACCR2 CCIFG
// TACCR2 CCIFG
P1OUT
P1OUT ^=^= 0x02;
0x02; break;
break;
case
case 10
10 :: //
// TAIFG
TAIFG
P1OUT
P1OUT ^=^= 0x01;
0x01; break;
break;
}}
}}

C Coding Tips …
17

3 - 12 Low Power RF Solutions - MSP430 Programming


Timers

Summary
C Coding Tips
‹ Use local variable as much as possible. Local variables use
CPU registers where global variables use RAM.
‹ Use bit mask instead of bit fields for unsigned int and unsigned
char.
‹ Use unsigned data types where possible
‹ Use pointers to access structures and unions
‹ Use “static const” class to avoid run-time copying of
structures, unions, and arrays.
‹ Avoid modulo
‹ Count down “for” loops
‹ Get to know your C code and its disassembly

Summary …
18

Summary
‹ Proper system design is necessary for the
best low-power performance
‹ Maximize power efficiency by minimizing
program duty cycle
‹ Make good use of all power modes
‹ Reduce program cycles

Lab Time …
19

Low Power RF Solutions - MSP430 Programming 3 - 13


Timers

*** this page isn’t really blank, you know ***

3 - 14 Low Power RF Solutions - MSP430 Programming


Lab3 – MSP430 Programming

Lab3 – MSP430 Programming


Description:
Let’s take our previous, energy-inefficient lab from Lab2 and see what we can do to it to make it
draw less power.

Lab3 – MSP430 Programming

‹ Measure current
draw of application
‹ Apply low power
design techniques
to reduce current

20

Low Power RF Solutions - MSP430 Programming 3 - 15


Lab3 – MSP430 Programming

Hardware list:

9 3 eZ430-RF2500 Target Boards


9 2 Battery Modules
9 4 AAA Batteries
9 1 eZ430-RF2500 Emulator Board
9 1 USB Extender Cable
9 1 Volt-Ohm-Milliampmeter
9 1 set of Banana plug to Mini-Clip test leads

Software list:

9 IAR Embedded Workbench for MSP430 version 4.11D

(You will find shortcuts for the above application on the desktop)

3 - 16 Low Power RF Solutions - MSP430 Programming


Lab3 – MSP430 Programming

Procedure
Measure the Current

1. Baseline

Reconnect the Power jumper on the battery module (the one we used at the end of the
last lab). The LEDs on the attached target board should be flashing again.

Take your Volt-Ohm-Milliameter (VOM) and connect the black banana plug to the COM
input and the red banana plug to the VΩmA input. Remove the Power jumper from the
battery module and put it where you’ll be able to find it again.

On the VOM, select 20m under DCA. Connect the black mini-clip at the other end of the
test lead to the battery module pin nearest the batteries. Connect the red mini-clip to the
pin furthest away from the batteries. If you manage to get this backwards, the current will
merely be negative.

The eZ430-RF2500 target board doesn’t just have a MSP430 on it. When connected to the
battery, the CC2500 is also powered. When powered up, the CC2500 goes into an idle
state and draws about 1.5mA. It is possible to send the device into a sleep state in which it
would draw nanoamps, but that would require us to add most of the SimpliciTI software to
the project. In the interest of doing things simply, we’ll just subtract 1.5mA from our
measurements to get the CPU current.

My measurement was about 6.5mA – 1.5MA = 5mA. Fill in yours below.

If my memory from my 9th grade electronics class is still accurate,

Power (P) = Voltage (E) x Current (I)

The Voltage is 2.9V (measure it yourself across the two batteries), so I calculate 14.5mW.
You will be awarded demerits for writing down any values below tenths of a mW.

Measured mA – 1.5mA Calculated Power (mW)


S/W loop with LEDs
S/W loop w/o LEDs
LPM0 w/o LEDs
LPM3 w/o LEDs

Table 1

Some of this current is for the LEDs. Let’s get rid of that in the next steps.

Low Power RF Solutions - MSP430 Programming 3 - 17


Lab3 – MSP430 Programming

2. Lose those LEDs

With the LEDs blinking, a substantial amount of the current draw could be from them
alone. Let’s turn them off and see what the MCU draws by itself.

Start IAR Embedded Workbench and open the existing workspace Lab3.eww in
C:\Texas Instruments\SimpliciTI-1.0.6\Projects\Examples\
Peer applications\eZ430RF\Lab3. This is simply the project from Lab2 copied over into
a new folder. Open main.c for editing.

Comment out the three LED control statements and replace each of them with
BSP_TURN_OFF_LED2(); as shown below. This will execute exactly the same number
of instructions, but will not light the LEDs.

3. Build/Load and Measure

Build and load the program by clicking the Debug button . When the download is
complete, click the Stop Debugging button . Remove the target board, attach it to the
battery module and measure the current. I got 4.3mA – 1.5mA = 2.8mA and calculated
8.1mW. Enter your values into the table in step 1.

4. Using a Timer

Using CPU cycles to provide a delay time is not only wasteful of CPU cycles and power,
it’s the wrong way to program an MSP430. As an example of how a programmer can use
the peripherals to save power, we’ll use a timer to provide us with a delay. Every MSP430
has a Timer_A peripheral, so let’s use that one.

I didn’t write the code that follows from scratch; I stole it from slac123, which is a
downloadable set of code example for the MSP430F2274 and others. www.ti.com/msp430
is where you can find a ton of example code. Save yourself some time and check it out
before you start coding your project at home.

I dropped the important code pieces into a file named Lab3 Code.txt so you wouldn’t
have to type them in. Click the Open button on the menu, navigate to:
C:\Texas Instruments\SimpliciTI-1.0.6\Projects\Examples\Peer applications\
eZ430RF\Lab3, select Lab3 Code.txt and click Open to open it for editing.

3 - 18 Low Power RF Solutions - MSP430 Programming


Lab3 – MSP430 Programming

5. LPM0 and Cut/Paste

Add LPM0; statements before and after the LED control statements inside the while()
loop, like below:

Cut/paste the top portion of the code from Lab3 Code.txt into main.c just above the
while(1) statement. Cut/paste the middle portion from Lab3 Code.txt into main.c at
the end of the file, like below:

Want to find out more about programming MSP430 timers? Search the TI website for the
MSP430x2xx User’s Guide.

Low Power RF Solutions - MSP430 Programming 3 - 19


Lab3 – MSP430 Programming

Re-enable the LEDs

The code won’t be very interesting if we can’t see it work. Comment/un-comment the LED
control statements like below:

6. Build/Load/Run

Take a target board and carefully insert it into the emulator, then build the project. Make
sure that it builds without error, and then click the Go button. The LEDs should be
blinking quite a bit faster than before, but it will do for the purposes of this exercise.

Warning: Code Explanation Ahead

The code that we added controls Timer_A and its interrupt response. In the code right
above while():

TACCTL0 0 = CCIE; enables Timer_A to generate an interrupt


TACCR0 = 65535; loads the maximum possible value into the
16-bit count register
TACTL = TASSEL_2 +MC_2; selects MCLK as the timer clock and continuous
as the count mode
_EINT(); enables global interrupts

The code we added at the bottom of main.c is the Timer_A interrupt service routine.

#pragma vector=TIMERA0_VECTOR places the ISR correctly in memory


__interrupt void Timer_A(void) tells the compiler this is an ISR
TACCR0 += 65535; adds offset to count register
LPM0_EXIT; returns to low power mode

By the way, LPM0; drops the MSP430 into this operating mode with the DCO on.

3 - 20 Low Power RF Solutions - MSP430 Programming


Lab3 – MSP430 Programming

7. Turn off the LEDs

Click the Stop Debugging button and un-do the LED code changes we just made:

Build and load the project to the target, then click the Stop Debugging button.

8. Measure Current

Connect the target board to the battery module and measure the current. I got 2.1mA –
1.5mA = 0.6mA or 1.7mW. Write your values into the table.

9. LPM3

In the existing code, we’re running the MSP430 on the DCO at 8MHz. For this code, that’s
overkill. We can easily run it on the VLO (Very Low frequency oscillator) that is internal
to the MSP430. But if we simply change out LPM0; statements to LPM3; the code simply
won’t work; the VLO needs to be setup and the timer needs to be changed to clock on the
VLO.

Start out by changing both LPM0; statements to LPM3; .

10. Clock setup Code

Delete the four lines of code just above the while() statement and replace them with the
six lines of code at the bottom of Lab3 Code.txt .

Add #include “VLO_Library.h” as the third include file at the top of main.c.

Add VLO_Library.s43 from C:\Texas Instruments\SimpliciTI-1.0.6\Projects\


Examples\Peer applications\eZ430RF\Lab3 to the Source group in the Workspace
window.

Delete TACCR0 += 65535; from the ISR at the bottom of main.c.

Finally, add unsigned int dco_delta; right below the line defining i.

Low Power RF Solutions - MSP430 Programming 3 - 21


Lab3 – MSP430 Programming

11. Build/Load/Run

Before testing the code, swap the LED comments back so you can see the code operating.
Build/load/run. The numbers I selected for the timer should look about like what we had
before. Click the Stop Debugging button, swap the LED comments back so the LEDs stay
off and Build/Load the project. Click the Stop Debugging button.

12. Measure

Connect this target board to the battery module and measure the current. I got 1.7mA –
1.5mA = 0.2mA or 0.6mW. Write your values into the table.

13. Shut Down

Shut the VOM off, wrap the test leads around it and give it to your instructor; we won’t
need it again in this workshop. Shut down IAR Embedded Workbench, disconnect the
eZ430-RF2500 hardware and extender cable and put them aside. Make sure to get that
jumper and place it on one of the battery pins for safekeeping.

You’re done

3 - 22 Low Power RF Solutions - MSP430 Programming

You might also like