Unit 02
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.
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:
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.