ECE 447 Fall 2009

Download Report

Transcript ECE 447 Fall 2009

ECE 447 Fall 2009
Lecture 2: TI MSP430
Software Development
C and MSP430 Assembly
ECE447: The C language
• Language of choice today for small
microcontrollers.
• Built-in and user-defined types, data structures,
and flexible control
• More concise and reliable to write and debug code
than assembly language.
• Sufficiently low level language that efficient code
can be generated by compilers’
• Aided by modern architectures like the MSP430
designed with compliers in mind verses hand
crafted assembly code.
ECE447: MSP430 Assembly language
• Specific to the platform (i.e.: The MSP430).
• No data types, structures, or explicit execution
control (for, while, if, switch)
• Define locations for variables and program
location
• No complex instructions, such a long integer
math, floating point.
• Close coupling to the hardware (peripherals and
input/output ports)
• Teaches a more in depth appreciation for the
device architecture.
ECE447: MSP430 Memory Map
Memory Address
End:
0FFFFh
Start:
0FFE0h
End:
0FFDFh
Description
Interrupt Vector Table
Flash/ROM
Start *:
End *:
Start:
End:
Start:
End *:
Start:
End:
Start:
End:
Start:
End:
Start:
0F800h
01100h
010FFh
0107Fh
01000h
0FFFh
0C00h
09FFh
027Fh
0200h
01FFh
0100h
00FFh
0010h
000Fh
0000h
Access
Word/Byte
Word/Byte
Information Memory
(Flash devices only)
Boot Memory
(Flash devices only)
Word/Byte
RAM
Word/Byte
Word/Byte
16-bit Peripheral modules
Word
8-bit Peripheral modules
Byte
Special Function Registers
Byte
• All memory including RAM, Flash, information memory, Special
Function Registers (SFRs), and peripheral registers.
• Starred (*) start and end addresses vary based on the particular
MSP430 variant
ECE 447: Writing data to / Reading data
from a particular memory location
Method 1 (first approximation)
C language
#define P1IFG_ptr ((unsigned char *) 0x0023)
unsigned char result;
*P1IFG_ptr = 0xAB;
/*write to the pointed location*/
result = *P1IFG_ptr;
/*read from the pointed location */
MSP430 Assembly
P1IFG EQU
RSEG
result DS
RSEG
mov.b
mov.b
0x0023
DATA16_N
1
CODE
#0xAB, &P1IFG
&P1IFG, &result
; defines a constant P1IFG
; creates a space for a variable
; writes to P1IFG
; reads P1IFG into result
ECE 447: Writing data to / Reading data
from a particular memory location
C language
Method 1:
#define P1IFG_ptr = ((volatile unsigned char *) 0x0023)
unsigned char result;
*P1IFG_ptr = 0xAB;
/*write to the pointed location*/
result = *P1IFG_ptr;
/*read from the pointed location */
MSP430 Assembly
P1IFG EQU 0x0023
; defines a constant P1IFG
RSEG DATA16_N
result DS
1
; creates a space for a variable
RSEG CODE
mov.b #0xAB, &P1IFG ; writes to P1IFG
mov.b &P1IFG, &result ; reads P1IFG into result
no volatile like in C, as you explicitly control the “mov” into and out
of specific registers/memory locations.
ECE 447: Writing data to / Reading data
from a particular memory location
Method 2 (not allowed in Experiment 1):
C language
#include <io430x20x3.h>
unsigned char result;
P1IFG = 0xAB;
/*write to the pointed location*/
result = P1IFG;
/* read from the pointed location */
MSP430 Assembly
#include <msp430x20x3.h>
RSEG DATA16_N
result DS
1
RSEG CODE
mov.b #0xAB, &P1IFG
mov.b &P1IFG, &result
; creates a space for a variable
; writes to P1IFG
; reads P1IFG into result
ECE 447: Setting and Clearing a bit
Setting a bit
C language:
Clearing a bit
C language:
#define BIT7 0x80
#define BIT7 0x80
unsigned char result;
unsigned char result;
result |= BIT3;
result &= ~BIT7;
MSP430 Assembly:
BIT7 EQU 0x80
RSEG
DATA16_N
result DS
1
RSEG
CODE
bis.b #BIT7, &result
MSP430 Assembly:
BIT7 EQU 0x80
RSEG
DATA16_N
result DS
1
RSEG
CODE
bic.b #BIT7, &result
ECE 447: Toggling a bit
C language:
#define BIT7 0x80
unsigned char result;
result ^= BIT7;
MSP430 Assembly:
BIT7 EQU 0x80
RSEG DATA16_N
result DS
1
RSEG CODE
xor.b #BIT7, &result
ECE 447: Registers
Registers - a small amount of storage available on
the CPU whose contents can be accessed more
quickly than storage available elsewhere, e.g. in RAM.
Registers can be accessed directly only using
assembly language.
Compilers are capable of turning a program written
in a high-level language (such as C) using variables into an
assembly language program taking advantage of CPU registers.
Still a skillful programmer can write a an assembly language
program that results in a faster and more compact code
than the code generated by a compiler.
ECE 447: Registers of MSP430
Central Processing Unit (CPU) of MSP430 includes sixteen
16-bit registers:
• 4 registers (R0, R1, R2 and R3) have dedicated functions;
• 12 registers are working registers (R4 to R15) for general-use.
In this lecture, we will focus on the use of general-purpose
(working) registers.
ECE 447: Handling Byte Data
 R4-R15 – General-purpose registers:
 Handling byte data (8 bits) using the suffix .B:
ECE 447: Handling Word Data
 R4-R15 – General-purpose registers:
 Handling word data (16 bits) using the suffix .W:
ECE 447: Testing a bit
C language
#define BIT7 0x80
unsigned char result;
if (result & BIT7)
….. ;
else
….. ;
MSP430 Assembly
BIT7 EQU 0x80
RSEG DATA16_N
result DS
1
RSEG CODE
mov.b &result, R4
and.b #BIT7, R4
jz
bitclr
….
jmp bitdone
bitclr:
….
bitdone:
….
ECE 447: Waiting for a bit to set
C language
#define BIT7 0x80
#define P1IFG (* (volatile unsigned char *) 0x0023);
while (!(P1IN & BIT7)) ;
do nothing until
expression is true
MSP430 Assembly
BIT7 EQU 0x80
P1IFG EQU 0x0023
bitcheckloop:
mov.b &P1IFG, R5
and.b #BIT7, R5
jz
bitcheckloop ; keep checking until bit is set
….
ECE 447: Running program continuously
C language
while (1) {
………..
Sequence of
repeated operations
}
MSP430 Assembly
main_func:
…
…
jmp
main_func
; go back and do it again
ECE 447: Looping a specific number of
times (do…while)
C language
#define LOOPS 60
LoopCtr = LOOPS;
do {
………..
--LoopCtr;
} while(LoopCtr != 0);
MSP430 Assembly
LOOPS EQU 60
RSEG CODE
mov.w #LOOPS, R4
Loop:
….
dec.w R4
jnz Loop
Sequence of
repeated operations
ECE 447: Looping a specific number of
times (for loop)
C language
#define LOOPS 60
for( i=0; i < LOOPS; i++ )
{
………..
}
MSP430 Assembly
LOOPS EQU 60
RSEG CODE
clr.w R4
Loop:
cmp.w R4, #LOOPS
jhs
Done
….
inc.w R4
jmp Loop
Done:
Sequence of
repeated operations
; zero into R4
; compare to see if end
; done, quit the loop
; increment loop control
; loop around again
ECE447: C Class Exercise
• Write a function in C that transfers 64
bytes of data from the beginning of the
information memory of MSP430F2013 to
the block of RAM starting at address
$0200.
ECE447: Assembly Class Exercise
• Write an assembly routine that transfers
64 bytes of data from the beginning of the
information memory of MSP430F2013 to
the block of RAM starting at address
$0200.