The MSP430 has gained in popularity in recent years due to its low power and ease of use. However, the resources that enable technical professionals to fully understand and leverage this device. As a leader in providing consulting and support for using the MSP430, Argenox provides a complete tutorial series that covers both the basics of the MSP430 and microcontrollers in general, as well as in depth advanced coverage of topics.
But other than that (and the use of TB instead of TA in the timing registers), this code looks very similar to the examples from the MSP430 PWM tutorial; it initializes the LED pin, sets up the timer/interrupt, and then puts the chip to sleep:
The Texas Instruments LaunchPad is a handy tool for evaluating and learning about the MSP430 Value Line series of microcontrollers. This tutorial uses the LaunchPad with its included MSP430G2231 processor to introduce MSP430 assembly language programming. A small program is developed which reads the status of a LaunchPad push button. While the button is not pushed the LaunchPad's red LED is turned on. If the button is pushed the green LED is turned on.
Although this first program is short - only 20 lines of code - the tutorial covers a lot of ground. In addition to an overview of the basic syntax and structure of MSP430 assembly language, information is provided on how to:
The syntax presented here is based on TI's Code Composer Studio (CCS) and therefore to complete this exercise you will need to have CCS downloaded and installed on your computer. A free version of CCS is available from the TI website.
The most fundamental of the elements are the machine instruction mnemonics, as these map directly to the functions of the microcontroller's CPU. The complete MSP430 instruction set consists of 27 core instructions. There are an additional 24 emulated instructions which are translated to core instructions by the assembler software. When compared to the command set of some higher level languages, these machine instructions are very basic, performing tasks such as mathematical and logical operations, writing and retrieving values from memory, and branching to different sections of code.
The example program for the tutorial appears below. As explained earlier, it is a simple program which is tied to the built-in hardware of the LaunchPad. If the button is pushed the green LED is on; if the button is not pushed the red LED is on. The code is fully commented and you will likely find it easy to follow the basic logic simply by reading the comments.
;------------------------------------------------------------------------------; Digital I/O example for the LaunchPad; Read the status of built in push button - P1.3; (Note that P1.3 is "1" when the push button is open ; and "0" when the button is closed); Red light if the button is not pushed - P1.0; Green light if the button is pushed - P1.6; Build with Code Composer Studio;------------------------------------------------------------------------------ .cdecls C,LIST,"msp430g2231.h" ; cdecls tells assembler to allow ; the c header file;------------------------------------------------------------------------------; Main Code;------------------------------------------------------------------------------ .text ; program start .global _main ; define entry point_main mov.w #0280h,SP ; initialize stack pointer mov.w #WDTPW+WDTHOLD,&WDTCTL ; stop watchdog timer bis.b #01000001b,&P1DIR ; make P1.0 and P1.6 output ; all others are inputs by defaultMainloop bit.b #00001000b,&P1IN ; read switch at P1.3 jc Off ; if P1.3 open branch to Off label On bic.b #00000001b,&P1OUT ; clear P1.0 (red off) bis.b #01000000b,&P1OUT ; set P1.6 (green on) jmp Wait ; branch to a delay routineOff bis.b #00000001b,&P1OUT ; set P1.0 (red on) bic.b #01000000b,&P1OUT ; clear P1.6 (green off) Wait mov.w #1834,R15 ; load R15 with value for delayL1 dec.w R15 ; decrement R15 jnz L1 ; if R15 is not zero jump to L1 jmp Mainloop ; jump to the Mainloop label ;------------------------------------------------------------------------------; Interrupt Vectors;------------------------------------------------------------------------------ .sect ".reset" ; MSP430 RESET Vector .short _main .end
.cdecls C,LIST,"msp430g2231.h" ; cdecls tells assembler to allow ; the c header file;------------------------------------------------------------------------------; Main Code;------------------------------------------------------------------------------ .text ; program start .global _main ; define entry point_main mov.w #0280h,SP ; initialize stack pointer mov.w #WDTPW+WDTHOLD,&WDTCTL ; stop watchdog timer
The first lines of our code repeated above are what might be called housekeeping tasks. They are necessary basic setups that will appear in one form or another in most of your programs. Unavoidably these setup tasks spill into a few concepts such as the "entry point" and the "stack pointer" that stray beyond the introductory notions intended for the tutorial. As these steps are examined these functions will be described, but most of the focus will be placed on the assembly language and syntax. When you set out to develop your own programs, you can at first simply copy these statements as a template.
Interspersed with comments, the first three statements are assembler directives. The .cdecls directive tells the assembler to allow the use of the C program header file. When you installed CCS it came with header files for all of the MSP430 variants. These files provide lots of useful information about the specific device you are programming, including the definitions for the symbols we will use to reference registers. The appropriate header file for the LaunchPad's MSP430G2231 is specified in quotation marks - msp430g2231.h. The .text directive indicates the beginning of a block of code. The .global directive is used here to declare the symbol _main as available across external modules so that when it is used in the next statement the assembler will recognize it as the entry point to the program.
bis behaves quite differently than mov. When using bis, any bit that is a "1" in the source operand will be set in the destination operand, but the remaining bits will be unaffected. In this case the entire P1DIR register would have been clear on startup, so there is no need to do anything to clear the bit for P.3 and the pin configuration is complete.
All told these manuals contain over 1000 pages of critical information about the assembler tools, the instruction set, the electrical characteristics and features of the microcontroller family. One of the goals of this tutorial has been to present the MSP430 assembly language in as direct and simple a fashion as possible and provide a quick start to the programming experience. Nonetheless, it should be clear by now that learning to program the MSP430 is no trivial matter. Numerous significant topics were only briefly introduced and countless others did not even receive a mention. Hopefully, we've whet your appetite for this platform and established a foundation from which you can begin to build your understanding and toolset.
Simply Embedded is a website dedicated to teaching and collaborating on embedded programming projects. All of the projects will be completely free and open source, as will the development tools used. The first series is intended for those who have little experience with embedded software but have a decent understanding of the C programming language. It is a great resource for hobbyists, novice programmers, CS/EE students and hardware designers alike who could benefit from a better understanding of software. I encourage questions and discussions in the comments section and will be available to provide guidance along the way. The projects will be as real-world and practical as possible. The goal is to provide you with knowledge that is applicable to other projects and platforms.
582128177f