Docsity

Introduction to the PIC18 Microcontroller | ECE 372, Study notes of Microprocessors

Toggle sidebar
bg1
1
1
Chapter 1: Introduction to
the PIC18 Microcontroller
Original slides by Han-Way Huang
Modified by Susan Lysecky
2
1.1 Objectives
zDefine or explain the following terms
zcomputer, processor, microprocessor, microcontroller, hardware, software, cross
assembler, cross compiler, RAM, SRAM, ROM, EPROM, EEPROM, flash
memory, byte, nibble, bus, KB, MB, GB, mnemonic, opcode, and operand
zExplain the differences among all the PIC18 addressing modes
zUse the appropriate PIC18 addressing mode to access operands
zExplain the banking operation of the PIC18 data memory
zPerform simple operations using add, subtract, and data movement
instructions
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Introduction to the PIC18 Microcontroller | ECE 372 and more Study notes Microprocessors in PDF only on Docsity!

Chapter 1: Introduction to

the PIC18 Microcontroller

Original slides by Han-Way Huang

Modified by Susan Lysecky

1.1 Objectives

z Define or explain the following terms

z computer, processor, microprocessor, microcontroller, hardware, software, cross

assembler, cross compiler, RAM, SRAM, ROM, EPROM, EEPROM, flash

memory, byte, nibble, bus, KB, MB, GB, mnemonic, opcode, and operand

z Explain the differences among all the PIC18 addressing modes

z Use the appropriate PIC18 addressing mode to access operands

z Explain the banking operation of the PIC18 data memory

z Perform simple operations using add, subtract, and data movement

instructions

Computer Hardware Organization

1.2 What is a Computer?

z Computer is made up of s oftware and

hardware

z Basic Hardware Components

z Processor

z Performs all computational operations and

coordinates the usage of resources

z May perform general-purpose or special-purpose

computations (graphical rendering, printing, etc)

z Input Devices

z Enter program to be exectued or data to be

processed

z Examples – keyboard, keypad, scanner, sensors

z Output Devices

z Presents information (output of program, status,

etc)

z Examples – CRT display, flat-panel display, seven-

segment display, printers, LEDs

z Memory Devices

z Stores program or data to be processed

Processor Common Bus (address, data, & control)

Memory

Program Storage, Data Storage

Output Units

Input Units

Control Unit

Datapath

Arithmetic Logic Unit

Registers

Processor

1.2.1 The Processor

z Also called the central processing unit (CPU)

z Processors minimally contain

z Registers

z Storage location inside CPU, close to processor so very fast

access to data

z Holds data or memory address

z Number of registers vary widely based on processor type

z Arithmetic logic unit (ALU)

z Perform numerical computations and logical evaluations for the

processor

z Control Unit

z Contains the hardware instruction logic – decodes and monitors

the execution of instructions

ƒ program counter register contains the address of the next instruction to

be executed

ƒ status register flags the instruction execution result (overflow, carry,

borrow, etc) used for decision making

z Arbiter as various portions of system compete for resources

z Synchronized by system clock (modern systems exceed 3 GHz)

1 GHz = 1 billion cycles

per second

clock period is 1 ns

(10-9^ sec)

1.2.3 Microcontrollers (MCU, μC)

z Semiconductor Industry forecasts $252 billion in sales for 2007

$28 Billion in desktop

and laptop computers

Where’s the rest coming

from?

z Average household has 35-50 embedded systems

z A system with one or more microcontrollers

z Cell phones, camcorders, calculators, home security systems, televisions, washing

machine, microwave, DVD players, gaming consoles, anti-lock breaks, cruise control

Automotive

20-80 microprocessor controlling

break systems, airbags,

infotainment, safety, etc.

Entertainment

Healthcare

Smart Pills electronically track and instructed to deliver medicine

to specific location, better imaging, remote surgery, etc.

Military

Consumer

Electronics

1.2.3 Microcontrollers (MCU, μC)

z Memories store program and data

z MxN memory - M words of N-bits wide each

z Classified into random-access memory (RAM)

and read-only memory (ROM)

1.2.4 Memory

N-bits

wide each

M x N memory

M words

z Strange name – Created several decades ago to contrast

with sequentially-accessed storage like tape drives

z Readable and writable memory

z Processor able to read data from memory (non-destructive) and

write data to memory

z Same amount of time is required to access any location on the

same chip

z Volatile – cannot retain data in absence of power

z Two types of RAM technology

z Dynamic random-access memory (DRAM)

z 1 transistor/1 capacitor to store one bit of information

z capacitor charge leak away over time, need periodic refresh

z Static random-access memory (SRAM)

z 4-6 transistors to store one bit of information

z no periodic refresh is required

z RAM mainly used to store dynamic programs and data

z Several programs may run on same computer and operate on

different sets of data

z Need to be loaded onto RAM from hard disk or secondary

storage

1.2.4 Memory

Random-Access Memory (RAM)

SRAM cell

data data’

d (^) d’ cell

word enable

data

addr

r w

en

1024 × 32
RA M

RAM block symbol

DRAM cell

word enable

data cell

d (^) capacitor slowly discharging

1.2.4 Memory

Read-Only Memory (ROM)

z Electrically erasable programmable ROM (EEPROM)

z Electrically programmable many times

z Electrically erasable many times (no UV light necessary)

z Can be erased one location, one row, or whole chip in

one operation

z Increased flexibility, but more $$$ than EPROM

z Flash memory

z Electrically programmable/erased many times

z In-system, without use of dedicated programmer

z Can only be erased in bulk (either a block or the whole

chip)

z Both types are in-system programmable

z Can be programmed with new stored bits while in the

system in which the ROM operates

z Requires bi-directional data lines, and write control input

z Also need busy output to indicate that erasing is in

progress – erasing takes some time

data

addr

en

write

busy

1024x EEPROM

images from http://www.digikey.com

1.3 The Computer Software

z Computer programs are known as software

z A program is a sequence of instructions

z Machine instruction

z A sequence of binary digits (stored in computer’s memory)

which can be executed by the processor

z Drawbacks

z Hard to understand, program, and debug for human being

z Assembly language

z Defined by assembly instructions

z Instructions are a mnemonic representation of a machine

instruction

z Mnemonic – intended to assist the memory, device used as an

aid in remembering

z Drawbacks

z Programmers need to work on the program logic at a very low

level and can’t achieve high productivity

z Need to be familiar with hardware architecture

z Still difficult to understand/maintain

Machine instructions

addwf 0x40, W, A

addlw 5

Assembly

1.3 The Computer Software

Source Code and Object Code Examples

address object code line no. Source code

00001E 0E06 00010 movlw 0x

000020 6E11 00011 movwf 0x11,A

000022 0E07 00012 movlw 0x

000024 6E12 00013 movwf 0x12,A

000026 0E08 00014 movlw 0x

000028 6E13 00015 movwf 0x13,A

00002A 0E05 00016 movlw 0x

00002C 5E10 00017 subwf 0x10,F,A

00002E 5E11 00018 subwf x11,F,A

z Hexadecimal (or hex) number is specified by adding the prefix 0x or enclosing the

number with single quotes and preceding it by an H.

z 0x02 , 0x1234 , H`2040’ are hex numbers

z Decimal numbers are enclosed by single quotes and preceded by letter D.

z D10’ and D123’ are decimal numbers

z Octal and binary numbers are similarly specified.

z O`234’ is an octal number; B’01011100’ is a binary number

1.3 The Computer Software

z High-level Language

z Syntax of a high-level language is similar

to English

z Becomes easier to understand

z Statement often needs to be implemented

by tens or hundreds of assembly

instructions

z Allows the user to work on the program

logic at higher level.

z Drawback

z Resulting machine code is longer than

code written in assembly

z Time-critical sections may still be written

in assembly

for(i=0; i<cnt; i++){

temp = arr[i];

High-level Language

1.5 The PIC18 Memory Organization

z Memory Addressing

z Memory consists of a sequence of directly addressable

locations

z A location is referred to as an information unit

z A memory location can be used to store data, instruction, and

the status of peripheral devices

z A memory location has two components: an address and its

contents

z PIC18 holds 8-bits of information, called a byte

z nibble is 4-bits of information

z Notation

z [register address] : refers to contents of register

z [WREG] is the contents of the WREG register, [0x20] is the contents

of the general purpose register at address 0x

z Hex locations have suffix of H or h, or a prefix of 0x

z Decimal numbers is just the numerical value

z address: refers to the register or memory location

z 0x10 refers to the special-function register at address 0x

Address Contents

1.5.1 Separation of Data Memory and

Program Memory

z Data Memory and Program Memory are separated

z Separation of data memory and program memory makes possible the simultaneous

access of data and instruction (separate busses required)

z Data memory are used as general-purpose registers or special function registers

z On-chip Data EEPROM are provided in some PIC18 MCUs

12-bit register address

Program Memory Space

(a portion of this space is on the MCU chip)

PIC18 CPU Data Memory Space

(Special function registers and general purpose RAM)

21-bit program address

16-bit instruction bus (^) 8-bit data bus

Inside the MCU chip

1.5.2 PIC18 Data Memory

z Data memory implemented in

SRAM

z Consists of general-purpose

registers and special-function

registers

z Both are referred to as data

registers.

z A PIC18 MCU may have up to

4096 bytes of data memory

z 12-bit address required

z (Most) PIC18 instructions limited to

16-bits with 8-bits for location

z Data memory is divided into banks

of 256 bytes (

000h 05Fh 060h

0FFh 100h

1FFh 200h

2FFh 300h

3FFh 400h

Access RAM

GPRs

Unused

SFRs

GPRs

GPRs

GPRs

GPRs

GPRs

DFFh E00h

EFFh F00h F5Fh F60h

FFFh

Access RAM low

Access RAM

high SFRs

000h

05Fh 060h

0FFh

Access Bank

Bank 0

Bank 1

Bank 2

Bank 3

Bank 4 to Bank 13

Bank 14

Bank 15

BSR<3:0>

z Only one bank is active at any time

z Active bank is specified by the BSR

register

z Depending on device, not all banks

implemented (smaller memory

available)

z Register Types

z General-purpose registers - used to

hold dynamic data

z Special-function registers - used to

control the operation of peripheral

functions

z Bank switching is an overhead and

can be error-prone

z Access bank implemented to reduce

the problem caused by bank switching

z Access bank consists of the lowest 96

bytes and the highest 160 bytes of the

data memory space.

1.5.2 PIC18 Data Memory

000h 05Fh 060h

0FFh 100h

1FFh 200h

2FFh 300h

3FFh 400h

Access RAM

GPRs

Unused

SFRs

GPRs

GPRs

GPRs

GPRs

GPRs

DFFh E00h

EFFh F00h F5Fh F60h

FFFh

Access RAM low

Access RAM

high SFRs

000h 05Fh 060h

0FFh

Access Bank

Bank 0

Bank 1

Bank 2

Bank 3

Bank 4 to Bank 13

Bank 14

Bank 15

BSR<3:0>

N: Negative bit

1 = arithmetic result is negative

0 = arithmetic result is positive

OV: Overflow bit

1 = Overflow occurred for signed arithmetic

0 = No overflow occurred

Z: Zero flag

1 = The result of an arithmetic or logic operation is zero.

0 = The result of an arithmetic or logic operation is not zero.

DC: Digit carry/borrow bit

For ADDWF, ADDLW, SUBLW, SUBWF instructions.

1 = A carry-out from the 4th low-order bit of the result occurred.

0 = No carry-out from the 4th low-order bit of the result occurred.

For borrow, the polarity is reversed. For rotate (RRF, RLF)

instructions, this bit is loaded with either the bit 4 or bit 3 of the

source register.

C: Carry/borrow bit

For ADDWF, ADDLW, SUBLW, SUBWF instructions.

1 = A carry-out from the most significant bit of the result occurred.

0 = No carry-out from the most significant bit of the result has

occurred.

For borrow, the polarity is reversed. For rotate (RRF, RLF)

instructions, this bit is loaded with either the high or low order bit

of the source register.

-- -- -- N OV Z DC C

1.6 PIC18 CPU Registers

The STATUS Register (0xFD8)

z Contains arithmetic status of the ALU

z Bits set or cleared according to the device logic

1.7 The PIC18 Pipelining

z Instructions executed in two stages - instruction fetch and instruction execution

z Up to two instructions are overlapped in their execution

z One instruction is in fetch stage while the second instruction is in execution stage

z Because of pipelining, each instruction appears to take one instruction cycle to complete

MOVLW 55h

MOVWF PORTB

BRA sub_

BSF PORTA,BIT

Instruction @address sub_

fetch 1 execute 1

fetch 2 execute 2

fetch 3 execute 3

fetch 4 flush

fetch sub_1 execute sub_

TCY0 TCY1 TCY2 TCY3 TCY4 TCY

Note: All instructions are single cycle, except for any program branches.

1.7 The PIC18 Pipelining

z Problems can arise from pipelining

z Data dependency hazard

z Next instruction needs result from previous instruction (result not yet

updated)

z Control hazard

z If a branch instructions reaches execution state and takes the branch,

following instructions need to be flushed

z Topics beyond the scope of this class

MOVLW 55h

MOVWF PORTB

BRA sub_

BSF PORTA,BIT

Instruction @address sub_

fetch 1 execute 1

fetch 2 execute 2

fetch 3 execute 3

fetch 4 flush

fetch sub_1 execute sub_

TCY0 TCY1 TCY2 TCY3 TCY4 TCY

Note: All instructions are single cycle, except for any program branches.

Result = A + B;

Out = Result * 2;

Mem[A] = B * 2;

if( Mem[A] > 100){

1.8 PIC18 Instruction Format

z Five basic instruction categories

z Byte oriented file register instructions

z opcode specifies the ALU operation

opcode d a f

d = 0 for result destination to be WREG register.

d = 1 for result destination to be file register (f)

a = 0 to force Access Bank

a = 1 for BSR to select bank

f = 8-bit file register address

1.8 PIC18 Instruction Format

z Literal Operations

z A literal is a number to be operated on directly by the CPU

opcode k

k = 8-bit immediate value

1.8 PIC18 Instruction Format

z Control Operations

z These instructions are used to change the program execution sequence and making

subroutine calls

z Notation <7:0> means bit 7 to bit 0 of the number n

opcode (^) n<7:0> (literal)

(^1111) n<19:8> (literal)

n = 20-bit immediate value

GOTO label

opcode n<7:0> (literal)

(^1111) n<19:8> (literal)

S = fast bit

S

CALL funct_name

opcode (^) n<10:0> (literal)

opcode (^) n<7:0> (literal)

BRA label

BC label

1.8 PIC18 Instruction Format

Access Bank

z Instructions formats presented use 8

bits to specify a data register (f field)

z Eight bits can specify only 256 registers

z Need to divide data registers (up to 4096

bytes) into banks

z Use the BSR register to store the

additional 4 bits

z Only one bank is active at a time

z When operating on a data register in a

different bank, bank switching is needed

z Incurs overhead and may cause program

errors

z Access bank is created to minimize the

problems of bank switching

z When operands are in the access bank,

no bank switching is needed

z Most SFRs are in the access bank

000h 05Fh 060h

0FFh 100h

1FFh 200h

2FFh 300h

3FFh 400h

Access RAM

GPRs

Unused

SFRs

GPRs

GPRs

GPRs

GPRs

GPRs

DFFh E00h

EFFh F00h F5Fh F60h

FFFh

Access RAM low

Access RAM

high SFRs

000h

05Fh 060h

0FFh

Access Bank

Bank 0

Bank 1

Bank 2

Bank 3

Bank 4 to Bank 13

Bank 14

Bank 15

BSR<3:0>

1.8 PIC18 Instruction Format

Examples of the Use of Access Bank

z Bank switching

z movlb k used to place the value of k in

the lower four bits of the BSR register

z Data registers in bank k becomes active

z Bank Selection – “a” field

z Use “A” in assembly language to select

access bank (a = 0)

z Use “BANKED” in assembly language

to allow the BSR register to do the bank

selection (a=1)

z Destination – “d” field

z Use “F” to specify file register (d=1)

z Use “W” to specify WREG register

addwf 0x20,F,A ; add the data register at 0x ; in access bank with WREG ; register and store the sum in 0x

subwf 0x30,F,BANKED ; subtract the value of WREG from ; the data register 0x30 in the bank ; specified by the current contents ; of the BSR register. The difference ; is stored in data register 0x

addwf 0x40,W,A ; add the WREG register with data ; register at 0x40 in access bank ; and leaves the sum in WREG

1.9 Addressing Modes

z Inherent Mode

z Operand is implied in the opcode field, opcode does not provide address

movlw 0x20 andlw 0x

adds hex value of 0x20 to WREG and

places sum in WREG, opcode specifies

destination is WREG (location 0xFE8)

performs AND operation on the

corresponding bits of the hex number 13 and

the WREG, opcode again specified WREG

1.9 Addressing Modes

z Indirect Mode

z A special function register (FSRx) is used

as a pointer to the actual data register

z Three indirect addressing registers

available: FSR0, FSR1, FSR

z Composed to two 8-bit registers FSRnH

and FSRnL

z No need to specify access bank, 12-bit

address provided

z Need to read/write to INDF0, INDF1, or

INDF2 to activate indirect addressing

movwf INDF

copies content of WREG to data memory

location specifed by contents of FSR0 register

0x

FSRnH FSRnL

Accessing INDFn

Data memory (^) 0xFFF

1.9 Addressing Modes

z Indirect Mode continued

z FSR register can be modified during the indirect

addressing operation too

clrf PLUSWO

copies contents of WREG to memory location specified by the FSR0 register, then increments the FSRO register by 1

0x

(^11870)

FSRnH FSRnL

Accessing INDFn

Data memory (^) 0xFFF

movwf PREINCO

movwf POSTDECO

movwf POSTINC

first increments the FSRO register by 1, then copies contents of WREG to memory location specified by the FSR0 register

copies contents of WREG to memory location specified by the FSR0 register, then decrements the FSRO register by 1

Clear memory location calculated by using WREG (signed) value as an offset to FSR0. Neither FSR0 or WREG changes after operation

Remember 3 indirect register

INDFn, POSTDECn, POSTINCn,

PREINCn, and PLUSWn operate

similarly on each FSRn where

n=0, 1, 2

1.9 Addressing Modes

z Bit-Direct Mode

z Five instructions to deal with bits (BCF, BSF, BTFSC, BTFSS, BTG)

z Deals with an individual bit, use three bits to specify the bit to be operated on

BCF PORTB, 3, A BSF PORTA, 4, A

Integer value 3 specifies the bit to be

cleared

Integer value 4 specifies the bit to be set