You are on page 1of 11

Objectives

CpE 511
MICROPROCESSOR SYSTEM
Jeannalen P. Lunod
Instructor I
Department of Computer Engineering

Introduction to Microprocessors
A microprocessor is a programmable logic device with a
designed set of instructions.

This course has been designed to provide:


An understanding of the basic principles of
microprocessors and appreciate the difference
between microprocessors, microcontrollers and
DSPs (Digital Signal Processors)
A working knowledge of the PIC16F84
microcontroller
Experience of assembler programming and a
basic appreciation of embedded systems

Microprocessors, Microcontrollers and DSPs


Microprocessors

Microcontrollers and DSPs evolved from


the original microprocessors.

It contains three primary components;


a processing unit (ALU), memory, and, input and
output (I/O).
Arithmetic/Logic Unit (ALU)
Performs arithmetic operations such as addition and
subtraction, and, logic operations such as AND, IOR
and XOR.
Memory
Storage of instructions and data.
Input and output (I/O)
Analogue or digital; for external communication.

Microprocessor is an umbrella term for


all types of processor.

Microcontrollers
Processor specifically designed for
control applications.
Microprocessors

DSPs

DSPs
Processors specifically designed for
digital signal processing.

Microprocessors
Processors for general purpose
processing.
Microcontrollers

What is the PIC ?

Meet the PIC!


PIC 16F84A

PIC (Peripheral Interface Controller) is the IC which was developed to


control peripheral devices, alleviating the load from the main CPU.

The PIC is the small computer


What is the PIC ?

Maximum clock
operating frequency is
about 20 MHz
Memory capacity is
about 1K to 14K words.

Is an 8-bit microcontroller of RISC (Reduced


Instruction Set Computer) architecture.
Uses a total number of 35 instructions
Has a total of 18 pins and is mostly found in a DIP18
type of case.

BLOCK DIAGRAM

BLOCK DIAGRAM cont


FLASH/PROGRAM MEMORY
For storing a program. It can be erased and
rewritten almost 100 times.
1 word is 14 bits long and 1024 words (1K words)
can be stored
Even if power is switched off the contents of the
flash memory will not be lost

BLOCK DIAGRAM cont


EEPROM

Electrically Erasable Programmable Read Only


Memory
Usually used for storing important data that will be
secured even in power failure
Nonvolatile memory, the contents are not lost when
power is turned off
Total capacity of 64 bytes and the number of times
it can be rewritten to is limited to about 1,000,000
It is used to store data which will not change
frequently. Data can be safely stored for 40 years.

BLOCK DIAGRAM cont

RAM
Memory used by a program to store
temporary data during its execution.

CENTRAL PROCESSING UNIT


Coordinates the work and executes the
user program

BLOCK DIAGRAM cont


PORTS

Hardware of the PIC16F84A


Pin Diagram

There are 2 ports portA (5 bits) and portB (8 bits)


These 13 pins can be configured as input/output

Pin Diagram

OSC1/CLKIN Oscillator crystal input/External clock


source input.
OSC2/CLKOUT Oscillator crystal output/Connects to crystal or
resonator in crystal oscillator mode.
MCLR(inv) Master clear (reset) input. Programming voltage
input. This pin is an active low reset to the device.
RA0 - RA3 Bi-directional I/O port.
RA4/T0CKI Bi-directional I/O port/Clock input to the TMR0
timer/counter.
RB0/INT Bi-directional I/O port/External interrupt pin.
RB1 - RB7 Bi-directional I/O port.
VSS Ground
VDD Positive supply (+2.0V to +5.5V)

INSTRUCTION SET (Cont.)

INSTRUCTION SET

INSTRUCTION SET SUMMARY


Each instruction is a 14-bit word
consists of:
opcode instruction type;
operands - one or more

3 basic categories
Byte-oriented instructions
Bit-oriented instructions
Literal and control operations

BYTE-ORIENTED INSTRUCTIONS
Ex: movf f,d

f represents a file register designator

File register designator specifies which file register is to be


used by the instruction

d represents a destination designator

Destination designator specifies where the result of the


operation is to be placed

If d is 0, the result is placed in the w register


If d is 1, the result is placed in the file register specified in
the instruction

LITERAL AND CONTROL OPERATION


Ex: movlw k
k represents an 8 or 11-bit constant or literal
value

BIT-ORIENTED INSTRUCTIONS
Ex: btfsc f,b
b represents a bit field designator which
selects the number of the bit affected by
the operation
f represents the address of the file in
which the bit is located

OPCODE FIELD DESCRIPTION


FIELD
DESCRIPTION
f Register file address (0x00 to 0x7f)
w Working register (accumulator)
b Bit address within an 8-bit file register
k Literal field, constant data or label
x Dont care location (=0 or 1). The assembler will generate
code with x=0. It is recommended form of use for compatibility
with all Microchip software tools
d Destination select; d=0: store result in w, d=1: store result
in file register f. Default is d=1.
PC Program counter
TO Time-out bit
PD Power-down bit

GENERAL FORMAT FOR INSTRUCTIONS

GENERAL FORMAT FOR INSTRUCTIONS


(cont.)

Byte-oriented file register operations


opcode
d f(FILE#)
13 8
7 6
0

Bit-oriented file register operations


opcode
b(BIT#) f(FILE#)
13 10
9
7 6
0

d = 0 for destination w
d = 1 for destination f
f = 7-bit file register address

b = 3-bit bit address


f = 7-bit file register address

GENERAL FORMAT FOR INSTRUCTIONS


(cont.)
Literal and control operations
General
opcode k(literal)
13 8 7
0
k = 8-bit immediate value

CALL & GOTO instructions


opcode k(literal)
13 11 10 0

BYTE-ORIENTED FILE REGISTER OPERATIONS


ADDWF f,d Add w and f
Add the contents of the w register with register f.
If d is 0, the result is stored in the w register.
If d is 1, the result is stored back in register f.

ANDWF f,d AND w with f


AND the w register with register f .
If d is 0, the result is stored in the w register.
If d is 1, the result is stored back in register f.

k = 11-bit immediate value

BYTE-ORIENTED FILE REGISTER OPERATIONS


(cont.)

BYTE-ORIENTED FILE REGISTER OPERATIONS


(cont.)

CLRF f Clear f

DECF f,d Decrement f

The contents of register f are cleared and the z bit is set.

CLRW Clear w
W register is cleared.
Zero bit (z) is set.

COMF f,d Complement f


The contents of register f are complemented.
If d is 0, the result is stored in w.
If d is 1, the result is stored back in register f.

Decrement register f.
If d is 0, the result is stored in the w register.
If d is 1, the result is stored back in register f.

DECFSZ f,d Decrement f, skip if 0

The contents of register f are decremented.


If d is 0, the result is placed in the w register.
If d is 1, the result is placed back in register f.
If the result is 1, the next instruction is executed, if the result is 0,
then a NOP is executed instead making it a 2TCY instruction.

BYTE-ORIENTED FILE REGISTER OPERATIONS


(cont.)

BYTE-ORIENTED FILE REGISTER OPERATIONS


(cont.)

INCF f,d Increment f

IORWF f,d Inclusive OR w with f

The contents of register f are incremented.


If d is 0, the result is placed in the w register.
If d is 1, the result is placed back in register f.

INCFSZ f,d Increment f, skip if 0

The contents of register f are incremented.


If d is 0, the result is placed in the w register.
If d is 1, the result is placed back in register f.
If the result is 1, the next instruction is executed.
If the result is 0, a NOP is executed instead, making it a 2TCY
instruction.

Inclusive OR the w register with register f.


If d is 0, the result is placed in the w register.
If d is 1, the result is placed back in register f.

MOVF f,d Move f


The contents of register f are moved to a destination dependent
upon the status of d.
If d = 0, destination is w register.
If d = 1, the destination is file register f itself.
d = 1 is useful to test a file register, since status flag z is affected.

BYTE-ORIENTED FILE REGISTER OPERATIONS


(cont.)

BYTE-ORIENTED FILE REGISTER OPERATIONS


(cont.)

MOVWF f Move w to f

RRF f,d Rotate right through carry

Move data from w register to register f.

The contents of register f are rotated one bit to the right


through the carry flag.
If d=0, the result is placed in the w register.
If d=1, the result is placed back in register f.

NOP No operation
RLF f,d Rotate left f through carry
The contents of register f are rotated one bit to the left
through the carry flag.
If d is 0, the result is placed in the w register.
If d is 1, the result is stored back in register f.

BYTE-ORIENTED FILE REGISTER OPERATIONS


(cont.)
SWAPF f,d Swap nibbles in f
The upper and lower nibbles of register f are exchanged.
If d=0, the result is placed in w register.
If d is 1, the result is placed in register f.

XORWF f,d Exclusive OR w with f


Exclusive OR the contents of the w register with register f.
If d = 0, the result is placed in the w register.
If d = 1, the result is stored back in register f.

BIT-ORIENTED FILE REGISTER OPERATIONS


(cont.)
BTFSS f,b

Bit Test f, Skip if Set

If bit b in register f = 0, the next instruction is


executed.
If bit b = 1, then the next instruction is discarded,
and a NOP is executed instead, making this a 2TCY
instruction.

SUBWF f,d Subtract w from f


Subtract (2s complement method) w register from
register f.
If d = 0, the result is stored in the w register.
If d = 1, the result is stored back in register f.

BIT-ORIENTED FILE REGISTER OPERATIONS


BCF f,b Bit clear f

Bit b in register f is cleared.

BSF f,b Bit Set f

Bit b in register f is set.

BTFSC f,b

Bit Test f, Skip if Clear

If bit b in register f is 1, the next instruction is executed


If bit b in register f is 0, the next instruction is discarded
and a NOP is executed instead, making this a 2TCY
instruction.

LITERAL AND CONTROL OPERATIONS


ADDLW k Add literal and w

The contents of the w register are added to the 8-bit literal k and the
result is placed in the w register.

ANDLW k AND literal with w

The contents of w register are ANDed with the 8-bit literal k.


The result is placed in the w register.

CALL k

Call Subroutine

Call subroutine.
First, return address (PC + 1) is pushed onto the stack.
The 11-bit immediate address is loaded into PC bits.
The upper bits of the PC are loaded from PCLATH, CALL is a 2-cycle
instruction.

LITERAL AND CONTROL OPERATIONS


(cont.)
CLRWDT Clear Watchdog Timer

CLRWDT instruction resets the Watchdog timer.


It also resets the prescaler of the WDT.
Status bits TO and PD are set.

GOTO k Go to address (a 2 cycle instruction)

Is an unconditional branch.
The eleven-bit immediate value is loaded into PC bits.
The upper bits of PS are loaded from PCLATH.

IORLW k Inclusive OR literal with W

The contents of the w register are ORed with the eight bit literal k.
The result is placed in the w register.

LITERAL AND CONTROL OPERATIONS


(cont.)
RETURN Return from subroutine
The stack is POPed and the top of the stack (TOS) is loaded
into the PC.
This is a 2cycle instruction

SLEEP

MOVLW k Move literal to w

The 8-bit literal k is loaded into w register.


The dont cares will assemble as 0s.

RETFIE

Return from Interrupt

RETLW k

Return with literal in w

The w register is loaded with the 8-bit literal k.


The program counter is loaded from the top of the stack (the
return address)
This is a 2 cycle instruction.

LITERAL AND CONTROL OPERATIONS


(cont.)
SUBLW k Subtract w from literal
The w register is subtracted (2s complement method) from
the eight-bit literal k.
The result is placed in the w register.

XORLW k Exclusive OR literal with w


Go into standby mode

The power-down status bit, PD is cleared.


Time-out status bit, TO is set.
Watchdog Timer and its prescaler are cleared.
The processor is put into sleep mode with the oscillator
stopped.

Port Read

1. Port Data Direction


controlled using tristate (TRIS) register. The TRIS register has 8
bits. Loading a 0 bit makes the corresponding port bit an
output and 1 results is an input.
movlw b00001111
portb

The contents of the w register are XORed with the 8-bit


literal k.
The result is placed in the w register.

2. Port Read/Write

USING THE PORTS

tris

LITERAL AND CONTROL OPERATIONS


(cont.)

; teach port B (0x0f)


; bits 7,6,5,4 outputs
; bits 3,2,1,0 inputs

1. movf

porta,w ; read and move value of A

2. btfss
btfsc

porta,3
portb,1

to w reg.
; bit 3 of port A Hi?
; bit 1 of port B Lo?

Port Write

1. Bsf portb,5
bcf porta,2

; set (1) bit 5 of port B.


; clear (0) bit 2 of port A.

DATA TRANSFER
1. movf portb,w

; move contents of port b to


w reg.

movwf

hold

;move contents of w reg to


file reg labeled hold

2. movlw 0x00
tris portb
movlw
0x0f
movwf
portb

BIT MANIPULATION

Example:
porta
equ
portb
equ
org
main
movlw
tris
movlw
tris
clrf
movf
movwf
circle
goto
end

0x05
0x06
0x000
0xff
porta
0x00
portb
portb
porta,w
portb
circle

RUNNING LIGHTS
1. Sequencing (rlf or rrf)

Example:
start movlw
0xff
tris porta
movlw
b11111110
tris portb
bcf portb,0
switchbtfss porta,2
goto switch
bsf portb,0
circle goto circle
end

status equ 0x03


c
equ 0
file
equ 0x0c
bcf status,c
rlf file,f

; status word register


; bit 0 is carry flag

; file reg w/ bit pattern


; clear carry flag
; rotates bits left, result in file

Example :
status
portb equ
shift
ncount
mcount
c
start

get_bit

equ
0x06
equ
equ
equ
equ
org
movlw
tris
clrf
bcf
movlw
movwf
movf
movwf
call

2. Data Transfer

0x03
0x0c
0x0d
0x0e
0
0c000
0x00
portb
portb
status,c
b01001001
shift
shift,w
portb
pause

rlf
goto
pause movlw
movwf
loadn movlw
movwf
decn decfsz
goto
decfsz
goto
end

shift,f
get_bit
0xff
mcount
0xff
ncount
ncount,f
decn
mcount,f
loadn

Example:
main

movlw
movwf
call
movlw
call
.
.
.

b00000011
portb
delay
b00001100
delay

My First Program:
; Turns on a LED connected to B0.

3. Bit Manipulation
Example

bsf
call
clrf
call
bsf

; Uses RC oscillator, about 100 kHz.


; CPU configuration
; (Its a 16F84, RC oscillator,

portb,0
delay
portb
; or bcf portb,0
delay
portb,1
.
.
.

; watchdog timer off, power-up timer on.)

processor 16f84
include <p16f84.inc>
__config _RC_OSC & _WDT_OFF & _PWRTE_ON
; Program

org

; start at address 0

; At startup, all ports A are inputs.


; Set Port B to all outputs.

movlw B00000000
tris
PORTB

; w := binary 00000000
; copy w to port B control reg

; Put a 1 in the lowest bit of port B.

movlw B00000001
movwf PORTB

; w := binary 00000001
; copy w to port B itself

; Stop by going into an endless loop

fin:

goto
end

The Source Code


Each instruction is divided into three parts:
label
opcode (operation
operand (also

code or instruction code)


called argument)

Example, in the line


fin: goto fin
fin: - is the label
goto - is the opcode
fin - is the operand

fin
; program ends here

How the Program Works?


Pseudo Instructions, commands that give information to the assembler but are not translated into machine
instructions.

processor PIC16F84 tells the assembler what kind of CPU.


include <p16f84.inc> - telling the assembler to read the file P16F84.INC,which
pseudo instructions giving the memory addresses of the

contains a lot more


ports and other particulars of this CPU.

Next comes the __config macro instruction:

__config _RC_OSC & _WDT_OFF & _PWRTE_ON


__config (two underscore marks at the beginning) this instruction specifies some
configuration
settings to be programmed.
RC oscillator using RC oscillator not crystal.
Watchdog timer is off The watchdog timer is a built-in device for rebooting the PIC every 18
milliseconds; some programs use this to protect themselves from endless loops, but its very
important to turn it off if youre not using it, or your program will keep restarting itself at
inopportune moments.
Power-up timer is on power-up timer imposes a slight delay at startup to give the 5-volt supply time to
stabilize.

org 0 the last pseudo instruction, which means, The next instruction should go at address 0 in program
memory. That is, youre telling the assembler where to start.

End which tells the assembler that the program is over.

What the instructions do?

CLOCK OSCILLATOR
movlw
tris
movlw
movwf
fin: goto

B00000000
PORTB
B00000001
PORTB
fin

1.
2.
3.
4.

4 different types of clock oscillators may be used


with PIC16F84 parts in general
RC resistor/capacitor
XT crystal or ceramic resonator (external)
HS high speed crystal or ceramic clock
resonator (external clock)
LP low power crystal (external clock)

PROGRAM FORMAT

LABELS

Standard header
Equates
Program
end

A label is a mnemonic symbolic name assigned to


an address
Assigned via an EQUATE statement equating the
symbol with a hexadecimal physical address

RULES
All labels must start in the first position in column 1
Labels must begin with an alpha character or an
under bar
Labels may contain alphanumeric characters, the
under bar and the question mark
Labels may up to 31 characters long
Labels are case sensitive by default

LITERALS
are constants or numbers, usually hexadecimal
numbers
defined using the movlw and some logic and
arithmetic instructions
start movlw 0x00
start label
movlw instruction
00 literal (constant)

The under bar ( _ ) is useful as a means of separating


words because spaces are not allowed.
Ex: temp_file

EQUATES
An equate statement may serve to assign a label to a specific
address in the PIC16 designated by a hexadecimal number
portb equ 0x06
port b label
equ tells assembler this is an equate
0x means hexadecimal
06 hexadecimal address

ORG stands for origin


ORG statements will be used for 3 purposes
.ORG defines the address where the program
code starts
org 0x000
.ORG is used to establish the reset vector for the
PIC16C54
org 0x1ff ; the reset vector points to this address
goto start

10

.ORG is used to establish the start of the interrupt


service routine for the PIC16F84
org 0x004 ; hex address 0x0004 (the start of the
interrupt service routine)

END
An END statement is used to tell the assembler it
has reached the end of the program

11