0% found this document useful (0 votes)
130 views14 pages

PIC18 Assembly Language Programming

This document is a laboratory manual for a course on microcontroller and interfaces. It outlines the objectives of learning assembly language programming for a PIC microcontroller, including conditional and unconditional branching instructions, looping, calling subroutines, and using the stack. It provides examples of coding various PIC assembly language instructions to perform tasks like input/output, arithmetic, logic operations, and program flow control. The equipment used includes an MPLAB IDE, PIC microcontrollers, resistors, LEDs, a breadboard, and programmer.

Uploaded by

xyzzyz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
130 views14 pages

PIC18 Assembly Language Programming

This document is a laboratory manual for a course on microcontroller and interfaces. It outlines the objectives of learning assembly language programming for a PIC microcontroller, including conditional and unconditional branching instructions, looping, calling subroutines, and using the stack. It provides examples of coding various PIC assembly language instructions to perform tasks like input/output, arithmetic, logic operations, and program flow control. The equipment used includes an MPLAB IDE, PIC microcontrollers, resistors, LEDs, a breadboard, and programmer.

Uploaded by

xyzzyz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 14

ENT 265 Microcontroller and Interfaces (Biomedical Programme)

LABORATORY MANUAL
Assembly Language Programming, PIC18 Input Output
Interfacing, Arithmetic Operations & Logic Operations

1. OBJECTIVES:
1.1 Code PIC Assembly language conditional branch instructions
1.2 Explain conditions that determine each conditional branch instruction
1.3 Calculate target addresses for conditional branch instructions
1.4 To learn how to code PIC programs to generate time delay
1.5 To learn how to code PIC Assembly language instructions to create loops
1.6 To describe the stack and its use in the subroutines
1.7 To program the I/O Port by using MPLAB assembly language.
1.8 To familiarize with hardware connections, registers configuration and I/O
ports.
1.9 To learn on how to code addition, subtraction, multiplication and division
instructions for unsigned data.
1.10 To learn how to code PIC assembly language logic instructions AND, OR,
and EX-OR.
1.11 To learn how to use PIC logic instruction for bit manipulation.
1.12 To learn how to use compare and skip instruction for program control.

2. COMPONENTS AND EQUIPMENTS:


2.1 MPLAB IDE
2.2 SK40B/SK40C
2.3 CIRCUIT SIMULATION
2.4 RESISTOR
2.5 LED
2.6 BREADBOARD
2.7 PICKIT2 PROGRAMMER
2.8 PIC DELAY CALCULATION SOFTWARE

3. INTRODUCTION:

3.1 Assembly Language Programming

3.1.1 Conditional Branches

-1-
ENT 265 Microcontroller and Interfaces (Biomedical Programme)

It must be noted that all conditional jumps are short jumps, meaning that the
address target must be within 256 bytes of the contents of the program
counter (PC).

3.1.1.1 BZ (Branch if Z = 1)
In this instruction, the Z flag is checked. If it is high, it jumps to
the target address. For example:

OVER MOVF PORTB, W ; read Port B and put it in WREG


JZ OVER ; jump if WREG is zero

In this program, if PORTB is zero, it jumps to the label OVER. It


stays there until PORTB has a value other than zero. Notice that
the BZ instruction can be used to see whether any fileReg or
WREG is zero. More importantly, you don’t have to perform an
arithmetic instruction such as decrement to use the BZ instruction.

3.1.1.2 BNC (branch if no carry, branch if CY = 0)


In this instruction, the carry flag bit in the Status register is used to
make the decision whether to jump. In executing “BNC label”, the
processor looks at the carry flag to see if it is raised (C = 1). If it is
not, the CPU starts to fetch and execute instructions from the
address of the label. If C = 1, it will not branch but will execute the
next instruction below BNC.

3.1.2 Unconditional BrancH


The unconditional jump in which control is transferred unconditionally to
the target location. In the PIC 18 there are two unconditional branches:
GOTO (go to) and BRA (branches). Deciding which one to use depends on
the target address.

3.1.2.1 GOTO (GOTO is a long jump)


GOTO is an unconditional jump that can go to any memory
location in the 2M address space of the PIC18. It is a 4-byte (32-
bit) instruction in which 12 bits are use for the opcode, and the
other 20 bits represents the 20-bit address of the target location.
The 20-bit target address allows a jump to 1M of memory
locations from 00000 to FFFFFH, instead of 2M.This problem is
solved by making the lowest bit of the program counter A0 = 0,
and the 20-bit target address of the GOTO becomes address bits
A21 – A1. In this way, the GOTO can cover the entire 2M address
space of 00000 – 1FFFFFH and also makes sure that the target
address lands on an even address location. Because all the PIC18
instructions are 2-byte or 4-byte instructions, the GOTO will not
land at the middle of an instruction.

-2-
ENT 265 Microcontroller and Interfaces (Biomedical Programme)

3.1.2.2 BRA (branch)


In this 2-byte (16-bit) instruction, the first 5 bits are the opcode and
the rest (lower 11 bits) id the relative address of the target location.
The relative address range of 000 – FFFH id divided into forward
and backward jumps; that is, within -1024 to +1023 bytes of
memory relative to the address of the current PC (program
counter). If the jump is forward, then the target address is positive.
If the jump is backward, then the target address is negative. In this
regards, BRA is like the conditional branch instructions except that
11 bits are used for the offset address instead of 8.

3.1.2.3 GOTO to itself using $ sign


In cases where there is no monitor program, we use the GOTO
(jump) to itself in order to keep the microcontroller busy. A simple
way of doing that is to use the $ sign. That means in place of this:

HERE GOTO HERE

We can use the following:

GOTO $

This will also work for the BRA instruction, as shown below:

OVER BRA OVER

Which is the same as:

BRA $ ; $ means same line

3.1.3 Looping in PIC


Repeating a sequence of instructions or an operation a certain number of
times is called a loop. The loop is one of the most widely used
programming techniques. In the PIC, there are several ways to repeat an
operation many times. One ways is to repeat the operation over and over
until it is finished. As shown below:

In the above program, we add 3 to WREG 5 times. That makes 5 x 3 = 15


= 0FH. One problem with the above program is that too much code space

-3-
ENT 265 Microcontroller and Interfaces (Biomedical Programme)

would be needed to increase the number of repetitions to 50 or 100. a


much better way is to use a loop. There are two ways to do a loop in PIC.

3.1.3.1 DECFSZ instruction and looping


The DECFSZ (decrement fileReg skip zero) instruction is widely
used instruction supported across all PIC families of
microcontrollers from PIC12 to PIC18. It has the following format:

In the instruction, the fileReg is decremented, and if its content is


zero, it skips the next instruction. By placing the “GOTO target”
instruction right below it we can create a loop.

3.1.3.2 Using Instruction BNZ For Looping


The BNZ (branch if not zero) is supported by the PIC18 family and
not earlier families such as PIC16 or PIC12. It uses the zero flag in
the status register. The BNZ instruction is used as follows:

In the last two instructions, the fileReg is decremented; if it is not


zero, it branches (jumps) back to the target address reffered to by
the label. Prior to the start of the loop, the fileReg is loaded with
the counter value for the number of repetitions. Notice that the
BNZ instruction refers to the Z flag of the status register affected
by the previous instruction, DECF.

3.1.3.3 Looping 100 000 times


Because two registers give us a maximum value of 65025 (255 x
255 = 65025), we can use three registers to get up to more than 16
million (224) iterations. The following code repeats an action 100
000 times:

-4-
ENT 265 Microcontroller and Interfaces (Biomedical Programme)

3.1.4 Call
Call can be used to call subroutines located anywhere within the 2M
address space of 00000-1FFFFH for the PIC18. To make sure that the PIC
knows where to come back to after execution of the called subroutine, the
microcontroller automatically saves on the stack the address of the
instruction immediately below the CALL. When a subroutine is called,
control is transferred to that subroutine, and the processor saves the PC
(program controller) of the next on the stack and begins to fetch
instructions from the new location. After finishing execution of the
subroutine, the instruction RETURN transfers control back to the caller.
Every subroutine needs RETURN as the last instruction.

3.1.5 Stack
The storing of CPU information such as the program counter on the stack
as called a PUSH, and loading the contents of the stack back into a CPU
register is called a POP. In other words, a register is pushed onto the stack
to save it and popped off the stack to retrieve it.

3.1.5.1 Pushing onto the stack


In the PIC, the stack pointer (SP) is pointing to the last used
location of the stack. This means that it referred as the top of the
stack (TOS). As data is pushed onto the stack, the stack pointer
incremented.

-5-
ENT 265 Microcontroller and Interfaces (Biomedical Programme)

3.1.5.2 Popping from the stack


Popping the contents of the stack back into a given register as the
program counter is the opposite process of pushing. When the
RETURN instruction at the end of the subroutine is executed, the
top location of the stack is copied back to the program counter and
the stack pointer is decremented once. That means the stack is
LIFO (last-in-first-out) memory.

3.1.6 Delay
In creating a delay using assembly language instructions, one must be
mindful of two factors that can affect the accuracy of the delay:
1. The crystal frequency
2. The PIC design

3.2 I/O Interfacing


This experiment will describe and perform the I/O Ports programming by refer
samples of exercises and problems.

3.2.1 I/O Ports


By using this PIC18F4580 (40 pins), it have 5 ports PORTA, PORTB,
PORTC, PORTD and PORTE. Each of the ports A-E can be used for input
or output. In order to use these ports as an input or output port, it must be
programmed. Port A has 7 pins, Ports B, C and D each have 8 pins and
Port E has only 3 pins. Each port has three Special Function Register
(SFRs) associated with it. They are designated as PORTx, TRISx and
LATx. For example Port A, we have PORTA, TRISA and LATA.

3.2.1.1 Ports Status Upon Reset


Upon reset, all ports have value FFH on their TRIS register.

3.2.1.2 TRIS register role in inputting and outputting data


To make a port an output port, we need to write 0s to the TRISx
register. It’s means, to output data to any of the pins of the (for
example) Port B, we must first put 0s into TRISB register to make
it an output port, and then send data to the Port B SFR itself.
To make a port an input port, we must first put 1s into the TRISx
register for that port, and then bring in (read) the data present at the
pins.

-6-
ENT 265 Microcontroller and Interfaces (Biomedical Programme)

Figure 1

3.2.1.3 PORTA
Port A occupies a total of 7 pins (RA0-RA6) refer figure 1, but for
the P1C8F4580, pin A6 is used for the OSC2 pin. A6 is not
available if we use a crystal oscillator to provide frequency to the
PIC18 chip.
To use the pins of PORT A as both input or output ports, each bit
must be connected externally to the pin by enabling the bits of the
TRISA register.

3.2.1.4 PORT B
Port B occupies a total of 8 pins (RB0-RB7) refer figure 1.To use
the pins of PORT B as both input or output ports, each bit must be
connected externally to the pin by enabling the bits of the TRISB
regis

3.2.1.5 PORT C
Port C occupies a total of 8 pins (RC0-RC7) refer figure 1.To use
the pins of PORT C as both input or output ports, each bit must be
connected externally to the pin by enabling the bits of the TRISC
register.

3.2.1.6 PORT D
Port D occupies a total of 8 pins (RD0-RD7) refer figure 1.To use
the pins of PORT D as both input or output ports, each bit must be

-7-
ENT 265 Microcontroller and Interfaces (Biomedical Programme)

connected externally to the pin by enabling the bits of the TRISD


register.

3.2.1.7 PORT E
Port E occupies a total of 3 pins (RE0-RE2) in the PIC18F4580
refer figure 1.Port E is used for 3 additional analog inputs or
simple I/O: AN5, AN6, and AN7.

3.2.2 I/O BIT MANIPULATION PROGRAMMING


Sometimes we need to access only 1 or 2 bits of the port instead of the
entire 8 bits. A powerful feature of PIC I/O ports is their capability to
access individual bits of the port without altering the rest of the bits in that
port.

3.2.2.1 BSF (bit set fileReg)


To set HIGH a single bit of a given fileReg, we use syntax “BSF
fileReg,bit_num” while fileReg can be any location in the file
register and bit_num is the desired bit number from 0 to 7.

3.2.2.2 BCF (bit clear fileReg)


To clear a single bit of a given fileReg, we use syntax “BCF
fileReg,bit_num”. Remember that for I/O ports, we must activate
the appropriate bit in the TRISx register if we want the pin to
reflect the changes.

For example, the following code toggles pin RB2 continuously:

3.2.2.3 BTG (Bit toggle fileReg)


To toggle a single bit of a given fileReg, we use the syntax “BTG
fileReg, bit_num”

3.2.2.4 BTFSS (bit test fileReg, skip if set)


To monitor the status of a single bit for HIGH, we use the BTFSS
instruction .This instruction tests the bit and skips the next
instruction if it is HIGH.

3.2.2.5 BTFSC (bit test fileReg, skip if clear)

-8-
ENT 265 Microcontroller and Interfaces (Biomedical Programme)

To monitor the status of a single bit for LOW, we use the BTFSC
instruction .This instruction tests the bit and skips the next
instruction if it is HIGH.

3.3 Arithmetic Operation

3.3.1 Addition of unsigned numbers

The sum is stored in the WREG register. The instruction could change any
of the C, DC, Z, N, or OV bits of the status register, depending on the
operands involved.

The value in WREG is added to the fileReg and the result is stored in
either the fileReg or WREG.

The value in WREG is added to the fileReg and the result is added with
carry. The result is stored in destination. As example below:

3.3.2 The DAW (decimal adjust WREG) instruction in the PIC18 is provided to
correct the hexadecimal-to-decimal problem associated with BCD
addition. The mnemonic “DAW” works only with an operand in the
WREG register. The DAW instruction will add 6 to the lower nibble or
higher nibble if needed; otherwise, it will leave the result alone. The
following example will verify these points.

After the program is executed, register WREG will contain 72H (47 + 25
= 72). Note that the “DAW” instruction only works on WREG.

3.3.3 Subtraction of unsigned numbers,

-9-
ENT 265 Microcontroller and Interfaces (Biomedical Programme)

The number in WREG is subtracted from K. The result will be saved in


WREG. After the execution of SUB, if N = 0 (or C = 1), the result is
positive; if N = 1 (or C = 0), the result is negative and the destination has
the 2’s complement of the result.

The number in WREG is subtracted from the fileReg and the result will be
saved either in fileReg or WREG.

This instructions is used for multibyte numbers and will take care of the
borrow of the lower byte. If C = 0 prior to executing the instructions, it
also subtracts 1 from the result. See example below:

After the SUBWF, loc 6 has = 62H – 96H = CCH and the carry flag is set
to 0, indicating there is a borrow (notice, N = 1). Because C = 0, when
SUBWFB is executed the fileReg location 7 has = 27H – 12H – 1 = 14H.
Therefore, we have 2762H – 1296H = 14CCH.

3.3.4 Multiplication of unsigned numbers,

In byte-by-byte multiplication, one of the operands must be in the WREG


register, and the second operand must be literal K value. After
multiplication, the result is in the special function registers PRODH and
PRODL; the lower byte is in PRODL, and the upper byte is in PRODH.
See the following example that multiplies 25H by 65H.

3.3.5 There is no single instruction for the division of byte/byte numbers in


thePIC18. We can write a program to perform division by repeated
subtraction. In dividing a byte by a byte, the numerator is placed in a fileReg

- 10 -
ENT 265 Microcontroller and Interfaces (Biomedical Programme)

and the denominator is subtracted from it repeatedly. The quotient is the


number of times we subtracted and the remainder is in fileReg upon
completion.

3.3.6 In signed byte operands, D7 (MSB) is the sign and D0 to D6 are set aside
for the magnitude of the number. If D7 = 0, the operand is positive, and if
D7 = 1, it is negative. The N Flag in the status register is the D7 bit.

3.3.7 The range of positive numbers that can be represented by the format is 0 to
+127. If a positive number is larger than +127, a 16-bit operand must be
used.
For negative numbers, D7 is 1; however, the magnitude is
represented in its 2’s compliment. To convert to negative number
representation (2’s compliment), follow these steps:
1. Write the magnitude of the number in 8-bit binary (no sign).

2. Invert each bit

3. Add 1 to it.

Below are some examples:

- 11 -
ENT 265 Microcontroller and Interfaces (Biomedical Programme)

3.3.8 Overflow problem in signed number operations, In 8-bit signed number


operations, OV is set to 1 if either of the following two conditions occurs:

1. There is a carry from D6 to D7 but no carry out of D7 (C = 0)

2. There is a carry from D7 out (C = 1) but no carry from D6 to


D7

In other words, the overflow flag is set to 1 if there is a carry from D6 to D7


or from D7 out, but not both. This means that if there is a carry both from
D6 to D7 and from D7 out, OV = 0.

- 12 -
ENT 265 Microcontroller and Interfaces (Biomedical Programme)

3.4 Logic and compare instructions

3.4.1 AND

This instruction will perform a logical AND on the two operands place the
result in WREG. There is also the “ANDWF fileReg, d” instruction where
the destination can be WREG or fileReg. The fileReg operand can be any
register in the data RAM file register. The AND instruction will effect the Z
and N flags. N is D7 of the result, and Z = 1 if the result is zero. The AND
instruction is often used to mask (set to 0) certain bits of operand.

Solution:

3.4.2 OR

3.4.3 EX-OR

3.4.4 COMF (complement fileReg)

3.4.5 Compare instructions

- 13 -
ENT 265 Microcontroller and Interfaces (Biomedical Programme)

4. EXAMPLE

4.1 EXAMPLE 1 – Port A as input.

4.2 EXAMPLE 2 – Port A as output.

- 14 -

You might also like