FUCULTY OF ENGINEERING AND TECHNOLOGY LAB 01 MODULE: ADVANCED DIGITAL SYSTEM DESIGN CODE: CTEN 511 LAB TITLE: MPLAB INTRODUCTION NAME: KARABO DAVIS BOGAISANG STUDENT ID: 14001009 PROGRAM: BENG COMPUTER AND TELECOMMUNICATIONS Abstract The purpose of this lab was to introduce the environment of MPLAB X IDE and assembly language through some simple examples of programming using the PIC24 family. Addition and subtraction were used as examples for programming in this lab. Introduction MPLAB X IDE is comprehensive software that has editing, project management and design in it. It used in development of embedded system application using microcontroller Microchip PIC. Assembly language is a language that used for programming microcontroller. Before programming, these assembly language instructions into microcontroller memory, it has to convert to machine language so that microcontroller easily understands. This conversion is done by software called assembler in MPLAB X. Objectives • • To use MPLAB X to simulate the PIC24 assembly language program in the mptst_word project to become familiar with the MPLAB X environment Do simple programming tasks using PIC24 assembly language Apparatus • MAPLAB X Procedure Task 1 Open the mptst_word project, and build all to assemble the project. Open the program memory to view program memory and file registers. Open watch window then add u16_i, u16_j,u16_k and WREG0, WREG1, WREG2. Using the simulator to single step the program, both the memory locations and watched locations observed as the instructions are being executed. Modify the value of i=2047 to i=1009. Building and simulating the program, the values watch contents and file register contents were observed. Task 2 : myadd Copy the project mptst_word and name the copy myadd. Few editing is done to make the program to add a four digit hexa number to another four digit hexa number. Reserving space for u16_lsp, u16_msp and u16_sum. Then the code for adding the two numbers was added as it will be shown in the code for myadd. Watch contents and file register contents were observed and the outcome is displayed in the results section. code for myadd ; ; Just check out MPLAB .include "p24Fxxxx.inc" .global __reset ;The label for the first line of code. .bss ;unitialized data section ;;These start at location 0x0800 because 0-0x07FF reserved for SFRs u16_lsp: .space 2 ;Allocating space (in bytes) to variable. u16_msp: .space 2 ;Allocating space (in bytes) to variable. u16_sum: .space 2 ;Allocating space (in bytes) to variable. ;.............................................................................. ;Code Section in Program Memory ;.............................................................................. .text ;Start of Code section __reset: ; first instruction located at __reset label 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 ;User Code starts here. ; C Program equivalent ; #define avalue 1009 ; #define bvalue 1400 ; uint16 lsp,msp,sum; ; ; .equ avalue,0x3F1 ;1009 .equ bvalue,0x578 ;1400 ;u16_lsp = avalue; /* myvalue = 1009 */ mov #avalue, W0 ; w0 = 1009 (w0 is wreg) mov wreg,u16_lsp ; i = 1009 (W0 is wreg) ;u16_msp = bvalue; /* myvalue = 1400 */ mov #bvalue, W0 ;w0 = 1400 mov wreg,u16_msp ;u16_msp=1400 ; u16_sum = u16_lsp + u16_msp mov u16_lsp,wreg ; w0 = u16_lsp add u16_msp,wreg ; w0 = w0+u16_msp mov wreg,u16_sum ; u16_sum = 0 done: goto .end done ;Place holder for last line of executed code ;End of program code in this file Task 3: mysub Project named mysub is created by applying almost the same procedure from task 2. For this particular one was to add and subtract a two digit hexa decimal number. Reservation of SFRs and adding the code for addition and subtraction is shown in the code for mysub. Outcome is displayed in the results section. code for mysub ; ; Just check out MPLAB .include "p24Fxxxx.inc" .global __reset ;The label for the first line of code. .bss ;unitialized data section ;;These start at location 0x0800 because 0-0x07FF reserved for SFRs u8_i: .space 2 ;Allocating space (in bytes) to variable. u8_j: .space 2 ;Allocating space (in bytes) to variable. u8_k: .space 2 ;Allocating space (in bytes) to variable. u8_l: .space 2 ;Allocating space (in bytes) to variable. u8_m: .space 2 ;Allocating space (in bytes) to variable. ;.............................................................................. ;Code Section in Program Memory ;.............................................................................. .text ;Start of Code section __reset: ; first instruction located at __reset label 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 ;User Code starts here. ; C Program equivalent ; #define avalue 2047 ; uint16 u8_i,u8_j,u8_k,u8_l,u8_m; ; ; i = avalue; /* myvalue = 2047 (0x7FF) */ ; i = i + 1; /* i++, i = 2048 (0x800) */ ; j = i; /* j is 2048 (0x0800) */ ; j = j - 1; /* j--, j is 2047 */ ; k = j + i; /* k = 4095 (0x0FFF) */ ; .equ avalue, 0x9 ;9 .equ bvalue, 0xA ;10 .equ cvalue, 0x1E ;30 ;u8_i = avalue; /* myvalue = 09 */ mov #avalue, w0 ; w0 = 09 (w0 is wreg) mov wreg,u8_i ; u8_i = 09 ;u8_j = avalue; /* myvalue = 10 */ mov #bvalue, w0 ;wreg = 10 mov wreg, u8_j ;u8_j = 10 ;u8_k = avalue; /* myvalue = 10 */ mov #cvalue, w0 ;wreg = 30 mov wreg, u8_k ;u8_k = 30 ; u8_l = u8_i + u8_j mov u8_i,wreg add u8_j,wreg mov wreg,u8_l ; w0 = u8_i ; w0 = w0+u8_j ; u8_l = 0 ; u8_m = u8_k - u8_l mov u8_l,wreg sub u8_k,wreg mov wreg,u8_m ; w0 = u8_l ; wreg = u8_k - w0 ; u8_m = 0 done: goto done ;Place holder for last line of executed code ;End of program code in this file .end Results Task 1 View of program memory View of file register Values of watches being observed Values of file registers being observed watch contents for i = 1009 file register contents for i =1009 watch contents for myadd file register contents for myadd watch contents for my sub file register contents for mysub Analysis Table showing convertion from hexa decimal to decimal and otherwise. HEXADECIMAL 0 DECIMAL 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 A 10 B 11 C 12 D 13 E 14 F 15 To convert from decimal to hexa decimal, the value is divided by 16 until getting 0 as the result, and the value of the hexa decimal is read from remainders starting from the bottom. DIVISION RESULT REMAINDER (in HEX) 1009/ 16 63 1 63 / 16 3 15 3 / 16 0 3 Answer taking reminders from the bottom 3151 but 15 =F in hexadecimal Answer =(3F1)hex Converting back to decimal, the values are multiplied by 16 considering the bit where the are. For (3F1)hex considering F =15 (3*16**2+15*16**1+1*16**0) = 1009 Converting 578 to decimal (5*16**2+7*16**1+8*16**0) = 1400 Converting 1E E = 14 (1*16**1+14*16**0) = 30 Converting 13 to decimal (1*16**1+3*16**0) = 19 Discussion It is quite a challenge to differiate 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 were it is stored, it is quite an experience and a good way to learn the language and its opearation. Conclusion The Objective of this lab we clearly meet, the intern was to introduce the environment of MPLABX. Doing some simple programming to get fimiliar 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. REFERENCES [1] T. Wilmshurst, Designing Enbedded Systems withPIC Microcontrollers, Principles and Applications, Second Edition, Newnes, 2009 [2] R. Arunthavanathan, MPLAB programming, Research gate, 23 November 2017