0% found this document useful (0 votes)
47 views

MPLab Asm

MPLAB X IDE Installation guide

Uploaded by

ayeshanadeem3690
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

MPLab Asm

MPLAB X IDE Installation guide

Uploaded by

ayeshanadeem3690
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

AVR Assembly Programming in MPLAB X

Step by Step Tutorial

Sepehr Naimi
BIHE University
3/18/2024
Contents
Dedication ................................................................................................................................... 2
Introduction ................................................................................................................................. 2
Downloading and Installing MPLAB X IDE and AVR Assembler ......................................................... 3
Adding AVR Assembler to MPLAB toolchain ................................................................................... 8
Creating the first project ............................................................................................................. 10
Writing the first Assembly program ............................................................................................. 14
Building...................................................................................................................................... 14
Debugging.................................................................................................................................. 15
Monitoring the CPU Registers...................................................................................................... 18
Using Breakpoints ....................................................................................................................... 19

Dedication
The tutorial is dedicated to the memory of Prof. Muhammad Ali Mazidi who wrote valuable books
about hardware and embedded systems and tried his best to serve human beings.

Introduction
This tutorial will teach you how to write, compile, and trace a simple Assembly program for AVR
microcontrollers using MPLAB X.

Page 2
Downloading and Installing MPLAB X IDE and AVR Assembler
1. Download AVR Assembler from the following link:

https://nicerland.com/eduFiles/AVR/Software/avrasm2.zip

2. Unzip the downloaded zip file and place avrasm2.exe in “C:\” or anywhere you prefer.
3. Microchip provides the MPLAB X IDE for Windows, Linux, and Mac OS for free. Download the
proper MPLAB X from the Microchip website:

https://www.microchip.com/en-us/tools-resources/develop/mplab-x-ide

4. Run the downloaded program to install the MPLAB X IDE. Installing the MPLAB X is straight
forward.
A. The first page is just a welcome page. Press Next.

Page 3
B. The second page is an agreement. Choose “I accept the agreement” and press Next.

C. In the third page, you can choose the path for installing the MPLAB X. Press Next.

Page 4
D. In the page, you can choose the chips you want to use. To install the IDE faster, uncheck
the MCUs except the 8-bit MCUs.

E. Press Next.

Page 5
F. Wait for a few minutes until the IDE installs.

G. Now, the installation is completed. Press Finish to close the installer and the MPLAB X
IDE opens.

Page 6
5. Windows Firewall might ask if MPLAB can access to the network or not. Choose Allow
access.

6. Microchip automatically installs Microchip Offline Help. Wait a few seconds until the install
finishes.

7. Now, the MPLAB X IDE opens.

Page 7
Adding AVR Assembler to MPLAB toolchain
8. Go to the Tools menu and choose Options.

9. Click on Embedded, choose Build Tools, and click on the Add… button.

Page 8
10. Type C:\ if you placed avrasm2.exe in the root of C. (or choose the path for avrasm2.exe by
clicking on the Browse button.) Then, press OK.

11. avrasm2 should be added to the toolchain list. Press Apply and then OK.

Page 9
Creating the first project
1. Go to the File menu and choose New Project.

2. In the opened dialog, choose Standalone Project and press Next.

Page 10
3. In the dialog:
a. Select 8-bit AVR MCUs as the device family.
b. Choose ATmega328P (or any other Chip you want to use)
c. Select Simulator.
d. Press Next.

4. Click on the + next to avrasm2 to expand and choose avrasm2 as the compiler. Then, press
Next.

Page 11
5. Type togglePrj as the Project Name. Choose the location where the project is being saved,
by clicking on the Browse button. Then, click on the Finish button. The project is created
after a few seconds.

6. To add an assembly file to the project:


a. Right click on Source Files. Choose New and then Other.

Page 12
b. Click on Assembler and choose AssemblyFile.asm. Then, click Next.

c. Type a name, e.g. main, as the File Name. Then, press Finish.

Page 13
Writing the first Assembly program
7. Type the following program.

;
; toggleProject
;
LDI R16,0xFF
OUT DDRB,R16

L1: OUT PORTB,R16


LDI R20,0
OUT PORTB,R20
RJMP L1

Building
8. Press F11 to assemble, or choose Build Main Project from the Production menu. The results
of assembling the program are shown in the Output window.

Page 14
Debugging
9. To add a breakpoint, click on the line number next to the “OUT DDRB,R16” (number 5 in the
figure). A red square appears on the left side of the " OUT DDRB,R16" instruction and the line
becomes red.

10. To start debugging choose Debug Main Project from the Debug menu. Now a green cursor
is on the “OUT DDRB,R16” instruction of the program and the IDE is ready to debug the
program.

Page 15
11. To monitor the peripherals, including the I/O ports, in the Window menu, choose
Debugging and then click on IO View.

12. To monitor the values of DDRB and PORTB, in the IO View tab scroll down using the scroll
bar and click on I/O Port (PORTB). The values of the related registers (DDRB, PINB, and
PORTB) will be shown below.

Page 16
13. To execute the instructions line by line, press F8 or click on the Step Over icon or click on
Step Over from the Debug menu.

Step Into vs. Step Over


Both F8 (Step over) and F7 (Step into) execute one instruction and go to the next instruction. But
they work differently when the cursor is on a function call. If the cursor is on the function call,
Step into goes into the first instruction of the function, but Step Over executes the whole function
and goes to the next instruction.

Step Out
If the execution is in a function, you can execute the function to the end by pressing the Step Out.

Run to Cursor
You can put the cursor on an instruction and then press the Run to Cursor button. In the case, the
program runs until it reaches the instruction which the cursor is on it.

14. Press F8 (Step Over) a few times and see the PORTB register changes in the IO View.

Page 17
Monitoring the CPU Registers
15. To monitor the CPU registers, including R0-R31 and the Program Counter, choose Target
Memory Views from the Window menu and click on I/O Memory (SFRs).

16. In the I/O Memory (SFRs) tab, scroll down to see the value of R16.

Page 18
Using Breakpoints
17. If you want to debug a portion of a program, add a breakpoint to the beginning of this part
of the code and press the run button. The IDE runs the program and when it reaches the
breakpoint, it stops running and the green cursor is shown on the breakpoint line. Below,
you see the steps in detail.
a. click on the line number of "OUT PORTB,R16" instruction.
b. click on the line number of " OUT PORTB,R20" instruction.
c. Press F5 or click on Continue from the Debug menu. The IDE runs the program until
it reaches the breakpoint. Now, you can continue debugging from the breakpoint
using the Step into and Step over buttons. Press F5 again to run to next breakpoint.
d. Using Finish Debugger Session from the Debug menu, you can stop debugging
whenever you want.

Page 19

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy