LAB1
LAB1
ENGINEERING
CTEN 412: MICROCONTROLLERS
LAB 1
By
OTSWELETSE N DITIRELO
17000044
ELECTRICAL AND ELECTRONICS
DR N.DITSHEGO
SUBMISSION DATE: 30-10-2020
1
ABSTRACT
The purpose of this lab was to is to introduce student to the MPLAB environment and also to
convert a C program that performs 8/16-bit unsigned operations into PIC24 assembly language
using the MPLAB X IDE environment .This lab contained 3 tasks where task 1 was to open the
mptst_word project ,assemble it and observe the program memory and the file registers.Task 2
and task 3 was to translate the C program to assembly instructions.All objectives were
successfully met.
OBJECTIVE
The objective of this lab was to;
Introduce students to the MPLAB environment
Convert a C program into PIC24 assembly language in MPLAB
PROCEDURE
TASK 1
Open MPLAB X and the mptst_word project was opened and assembled.
The program starting at 0x0200 was viewed on the program memory window and the
take screenshot of the program was taken.
Program’s data on the file registers window was viewed and the screenshot was taken.
TASK 2;myadd.s
Copy of the mptst_word project was created.
To do so was done by right clicking on the mptst_word project in the Projects window
and copy was selected ,then myadd was added as the project name.
Mpts_word.s was open and the file>source file was selected,right clicked on
mptst_word.s and Remove was selectedfrom project.After ,right-clicked on source on
source file and Add existing item was selected and myadd.s was selected.
The following C program was witten in assembly language.
NB given that BIUST student ID is 17000044
uint16_t u16_lsp;
uint16_t u16_msp;
uint16_t u16_sum;
u16_lsp = 0xY3Y2Y1Y0; // Note that this is a hex value /*0044*/
u16_msp = 0xY7Y6Y5Y4; // Same as above /*1700*/
u16_sum = u16_lsp + u16_msp; /*1700+0044*/
2
TASK 3;mysub
Mpst_word was copied using the procedure given in task 2 with the corresponding assembly
language file named mysub.s.Using the digit of BIUST student ID,an assembly language
program was written that does the following C program .
uint8_t u8_i;
uint8_t u8_j;
uint8_t u8_k;
uint8_t u8_l;
uint8_t u8_m;
.include "p24ep128gp202.inc"
3
i: .space 2 ;Allocating space (in bytes) to variable.
j: .space 2 ;Allocating space (in bytes) to variable.
k: .space 2 ;Allocating space (in bytes) to variable.
;..............................................................................
;Code Section in Program Memory
;..............................................................................
4
mov #avalue, w0 ; w0 = 2047 (w0 is wreg)
mov wreg,i ; i = 2047
; i = i + 1;
inc i ;i=i+1
;j=i
mov i,wreg ; w0 = i
mov wreg,j ; j = w0
; j = j - 1; /* j--, j is 100 */
dec j ; j= j - 1
;k=j+i
mov i,wreg ; w0 = i
add j,wreg ; w0 = w0+j
mov wreg,k ;k=0
done:
goto done ;Place holder for last line of executed code
5
View of a file register window of program data at 0x1000
The added watch window is shown below with the type,address and the value
6
BINARY
0000 0111 1111 1111
ADDITION OF TWO NUMBERS
DECIMAL HEXA-DECIMAL BINARY
2048 0800 0000 1000 0000 0000
+2047 + 07FF + 0000 0111 1111 1111
4095 0FFF 0000 1111 1111 1111
The calculation above show the expected results hence it matches with the results from the file
registers.
TASK 2;myadd.s
CODE
;
; Just check out MPLAB
.include "p24ep128gp202.inc"
;..............................................................................
;Code Section in Program Memory
;..............................................................................
7
mov #__SP_init, w15 ;Initalize the Stack Pointer
mov #__SPLIM_init,W0
mov W0, SPLIM ;Initialize the stack limit register
;__SP_init set by linker to be after allocated data
;uint16_t u16_lsp;
;uint16_t u16_msp;
;uint16_t u16_sum;
;u16_lsp = 0xY3Y2Y1Y0; // Note that this is a hex value /*lsp=0x1700 (5888 decimal)*/
;u16_msp = 0xY7Y6Y5Y4; // Same as above /*msp=0x0044 (68 decimal)*/
;u16_sum = u16_lsp + u16_msp; /*sum=0x1744 (5956 decimal)*/
8
done:
goto done ;Place holder for last line of executed code
9
Given the lsp value as 1700 and msp as 0044 in hexadecimal
1700 0044
IN DECIMAL IN DECIMAL
5888 68
IN BINARY IN BINARY
0001 0111 0000 0000 0000 0000 0100 0100
ADDITION OF NUMBERS
IN HEXA-DECIMAL IN DECIMAL IN BINARY
1700 5888 0001 0111 0000 0000
+0044 +0068 +0000 0000 0100 0100
1700 5956 0001 0111 0100 0100
The calculated results correspond with the values found in watches and in the file registers.
TASK 3;mysub
Mysub.s code
;
; Just check out MPLAB
.include "P24EP128GP202.inc"
10
;..............................................................................
;Code Section in Program Memory
;..............................................................................
;u8_i = avalue;
mov #0x0000, w0 ;initialising w0 to 0x0000.
mov.b #avalue, w0 ; w0 = 17 (w0 is wreg)
mov.b wreg,u8_i ; u8_i = 17
11
; u8_j = bvalue;
mov.b #bvalue, w0 ;w0 = 00
mov.b wreg,u8_j ;u8_j = 00
; u8_k = cvalue;
mov.b #cvalue, w0 ; w0=44
mov.b wreg,u8_k ; u8_k = 44
12
View from file registers
u8_l =u8_i+u8_j
DECIMAL HEXA-DECIMAL BINARY
17 11 10001
+00 +00 +10001
17 11 100010
u8_m=u8_k-u8_l
DECIMAL HEXA-DECIMAL BINARY
44 2C 101100
-17 -11 -100010
27 1B 011011
From the Task the challenge was into subtracting binary numbers.Also values of u8_k and u8_l
didn’t appear in the file register but they did appeared in the watches window,but the calculated
answers correspond to the file register values.
DISCUSSION
13
It is quite a challenge to differentiate between data address and the actual data. Having to move
the value from data address where it is stored to a temporary storage for the case where watches
were used for the purpose of arithmetic. Then move the result to the address to where it is stored,
it is quite an experience and a good way to learn the language and its operation.
CONCLUSION
The Objective of this lab were clearly met hence the lab was successful,the intern was to
introduce the environment of MPLAB X and aslo to learn how to convert C program to the
assembly laungage . Doing some simple programming to get familiar with the coding of the
environment in which it is assembly language. The two exercises given for adding and
subtracting hexa-decimal numbers is quite a good example for introduction of assembly
language.
14