0% found this document useful (0 votes)
12 views41 pages

Unit 02

Uploaded by

pavantejareddy85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views41 pages

Unit 02

Uploaded by

pavantejareddy85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Unit 02

PROGRAMMING MSP430
Development Environment
A development system includes some or all of these functions. The
editor, compilers, and linker are usually combined into an integrated
development environment (IDE), which keeps track of all the files
required for a complete project—source files, headers, linker scripts,
and so on—and looks much like you would expect for a desktop
computer.
• Editor: Used to write and edit programs (usually C or assembly
code).A good editor helps you to lay out the code logically and use
colors to highlight syntax. It is very helpful to have a quick way of
locating the definition of symbols in header files, because these are
used heavily in embedded systems.
• Assembler or compiler: Produces executable code and checks for errors,
preferably providing helpful messages. There may be add-ons to provide extra
checking, such as MISRA C or “lint” for C programs. The degree of optimization
can be changed; typically, you want limited optimization or none at all during
debugging. Different dialects of C may be available with extensions for embedded
systems.

• Linker: Combines compiled files and routines from libraries and arranges them
for the correct types of memory in the MCU.

• Stand-alone simulator: Simulates the operation of the MCU on a desktop


computer without the real hardware. Many simulators model only the CPU and
memory but some also include peripherals. Simulation has the advantage that
you have complete control and view of the MCU; the disadvantage is that it is
tricky to simulate inputs and interrupts, which are an important part of most
systems.
• Embedded emulator/debugger: Allows software to run on the MCU in its
target system under the control of a debugger running on a desktop
computer, which usually has the same front end as the simulator. The
computer and MCU communicate through a special interface, JTAG for the
MSP430. This needs special emulation hardware on the MCU, which
improves all the time.
• In-circuit emulator: Specialized and expensive hardware that emulates the
operation of the MCU under the control of debugging software running on
a desktop computer. It does not use the usual version of the MCU but a
special “bond-out” chip that provides access to the internal hardware of
the emulated device. It is difficult even to connect an emulator in place of a
modern MCU, whose pins (if any) may be only 0.5 mm apart. Highly
sophisticated breakpoints and other traces can be made, but this
equipment is now largely obsolete for small microcontrollers, which
contain embedded emulation for free.
• Flash programmer: Downloads (“burns”) the program into flash
memory on the MCU. This is done automatically by the debugger in
development but a dedicated programmer is used in production.
• IAR Embedded Workbench Kickstart: The free version of IAR
Embedded Workbench for MSP430 (EW430). It is limited to 4KB of C
code and has some other restrictions. There are several commercial
versions with fewer restrictions and an evaluation version with a time
limit of 30 days. Most of the documentation and examples provided
by TI are currently written for EW430.
• Code Composer Essentials Evaluation: The free version of Code
Composer Essentials for the MSP430 (CCE), limited to 16KB of C code.
It was developed by TI itself but is less closely related to Code
Composer for its DSP chips than you might expect from the name.
Aspects of C for Embedded Systems
Core Concepts
• Low-level access: C provides direct manipulation of memory,
registers, and I/O ports, essential for interacting with hardware
components.
• Efficiency: Its compact code and efficient execution make it ideal for
resource-constrained embedded systems.
• Portability: While not as platform-independent as higher-level
languages, C code can often be adapted for different architectures
with minimal changes.
• Real-time capabilities: C's deterministic behavior and low-level
control are crucial for time-critical applications.
Specific Considerations
• Memory management: Careful attention to memory usage is vital
due to limited resources.
• Interrupt handling: Efficiently managing interrupts is essential for
responsive systems.
• I/O operations: Direct interaction with peripherals requires
understanding of hardware interfaces and timing.
• Compiler optimization: Effective use of compiler options can
significantly impact code size and performance.
• Debugging: Limited debugging tools often necessitate thorough code
testing and analysis.
The C Programming Language
• Aspects of C for Embedded Systems Programs for small embedded
systems tend not to contain a lot of complicated manipulation of
complex data objects. Instead, much code is usually devoted to the
control of peripherals through their special registers. This means that
the details of individual bits, bytes, and words are often important.
• Declarations: The const and volatile qualifications are often critical,
particularly to define special function registers. Their addresses must
be treated as constant but the contents are often volatile, so this is a
good exercise in the meaning of these key words.
• const: Means that the value should not be modified: it is constant
• volatile: Means that a variable may appear to change “spontaneously,” with no
direct action by the user’s program. The compiler must therefore not keep a copy
of the variable in a register for efficiency (like a cache). Nor can the compiler
assume that the variable remains constant when it optimizes the structure of the
program—rearranging loops, for instance. If the compiler did either of these, the
program might miss externally induced changes to the contents of the memory
associated with the variable.
• The peripheral registers associated with the input ports must obviously be
treated as volatile in an embedded system. The values in these depend on the
settings of the switches or whatever is connected to the port outside the MCU.
Clearly the compiler must not assume that these never change. Here is a simple
example, where the program waits for the value on its input port P1IN to change
from the value OldP1IN. The loop would be pointless, and would be optimized
out of existence, if P1IN were not declared volatile.
Shifts
• It is sometimes necessary to shift bits in a variable, most commonly
when handling communications. For example, serial data arrives 1 bit
at a time and needs to be assembled into bytes or words for further
processing. This would be done in hardware with a shift register and
similarly by a shift operation in software.
• Assignment operators can also be used for shifts so you will see
expressions like value <<= 1 to shift value left by one place. Right-
shifts work differently for signed and unsigned numbers.
• Communications: When you receive data bit by bit, you can use shifts to combine
those bits into meaningful bytes or words.
• Calculations: Shifts can be used as a fast way to multiply or divide a number by 2.
For example, shifting a number left by one position is the same as multiplying it
by 2.
How it works:
• Left Shift (<<): Moves all bits to the left, filling the empty space on
the right with zeros. This effectively multiplies the number by 2 for
each position shifted.
• Right Shift (>>): Moves all bits to the right. The behavior for the
leftmost bit depends on whether the number is signed or unsigned.
• Example:
• Let's say you have the number 5 (which is 00000101 in binary).
• Shifting it left once (5 << 1) gives you 10 (00001010 in binary).
• Shifting it right once (5 >> 1) gives you 2 (00000010 in binary).
Masks to Test Individual Bits
• Suppose that we want to know the value of bit 3 on the input of port 1, for
instance. This means bit 3 of the byte P1IN, abbreviated to P1IN.3, for which we
can use the standard definition BIT3 = 00001000.
• Now consider the bitwise AND operation TestP13 = P1IN & BIT3.All bits of TestP13
will be zero except for bit 3 because x&0=0for either value of the bit x.Bit3 of
TestP13 follows from x&1=x and is therefore made equal to bit 3 of P1IN. Overall,
TestP13 is zero if P1IN.3 = 0 and nonzero if P1IN.3= 0
Layout of Assembly Language
• The layout of assembly language is much more rigid than for C. A typical line has
four parts:

The C-style notation 0xA5 for hexadecimal numbers is now widely accepted by assemblers. Other
common notations include $A5, h'A5' and 0A5h. TI often use the last form, where the leading zero is necessary
because A5h could be the name of a variable. Binary numbers can similarly be written as 10100101b, which I use,
or b'00011000'.
TI recommend the following coding style guidelines for assembly language.
Again they are taken from the readme file in the code examples for the
MSP430x11x1 (slac010).
• 1. No line should exceed 80 characters.
• 2. Use macros provided in the MSP430 header file.
• 3. Labels start in column 1 and are 10 characters or fewer.
• 4. Instructions/DIRECTIVES start in column 13.
• 5. Instructions are lower case and DIRECTIVES are UPPER CASE.
• 6. Operands start in column 21.
• 7. Comments start in column 45, the first word is capitalized.
• 8. For multiline comments, additional lines are not capitalized.
Example Code:
Register Organization
The MSP430 microcontroller has a set of 16 registers, each 16 bits
wide:
1.Program Counter (PC/R0): Points to the next instruction to be
executed.
2.Stack Pointer (SP/R1): Points to the top of the stack.
3.Status Register (SR/R2): Holds flags that indicate the status of
the CPU.
4.Constant Generator Registers (CG1/R2 and CG2/R3): Used to
generate common constants.
5.General-Purpose Registers (R4 to R15): Used for data storage
and manipulation.
Addressing Modes
The MSP430 supports seven addressing modes:

1.Register Mode: Directly uses the contents of a register.

•Example: MOV R5, R6 (Move contents of R5 to R6)

2.Indexed Mode: Uses a base register and an offset.


•Example: MOV 4(R5), R6 (Move contents of memory at address [R5 + 4] to
R6)

This instruction moves the contents of the memory location at the address ( \text{R5} + 4 )
into register R6. If R5 contains the value 0x0200, the effective address will be 0x0204, and
the contents of memory location 0x0204 will be moved to R6.
3.Symbolic Mode: Similar to indexed mode but uses the program counter as the base register.
•Example: MOV LABEL, R6 (Move contents of memory at address [PC + LABEL] to R6)

This instruction moves the contents of the memory location at the address ( \text{PC} + \text{LABEL}
) into register R6. If the program counter (PC) is at address 0x0200 and LABEL is defined as 0x0010,
the effective address will be 0x0210, and the contents of memory location 0x0210 will be moved to
R6.
4.Absolute Mode: Uses a fixed memory address.
•Example: MOV &0x0200, R6 (Move contents of memory at address 0x0200 to R6)
This instruction moves the contents of the memory location at address 0x0200 into
register R6. Here, 0x0200 is the absolute address.
5.Indirect Register Mode: Uses the contents of a register as the address.
•Example: MOV @R5, R6 (Move contents of memory at address [R5] to R6)
This instruction moves the contents of the memory location at the address stored in R5
into register R6. If R5 contains the value 0x0200, the contents of memory location
0x0200 will be moved to R6.
6.Indirect Autoincrement Mode: Uses the contents of a register as the address and
then increments the register.
•Example: MOV @R5+, R6 (Move contents of memory at address [R5] to R6,
then increment R5)
This instruction moves the contents of the memory location at the address stored
in R5 into register R6. After the move, R5 is incremented. If R5 contains the
value 0x0200, the contents of memory location 0x0200 will be moved to R6,
and then R5 will be incremented to 0x0202.

7.Immediate Mode: Uses a constant value.


•Example: MOV #0x1234, R6 (Move the constant 0x1234 to R6)
This instruction moves the constant value 0x1234 directly into register
R6.
Constant Generator
The MSP430 microcontroller includes two special registers, known as constant
generators (CG1 and CG2), which help optimize the instruction set by providing
frequently used constants without needing additional memory access.
These constants are:
• CG1 (R2): Provides constants 0, 1, 2, 4, and 8.
• CG2 (R3): Provides constants -1, 0, 1, 2, and 8.
These constants can be accessed quickly, similar to general-purpose registers,
which helps in reducing the instruction cycle count and improving efficiency.
• MOV #1, R6 ; Move the constant 1 into register R6
In this example, the constant 1 is provided by the constant generator (CG1),
which is mapped to register R2. This instruction is efficient because it doesn’t
require fetching the constant from memory.
Core Instructions
Double Operand Instructions: These instructions operate on two
operands.
1.MOV: Move source to destination.
2.ADD: Add source to destination.
3.SUB: Subtract source from destination.
4.CMP: Compare source with destination.
5.DADD: Decimal add source to destination.
6.BIT: Bitwise AND source with destination.
7.BIC: Bitwise clear source from destination.
8.BIS: Bitwise set source to destination.
9.XOR: Bitwise XOR source with destination.
Single Operand Instructions:
These instructions operate on a single operand.

1.RRC: Rotate right through carry.


2.SWPB: Swap bytes.
3.RRA: Rotate right arithmetic.
4.SXT: Sign extend.
5.PUSH: Push operand onto stack.
6.CALL: Call subroutine.
7.RETI: Return from interrupt.
Jump Instructions:
These instructions control the flow of the program.
1.JNE/JNZ: Jump if not equal/zero.
2.JEQ/JZ: Jump if equal/zero.
3.JNC/JLO: Jump if no carry/lower.
4.JC/JHS: Jump if carry/higher or same.
5.JN: Jump if negative.
6.JGE: Jump if greater or equal.
7.JL: Jump if less.
8.JMP: Unconditional jump.
Emulated Instructions
The MSP430 has a Reduced Instruction Set Computing (RISC) architecture with a
core set of 27 instructions. However, the constant generator allows the assembler to
support additional emulated instructions. These emulated instructions are not directly
implemented in hardware but are constructed using the core instructions and the
constant generator. This approach provides more functionality without increasing the
complexity of the hardware.
Some examples of emulated instructions include:
• DADD (Decimal Add): Adds two BCD (Binary-Coded Decimal) numbers.
• SUBC (Subtract with Carry): Subtracts the source operand and the carry bit from
the destination operand.
• RRC (Rotate Right through Carry): Rotates the bits of the destination operand
right through the carry bit.
• CLR R6 ; Clear the contents of register R6
The CLR (clear) instruction is an emulated instruction that clears the contents of a
register or memory location. It is implemented using the MOV instruction with the
constant generator.
Emulated Instructions
These instructions are not directly implemented in hardware but are
constructed using core instructions and the constant generator:

• CLR: Clear the destination (equivalent to MOV #0, dst).


• TST: Test the destination (equivalent to CMP #0, dst).
• DEC: Decrement the destination (equivalent to SUB #1, dst).
• INC: Increment the destination (equivalent to ADD #1, dst).
• ADC: Add carry to the destination (equivalent to ADD #0, dst).
Example 1: Light an LED
Read Input from a Switch
Example 3: Flashing Light by Delay
Subroutines and Functions in Embedded Systems
• Subroutines and functions are essential programming constructs that
allow you to break down complex tasks into smaller, more
manageable units. This improves code organization, readability, and
reusability.
• Subroutines
• Definition: A subroutine is a block of code that performs a specific
task. It can be called from different parts of your program.
• Syntax:
• Functions Definition: Functions are similar to subroutines but can
return a value. Syntax:

Advantages of Subroutines and Functions:


•Modularity: Breaks down code into smaller, reusable units.
•Readability: Improves code organization and understanding.
•Maintainability: Easier to modify and debug specific parts of the code.
•Reusability: Can be used in multiple parts of the program.
Basic Clock System
In embedded systems, the clock system is essential for timing-critical operations.
It provides a reference frequency that can be used for various tasks, such as
controlling timers, generating interrupts, and synchronizing peripherals.
• Components of a Basic Clock System:
• Oscillator: Generates a periodic signal, typically a crystal oscillator or a ceramic
resonator.
• Clock Divider: Reduces the frequency of the oscillator to a suitable value for
the system.
• Clock Distribution Network: Distributes the clock signal to various components
within the system.
• Clock Configuration:
• Clock Source: Select the oscillator as the clock source.
• Clock Frequency: Set the desired clock frequency using the clock divider.
• Clock Enable: Enable the clock for specific modules or peripherals.
Interrupts
• Interrupts are hardware mechanisms that allow the microcontroller
to respond to external events or internal conditions. They can be used
to handle time-critical tasks, communicate with peripherals, and
improve system efficiency.
• Interrupt Sources:
• External Interrupts: Triggered by external events, such as button
presses or sensor signals.
• Timer Interrupts: Triggered by a timer reaching a specific value.
• UART Interrupts: Triggered by UART communication events, such as
data reception or transmission.
• Other Interrupts: Can be triggered by various other conditions, such
as ADC conversions or watchdog timer expiration.
Interrupt Handling:

• Interrupt Service Routine (ISR): A function that is executed when an


interrupt occurs.
• Interrupt Enable: Enable the desired interrupts.
• Interrupt Priority: Set the priority of interrupts to determine the
order in which they are handled.
• Interrupt Latency: The time it takes for the microcontroller to
respond to an interrupt.
Low Power Modes
• Embedded systems often need to operate on battery power, so it's
important to minimize power consumption. Low power modes allow
the microcontroller to reduce its power consumption when it's not
actively performing tasks.
• Common Low Power Modes:
• Active Mode: The microcontroller is fully operational.
• Low Power Mode (LPM): The microcontroller reduces its power
consumption by disabling certain peripherals or clock sources.
• Very Low Power Mode (VLPM): The microcontroller enters a deep
sleep state, consuming minimal power.
Choosing the Right Mode:
• Power Consumption: Consider the required power consumption level.
• Wake-up Time: Determine the acceptable wake-up time from the low power
mode.
• Interrupt Handling: Ensure that interrupts can wake the microcontroller from the
low power mode.

You might also like