Download presentation
Presentation is loading. Please wait.
Published byTrevor Eatherton Modified over 9 years ago
1
ECE 447 Fall 2009 Lecture 2: TI MSP430 Software Development C and MSP430 Assembly
2
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.
3
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.
4
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
5
ECE 447: Writing data to / Reading data from a particular memory location 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 P1IFGEQU0x0023 ; defines a constant P1IFG RSEGDATA16_N resultDS1 ; creates a space for a variable RSEGCODE mov.b #0xAB, &P1IFG ; writes to P1IFG mov.b &P1IFG, &result ; reads P1IFG into result Method 1 (first approximation)
6
ECE 447: Writing data to / Reading data from a particular memory location 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 P1IFGEQU0x0023 ; defines a constant P1IFG RSEGDATA16_N resultDS1 ; creates a space for a variable RSEGCODE 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. Method 1:
7
ECE 447: Writing data to / Reading data from a particular memory location C language #include unsigned char result; P1IFG = 0xAB; /*write to the pointed location*/ result = P1IFG; /* read from the pointed location */ MSP430 Assembly #include RSEGDATA16_N resultDS1 ; creates a space for a variable RSEGCODE mov.b #0xAB, &P1IFG ; writes to P1IFG mov.b &P1IFG, &result ; reads P1IFG into result Method 2 (not allowed in Experiment 1):
8
ECE 447: Setting and Clearing a bit Setting a bit C language: #define BIT7 0x80 unsigned char result; result |= BIT3; MSP430 Assembly: BIT7EQU0x80 RSEGDATA16_N resultDS1 RSEGCODE bis.b #BIT7, &result Clearing a bit C language: #define BIT7 0x80 unsigned char result; result &= ~BIT7; MSP430 Assembly: BIT7EQU0x80 RSEGDATA16_N resultDS1 RSEGCODE bic.b #BIT7, &result
9
ECE 447: Toggling a bit C language: #define BIT7 0x80 unsigned char result; result ^= BIT7; MSP430 Assembly: BIT7EQU0x80 RSEG DATA16_N resultDS1 RSEG CODE xor.b #BIT7, &result
10
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.
11
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.
12
ECE 447: Handling Byte Data R4-R15 – General-purpose registers: Handling byte data (8 bits) using the suffix.B :
13
ECE 447: Handling Word Data R4-R15 – General-purpose registers: Handling word data (16 bits) using the suffix.W :
14
ECE 447: Testing a bit C language #define BIT7 0x80 unsigned char result; if (result & BIT7) ….. ; else ….. ; MSP430 Assembly BIT7EQU0x80 RSEG DATA16_N resultDS1 RSEG CODE mov.b &result, R4 and.b #BIT7, R4 jzbitclr …. jmpbitdone bitclr: …. bitdone: ….
15
ECE 447: Waiting for a bit to set C language #define BIT7 0x80 #define P1IFG (* (volatile unsigned char *) 0x0023); while (!(P1IN & BIT7)) ; MSP430 Assembly BIT7EQU0x80 P1IFGEQU0x0023 bitcheckloop: mov.b &P1IFG, R5 and.b #BIT7, R5 jz bitcheckloop ; keep checking until bit is set …. do nothing until expression is true
16
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
17
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
18
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
19
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.
20
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.