In this exercise, you will validate the installation of the the IDE, the simulator, and assembler (packaged with the compiler).
You will create a simple program that reads the input pins of Port A and replicates those values to Port B.
The program sets all bits of the pin direction control register DDRA to low
so that the eight pins of Port A operate as inputs, with the expectation to be driven from an external source.
The program sets all bits of the DDRB register to high
so that the eight pins operate as outputs.
PINA refers to the input value of Port A. PORTB refers to the output value being driving to Port B.
You will run this code on a simulator. You will manually manipulate the value of PINA in the simulation, emulate external drive of the physical pins of Port A.
Download and install
MPLAB X : https://www.microchip.com/en-us/tools-resources/develop/mplab-x-ide
XC8 compiler: https://www.microchip.com/en-us/tools-resources/develop/mplab-xc-compilers#tabs
Create a folder to store your projects (each project will live as a sub-directory under this)
In Linux:
mkdir -p ~/MPLABXProjects
Create a project in the projects directory.
In the following example it will be ~/MPLABXProjects/first_cmpe311.X
Start MPLAB IDE
Menu New Project…
Standalone
Toolchain: xc8 compiler
Project Name: first_cmpe311
Check “Set as main project” if not already selected
On the left panel, Right Click Sources
New >
Assembly File .s …
For the file name, use
main
The file extension .s is appended automatically so don’t type main.s
Here are the contents of the file that you should enter.
;; main.s
.equ PINA , 0x00
.equ DDRA , 0x01
.equ PORTA , 0x02
.equ PINB , 0x03
.equ DDRB , 0x04
.equ PORTB , 0x05
.globl main
.org 0x0000
main:
ldi r16,0x00
out DDRA, r16 ;set PORTA direction as input
ldi r16,0xFF
out DDRB, r16 ;set PORTB direction as output
loop:
in r16, PINA ;copies input value seen at PORTA to r16
out PORTB, r16 ;copies r16 to PORTB output
rjmp loop
I/O PORT (PORTA)
and I/O PORT (PORTB)
Menu Debug -> Debug Main Project
In main.s, (double) click on a line number within the “main loop” to “break” the execution
Find PortA in the I/O View to alter the input PINA
Set some bits in PINA, or type in a value
Inspect PortB: PORTB…it should be a copy of the valued copied via r16 once you resume
Resume/Step through the program to see the updated output to PORTB
You can view the program memory to see the binary contents. The view will also display what instruction can be interpreted from the memory contents at each location.
Window->Target Memory View-> Program Memory