200 likes | 214 Views
Learn about using C and Assembly languages on MSP430, efficient code writing, handling memory locations, toggling bits, and working with general-purpose registers.
E N D
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 • 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 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
ECE 447: Writing data to / Reading data from a particular memory location Method 1: C language #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 ; creates a space for a variable RSEG CODE mov.b #0xAB, &P1IFG ; writes to P1IFG mov.b &P1IFG, &result ; reads P1IFG into result
ECE 447: Setting and Clearing a bit Setting a bit Clearing a bit C language: #define BIT7 0x80 unsigned char result; result |= BIT3; MSP430 Assembly: BIT7 EQU 0x80 RSEG DATA16_N result DS 1 RSEG CODE bis.b #BIT7, &result C language: #define BIT7 0x80 unsigned char result; result &= ~BIT7; 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 MSP430 Assembly #define BIT7 0x80 unsigned char result; if (result & BIT7) ….. ; else ….. ; 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)) ; 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 …. do nothing until expression is true
ECE 447: Running program continuously C language while (1) { ……….. } MSP430 Assembly main_func: … … jmp main_func ; go back and do it again Sequence of repeated operations
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 ; zero into R4 Loop: cmp.w R4, #LOOPS ; compare to see if end jhs Done ; done, quit the loop …. inc.w R4 ; increment loop control jmp Loop ; loop around again Done: Sequence of repeated operations
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.