VanTay HaiNam HaTien Lab1

Embed Size (px)

Citation preview

  • 7/29/2019 VanTay HaiNam HaTien Lab1

    1/19

    Group 1

    REPORT LABORATORY 1

    Nguyn Vn TyH Hi Nam

    Don H Tin

  • 7/29/2019 VanTay HaiNam HaTien Lab1

    2/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    Introduction to the Lab Environment

    Danang University of TechnologyAdvanced Program in Digital Systems

    Ho Viet Viet, Pham Xuan Trung, Nguyen The Nghia

    1. Objectives:This lab will give the step by step procedures on creating C language and assembly

    language programs in Code Composer Studio that are executed on the MSP-

    EXP430FG4618 experimenter board.

    2. Procedure 1: Introduction to CCS in generating a C language program1. Double click on the CCS icon and start execution of code composer studio. The default

    location of where the projects are stored is given and can be changed to

    another location. Use the default location given.

    2. To create a new project, select from the File menu, New and then CCS Project(File -> New -> CCS Project).

    3. Select the desired project name and the desired version of the MSP430. For theexperimenter board its the MSP430FG4618. Also select the type of project: Empty C

    program, a Hello World C program, or an Assembly Language program. For this step,

    choose the Hello World program.

    4. Highlighting the project makes it the activedebug project. Also, clicking on the leftarrow opens all of the subdirectories and files associated with the project. Selecting

    main.c will open the C language source file.

  • 7/29/2019 VanTay HaiNam HaTien Lab1

    3/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    Create a New CCS Project

    Makes Project the ActiveDebug Project

  • 7/29/2019 VanTay HaiNam HaTien Lab1

    4/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    5. To use the printf function in C, the reserved number of bytes for the stack and the heapmust be increased from 80 to a minimum of 500 bytes. Open under the Project

    menu the Project Properties and under Basic Linker Options change the heap and

    stack memory size to 500.

    The default printf option is set to minimum which only allows strings to be

    printed. To print integer or floating point variables this option must be changed and it

    is located under MSP430 Compiler Advanced options. The options are minimum

    support, no floating variable support, or full support. Full support uses the most

    program memory while minimum uses the least program memory.

    6. To build and compile a project, select the Project menu and then the BuildProject option. CCS will then build the project and indicate if there are any errors that

    need to be corrected.Notes: When we build a project, it may contain some errors such as:

    - If you use operating systems like window vista or higher, we need to set the useraccount in Control Panel to lowest level to avoid a user error in building project.

    - If you create an assembly project, you need to change the output format inproject properties into legacy COFF - Common Object File Format, which is a

    specification of a format forexecutable, object code, and shared

    library computer filesused on Unix systems.

    http://en.wikipedia.org/wiki/File_formathttp://en.wikipedia.org/wiki/Executablehttp://en.wikipedia.org/wiki/Object_codehttp://en.wikipedia.org/wiki/Shared_librarieshttp://en.wikipedia.org/wiki/Shared_librarieshttp://en.wikipedia.org/wiki/Computer_filehttp://en.wikipedia.org/wiki/Unixhttp://en.wikipedia.org/wiki/Unixhttp://en.wikipedia.org/wiki/Computer_filehttp://en.wikipedia.org/wiki/Shared_librarieshttp://en.wikipedia.org/wiki/Shared_librarieshttp://en.wikipedia.org/wiki/Object_codehttp://en.wikipedia.org/wiki/Executablehttp://en.wikipedia.org/wiki/File_format
  • 7/29/2019 VanTay HaiNam HaTien Lab1

    5/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    7. To debug a project, the debug option must be selected under the Run menu andthe experimenter board must be connected to the computer. The Build Project

    and Debug options should be selected every time the program source code changes.

    Notes: To debug a project, we need to build the project successfully. If we debug the

    project and it occurs errors, you may need to check the building file again.

    8. To execute the program, the Resume option under the Run menu is selected.The console window should now display the Hello World message.

    9. To quit the program, the Terminate option under the Run menu is selected.10.Also available for debugging is the ability of stepping through the program one

    line at a time. F5 (Step Into) executes the program one line at a time. F6 (Step

    Over) executes the various functions and subroutines but does not trace them one line

    at a time. The F5 option traces even subroutines and functions.

    Note:

    - We need to add one more command in the Hello World Program to stopwatchdog timer. Since without the command, the program will run infinitely due

    to continuously resetting of the MSP430.

    - A watchdog timer (or computer operating properly (COP) timer) isa computerhardware or software timerthat triggers a system reset or other

    corrective action if the main program, due to some fault condition such as

    a hang, neglects to regularly service the watchdog by writing a "service pulse"

    to it.

    http://en.wikipedia.org/wiki/Computerhttp://en.wikipedia.org/wiki/Timerhttp://en.wikipedia.org/wiki/Reset_(computing)http://en.wikipedia.org/wiki/Computer_programhttp://en.wikipedia.org/wiki/Hang_(computing)http://en.wikipedia.org/wiki/Hang_(computing)http://en.wikipedia.org/wiki/Computer_programhttp://en.wikipedia.org/wiki/Reset_(computing)http://en.wikipedia.org/wiki/Timerhttp://en.wikipedia.org/wiki/Computer
  • 7/29/2019 VanTay HaiNam HaTien Lab1

    6/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    11.Add the following for(;;) to allow the program to run indefinitely. Build the project,debug the project, and then trace then program one line at a time using the Step Into

    option (F5). Note how the program continuously loops printing the Hello WorldMessage

    F5 at the Loop 1 time

    F5 at the Loop 2 times

  • 7/29/2019 VanTay HaiNam HaTien Lab1

    7/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    F5 at the Loop 3 times

    12.Next, create a new project that has an empty file. This creates an empty Cprogram template. Make sure to click on this project name in the Project Explorer

    windows so this project is the Active-Debug project.

  • 7/29/2019 VanTay HaiNam HaTien Lab1

    8/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    13.Next, types the following C program into the blank main.c file under the desired theActive-Debug project. Read the comments contained in this program, as these

    comments cover in detail the purpose of each line of code. The definition for each

    variable is defined in the included file for the MSP430FG4618 #include

    msp430fg4618.h. The notation for the MSP430 for a port is PX.Y where X isthe port number and Y is the bit. Port 2 bit 2 is the green LED and Port 2 bit 1 is

    the yellow LED. All ports also have three variables associated with the port: the

    direction control PXDIR, the input PXIN, and the output PXOUT. For a

    port bit to be an output, its value must be 1 in the direction control variable. If its azero, then the bit is an input pin.

    To set a bit to a one on a port, the program sets this bit in PXOUT to a one. To read an

    input bit on a port, the PXIN variable is used. For example, to read the status of the two

    push buttons on the experimenter board, (input buttons on port 1 bits SW1 = bit 0 and

    SW2 = bit 1), the variable P1IN is used. A value of 1 indicates that the appropriate

    switch is not pressed and a value of zero indicates the switch is pressed.

    Answers:

    After typing the C program, we get the pure-comment code:

    Execute the blinking LED program using the Resume menu option under the Run

    menu. Take note that the LED is blinking on the experimenter board. Terminate the

    program and re-run the program using the step into menu option (F5) executing one

    line at a time. Once the program is at the line that the for loop is on, highlight the

    variable i with the mouse and observe the value of i indicated. Every time the program

    executes this line the value of i is incremented.

    #include"msp430fg4618.h"

    intmain(void)

    {

    volatileunsignedint i;

    WDTCTL = WDTPW + WDTHOLD;

    P2DIR |= 0x02;

    P2OUT=0x02;

    for (;;){

    for(i=0;i

  • 7/29/2019 VanTay HaiNam HaTien Lab1

    9/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    F5 at the Loop 1 time

    F5 at the Loop 2 times

  • 7/29/2019 VanTay HaiNam HaTien Lab1

    10/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    F5 at the Loop 3 times

    Notes: When we step into (F5) at the for loop, we will observe the increment of variable

    i. In the figures, i increases from 1 (decimal) to 3 (decimal).

    3. Procedure 2 - Introduction to CCS in generating an assembly language program1. To create an assembly language program, the same steps are used as creating a C-

    language program. But for an assembly language program, an empty assembly-only-

    project is selected.

  • 7/29/2019 VanTay HaiNam HaTien Lab1

    11/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    2. Next, under the File menu, select New and then Other. Under the General option, selectthe File option, and then the Next button. Choose a name for the blank assemble

    language project. Also make sure the desired project name for the assembly language

    file is selected as the parent folder. Make sure that the file extension is *.asm so the

    CCS knows that this is an assembly language file. Finally, click the finished button.

    Create an Assembly File

    Name the Assembly File

  • 7/29/2019 VanTay HaiNam HaTien Lab1

    12/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    3. Type in the following assembly language program to make the LED lights flash. Buildthe project as done for a C-language project by selecting under the Project menu the

    Build Project option (PROJECT-> Build project). Read the comments contained in this

    program as these comments cover in detail the purpose of each line of code. The

    definitions for each variable are defined in the included file for the MSP430FG4618

    #include msp430fg4618.h. The notation for the MSP430 for a port is PX.Y where Xis the port number and Y is the bit. Port 2 bit 2 is the green LED and Port 2 bit 1 is the

    yellow LED. All ports also have three variables associated with the port: the direction

    control PXDIR, the input PXIN, and the output PXOUT. For a port bit to be an output

    its value must be 1 in the direction control variable. If its a zero, then the bit is aninput pin.

    Answers:

    After typing the C program, we get the pure-comment code:

    4. Next, debug this project using the Debug option under the Run menu. Finally, executethis project using Resume option under the RUN menu. As with the C language

    program the Step-Into (F5) and the Step-Over (F6) options can be used to execute one

    line of instructions at a time. To help in the debugging of an assembly language

    program, there are several windows available in CCS. These windows are available

    under the VIEW menu. The first is the Disassembly window. This window gives the

    assembled code listing file. It gives the assembly language instruction along with the

    machine code generated for each instruction and the location in program flash memory

    where the instructions are located.

    The next window that is commonly used in debugging an assembly language

    program is the Memory Browser window. The user must enter a memory address

    to view. For example, if 0x3100 is chosen, the memory browser window displays

    the machine code of the assembly instructions.

    The last window that is used for debugging a program is the Register window.This window allows the user to view the MSP430 registers and the various states of the

    MSP430 I/O devices. Clicking on the Core Register arrow gives the MSP430 registers.

    .cdecls C,LIST,"msp430fg4618.h"

    .text

    .globalSTART

    START mov.w #0x300,SP

    StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL

    SetupP1 bis.b #0x04,&P2DIR

    Mainloop xor.b #0x04,&P2OUT

    mov.w #0xA000,R7

    L1 dec.w R7jnz L1

    jmp Mainloop

    .sect".reset"

    .shortSTART

    .end

  • 7/29/2019 VanTay HaiNam HaTien Lab1

    13/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    Disassembly Window

    Memory Browser Window

    Register Window

  • 7/29/2019 VanTay HaiNam HaTien Lab1

    14/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    5. Modify the assembly language program to load a value of 0x0005 into register R7.Build the project and then debug this project. Next, trace through each line of the code

    using the Step Into option observing the register values as each line of code is executed.

    In particular, take note how R7 starts with a value 0x0005 and decreases in value until

    it reaches zero.

    Answers:

    After load a value of 0x0005 into register R7, we trace each line to see decrement of R7

    Current Value of R7 is 0x0005

    Decreasing by 1 into 0x0004

  • 7/29/2019 VanTay HaiNam HaTien Lab1

    15/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    Decreasing by 1 into 0x0003

    And then into 0x0002

    4. Procedure 3 - Additional Programs to be written and executed1. Modify and execute the C-language program so that both the green and yellow LEDs

    blink alternatively on and off about once every second. For one second, the green LED

    is on and the yellow LED is off. For the next second, the green LED is off and

    the yellow LED is on. Adjust the value used in the delay loop to obtain the

    correct timing. Make sure that this program is properly commented. An

    additional for loop may be needed to obtain the desired timing. Make sure to have

    the lab assistant verify the functionality of this program.

    Answers:

  • 7/29/2019 VanTay HaiNam HaTien Lab1

    16/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    Source Code:

    2. Repeat step 1, but this time modify and execute the assembly language program.An additional loop may be needed to obtain the desired timing for second on and one

    second off.

    Answers:

    Source Code:

    .cdecls C,LIST,"msp430fg4618.h"

    .text

    .globalSTART

    START mov.w #0x300,SP ; Initialize '0x1200 or

    ; 0x300 stackpointer

    ; Turn off the watchdog time so the program can run indefinitely

    StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; Stop WDT

    #include"msp430fg4618.h"

    intmain(void)

    {

    volatilelongunsignedint i, j;

    WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer so the program

    // runs indefinitely

    P2DIR |= 0x06; // Set port 2 bit 1 & bit 2 as outputs

    // 1 = output 0 = input

    // turn the green light on, the yellow light off.

    P2OUT=0x04;

    // go run the program forever

    for (;;){

    // delay before turning changing the state of the LED (about 1s)

    for(i=0;i

  • 7/29/2019 VanTay HaiNam HaTien Lab1

    17/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    ; Set port 2 bit 1 and bit 2 direction so that P2.1 and P2.2 are outputs

    SetupP1 bis.b #0x06,&P2DIR ; P2.1 and P2.2 direction = 1

    ; Set port P2.2 to a one to turn on the Green LED

    mov.w #0x04,&P2OUTMainloop xor.b #0x06,&P2OUT ; Toggle P2.1 and P2.2

    ; Move the value of 0xFFFF into register 7 so to create a delay

    mov.w #0xFFFF,R7 ; Delay with a loop is not

    ; the best way; interrupts

    ; are better given in Lab 6

    ; Decrement register 7 until its zero. Stay in this loop until

    ; register 7 is zero

    L1 dec.w R7 ; Decrement R7

    jnz L1 ; Delay over?

    ; Use more similar loops to create longer delay (about 1s)

    mov.w #0xFFFF,R7

    L2 dec.w R7 ; Decrement R7

    jnz L2 ; Delay over?

    mov.w #0xFFFF,R7

    L3 dec.w R7 ; Decrement R7

    jnz L3 ; Delay over?

    mov.w #0xFFFF,R7

    L4 dec.w R7 ; Decrement R7

    jnz L4 ; Delay over?

    ; Lets run the program forever

    jmp Mainloop ; Again

    .sect".reset"

    .shortSTART

    .end

  • 7/29/2019 VanTay HaiNam HaTien Lab1

    18/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    3. Write a C-language program that turns on the green LED when SW1 on theexperimenter board is pressed and the yellow LED when SW2 is pressed.

    Execute this program and verify that it is running correctly. Make sure that this

    program is properly commented. Make sure to have the lab assistant verify the

    functionality of this program.

    Answers:

    Source Code:

    4. Repeat step 3, but this time writes an assembly language program to turn onand off the green and yellow LEDs.

    Answers:

    Source Code:

    #include"msp430fg4618.h"

    intmain(void){// tell the compiler not to optimize the variable i, otherwise the// compiler may change how the variable is used

    volatileunsignedint i;

    WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer so the program// runs indefinitely

    P2DIR |= 0x06; // Set port 2 bit 1 and bit 2 to as outputsP1IN |= 0x03; // Set port 1 bit 0 and bit 1 to as inputs

    // go run the program foreverfor (;;){

    if(P1IN==0x01){ // Sw2 pressed = Yellow led onP2OUT=0x02;

    } elseif (P1IN==0x02){ // Sw1 pressed = Green led onP2OUT=0x04;

    } elseif (P1IN==0x00){ // Both switch pressed = Two leds onP2OUT=0x06;

    } else {P2OUT=0x00; // No switch pressed = No led on

    }

    }}

    .cdecls C,LIST,"msp430fg4618.h"

    .text

    .globalSTART

    START mov.w #0x300,SPStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; Stop WDT

  • 7/29/2019 VanTay HaiNam HaTien Lab1

    19/19

    Report Laboratory 1December 25, 2012

    EE47209ECEVan TayHai NamHa Tien

    ; Set port 2 bit 1 and bit 2 direction so that P2.1 and P2.2 are outputs

    ; Set port 1 bit 0 and bit 1 direction so that P1.0 and P1.1 are inputs

    SetupP1 bis.b #0x06,&P2DIR

    bis.b #0x03,&P1IN

    Mainloop cmp #0x00,&P1IN ; compare to check which switch is pressedjeq Both_On ; if both switches are pressed, jump Both_On

    cmp #0x01,&P1INjeq Yellow_On ; if switch 2 is pressed, jump Yellow_On

    cmp #0x02,&P1INjeq Green_On ; if switch 1 is pressed, jump Green_On

    cmp #0x03,&P1INjeq Both_Off ; if no switch is pressed, jump Both_Off

    Both_On mov.w #0x06,&P2OUT ; turn on two leds.jmp Mainloop

    Green_On mov.w #0x04,&P2OUT ; turn on Green led.jmp Mainloop

    Yellow_On mov.w #0x02,&P2OUT ; turn on Yellow led.jmp Mainloop

    Both_Off mov.w #0x00,&P2OUT ; turn off both leds.jmp Mainloop

    .sect".reset"

    .shortSTART

    .end