Sie sind auf Seite 1von 44

Da Nang University of Science and Technology DaNang University of Science and Technology Advanced Program in Digital Systems

Ho Viet Viet, Pham Xuan Trung, Nguyen The Nghia

EE472 LAB 5 REPORT


Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

March 3, 2014

10ECE Group 03: L Quang Ha Trng Hong Lnh V Quang Tuyn

OBJECTIVES:
To develop a C language and an assembly language program that adds, subtracts and multiplies two hexadecimal digits. The resulting answer is then displayed on the LCD display of the MSP430FG4618 experimenter board.

PROCEDURE:
Part 1. Generating Basic input with C language programs
1. Write a C language program that input from the keyboard two hexadecimal numbers via hyperterminal. Next these two numbers are to be displayed in the hyperterminal window and on the right most two digits of the LCD display on the experimenter board. To convert an ASCII 0- 9 to a four bit number, 0x30 is subtracted from the ASCII number. To convert the ASCII capital letters A-F to a four bit number, 0x37 is subtracted from these capital letters. To help write this program, please use as reference the C language programs written for labs #3 and #4.

Source code

2. Write a C language program that inputs two ASCII hexadecimal digits followed by the + symbol to indicate addition. Next, the user enters a second set of two hexadecimal digits that are to be added to the first two hexadecimal digits. The addition result is shown on both the hyperterminal display as well as the LCD display. Three digits are required for this resulting addition. For example, what should be displayed is: FF + 02 = 101 (0xFF + 0x01 = 0x101) Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

This program should run indefinitely and start on a new line each time waiting for user input.

Source Code 3. Write a C language program that inputs two ASCII hexadecimal digits followed by the symbol to indicate subtraction. Next, the user enters a second set of two hexadecimal digits that are to be subtracted from the first two hexadecimal digits. The subtraction result is shown on both the hyperterminal display as well as the LCD display. Care must be taken when the second hexadecimal number is greater than the first so the proper sign is given. The best way to perform the subtraction is to subtract the smaller of the two numbers from the larger of the two numbers and display a negative sign if the second number is larger than the first number. For example, what should be displayed is: 01 FF = FE (0x01 0xFF = 0xFE) This program should run indefinitely and start on a new line each time waiting for user input.

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

Source Code 4. Write a C language program that inputs two ASCII hexadecimal digits followed by the * symbol to indicate multiplication. Next, the user enters a second set of two hexadecimal digits that are to be multiplied with the first two hexadecimal digits. The multiplication result is shown on both the Hyperterminal display as well as the LCD display. Multiplication of two eight bit hexadecimal numbers can produce a result as large as sixteen bits. As such four hexadecimal digits should be used to display the result. For example what should be displayed is: 4E * A1 = 310E (0x4E * 0xA1 = 0x310E)

Source Code 5. Write a program that merges steps 2, 3, and 4 together and uses the ASCII symbols +, -, and * to determine which arithmetic operation to perform. This program should run indefinitely and start on a new line each time waiting for user input.

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

Source Code

Part 2. Generating Basic input with assembly language programs.


The demonstrate are the same in Part I 1. Write an assembly language program that inputs from the keyboard two hexadecimal numbers via hyperterminal. Next, these two numbers are displayed in the hyperterminal window and on the right most two digits of the LCD display on the experimenter board. To convert an ASCII 0- 9 to a four bit number, 0x30 is subtracted from the ASCII number. To convert the ASCII capital letters A-F to a four bit number, 0x37 is subtracted from these capital letters. To help write this program, please use as reference the assembly language programs written for laboratory experiments #3 and #4. Source code

2. Write an assembly language program that inputs two ASCII hexadecimal digits followed by the + symbol to indicate addition. Next, the user enters a second set of two hexadecimal digits that are to be added to the first two hexadecimal digits. The addition result is shown on both the hyperterminal display as well as the LCD display. Three digits are required for this resulting addition. For example, what should be displayed is: FF + 01 = 100 (0xFF + 0x01 = 0x100) This program should run indefinitely and start on a new line each time waiting for user input. Source code Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

3. Write an assembly language program that inputs two ASCII hexadecimal digits followed by the - symbol to indicate subtraction. Next, the user enters a second set of two hexadecimal digits that are to be subtracted from the first two hexadecimal digits. The subtraction result is shown on both the hyperterminal display as well as the LCD display. Care must be taken when the second hexadecimal number is greater than the first so the proper sign is given. The best way to perform the subtraction is to subtract the smaller of the two numbers from the larger of the two numbers and display a negative sign if the second number is larger than the first number. For example, what should be displayed is: 01 FE = FD (0x01 0xFF = 0xFD) This program should run indefinitely and start on a new line each time waiting for user input. Source code

4. Write an assembly language program that inputs two ASCII hexadecimal digits followed by the * symbol to indicate multiplication. Next, the user enters a second set of two hexadecimal digits that are to be multiplied with the first two hexadecimal digits. The multiplication result is shown on both the hyperterminal display as well as the LCD display. The MSP430 does not have an assembly language multiplication instruction. For this project, the program needs to implement the shift and add multiplication algorithm. For example, what should be displayed is: 4E * A1 = 310E (0x4E * 0xA1 = 0x310E) Finally, this program should run indefinitely and start on a new line each time waiting for user input. Source code

5. Write a program that merges steps 2, 3, and 4 together and uses the ASCII symbols +, -, and * to determine which arithmetic operation to perform. This program should run indefinitely and start on a new line each time waiting for user input. Source code

SOURCE CODE:
Part I C language programs
Procedure 1:
//--------------------------------------------------------------// Console I/O through the on board UART for MSP 430X4XXX //--------------------------------------------------------------#include "msp430fg4618.h" #include "stdio.h" #include "string.h"

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

void Init_UART(void); void OUTA_UART(unsigned char A); void OUTA_UART_ENTER(); unsigned char INCHAR_UART(void); void printString(char *myString); void Init_LCD(void); unsigned char *LCDSeg = (unsigned char *) &LCDM3; int LCD_SIZE=11; int main(void){ volatile unsigned char a; volatile unsigned int i; // volatile to prevent optimization volatile unsigned char j; // LCD digit Encoding unsigned char LCDdigit[] = {0x5F,0x06,0x6B,0x2F,0x36,0x3D,0x7D,0x07,0x7F,0x3F,0x77,0x7C,0x68,0x6E,0x79,0x71}; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer Init_UART(); Init_LCD(); //Finish SET-UP for (;;){ a = INCHAR_UART(); OUTA_UART(a); // First Hex Display i = a; if(i>=0x30 && i <= 0x39) i = i - 0x30; else if(i>=0x41 && i <= 0x46) i = i - 0x37; else if(i>=0x61 && i <= 0x66) i = i - 0x57; LCDSeg[1]= LCDdigit[i]; // Second Hex Display a = INCHAR_UART(); OUTA_UART(a); OUTA_UART(0x0D); OUTA_UART(0x0A); j = a ;

// Convert digit // Convert lowercase char // Convert uppercase char // LCD Display

if(j>= 0x30 && j <= 0x39) j = j - 0x30; // Convert Digit else if(j>=0x41 && j <= 0x46) j = j - 0x37; // Convert lowercase char else if(j>=0x61 && j <= 0x66) j = j - 0x57; // Convert uppercase char LCDSeg[0]= LCDdigit[j]; // LCD Display } } void OUTA_UART_ENTER(){ do{ }while ((IFG2 & 0x02)==0); // send the data to the transmit buffer UCA0TXBUF =0x0a; } void OUTA_UART(unsigned char A){ do{

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

}while ((IFG2 & 0x02)==0); // send the data to the transmit buffer UCA0TXBUF =A; } unsigned char INCHAR_UART(void){ do{ }while ((IFG2 & 0x01)==0); // go get the char from the receive buffer return (UCA0RXBUF); } void Init_UART(void){ P2SEL=0x30; UCA0CTL0=0; UCA0CTL1= 0x41; UCA0BR1=0; UCA0BR0=3; UCA0MCTL=0x06; UCA0STAT=0; UCA0CTL1=0x40; IE2=0; } void Init_LCD(void){ int n; for (n=0;n<LCD_SIZE;n++){ *(LCDSeg+n) = 0; } P5SEL = 0x1C; // BIT4 | BIT3 |BIT2 = 1 P5.4, P.3, P5.2 = 1 LCDAVCTL0 = 0x00; LCDAPCTL0 = 0x7E; LCDACTL = 0x7d; }

Procedure 2:
//--------------------------------------------------------------// Console I/O through the on board UART for MSP 430X4XXX //--------------------------------------------------------------#include "msp430fg4618.h" #include "stdio.h" #include "string.h" void Init_UART(void); void OUTA_UART(unsigned char A); void OUTA_UART_ENTER(); unsigned char INCHAR_UART(void); void printString(char *myString); char ascii2digit(unsigned char ascii); char hex2ascii(unsigned char hex); void Init_LCD(void); unsigned char *LCDSeg = (unsigned char *) &LCDM3; int LCD_SIZE=11; int main(void){

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

volatile volatile volatile volatile

unsigned unsigned unsigned unsigned

char a; int i; // volatile to prevent optimization char j; int sum;

unsigned char LCDdigit[] = {0x5F,0x06,0x6B,0x2F,0x36,0x3D,0x7D,0x07,0x7F,0x3F,0x77,0x7C,0x68,0x6E,0x79,0x71}; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer Init_UART(); Init_LCD(); //Finish SET-UP for (;;){ sum = 0; // Reset the Sum //--|#1 Hex |----------------------------a = INCHAR_UART(); // Get Char OUTA_UART(a); i = ascii2digit(a); // Convert ASCII code into digit code (0-F) //--|#2 Hex |----------------------------a = INCHAR_UART(); OUTA_UART(a); j = ascii2digit(a); // Convert ASCII code into digit code (0-F) OUTA_UART(0x2B); i = i << 4; sum = i + j; // Print out "+" on Hyperterminal

// John 2 first Hex

//--|#3 Hex |----------------------------a = INCHAR_UART(); OUTA_UART(a); i = ascii2digit(a); // Convert ASCII code into digit code (0-F) //--|#4 Hex |----------------------------a = INCHAR_UART(); OUTA_UART(a); j = ascii2digit(a); // Convert ASCII code into digit code (0-F) i = i << 4; i = i + j; //Finish Getting 2 last Hex number sum = sum + i; // Calculation for Display a = (sum & 0x00F) ; i = (sum & 0x0F0) >> 4; j = sum >> 8; OUTA_UART(0x3D); //Display in Hyperterminal OUTA_UART(hex2ascii(j) ); OUTA_UART(hex2ascii(i) ); OUTA_UART(hex2ascii(a) ); // New Line in Hyperterminal OUTA_UART(0x0D);

// Add Arithmetric

// Get units // Get tens // Get hundreds // Print out "="

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

OUTA_UART(0x0A); // Display on LCD LCDSeg[0]= LCDdigit[a]; LCDSeg[1]= LCDdigit[i]; LCDSeg[2]= LCDdigit[j]; } } char hex2ascii(unsigned char hex){ if (hex < 0x0A) hex += 0x30; else if (hex> 0x09 && hex < 0x10) hex += return hex; } char ascii2digit(unsigned char ascii){ unsigned char digit; if(ascii>=0x30 && ascii <= 0x39) digit = ascii - 0x30; digit else if(ascii>=0x41 && ascii <= 0x46) digit = ascii - 0x37; lowercase char else if(ascii>=0x61 && ascii <= 0x66) digit = ascii - 0x57; uppercase char return digit; } void OUTA_UART_ENTER(){ do{ }while ((IFG2 & 0x02)==0); // send the data to the transmit buffer UCA0TXBUF =0x0a; } void OUTA_UART(unsigned char A){ do{ }while ((IFG2 & 0x02)==0); // send the data to the transmit buffer UCA0TXBUF =A; } unsigned char INCHAR_UART(void){ do{ }while ((IFG2 & 0x01)==0); // go get the char from the receive buffer return (UCA0RXBUF); } void Init_UART(void){ P2SEL=0x30; // transmit and receive to port 2 bits 4 and 5 UCA0CTL0=0; // 8 data, no parity 1 stop, uart, async UCA0CTL1= 0x41; UCA0BR1=0; // upper byte of divider clock word UCA0BR0=3; // clock divide from a clock to bit clock 32768/9600 UCA0MCTL=0x06; UCA0STAT=0; // do not loop the transmitter back to the UCA0CTL1=0x40; IE2=0; // turn transmit interrupts off // Convert // Convert // Convert 0x37;

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

} void Init_LCD(void){ int n; for (n=0;n<LCD_SIZE;n++){ *(LCDSeg+n) = 0; } P5SEL = 0x1C; LCDAVCTL0 = 0x00; LCDAPCTL0 = 0x7E; LCDACTL = 0x7d; }

Procedure 3:
//--------------------------------------------------------------// Console I/O through the on board UART for MSP 430X4XXX //--------------------------------------------------------------#include "msp430fg4618.h" #include "stdio.h" #include "string.h" void Init_LCD(void); void Init_UART(void); void OUTA_UART(unsigned char A); unsigned char INCHAR_UART(void); char getting_data(); char ascii2digit(unsigned char ascii); char hex2ascii(unsigned char hex); //Declaration unsigned char *LCDSeg = (unsigned char *) &LCDM3; const unsigned int LCD_SIZE=11; volatile unsigned char a; volatile unsigned int i; // volatile to prevent optimization volatile unsigned char j; volatile unsigned char sub; const unsigned char LCDdigit[] = {0x5F,0x06,0x6B,0x2F,0x36,0x3D,0x7D,0x07,0x7F,0x3F,0x77,0x7C,0x68,0x6E,0x79,0x71}; int main(void){ WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer Init_UART(); Init_LCD(); //Finish SET-UP for (;;){ sub = 0; i = getting_data(); j = getting_data(); OUTA_UART(0x2D); // Initial Sub // Gather data // Print out "-"

i = i << 4; // Join 2 first Hex sub = i + j; //---------------------------------------------

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

i = getting_data(); j = getting_data(); OUTA_UART(0x3D); i = i << 4; i = i + j; //---- Finish gather data // Calculation if(sub >= i){ sub = sub - i; LCDSeg[2]= 0x00; } else { sub = i - sub ; OUTA_UART(0x2D); LCDSeg[2]= 0x20; }

// #3 HEX // #4 HEX // Print out "=" // Join 2 last Hex

// Clear the third LED

// Print out "-" // Display "-"

i = sub & 0x0F ; // units Digit j = sub >> 4; // tens Digit // Display in Hyperterminal OUTA_UART(hex2ascii(j) ); OUTA_UART(hex2ascii(i) ); OUTA_UART(0x0D); OUTA_UART(0x0A); // Display on LCD LCDSeg[0]= LCDdigit[i]; LCDSeg[1]= LCDdigit[j]; } } char getting_data(){ a = INCHAR_UART(); OUTA_UART(a); return ascii2digit(a); } char hex2ascii(unsigned char hex){ if (hex < 0x0A) hex += 0x30; else if (hex> 0x09 && hex < 0x10) hex += return hex; } char ascii2digit(unsigned char ascii){ unsigned char digit; if(ascii>=0x30 && ascii <= 0x39) digit = ascii - 0x30; digit else if(ascii>=0x41 && ascii <= 0x46) digit = ascii - 0x37; lowercase char else if(ascii>=0x61 && ascii <= 0x66) digit = ascii - 0x57; uppercase char return digit; } // Convert // Convert // Convert 0x37; // New Line

// Display on LCD

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

void OUTA_UART(unsigned char A){ do{ }while ((IFG2 & 0x02)==0); // send the data to the transmit buffer UCA0TXBUF =A; } unsigned char INCHAR_UART(void){ do{ }while ((IFG2 & 0x01)==0); // go get the char from the receive buffer return (UCA0RXBUF); } void Init_UART(void){ P2SEL=0x30; // transmit and receive to port 2 bits 4 and 5 UCA0CTL0=0; // 8 data, no parity 1 stop, uart, async UCA0CTL1= 0x41; UCA0BR1=0; // upper byte of divider clock word UCA0BR0=3; // clock divide from a clock to bit clock 32768/9600 UCA0MCTL=0x06; UCA0STAT=0; // do not loop the transmitter back to the UCA0CTL1=0x40; IE2=0; // turn transmit interrupts off } void Init_LCD(void){ int n; for (n=0;n<LCD_SIZE;n++){ *(LCDSeg+n) = 0; } P5SEL = 0x1C; LCDAVCTL0 = 0x00; LCDAPCTL0 = 0x7E; LCDACTL = 0x7d; }

Procedure 4:
//--------------------------------------------------------------// Console I/O through the on board UART for MSP 430X4XXX //--------------------------------------------------------------#include "msp430fg4618.h" #include "stdio.h" #include "string.h" void Init_LCD(void); void Init_UART(void); void OUTA_UART(unsigned char A); unsigned char INCHAR_UART(void); char char char void getting_data(); ascii2digit(unsigned char ascii); hex2ascii(unsigned char hex); display_result();

//Declaration

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

unsigned char *LCDSeg = (unsigned char *) &LCDM3; const unsigned int LCD_SIZE=11; volatile unsigned char a; volatile unsigned int i; // volatile to prevent optimization volatile unsigned char j; volatile unsigned int multi; const unsigned char LCDdigit[] = {0x5F,0x06,0x6B,0x2F,0x36,0x3D,0x7D,0x07,0x7F,0x3F,0x77,0x7C,0x68,0x6E,0x79,0x71}; int main(void){ WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer Init_UART(); Init_LCD(); //Finish SET-UP for (;;){ multi = 0; i = getting_data(); j = getting_data(); OUTA_UART(0x2A); // Initial Multiple data // Gather data #1 HEX // #2 HEX // Print out "-"

i = i << 4; multi = i + j; // Join 2 first Hex //--------------------------------------------i = getting_data(); // #3 HEX j = getting_data(); // #4 HEX OUTA_UART(0x3D); i = i << 4; i = i + j; //---- Finish gather data multi = multi * i ; // Print out "=" // Join 2 last Hex // Multiple Arithmetic

//Display Result display_result(multi); } } void display_result(){ i = (multi & 0xF000) >> 12; OUTA_UART(hex2ascii(i) ); LCDSeg[3]= LCDdigit[i]; i = (multi & 0x0F00) >> 8; OUTA_UART(hex2ascii(i) ); LCDSeg[2]= LCDdigit[i]; i = (multi & 0x0F0) >> 4; OUTA_UART(hex2ascii(i) ); LCDSeg[1]= LCDdigit[i]; i = multi & 0x0F ; OUTA_UART(hex2ascii(i) ); LCDSeg[0]= LCDdigit[i]; OUTA_UART(0x0D); OUTA_UART(0x0A);

// #4 digit // LCD Display for #4 digit // #3 digit

// #2 digit

// units Digit

// New Line

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

} char getting_data(){ a = INCHAR_UART(); OUTA_UART(a); return ascii2digit(a); } char hex2ascii(unsigned char hex){ if (hex < 0x0A) hex += 0x30; else if (hex> 0x09 && hex < 0x10) hex += return hex; } char ascii2digit(unsigned char ascii){ unsigned char digit; if(ascii>=0x30 && ascii <= 0x39) digit = ascii - 0x30; digit else if(ascii>=0x41 && ascii <= 0x46) digit = ascii - 0x37; lowercase char else if(ascii>=0x61 && ascii <= 0x66) digit = ascii - 0x57; uppercase char return digit; } void OUTA_UART(unsigned char A){ do{ }while ((IFG2 & 0x02)==0); // send the data to the transmit buffer UCA0TXBUF =A; } unsigned char INCHAR_UART(void){ do{ }while ((IFG2 & 0x01)==0); // go get the char from the receive buffer return (UCA0RXBUF); } void Init_UART(void){ P2SEL=0x30; // transmit and receive to port 2 bits 4 and 5 UCA0CTL0=0; // 8 data, no parity 1 stop, uart, async UCA0CTL1= 0x41; UCA0BR1=0; // upper byte of divider clock word UCA0BR0=3; // clock divide from a clock to bit clock 32768/9600 UCA0MCTL=0x06; UCA0STAT=0; // do not loop the transmitter back to the UCA0CTL1=0x40; IE2=0; // turn transmit interrupts off } void Init_LCD(void){ int n; for (n=0;n<LCD_SIZE;n++){ *(LCDSeg+n) = 0; } P5SEL = 0x1C; LCDAVCTL0 = 0x00; LCDAPCTL0 = 0x7E; LCDACTL = 0x7d; // Convert // Convert // Convert 0x37;

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

Procedure 5:
//--------------------------------------------------------------// Console I/O through the on board UART for MSP 430X4XXX //--------------------------------------------------------------#include "msp430fg4618.h" #include "stdio.h" #include "string.h" void Init_LCD(void); void Init_UART(void); void OUTA_UART(unsigned char A); unsigned char INCHAR_UART(void); char void void char char void getting_data(); determine_arithmetic(); implement_arithmetic(); ascii2digit(unsigned char ascii); hex2ascii(unsigned char hex); display_result();

//Declaration unsigned char *LCDSeg = (unsigned char *) &LCDM3; const unsigned int LCD_SIZE=11; volatile unsigned char a; volatile unsigned int i; // volatile to prevent optimization volatile unsigned char j; volatile unsigned int results; volatile unsigned char arithmetic; const unsigned char LCDdigit[] = {0x5F,0x06,0x6B,0x2F,0x36,0x3D,0x7D,0x07,0x7F,0x3F,0x77,0x7C,0x68,0x6E,0x79,0x71}; int main(void){ WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer Init_UART(); Init_LCD(); //Finish SET-UP for (;;){ results = 0; // Initial Result

i = getting_data(); // Gather data for #1 HEX j = getting_data(); // #2 HEX //---------------------------------------determine_arithmetic(); // Determine Arithmetic ( + - * ) //---------------------------------------i = i << 4; // Join 2 first Hex results = i + j; // Save in result i = getting_data(); j = getting_data(); OUTA_UART(0x3D); i = i << 4; i = i + j; //---- Finish gather data // #3 HEX // #4 HEX // Print out "=" // Join 2 second Hex

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

implement_arithmetic(); //Display result display_result(); } } void determine_arithmetic(){ a = INCHAR_UART(); OUTA_UART(a); if (a == 0x2D) arithmetic = 0x02; else if (a == 0x2A) arithmetic = 0x03; else arithmetic = 0x01;

// Subtract // Multiple // Add

} void implement_arithmetic(){ if (arithmetic == 0x03) results = results * i; else if (arithmetic == 0x02) { arithmetic if (results >= i){ results = results - i; LCDSeg[2]= 0x00; LCDSeg[3]= 0x00; } else { results = i - results; OUTA_UART(0x2D); LCDSeg[2]= 0x20; LCDSeg[3]= 0x00; } } else results = results + i; Arithmetic } void display_result(){ if(results> 0x0FFF){ i = (results & 0xF000) >> 12; OUTA_UART(hex2ascii(i) ); LCDSeg[3]= LCDdigit[i]; }

// Multiple arithmetic // Substract

// Print out "-" // Turn off LCD Led #4

// Add

// #4 digit // LCD Display

if(results> 0x0FF & arithmetic != 0x02){ i = (results & 0x0F00) >> 8; // #3 digit OUTA_UART(hex2ascii(i) ); LCDSeg[2]= LCDdigit[i]; } if (results <= 0x0FF && arithmetic != 0x02) LCDSeg[2]= 0x00; i = (results & 0x0F0) >> 4; OUTA_UART(hex2ascii(i) ); LCDSeg[1]= LCDdigit[i]; i = results & 0x0F ; OUTA_UART(hex2ascii(i) ); LCDSeg[0]= LCDdigit[i]; // #2 digit

// #1 digit

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

OUTA_UART(0x0D); OUTA_UART(0x0A);

// New Line

} char getting_data(){ a = INCHAR_UART(); OUTA_UART(a); return ascii2digit(a); } char hex2ascii(unsigned char hex){ if (hex < 0x0A) hex += 0x30; else if (hex> 0x09 && hex < 0x10) hex += return hex; } char ascii2digit(unsigned char ascii){ unsigned char digit; digit

0x37;

if(ascii>=0x30 && ascii <= 0x39) digit = ascii - 0x30; else if(ascii>=0x41 && ascii <= 0x46) digit = ascii - 0x37; lowercase char else if(ascii>=0x61 && ascii <= 0x66) digit = ascii - 0x57; uppercase char return digit; } void OUTA_UART(unsigned char A){ do{ }while ((IFG2 & 0x02)==0); // send the data to the transmit buffer UCA0TXBUF =A; } unsigned char INCHAR_UART(void){ do{ }while ((IFG2 & 0x01)==0); // go get the char from the receive buffer return (UCA0RXBUF); } void Init_UART(void){ P2SEL=0x30; UCA0CTL0=0; UCA0CTL1= 0x41; UCA0BR1=0; UCA0BR0=3; UCA0MCTL=0x06; UCA0STAT=0; UCA0CTL1=0x40; IE2=0; } void Init_LCD(void){ int n; for (n=0;n<LCD_SIZE;n++){ *(LCDSeg+n) = 0; } P5SEL = 0x1C; LCDAVCTL0 = 0x00; LCDAPCTL0 = 0x7E; LCDACTL = 0x7d;

// Convert // Convert // Convert

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

Part II Assembly language programs


Procedure 1:
.cdecls C,LIST,"msp430.h" .sect ".sysmem" ; Include device header file ; data ram for

initialized LOOKUP .byte 0x5F,0x06,0x6B,0x2F,0x36 ; Encoded digits .byte 0x3D,0x7D,0x07,0x7F,0x3F .byte 0x77,0x7C,0x68,0x6E,0x79,0x71 LCD_SIZE .byte 11 ; eleven bytes needed by the LCD ;------------------------------------------------------------------------------.text ; Assemble into program memory .retain ; Override ELF conditional linking .retainrefs ; Additionally retain any sections ;------------------------------------------------------------------------------RESET mov.w #__STACK_END,SP ; Initialize stackpointer StopWDT mov.w #WDTPW|WDTHOLD,&WDTCTL ; Stop watchdog timer ; Initial call #Init_LCD call #Init_UART ;------------------------------------------------------------------------------Mainloop call #INCHAR_UART call #OUTA_UART call #Ascii2HexDigit mov R4,R12 ; Get #1 Hex call #INCHAR_UART call #OUTA_UART call #Ascii2HexDigit mov R4,R11 call #Dis_digit #2 Hex call #Dis_tenth #1 Hex ;------- New line in Hyperterminal mov #0x0D, R4 call #OUTA_UART mov #0x0A, R4 call #OUTA_UART ; Display on LCD

; Get #2 Hex ; Display on LCD

jmp Mainloop ;------------------------------------------------------------------------------Ascii2HexDigit ; Only accept the uppercase for character from A -> F ;------------------------------------------------------------------------------;------------Value of R4 in range 0 - 9 Number1 cmp #0x3A, R4 ; if R4 < A (10) jl Number2 ; move to Number2 jmp Char1 ; else move to Char1 (R4 not a number)

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

Number2 cmp #0x30, R4 jge convert1 jmp retn

; R4 > 0 ; ; Finish convert

convert1 sub #0x30, R4 jmp retn ;------------Value of R4 in range A - F Char1 cmp #0x41, R4 ; if R4 >= A jge Char2 ; move to Char2 jmp retn Char2 cmp #0x47, R4 jl convert2 convert2 sub #0x37, R4 jmp retn retn ret ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ; 0x00 <= R11,R12,R13 <= 0x0F Dis_digit ;Display value in R11 push R8; push R5 mov.w #LCDM3, R5 mov.w #LOOKUP, R8 ; Initialize lookup table to R8 add.w R11,R8 ; Offset address of R8 mov.b 0(R8),0(R5) ; Display Digit pop R5 pop R8 ret Dis_tenth ;Display value in R12 push R8 push R5 mov.w #LCDM3, R5 mov.w #LOOKUP, R8 table to R8 add.w R12,R8 mov.b 0(R8),1(R5) pop R5 pop R8 ret ; if R4 < G (mean: R4 <= F)

; Initialize lookup ; Offset address of R8 ; Display tenth

;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ OUTA_UART push R5 mov.b &IFG2,R5 and.b #0x02,R5 cmp.b #0x00,R5 jz lpa mov.b R4,&UCA0TXBUF pop R5 ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lpa

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

INCHAR_UART push R5 mov.b &IFG2,R5 and.b #0x01,R5 cmp.b #0x00,R5 jz lpb mov.b &UCA0RXBUF,R4 pop R5 ret ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lpb Init_UART mov.b #0x30,&P2SEL mov.b #0x00,&UCA0CTL0 mov.b #0x41,&UCA0CTL1 mov.b #0x00,&UCA0BR1 mov.b #0x03,&UCA0BR0 mov.b #0x06,&UCA0MCTL mov.b #0x00,&UCA0STAT mov.b #0x40,&UCA0CTL1 mov.b #0x00,&IE2 ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Init_LCD mov.b #0x00, R6 mov.w #LCDM3, R5 mov.b #0x00, R7 lpt mov.b R7, 0(R5) inc.w R5 inc.b R6 cmp.b LCD_SIZE, R6 jnz lpt mov.b #0x1C, &P5SEL mov.b #0x00, &LCDAVCTL0 mov.b #0x7E, &LCDAPCTL0 mov.b #0x7d, &LCDACTL ret ; Stack Pointer definition .global __STACK_END .sect .stack ; Interrupt Vectors .sect ".reset" .short RESET

; MSP430 RESET Vector

Procedure 2:
.sect initialized LCDencode .byte .byte Hex .word RESULT .word .cdecls C,LIST,"msp430.h" ".sysmem" .byte 0x5F,0x06,0x6B,0x2F,0x36 0x3D,0x7D,0x07,0x7F,0x3F 0x77,0x7C,0x68,0x6E,0x79,0x71 0x0000,0x0000 0x0000 ; Include device header file ; data ram for ; Encoded digits

; Store result value

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

Offsets .byte 0x00; Dis_digit .byte 0x00,0x00,0x00,0x00 Result into digit LCD_SIZE .byte 11 the LCD

; Temporary for spliting ; eleven bytes needed by

;------------------------------------------------------------------------------.text ; Assemble into program memory .retain ; Override ELF conditional linking .retainrefs ; Additionally retain any sections ;------------------------------------------------------------------------------RESET mov.w #__STACK_END,SP ; Initialize stackpointer StopWDT mov.w #WDTPW|WDTHOLD,&WDTCTL ; Stop watchdog timer ;------- Initial --------------------------------------------------------------call #Init_LCD call #Init_UART mov #Dis_digit,R10 ; Use R10 as address for display digit mov #Hex,R6 ; Use R5 as address for stores value ;---| Main loop |--------------------------------------------------------------Mainloop call #Get2_hex mov 0(R6),&RESULT Result mov #0x2B,R4 call #OUTA_UART add #0x02,R6 call #Get2_hex mov #0x3D,R4 call #OUTA_UART add 0(R6),&RESULT call #Split_digit call #Dis_hyper ; Print out "+"

; Store 2 first Hex in

; R6 point to next .WORD ; Print out "="

; ADD arithmetic ; Split into digits ; Display on

Hyperterminal ;------| Display on LCD |--------------------------------------------------mov.b 0(R10),R9 mov #0x00,&Offsets ; Display on LCD #1 Hex call #Display_digit mov.b 1(R10),R9; mov #0x01,&Offsets #2 Hex call #Display_digit mov.b 2(R10),R9; mov #0x02,&Offsets #3 Hex call #Display_digit ; Display on LCD ; Display on LCD

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

;------| New line in Hyperterminal |------------------------------------------mov #0x0D, R4 call #OUTA_UART mov #0x0A, R4 call #OUTA_UART jmp Mainloop

;------------------------------------------------------------------------------Dis_hyper ;----------- Display on Hyperterminal mov.b 2(R10),R4; call #HexDigit2Acsii call #OUTA_UART mov.b 1(R10),R4; call #HexDigit2Acsii call #OUTA_UART mov.b 0(R10),R4; call #HexDigit2Acsii call #OUTA_UART ret ;++++++++++++++++++++++++++++++++++++++++++++++++++ Split_digit push R11 push R12 mov #Dis_digit,R10 mov &RESULT,R11 mov R11,R12 bic #0xFFF0,R11 mov.b R11,0(R10) mov R12,R11 rram #0x04, R11 mov R11,R12 bic #0xFFF0,R11 mov.b R11,1(R10) mov R12,R11 rram #0x04, R11 mov R11,R12 bic #0xFFF0,R11 mov.b R11,2(R10) mov R12,R11 rram #0x04, R11 mov R11,R12 bic #0xFFF0,R11 mov.b R11,3(R10)

; Set R10 as Address ; ; ; ; Store data in R11 for process Back-up R11 Get the LSB digit Store units digit

; Restore R11 for shifting ; Back-up after shifting ; Store units digit

; Store tens digit

; Store hundreds digit

pop R12 pop R11 ret ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

Get2_hex ;Store 2 Hexadicimal at address: R6 call #INCHAR_UART call #OUTA_UART call #Ascii2HexDigit rlam #0x04,R4 X0 h mov R4,0(R6) call #INCHAR_UART call #OUTA_UART call #Ascii2HexDigit add R4,0(R6) ret ;+++++++++++++++++++++++++++++++++++++++++++++++++ HexDigit2Acsii Num1 cmp #0x0A, R4 jl Num2 jmp Char1c Num2 cmp #0x00, R4 jge con1 jmp retn1 con1 add #0x30, R4 jmp retn1 Char1c cmp #0x0A, R4 jge Char2c jmp retn1 Char2c cmp #0x10, R4 jl con2 con2 add #0x37, R4 jmp retn1 retn1 ret ;+++++++++++++++++++++++++++++++++++++++++++++++++++++

; 0X h => ; store #1

; store next HEX ( XX h)

Ascii2HexDigit ; Only accept the uppercase for character from A -> F ;------------------------------------------------------;------------| R4 in range 0 - 9 |---------------------Number1 cmp #0x3A, R4 ; if R4 < A (10) jl Number2 ; move to Number2 jmp Char1 ; else move to Char1 (R4 not a number) Number2 cmp #0x30, R4 jge convert1 jmp retn convert1 sub #0x30, R4 jmp retn ; R4 > 0 ; ; Finish convert

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

;------------| R4 in range A - F |---------------------Char1 cmp #0x41, R4 ; if R4 >= A jge Char2 ; move to Char2 jmp retn Char2 cmp #0x47, R4 jl convert2 convert2 sub #0x37, R4 jmp retn retn ret ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Display_digit ;----------------------------------------------------------;Display value in R9 with the &Offsets for the LED push R8; push R5; mov.w #LCDM3, R5 ; Initial LCD mapping mov.w #LCDencode, R8 ; Initialize lookup table to R8 add.w R9,R8 of R8 add &Offsets,R5 to display mov.b 0(R8), 0(R5) ; Display Digit pop R5 pop R8 ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ OUTA_UART push R5 mov.b &IFG2,R5 and.b #0x02,R5 cmp.b #0x00,R5 jz lpa mov.b R4,&UCA0TXBUF pop R5 ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lpa INCHAR_UART push R5 mov.b &IFG2,R5 and.b #0x01,R5 cmp.b #0x00,R5 jz lpb mov.b &UCA0RXBUF,R4 pop R5 ret ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lpb Init_UART mov.b #0x30,&P2SEL ; Choose the LED ; Offset address ; if R4 < G (mean: R4 <= F)

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

mov.b #0x00,&UCA0CTL0 mov.b #0x41,&UCA0CTL1 mov.b #0x00,&UCA0BR1 mov.b #0x03,&UCA0BR0 mov.b #0x06,&UCA0MCTL mov.b #0x00,&UCA0STAT mov.b #0x40,&UCA0CTL1 mov.b #0x00,&IE2 ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Init_LCD mov.b #0x00, R6 mov.w #LCDM3, R5 mov.b #0x00, R7 lpt mov.b R7, 0(R5) inc.w R5 inc.b R6 cmp.b LCD_SIZE, R6 jnz lpt mov.b #0x1C, &P5SEL mov.b #0x00, &LCDAVCTL0 mov.b #0x7E, &LCDAPCTL0 mov.b #0x7d, &LCDACTL ret ;------------| Stack Pointer definition |--------------------------------------.global __STACK_END .sect .stack ;---------------| Interrupt Vectors |------------------------------------------.sect ".reset" ; MSP430 RESET Vector .short RESET

Procedure 3:
.sect initialized LCDencode .byte .byte .cdecls C,LIST,"msp430.h" ".sysmem" ; Include device header file ; data ram for ; Encoded digits

.byte 0x5F,0x06,0x6B,0x2F,0x36 0x3D,0x7D,0x07,0x7F,0x3F 0x77,0x7C,0x68,0x6E,0x79,0x71,0x20,0x00

Hex .word 0x0000,0x0000 RESULT .word 0x0000 Offsets .byte 0x00; Dis_digit .byte 0x00,0x00,0x00,0x00 Result into digit LCD_SIZE .byte 11 the LCD

; Store result value ; Temporary for spliting ; eleven bytes needed by

;------------------------------------------------------------------------------.text ; Assemble into program memory .retain ; Override ELF conditional linking .retainrefs ; Additionally retain any sections ;------------------------------------------------------------------------------RESET mov.w #__STACK_END,SP ; Initialize stackpointer

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

StopWDT mov.w #WDTPW|WDTHOLD,&WDTCTL ; Stop watchdog timer ;------- Initial --------------------------------------------------------------call #Init_LCD call #Init_UART mov #Dis_digit,R10 ; Use R10 as address for display digit mov #Hex,R6 ; Use R6 as address for stores value ;------------------------------------------------------------------------------Mainloop call #Get2_hex ; Gather 2 first HEX mov #0x2D,R4 call #OUTA_UART add #0x02,R6 call #Get2_hex HEX mov #0x3D,R4 call #OUTA_UART call #Sub_arithmetic call #Split_digit call #Dis_hyper Hyperterminal call #Dis_LCD ;------- New line in Hyperterminal mov #0x0D, R4 call #OUTA_UART mov #0x0A, R4 call #OUTA_UART jmp Mainloop ;------------------------------------------------------------------------------Sub_arithmetic mov #Hex,R6 ; Reset R6 pointer cmp 2(R6),0(R6); jl minus_sign ; If 2(R6) > 0(R6) mov 0(R6),&RESULT sub 2(R6),&RESULT mov.b #0x00,R15 ; Use R15 = 0 for plus sign jmp finish_sub minus_sign mov 2(R6),&RESULT sub 0(R6),&RESULT mov #0x01,R15 ret ; R15 = 1 for minus sign ; Display on LCD ; Print out "=" ; SUB arithmetic ; Split into digits ; Display on ; Print out "-" ; R6 point to next .WORD ; Gather 2 last

finish_sub Dis_LCD

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

;----------- Display on LCD mov.b 0(R10),R9 mov #0x00,&Offsets #1 Hex call #Display_digit mov.b 1(R10),R9; mov #0x01,&Offsets #2 Hex call #Display_digit cmp #0x00,R15 jeq plus_sign mov.b #0x10,R9; jmp dis_next plus_sign dis_next mov.b #0x11,R9; mov #0x02,&Offsets call #Display_digit ret Dis_hyper ;----------- Display on Hyperterminal cmp #0x00,R15 jeq Not_minus mov #0x2D,R4 call #OUTA_UART Not_minus mov.b 1(R10),R4; call #HexDigit2Acsii call #OUTA_UART mov.b 0(R10),R4; call #HexDigit2Acsii call #OUTA_UART ret ;+++++++++++++++++++++++++++++++++++++++++ Split_digit push R11 push R12 mov #Dis_digit,R10 mov &RESULT,R11 mov R11,R12 bic #0xFFF0,R11 mov.b R11,0(R10) mov R12,R11 rram #0x04,R11 mov R11,R12 bic #0xFFF0,R11 mov.b R11,1(R10) mov R12,R11 rram #0x04,R11

; Display on LCD

; Display on LCD

; Plus sign ; Display "-" ; Turn Off the LCD ; Display on LCD #3 Hex

; Display "-"

; Set R10 as Address ; ; ; ; Store data in R11 for process Back-up R11 Get the LSB digit Store units digit

; Restore R11 for shifting ; Back-up after shifting ; Store units digit

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

mov R11,R12 bic #0xFFF0,R11 mov.b R11,2(R10) mov R12,R11 rram #0x04,R11 mov R11,R12 bic #0xFFF0,R11 mov.b R11,3(R10) pop R12 pop R11 ret

; Store tens digit

; Store hundreds digit

Get2_hex ;Store 2 Hexadicimal at address: R6 call #INCHAR_UART call #OUTA_UART call #Ascii2HexDigit rlam #0x04,R4 mov R4,0(R6) call #INCHAR_UART call #OUTA_UART call #Ascii2HexDigit add R4,0(R6) ret ;--------------------------------HexDigit2Acsii Num1 cmp #0x0A, R4 jl Num2 jmp Char1c Num2 cmp #0x00, R4 jge con1 jmp retn1 con1 add #0x30, R4 jmp retn1 Char1c cmp #0x0A, R4 jge Char2c jmp retn1 Char2c cmp #0x10, R4 jl con2 con2 add #0x37, R4 jmp retn1

; 0X h => X0 h ; store #1

; store next HEX ( XX h)

retn1 ret ;+++++++++++++++++++++++++++++++++++++++++++++++++++++ Ascii2HexDigit ; Only accept the uppercase for character from A -> F ;------------------------------------------------------------------------------;------------ R4 in range 0 - 9

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

Number1 cmp #0x3A, R4 jl Number2 jmp Char1 number) Number2 cmp #0x30, R4 jge convert1 jmp retn convert1 sub #0x30, R4 jmp retn ;------------ R4 in range A - F Char1 cmp #0x41, R4 jge Char2 jmp retn Char2 cmp #0x47, R4 jl convert2 convert2 sub #0x37, R4 jmp retn

; if R4 < A (10) ; move to Number2 ; else move to Char1 (R4 not a

; R4 > 0 ; ; Finish convert

; if R4 >= A ; move to Char2 ; if R4 < G (mean: R4 <= F)

retn ret ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Display_digit ;----------------------------------------------------------;Display value in R9 with the &Offsets for the LED push R8; push R5; mov.w #LCDM3, R5 ; Initial LCD mapping mov.w #LCDencode, R8 ; Initialize lookup table to R8 add.w R9,R8 of R8 add &Offsets,R5 to display mov.b 0(R8), 0(R5) ; Display Digit pop R5 pop R8 ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ OUTA_UART push R5 mov.b &IFG2,R5 and.b #0x02,R5 cmp.b #0x00,R5 jz lpa mov.b R4,&UCA0TXBUF pop R5 ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lpa INCHAR_UART push R5 ; Choose the LED ; Offset address

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

lpb

mov.b &IFG2,R5 and.b #0x01,R5 cmp.b #0x00,R5 jz lpb mov.b &UCA0RXBUF,R4 pop R5 ret ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Init_UART mov.b #0x30,&P2SEL mov.b #0x00,&UCA0CTL0 mov.b #0x41,&UCA0CTL1 mov.b #0x00,&UCA0BR1 mov.b #0x03,&UCA0BR0 mov.b #0x06,&UCA0MCTL mov.b #0x00,&UCA0STAT mov.b #0x40,&UCA0CTL1 mov.b #0x00,&IE2 ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Init_LCD mov.b #0x00, R6 mov.w #LCDM3, R5 mov.b #0x00, R7 lpt mov.b R7, 0(R5) inc.w R5 inc.b R6 cmp.b LCD_SIZE, R6 jnz lpt mov.b #0x1C, &P5SEL mov.b #0x00, &LCDAVCTL0 mov.b #0x7E, &LCDAPCTL0 mov.b #0x7d, &LCDACTL ret ;-------------| Stack Pointer definition |-------------------------------------.global __STACK_END .sect .stack ;-------------| Interrupt Vectors |--------------------------------------------.sect ".reset" ; MSP430 RESET Vector .short RESET

Procedure 4:
.sect initialized LCDencode .byte .byte .cdecls C,LIST,"msp430.h" ".sysmem" ; Include device header file ; data ram for ; Encoded digits

.byte 0x5F,0x06,0x6B,0x2F,0x36 0x3D,0x7D,0x07,0x7F,0x3F 0x77,0x7C,0x68,0x6E,0x79,0x71,0x20,0x00

Hex .word 0x0000,0x0000 RESULT .word 0x0000 Offsets .byte 0x00;

; Store result value

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

Dis_digit .byte 0x00,0x00,0x00,0x00 Result into digit LCD_SIZE .byte 11 the LCD

; Temporary for spliting ; eleven bytes needed by

;------------------------------------------------------------------------------.text ; Assemble into program memory .retain ; Override ELF conditional linking .retainrefs ; Additionally retain any sections ;------------------------------------------------------------------------------RESET mov.w #__STACK_END,SP ; Initialize stackpointer StopWDT mov.w #WDTPW|WDTHOLD,&WDTCTL ; Stop watchdog timer ;------- Initial --------------------------------------------------------------call #Init_LCD call #Init_UART mov #Dis_digit,R10 ; Use R10 as address for display digit mov #Hex,R6 ; Use R6 as address for stores value ;------------------| Main loop |------------------------------------------Mainloop call #Get2_hex add #0x02,R6 mov #0x2A,R4 call #OUTA_UART call #Get2_hex mov #0x3D,R4 call #OUTA_UART call #Multi_arithmetic call #Split_digit call #Dis_hyper Hyperterminal call #Dis_LCD ;------- New line in Hyperterminal mov #0x0D, R4 call #OUTA_UART mov #0x0A, R4 call #OUTA_UART jmp Mainloop ;---------------------------------------------------------------------------------Multi_arithmetic mov #Hex,R6 ; Reset R6 pointer mov #0x0000,&RESULT ; Empty the results push R9 ; Use R9 push R10 ; and R10 as temporary lp8 mov #0x08,R10 ; Display on LCD ; Split into digits ; Display on ; Gather 2 first HEX ; R6 point to next .WORD ; Print out "*"

; Gather 2 last HEX ; Print out "="

mult_start

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

mov bic cmp jeq jmp

2(R6),R9 #0xFE,R9 #0x01,R9 implement mult_skip

; Back-up 2nd term ; If the current digit = 1

implement add 0(R6),&RESULT mult_skip rla 0(R6) rra 2(R6) dec R10 cmp #0x00,R10 jne mult_start

; implement binary adding

pop R9 pop R10 ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Dis_LCD ;----------- Display on LCD mov.b 0(R10),R9 mov #0x00,&Offsets #1 Hex call #Display_digit mov.b 1(R10),R9; mov #0x01,&Offsets #2 Hex call #Display_digit mov.b 2(R10),R9; mov #0x02,&Offsets #3 Hex call #Display_digit mov.b 3(R10),R9; mov #0x03,&Offsets #4 Hex call #Display_digit ret ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++ Dis_hyper ;----------- Display on Hyperterminal mov.b 3(R10),R4; call #HexDigit2Acsii call #OUTA_UART mov.b 2(R10),R4; call #HexDigit2Acsii call #OUTA_UART mov.b 1(R10),R4; call #HexDigit2Acsii call #OUTA_UART

; Display on LCD

; Display on LCD

; Display on LCD

; Display on LCD

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

mov.b 0(R10),R4; call #HexDigit2Acsii call #OUTA_UART ret ;+++++++++++++++++++++++++++++++++++++++++ Split_digit push R11 push R12 mov #Dis_digit,R10 mov &RESULT,R11 mov R11,R12 bic #0xFFF0,R11 mov.b R11,0(R10) mov R12,R11 rram #0x04,R11 mov R11,R12 bic #0xFFF0,R11 mov.b R11,1(R10) mov R12,R11 rram #0x04,R11 mov R11,R12 bic #0xFFF0,R11 mov.b R11,2(R10) mov R12,R11 rram #0x04,R11 mov R11,R12 bic #0xFFF0,R11 mov.b R11,3(R10)

; Set R10 as Address ; ; ; ; Store data in R11 for process Back-up R11 Get the LSB digit Store units digit

; Restore R11 for shifting ; Back-up after shifting ; Store units digit

; Store tens digit

; Store hundreds digit

pop R12 pop R11 ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++ Get2_hex ;Store 2 Hexadicimal at address: R6 call #INCHAR_UART call #OUTA_UART call #Ascii2HexDigit rlam #0x04,R4 mov R4,0(R6) call #INCHAR_UART call #OUTA_UART call #Ascii2HexDigit add R4,0(R6) ret ;+++++++++++++++++++++++++++++++++++++++++++ HexDigit2Acsii Num1 cmp #0x0A, R4

; 0X h => X0 h ; store #1

; store next HEX ( XX h)

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

jl Num2 jmp Char1c Num2 cmp #0x00, R4 jge con1 jmp retn1 con1 add #0x30, R4 jmp retn1 Char1c cmp #0x0A, R4 jge Char2c jmp retn1 Char2c cmp #0x10, R4 jl con2 con2 add #0x37, R4 jmp retn1 retn1 ret ;+++++++++++++++++++++++++++++++++++++++++++++++++++++ Ascii2HexDigit ; Only accept the uppercase for character from A -> F ;-----------------------------------------------------;------------| R4 in range 0 - 9 |--------------------Number1 cmp #0x3A, R4 ; if R4 < A (10) jl Number2 ; move to Number2 jmp Char1 ; else move to Char1 (R4 not a number) Number2 cmp #0x30, R4 jge convert1 jmp retn ; R4 > 0 ; ; Finish convert

convert1 sub #0x30, R4 jmp retn ;------------| R4 in range A - F |--------------------Char1 cmp #0x41, R4 ; if R4 >= A jge Char2 ; move to Char2 jmp retn Char2 cmp #0x47, R4 jl convert2 convert2 sub #0x37, R4 jmp retn retn ret ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Display_digit ;----------------------------------------------------------;Display value in R9 with the &Offsets for the LED push R8; push R5; mov.w #LCDM3, R5 ; Initial LCD mapping ; if R4 < G (mean: R4 <= F)

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

mov.w #LCDencode, R8 table to R8 add.w R9,R8 of R8 add &Offsets,R5 to display mov.b 0(R8), 0(R5)

; Initialize lookup ; Offset address ; Choose the LED ; Display Digit

pop R5 pop R8 ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ OUTA_UART push R5 mov.b &IFG2,R5 and.b #0x02,R5 cmp.b #0x00,R5 jz lpa mov.b R4,&UCA0TXBUF pop R5 ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lpa INCHAR_UART push R5 mov.b &IFG2,R5 and.b #0x01,R5 cmp.b #0x00,R5 jz lpb mov.b &UCA0RXBUF,R4 pop R5 ret ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lpb Init_UART mov.b #0x30,&P2SEL mov.b #0x00,&UCA0CTL0 mov.b #0x41,&UCA0CTL1 mov.b #0x00,&UCA0BR1 mov.b #0x03,&UCA0BR0 mov.b #0x06,&UCA0MCTL mov.b #0x00,&UCA0STAT mov.b #0x40,&UCA0CTL1 mov.b #0x00,&IE2 ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Init_LCD mov.b #0x00, R6 mov.w #LCDM3, R5 mov.b #0x00, R7 lpt mov.b R7, 0(R5) inc.w R5 inc.b R6 cmp.b LCD_SIZE, R6 jnz lpt

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

mov.b #0x1C, &P5SEL mov.b #0x00, &LCDAVCTL0 mov.b #0x7E, &LCDAPCTL0 mov.b #0x7d, &LCDACTL ret ;-------------------| Stack Pointer definition |-------------------------------.global __STACK_END .sect .stack ;------------| Interrupt Vectors |---------------------------------------------.sect ".reset" ; MSP430 RESET Vector .short RESET

Procedure 5:
.sect initialized LCDencode .byte .byte .cdecls C,LIST,"msp430.h" ".sysmem" ; Include device header file ; data ram for ; Encoded digits

.byte 0x5F,0x06,0x6B,0x2F,0x36 0x3D,0x7D,0x07,0x7F,0x3F 0x77,0x7C,0x68,0x6E,0x79,0x71,0x20,0x00

Hex .word 0x0000,0x0000 RESULT .word 0x0000

; Store result value

Offsets .byte 0x00; Dis_digit .byte 0x00,0x00,0x00,0x00 ; Temporary for spliting Result into digit LCD_SIZE .byte 11 ; eleven bytes needed by the LCD ;------------------------------------------------------------------------------.text ; Assemble into program memory .retain ; Override ELF conditional linking .retainrefs ; Additionally retain any sections ;------------------------------------------------------------------------------RESET mov.w #__STACK_END,SP ; Initialize stackpointer StopWDT mov.w #WDTPW|WDTHOLD,&WDTCTL ; Stop watchdog timer ;------- Initial --------------------------------------------------------------call #Init_LCD call #Init_UART mov #Dis_digit,R10 ; Use R10 as address for display digit mov #Hex,R6 ; Use R6 as address for stores value ;--------------------| Main loop here |----------------------------------------Mainloop call #Get2_hex call #Dect_arithmetic add #0x02,R6 call #Get2_hex mov #0x3D,R4 call #OUTA_UART ; Gather 2 first HEX ; Store in R14 ; R6 point to next .WORD ; Gather 2 last HEX ; Print out "="

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

call #Arithmetic call #Split_digit call #Dis_hyper Hyperterminal call #Dis_LCD ;------- New line in Hyperterminal mov #0x0D, R4 call #OUTA_UART mov #0x0A, R4 call #OUTA_UART jmp Mainloop ; Display on LCD ; Split into digits ; Display on

;----------------------------------Dect_arithmetic ; Store arithmetic in R14 call #INCHAR_UART call #OUTA_UART cmp jeq cmp jeq #0x2A,R4 multi_arith #0x2D,R4 sub_arith ; * ; ; +

jmp add_arith sub_arith mov #0x01,R14; jmp quit_dectecion multi_arith mov #0x02,R14; * jmp quit_dectecion add_arith mov #0x00,R14; +

quit_dectecion ret ;++++++++++++++++++++++++++++++++++++ Arithmetic cmp #0x02,R14 jeq call_mult cmp #0x01,R14 jeq call_sub cmp #0x00,R14 call_plus call_mult call_sub call #Plus_arithmetic jmp finish_arith call #Multi_arithmetic jmp finish_arith call #Sub_arithmetic

; SUB arithmetic

finish_arith ret ;------------------------------------------------------------------------------Plus_arithmetic mov #Hex,R6

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

mov 0(R6),&RESULT add 2(R6),&RESULT ret ;++++++++++++++++++++++++++ Sub_arithmetic mov #Hex,R6 pointer cmp 2(R6),0(R6); jl minus_sign 0(R6) mov 0(R6),&RESULT sub 2(R6),&RESULT mov.b #0x00,R15 plus sign jmp finish_sub minus_sign

; Reset R6 ; If 2(R6) >

; Use R15 = 0 for

mov 2(R6),&RESULT sub 0(R6),&RESULT mov #0x01,R15 ; R15 = 1 for minus sign finish_sub ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Multi_arithmetic mov #Hex,R6 ; Reset R6 pointer mov #0x0000,&RESULT ; Empty the results push R9 ; Use R9 push R10 ; and R10 as temporary lp8 mov #0x08,R10 ; Back-up 2nd term ; If the current digit = 1

mult_start mov 2(R6),R9 bic #0xFE,R9 cmp #0x01,R9 jeq implement jmp mult_skip implement add 0(R6),&RESULT mult_skip rla 0(R6) rra 2(R6) dec R10 cmp #0x00,R10 jne mult_start

; implement binary adding

pop R9 pop R10 ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Dis_LCD ;----------- Display on LCD mov.b 0(R10),R9 mov #0x00,&Offsets #1 Hex call #Display_digit

; Display on LCD

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

mov.b 1(R10),R9; mov #0x01,&Offsets #2 Hex call #Display_digit cmp jeq cmp jeq cmp jeq lcd_sub mov.b #0x11,R9; LCD led mov #0x03,&Offsets call #Display_digit cmp #0x00,R15 jeq plus_sign mov.b #0x10,R9; #3 LCD Led mov #0x02,&Offsets call #Display_digit jmp dis_next plus_sign dis_next mov.b #0x11,R9; mov #0x02,&Offsets call #Display_digit jmp exit_lcd #0x01,R14 lcd_sub #0x00,R14 lcd_plus #0x02,R14 lcd_multi ; Sub ; ADD

; Display on LCD

; Turn Off the #4

; Plus sign ; Display "-" on

; Turn Off the LCD ; Display on LCD #3 Hex

lcd_plus mov.b 2(R10),R9; mov #0x02,&Offsets #2 Hex call #Display_digit jmp exit_lcd lcd_multi mov.b 2(R10),R9; mov #0x02,&Offsets #3 Hex call #Display_digit mov.b 3(R10),R9; mov #0x03,&Offsets #4 Hex call #Display_digit exit_lcd ret Dis_hyper ;----------- Display on Hyperterminal cmp #0x02,R14 jeq hyper_multi cmp #0x01,R14; jeq hyper_sub ; Display on LCD ; Display on LCD

; Display on LCD

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

cmp #0x00,R14; + jeq hyper_add hyper_multi mov.b 3(R10),R4; call #HexDigit2Acsii call #OUTA_UART mov.b 2(R10),R4; call #HexDigit2Acsii call #OUTA_UART jmp Not_minus hyper_sub cmp #0x00,R15 jeq Not_minus mov #0x2D,R4 call #OUTA_UART jmp Not_minus hyper_add mov.b 2(R10),R4; call #HexDigit2Acsii call #OUTA_UART Not_minus mov.b 1(R10),R4; call #HexDigit2Acsii call #OUTA_UART mov.b 0(R10),R4; call #HexDigit2Acsii call #OUTA_UART ret ;+++++++++++++++++++++++++++++++++++++++++ Split_digit push R11 push R12 mov #Dis_digit,R10 mov &RESULT,R11 mov R11,R12 bic #0xFFF0,R11 mov.b R11,0(R10) mov R12,R11 rram #0x04,R11 mov R11,R12 bic #0xFFF0,R11 mov.b R11,1(R10) mov R12,R11 rram #0x04,R11 mov R11,R12 bic #0xFFF0,R11 mov.b R11,2(R10) mov R12,R11 ; Display "-"

; Set R10 as Address ; ; ; ; Store data in R11 for process Back-up R11 Get the LSB digit Store units digit

; Restore R11 for shifting ; Back-up after shifting ; Store units digit

; Store tens digit

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

rram #0x04,R11 mov R11,R12 bic #0xFFF0,R11 mov.b R11,3(R10) pop R12 pop R11 ret

; Store hundreds digit

Get2_hex ;Store 2 Hexadicimal at address: R6 call #INCHAR_UART call #OUTA_UART call #Ascii2HexDigit rlam #0x04,R4 mov R4,0(R6) call #INCHAR_UART call #OUTA_UART call #Ascii2HexDigit add R4,0(R6) ret ;--------------------------------HexDigit2Acsii Num1 cmp #0x0A, R4 jl Num2 jmp Char1c Num2 cmp #0x00, R4 jge con1 jmp retn1 con1 add #0x30, R4 jmp retn1 Char1c cmp #0x0A, R4 jge Char2c jmp retn1 Char2c cmp #0x10, R4 jl con2 con2 add #0x37, R4 jmp retn1 retn1 ret ;+++++++++++++++++++++++++++++++++++++++++++++++++++++

; 0X h => X0 h ; store #1

; store next HEX ( XX h)

Ascii2HexDigit ; Only accept the uppercase for character from A -> F ;------------------------------------------------------------------------------;------------ R4 in range 0 - 9 Number1 cmp #0x3A, R4 ; if R4 < A (10) jl Number2 ; move to Number2 jmp Char1 ; else move to Char1 (R4 not a number)

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

Number2 cmp #0x30, R4 jge convert1 jmp retn convert1 sub #0x30, R4 jmp retn ;------------ R4 in range A - F Char1 cmp #0x41, R4 jge Char2 jmp retn Char2 cmp #0x47, R4 jl convert2 convert2 sub #0x37, R4 jmp retn

; R4 > 0 ; ; Finish convert

; if R4 >= A ; move to Char2 ; if R4 < G (mean: R4 <= F)

retn ret ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Display_digit ;----------------------------------------------------------;Display value in R9 with the &Offsets for the LED push R8; push R5; mov.w #LCDM3, R5 ; Initial LCD mapping mov.w #LCDencode, R8 ; Initialize lookup table to R8 add.w R9,R8 of R8 add &Offsets,R5 to display mov.b 0(R8), 0(R5) ; Display Digit pop R5 pop R8 ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ OUTA_UART push R5 mov.b &IFG2,R5 and.b #0x02,R5 cmp.b #0x00,R5 jz lpa mov.b R4,&UCA0TXBUF pop R5 ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lpa INCHAR_UART lpb push R5 mov.b &IFG2,R5 and.b #0x01,R5 cmp.b #0x00,R5 jz lpb mov.b &UCA0RXBUF,R4 ; Choose the LED ; Offset address

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

pop R5 ret ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Init_UART mov.b #0x30,&P2SEL mov.b #0x00,&UCA0CTL0 mov.b #0x41,&UCA0CTL1 mov.b #0x00,&UCA0BR1 mov.b #0x03,&UCA0BR0 mov.b #0x06,&UCA0MCTL mov.b #0x00,&UCA0STAT mov.b #0x40,&UCA0CTL1 mov.b #0x00,&IE2 ret ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Init_LCD mov.b #0x00, R6 mov.w #LCDM3, R5 mov.b #0x00, R7 lpt mov.b R7, 0(R5) inc.w R5 inc.b R6 cmp.b LCD_SIZE, R6 jnz lpt mov.b #0x1C, &P5SEL mov.b #0x00, &LCDAVCTL0 mov.b #0x7E, &LCDAPCTL0 mov.b #0x7d, &LCDACTL ret ;-----------| Stack Pointer definition |---------------------------------------.global __STACK_END .sect .stack ;-----------| Interrupt Vectors |-------------------------------------------.sect ".reset" ; MSP430 RESET Vector .short RESET

SUMMARY:
After Laboratory 5, we have reviewed about the serial communications and the on board Universal Asynchronous Receiver and Transmitter (UART) and LCD display. Besides, we continue to develop the programing skill on C language program and assembly language program. In this lab, we had the difficulties when we translated the C language to assembly language. Fortunately, we solved our problems with the help of textbook. Also, it is the chance for all members of our group deeply understood about the instructions set used in assembly code. Moreover, we can apply what we have learned in class to the practical module and realize the differences between two aspects.

Lab 5 Hexadecimal Calculator using MSP-EXP430FG4618/2013 Experimenter Board

Das könnte Ihnen auch gefallen