EMK310
Lecture 1
Introduction to PIC
Professor Tania Hanekom
tania.hanekom@up.ac.za
Refs: Projects.pdf
PIC datasheets
Prescribed textbook
Electrical, Electronic & Computer Engineering
Micros can’t think…
…so you have to tell them exactly what to do.
Programs (software)
set of instructions stored in computer's memory
machine instructions coded as binary numbers
Most of PIC18 instructions are 16 bits wide
Instruction fetched from memory > decoded in
control unit of CPU > control signals generated to
trigger operation
Electrical, Electronic & Computer Engineering
Assembly language
Programmer needs to enter instructions, debug and maintain code
using binary format
– e.g. 0000 1000 0000 0011 (Not likely!)
or in hex
– 0803h (Still doesn't mean anything to the novice.)
or perhaps
– movf 03h, 0 (Looks a tiny bit better. Maybe move something?)
or how about
– movf STATUS, W
– meaning "Move the contents of a file register called Status into a
register called W."
– Yes, much better if we know what "STATUS" and "W" means!
Assembly language was developed to simplify the
programming job.
Electrical, Electronic & Computer Engineering
Assembly language (2)
… is a low-level programming language which is used
to manipulate microcontroller on bit and word level.
How does it get into the μ?
– Source code: the code we write
• instructions: PIC understands them
• directives: control how the assembler builds the object code
(see MPLAB help)
– Assembled to hex file (object code) by assembler software
• Assembler must understand each line of code to be able to do
the translation
Electrical, Electronic & Computer Engineering
High-level languages
To code in Assembly, the programmer must know
hardware architecture of micro on which software is
to be executed
– (This is exactly why we program in Assembly in EMK310)
High-level languages require less knowledge of
underlying hardware and the code is easier to follow
– Machine code compiled from high-level language is much
longer and caanot run as fast as equivalent Assembly code.
– Time-critical applications frequently written in Assembly.
Electrical, Electronic & Computer Engineering
Architecture: 8-bit
PIC18F45K20 has a(n)
8-bit wide data path.
16-bit wide instruction set.
Electrical, Electronic & Computer Engineering
Architecture: CPU modules
ALU
– arithmetic and logic unit
– (Afr: rekenkundige- en logiesebewerkingseenheid)
– 8 bit
Separate memories to store program & data
Oscillator & timers
Interfaces, e.g.
– ports
– ADC
– Comparators
– PWM
To program them, you need to understand what they look like: architecture
PIC18F45K20 datasheet p15
PIC16F917 datasheet p. 9
PIC16F628A datasheet p. 10
PIC10F200 datasheet p. 10
PIC16F887 datasheet p. 15
Electrical, Electronic & Computer Engineering
Architecture
PIC18F45K20
datasheet p. 15
I/O Ports
ALU
Clock & timing unit
Timers
Electrical, Electronic & Computer Engineering
PIC16F628A
datasheet p. 10
Electrical, Electronic & Computer Engineering
PIC16F200
datasheet p. 10
6 pin!
Electrical, Electronic & Computer Engineering
Architecture: RISC
RISC
reduced instruction set computer
simplified instructions which "do less" may
still provide for higher performance if this
simplicity can be utilized to make
instructions execute very fast
Electrical, Electronic & Computer Engineering
Architecture: Harvard
Harvard architecture
program & data accessed from separate
memories via separate busses
word width, timing, implementation
technology, and memory address structure
can differ
instruction memory often wider than data
memory
advantage: bandwidth, i.e. much faster
Electrical, Electronic & Computer Engineering
Architecture: Pipeline
CPUs built up from dedicated sub-units e.g. instruction
decoders, ALUs, memory handling units, etc.
non-optimized design, instruction in program
sequence must be (almost) completed before next
can be issued for execution
pipelined architecture, successive instructions
can overlap in execution
barrier to pipelining: some instructions (e.g.
division) take longer to complete - CPU has to wait
before passing next instruction into pipeline
Electrical, Electronic & Computer Engineering
Memory
Memory location (information unit)
– Address & content
Electrical, Electronic & Computer Engineering
Types of memory
Two very general types of memory
– on-chip
– external
On-chip memory can be divided into
– program memory (instruction code)
– data memory (variables)
For PIC processors the two blocks of
memory each has its own bus
– access to each in same oscillator cycle
Electrical, Electronic & Computer Engineering
Memory organization
Program memory
21-bit program counter,
capable of addressing 2 MB
program memory space
32 Kbytes of Flash Memory
implemented on
PIC18F45K20
– up to 16,384 single-word
instructions
Electrical, Electronic & Computer Engineering
Memory organization
Program memory (2)
– Each PIC18 instruction is a 16-bit word divided into an opcode,
which specifies the instruction type and one or more
operands, which further specify the operation of the
instruction.
Electrical, Electronic & Computer Engineering
Memory organization
Program memory (3)
Electrical, Electronic & Computer Engineering
Memory organization
Program memory (4)
PICmicro® 18C MCU Family Reference
Manual, Section 7.2
PIC18F45K20 datasheet sections 5, 6 & 23
Electrical, Electronic & Computer Engineering
Memory organization
Data memory (1)
Data memory = Special Function Registers
(SFR) area + General Purpose Registers
(GPR) area.
– SFRs control the operation of the device
– GPRs are general area for data storage and
scratch pad operations
Electrical, Electronic & Computer Engineering
Memory organization
Data memory (2)
12 bit data address
– up to 4096 bytes of data
– Divided in up to 16 banks (4 bits) of 256 bytes each (8 bits)
Data memory banked for both the GPR and SFR
areas.
– GPR area is banked to allow greater than 256 bytes of
general purpose RAM to be addressed.
Most instructions in PIC18 instruction set use Bank
Pointer/ Bank Select Register (BSR).
– This SFR holds 4 MSBs of location’s address; the instruction
itself includes the 8 LSBs.
– BSR can be loaded directly by MOVLB instruction.
Electrical, Electronic & Computer Engineering
Memory organization
Data memory (3)
To ensure that commonly used registers
(SFRs and select GPRs) can be accessed in a
single cycle, PIC18 devices implement an
Access Bank.
– 256-byte memory space that provides fast access
to SFRs and the lower portion of GPR Bank 0
without using the Bank Select Register (BSR).
– ”a” parameter in instruction defines use of access
bank (ref slide 5)
Electrical, Electronic & Computer Engineering
Memory
organization
Data memory (4)
Electrical, Electronic & Computer Engineering
Memory organization
Data memory (5)
PICmicro mid-range MCU family reference
manual: Section 7.6
PIC18F45k20 datasheet section 5
Electrical, Electronic & Computer Engineering
Configure it to fit your requirements…
(Also see Huang p.17)
Electrical, Electronic & Computer Engineering
Architecture: pin definition
PINS
multifunction
define appropriate setup (configuration) in
code
SFRs, e.g. TRISA
Electrical, Electronic & Computer Engineering
Let's flash an LED…
What do we need?
– power to power the μ
– clock source required for processing
– code to tell the PIC what to do
– LED and resistor to limit current through LED
• port pins can source/sink 25 mA but it is better design
practice to use a resistor
Electrical, Electronic & Computer Engineering
But…
PICs understand only 0 and 1 because they
are made of transistors (switches)
we don't think in terms of 0 and 1 (maybe
after RAG?)
Electrical, Electronic & Computer Engineering
What to do?
give instructions in 0/1 format
– e.g. 0000100000000011 (Not likely!)
or in hex
– 0803h (Still doesn't mean anything to the novice.)
or perhaps
– movf 03h, 0 (Looks a tiny bit better. Maybe move
something?)
or how about
– movf STATUS, W
– meaning "Move the contents of a file register called Status
into a register called W."
– Yes, much better if we know what "STATUS" and "W"
means!
Electrical, Electronic & Computer Engineering
Mnemonics
Interface between student and
microcontroller
An assembler is used to translate the code
written using mnemonics (that we understand
easier) to machine code (the micro’s
language)
– some people learn to understand machine code as
if it were English!
Electrical, Electronic & Computer Engineering
Example of Assembler code
Label Mnemonic Operand Comments
Instruction Space
Directive
Title "Our first program" (directive)
list p=pic18f45k20 ; processor type (directive)
;
; ------------- (comment)
; PROGRAM START (comment)
; ------------- (comment)
;
org 0h ; startup address = 0000 (directive)
start movlw 0x00 ; simple code (instruction)
movwf 0F80h (instruction)
goto start ; do this loop forever (instruction)
end (directive)
projects.pdf p. 16
Electrical, Electronic & Computer Engineering
Ready to go…
MPLAB
– Microchip's Integrated Development Environment
– Used to write code (editor)
– Compile code
– Debug
– Download object code to PIC via some interface
• e.g. PICkit
MPLAB demo (code lecture)
– new project; type code in new file; save; add to project;
compile; debugger & MPLAB sim; run (watchdog timer
timeout); configuration bits (explain + not accessible after
PIC has been programmed); oscillator; watchdog (reset PIC
after some predefined time); power up timer.
Electrical, Electronic & Computer Engineering
Let's flash a LED…
To be continued ...
Electrical, Electronic & Computer Engineering