Lecture01 Picintro-1
Lecture01 Picintro-1
MICROCONTROLLERS
PIC Introduction
Adopted from - ECE 3724 - Microprocessors
Microcontroller (µC) vs. Microprocessor (µP)
• µC intended as a single chip solution, µP requires external
support chips (memory, interface)
• µC has on-chip non-volatile memory for program storage,
µP does not.
• µC has more interface functions on-chip (serial interfaces,
analog-to-digital conversion, timers, etc.) than µP
• µC does not have virtual memory support (i.e., could not
run Linux), while µP does.
• General purpose µPs are typically higher performance
(clock speed, data width, instruction set, cache) than µCs
• Division between µPs and µCs becoming increasingly
blurred
V 0.2 2
Microchip PIC24 Family µC
Features Comments
Instruction width 24 bits
V 0.2 3
PIC24 Core (Simplified Block Diagram)
Data Mem
Program Counter 24
Inst. Reg
23 16 address
Data
address 16
Program Memory,
non-volatile, up to 16 x 16
16
Working
4M words (4M x 24) Reg array 16
DOUT
16
16
The instruction register contains the machine
code of the instruction currently being ALU
executed.
ALU (Arithmetic Logic Unit) is 16 bits wide, 16
can accept as operands working registers or
data memory. 17 x 17 Multiplier
not shown
V 0.2 4
Memory Organization
Memory on the PIC24 µC family is split into two types:
Program Memory and Data Memory.
PIC24 instructions are stored in program memory, which is
non-volatile (contents are retained when power is lost).
A PIC24 instruction is 24 bits wide (3 bytes).
PIC24HJ32GP202 program memory supports 11264
instructions; the PIC24 architecture can support up to 4M
instructions.
PIC24 data is stored in data memory, also known as the file
registers, and is a maximum size of 65536 x 8. Data memory
is volatile (contents are lost when power is lost).
V 0.2 5
Program Memory
V 0.2 6
Copyright Delmar Cengage Learning 2008. All RightsReserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
Special Function Registers (SFRs)
Special Function Registers (SFR) are addressed like normal data memory
locations but have specified functionality tied to hardware subsystems in the
processor. We typically refer to SFRs by name (W0, T3CON, STATUS, etc)
instead of by address.
There are many SFRs in the PIC24 µC – they are used as control registers and
data registers for processor subsystems (like the serial interface, or the analog-
to-digital converter). We will cover their use and names as we need to.
SFRs live in the address range 0x0000 to 0x07FE in data memory. See the
datasheet for a complete list of SFRs.
Other locations in data memory that are not SFRs can be used for storage of
temporary data; they are not used by the processor subsystems. These are
sometimes referred to as GPRs (general purpose registers). MPLAB refers to
these locations as file registers.
V 0.2 8
8-bit, 16-bit, 32-bit Data
We will deal with data that is 8 bits, 16 bits (2 bytes), and
32 bits (4 bytes) in size. Initially we will use only 8 bit and
16 bit examples.
Size Unsigned Range
8-bits 0 to 28-1 (0 to 255, 0 to 0xFF)
16-bit 0 to 216-1 (0 to 65536, 0 to 0xFFFF)
32-bit 0 to 232-1 (0 to 4,294,967,295), 0 to 0xFFFFFFFF)
The addressing mode used for both the source and destination
operands is called register direct. The mov instruction supports
other addressing modes which are V 0.2not shown. 12
MOV Wso, Wdo Instruction Execution
V 0.2 15
MOV Wns, f Instruction Execution
V 0.2 18
MOV f, Wnd Instruction Execution
V 0.2 19
Copyright Delmar Cengage Learning 2008. All RightsReserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
MOV f, Wnd Instruction Execution
V 0.2 19
Copyright Delmar Cengage Learning 2008. All RightsReserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
A Note on Instruction Formats
• The instruction formats (machine code) of some
instructions will be presented for informational
purposes
– However, studying the machine code formats of the
instructions is not a priority; understanding instruction
functionality will be emphasized.
– All instruction formats can be found in the
dsPIC30F/dsPIC33F Programmers Reference manual
from Microchip
– The PIC24 family is a subset of the
dsPIC30F/dsPIC33FF instruction set – the PIC24
family does not implement the DSP instructions.
V 0.2 20
MOV{.B} WREG, f Instruction
“Copy content of WREG (default working register) to data memory
location f”. General form:
MOV{.B} WREG, f (WREG) → f
This instruction provides upward compatibility with earlier PIC µC.
WREG is register W0, and f is a location within the first 8192 bytes of data
memory (near data memory)
MOV WREG, 0x1000 (W0) → 0x1000
Contents of register W0 copied to data memory location 0x1000.
Can be used for either WORD or BYTE operations:
MOV WREG, 0x1000 word operation
MOV.B WREG, 0x1001 lower 8-bits of W0 copied to 0x1001
Word copy must be to even (word-aligned) location.
Note: The previously covered MOV Wns, f instruction cannot be used for
byte operations! V 0.2 21
MOV.B WREG, f Instruction Execution
V 0.2 23
Copyright Delmar Cengage Learning 2008. All RightsReserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
MOV{.B} WREG, f Instruction Format
V 0.2 23
Copyright Delmar Cengage Learning 2008. All RightsReserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
MOV{.B} f {,WREG} Instruction
“Copy contents of data memory location f to WREG (default working
register) . General form:
MOV{.B} f, WREG (f )→ WREG
MOV{.B} f (f )→ f
This instruction provides upward compatibility with earlier PIC µC.
WREG is register W0, and f is a location within the first 8192 bytes of data
memory (near data memory)
Can be used for either WORD or BYTE operations:
MOV 0x1000, WREG word operation
MOV.B 0x1001, WREG only lower 8-bits of W0 are affected.
MOV 0x1000 Copies contents of 0x1000 back to
itself, will see usefulness of this later
Word copy must be from even (word-aligned) data memory location.
Note: The MOV f,Wnd instruction cannot be used for byte operations!
V 0.2 24
MOV{.B} f {,WREG} Format
V 0.2 32
Copyright Delmar Cengage Learning 2008. All RightsReserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
MOV{.B} f {,WREG} Format
V 0.2 33
Copyright Delmar Cengage Learning 2008. All RightsReserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
MOV.{B} f, WREG Instruction Execution
V 0.2 34
Copyright Delmar Cengage Learning 2008. All RightsReserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
MOV.{B} f, WREG Instruction Execution
V 0.2 35
Copyright Delmar Cengage Learning 2008. All RightsReserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
Move a literal into a Working Register
Moves a literal into a working register. The ‘#’ indicates the
numeric value is a literal, and NOT a memory address.
General form:
MOV #lit16, Wnd lit16 → Wnd (word operation)
MOV.B #lit8, Wnd lit8 → Wnd.lsb (byte operation)
V 0.2 27
More on Literals
V 0.2 29
Copyright Delmar Cengage Learning 2008. All RightsReserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
MOV Literal Execution
V 0.2 29
Copyright Delmar Cengage Learning 2008. All RightsReserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
MOV Literal Instruction Formats
V 0.2 33
Indirect Addressing Coverage
• There are six forms of indirect addressing
• The need for indirect addressing makes the most
sense when covered in the context of C pointers
– This is done in Chapter 5
• At this time, you will only need to understand the
simplest form of indirect addressing, which is
register indirect as shown on the previous two slides.
• Most instructions that support register direct for an
operand, also support indirect addressing as well for
the same operand
– However, must check PIC24 datasheet and book to
confirm.
ADD{.B} Wb, Ws, Wd Instruction
Three operand addition, register-to-register form:
ADD{.B} Wb, Ws, Wd (Wb) + (Ws) → Wd
Wb, Ws, Wd are any of the 16 working registers W0-W15
V 0.2 35
ADD{.B} Wb, Ws, Wd Execution
V 0.2 36
ADD{.B} Wb, Ws, Wd Execution
(a) Execute: add W0,W1,W2
W0 0x1AF3 0x1AF3 W0 0x1AF3
W1 0x8B1A + 0x8B1A W1 0x8B1A
W2 0x64DE W2 0xA60D Modified
W3 0xFB90 0xA60D W3 0xFB90
Before After
V 0.2 54
Assembly Language Efficiency
The effects of the following instruction:
ADD 0x1000 (0x1000) + (WREG) → 0x1000
Can also be accomplished by:
V 0.2 55
ADD{.B} f {,WREG} Execution
V 0.2 42
Copyright Delmar Cengage Learning 2008. All RightsReserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
ADD{.B} f {,WREG} Execution
V 0.2 42
Copyright Delmar Cengage Learning 2008. All RightsReserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
SUB{.B} f {,WREG} Instruction
Two operand subtraction form:
SUB{.B} f (f) – (WREG) → f
SUB{.B} f, WREG (f) – (WREG) → WREG
WREG is W0, f is limited to first 8192 bytes of memory.
One of the operands, either f or WREG is always destroyed!
V 0.92 43
Increment
Increment operation, register-to-register form:
INC{.B} Ws, Wd (Ws) +1 → Wd
Increment operation, memory to memory/WREG form:
INC{.B} f (f) + 1 → f
INC{.B} f, WREG (f) + 1 → WREG
(f must be in first 8192 locations of data memory)
Examples:
INC W2, W4 (W2) + 1 → W4
INC.B W3, W3 (W3.lsb) + 1 → W3.lsb
INC 0x1000 (0x1000) +1 → 0x1000
V 0.2 66
ADD forms
ADD Wb, Ws, Wd
Legal:
ADD W0, W1, W2
ADD W0, [W1], [W4]
Illegal:
ADD [W0],W1,W2 ;first operand illegal!
V 0.2 67
Video tutorials
A number of videos illustrate important concepts; all are listedon
the video page at http://www.reesemicro.com/site/pic24micro/Home/pic24-video-tutorials-1.
Available tutorials, which cover topics on the following pages of
these lecture notes:
• MPLAB IDE introduction at
http://www.ece.msstate.edu/courses/ece3724/main_pic24/videos/mplab_assem/index.htm
V 0.2 68
A Simple Program
In this class, will present programs in C form, then translate
(compile) them to PIC24 µC assembly language.
C Program equivalent A uint8 variable is
#define avalue 100 8 bits (1 byte)
uint8 i,j,k;
i = avalue; // i = ?
i = i + 1; // i = ?
j = i; // j = ?
j = j - 1; // j = ?
k = j + i; // k = ?
V 0.2 69
A Simple Program
In this class, will present programs in C form, then translate
(compile) them to PIC24 µC assembly language.
C Program equivalent A uint8 variable is
#define avalue 100 8 bits (1 byte)
uint8 i,j,k;
i = avalue; // i = 100
i = i + 1; // i++, i = 101
j = i; // j is 101
j = j - 1; // j--, j is 100
k = j + i; // k = 201
V 0.2 70
Where are variables stored?
When writing assembly language, can use any free data memory
location to store values, it your choice.
A logical place to begin storing data in the first free location in
data memory, which is 0x0800 (Recall that 0x0000-0x07FF is
reserved for SFRs).
Assign i to 0x0800, j to 0x0801, and k to 0x0802. Other choices
could be made.
V 0.2 71
C to PIC24 Assembly
V 0.2 74
.include "p24Hxxxx.inc"
.global reset
.bss
i:
;reserve space for variables
.space 1
mptst_byte.s
j: .space 1
k: .space 1
.text ;Start of Code section
reset: ; first instruction located at reset label
mov # SP_init, W15 ;;initialize stack pointer
mov # SPLIM_init,W0
mov W0,SPLIM ;;initialize Stack limit reg.
avalue = 100
; i = 100; This file can be assembled
mov.b #avalue, W0 ; W0 = 100 by the MPLAB
mov.b WREG,i ; i = 100
assembler into PIC24
; i = i + 1;
inc.b i ; i = i + 1 machine code and
; j = i simulated.
mov.b i,WREG ; W0 = i
mov.b WREG,j ; j = W0
Labels used for memory
; j = j – 1; locations 0x0800 (i),
dec.b j ; j= j – 1 0x0801(j), 0x0802(k) to
; k = j + i
mov.b i,WREG ; W0 = i increase code clarity
add.b j,WREG ; W0 = W0+j (WREG is W0)
mov.b WREG,k ; k = W0
done:
goto done ;loop forever
V 0.2 75
Include file that defines various labels
mptst_byte.s (cont.) for a particular processor. ‘ include’ is
an assembler directive.
.include "p24Hxxxx.inc"
avalue = 100
V 0.2 77
mptst_byte.s (cont.) The use of labels and
; i = 100;
comments greatly improves
mov.b #avalue, W0 ; W0 = 100 the clarity of the program.
mov.b WREG,i ; i = 100
It is hard to over-comment
an assembly language
; i = i + 1;
inc.b i ; i = i + 1 program if you want to be
; j = i able to understand it later.
mov.b i,WREG ; W0 = i
mov.b WREG,j ; j = W0
Strive for at least a
; j = j – 1; comment every other line;
dec.b j ; j= j – 1 refer to lines
; k = j + i
mov.b i,WREG ; W0 = i
add.b j,WREG ; W0 = W0+j (WREG is W0)
mov.b WREG,k ; k = W0
V 0.2 78
mptst_byte.s (cont.) A label that is the target
of a goto instruction.
Lables are case sensitive
done: (instruction mnemonics
goto done ;loop forever
and assembler directives
are not case sensitive.
.end Acomment
V 0.2 79
General MPLAB IDE Comments
• See Experiment #2 for detailed instructions on
installing the MPLAB IDE on your PC and
assembling/simulating programs.
• The assembly language file must have the .s
extension and must be a TEXT file
– Microsoft .doc files are NOT text files
The MPLAB IDE has a built-in text editor. If you use an
external text editor, use one that displays line numbers
(e.g. don’t use notepad – does not display line numbers)
• You should use your portable PC for experiments 1-
5 in this class; all of the required software is freely
available.
V 0.2 80
An Alternate Solution
C Program equivalent ;Assign variables to registers
;Move variables into registers.
#define avalue 100
;use register-to-register operations for
uint8 i,j,k;
computations;
i = avalue; // i = 100
;write variables back to memory
i = i + 1; // i++, i = 101
j = i; // j is 101 ;assign i to W1, j to W2, k to W3
j = j - 1; // j--, j is 100
k = j + i; // k = 201
mov #100,W1 ; W1 (i) = 100
inc.b W1,W1 ; W1 (i) = W1 (i) + 1
mov.b W1,W2 ; W2 (j) = W1 (i)
Previous approach took 9 dec.b W2,W2 ; W2 (j) = W2 (j) -1
add.b W1,W2,W3 ; W3 (k) = W1 (i) + W2 (j)
instructions, this one took ;;write variables to memory
mov.b W1,W0 ; W0 = i
11 instructions. Use mov.b WREG,i ; 0x800 (i) = W0
whatever approach that mov.b W2,W0 ; W0 = j
mov.b WREG,j ; 0x801 (j) = W0
you best understand. mov.b W3,W0 ; W3 = k
mov.b WREG,k ; 0x802 (k) = W0
V 0.2 81
Clock Cycles vs. Instruction Cycles
The clock signal used by a PIC24 µC to control instruction execution can be
generated by an off-chip oscillator or crystal/capacitor network, or by using the
internal RC oscillator within the PIC24 µC.
For the PIC24H family, the maximum clock frequency is 80 MHz.
An instruction cycle (FCY) is two clock (FOSC) cycles. Important!!!!!!!
A PIC24 instruction takes 1 or 2 instruction (FCY) cycles, depending on the
instruction (see Table 19-2, PIC24HJ32GP202 data sheet). If an instruction causes
the program counter to change (i.e, GOTO), that instruction takes 2 instruction
cycles.
An add instruction takes 1 instruction cycle. How much time is this if the clock
frequency (FOSC) is 80 MHz ( 1 MHz = 1.0e6 = 1,000,000 Hz)?
1/frequency = period, 1/80 MHz = 12.5 ns (1 ns = 1.0e-9 s)
1 Add instruction @ 80 MHz takes 2 clocks * 12.5 ns = 25 ns (or 0.025 us).
By comparison, an Intel Pentium add instruction @ 3 GHz takes 0.33 ns (330 ps). An Intel
Pentium could emulate a PIC24HJ32GP202 faster than a PIC24HJ32GP202 can execute!
But you can’t put a Pentium in a toaster, or Vbu0y.2one from Digi-key for $5.00. 64
How long does mptst_byte.s take to execute?
Beginning at the reset label, and ignoring the goto at the end.
Instruction
Cycles
1. How many
mov # SP_init, W15 ?
instruction cycles? mov # SPLIM_init,W0 ?
mov W0,SPLIM ?
2. How many clock mov.b #avalue, W0 ?
cycles? mov.b WREG,i ?
inc.b i ?
mov.b i,WREG ?
mov.b WREG,j ?
dec.b j ?
mov.b i,WREG ?
add.b j,WREG ?
mov.b WREG,k ?
V 0.2 Total ?
65
How long does mptst_byte.s take to execute?
Beginning at the reset label, and ignoring the goto at the end,
takes 12 instruction cycles, which is 24 clock cycles.
Instruction
Cycles
mov # SP_init, W15 1
mov # SPLIM_init,W0 1
mov W0,SPLIM 1
mov.b #avalue, W0 1
mov.b WREG,i 1
inc.b i 1
mov.b i,WREG 1
mov.b WREG,j 1
dec.b j 1
mov.b i,WREG 1
add.b j,WREG 1
mov.b WREG,k 1
V 0.2 Total 12 65
What if we used 16-bit variables instead
of 8-bit variables?
C Program equivalent
A uint16 variable is
#define avalue 2047
16 bits (1 byte)
uint16 i,j,k;
i = avalue; // i = ?
i = i + 1; // i = ?
j = i; // j = ?
j = j - 1; // j = ?
k = j + i; // k = ?
V 0.2 66
What if we used 16-bit variables instead
of 8-bit variables?
C Program equivalent
A uint16 variable is
#define avalue 2047
16 bits (1 byte)
uint16 i,j,k;
i = avalue; // i = 2047
i = i + 1; // i++, i = 2048
j = i; // j is 2048
j = j - 1; // j--, j is 2047
k = j + i; // k = 4095
V 0.2 66
.include "p24Hxxxx.inc"
.global reset Reserve 2 bytes for each
.bss ;reserve space for variables
i: .space 2
variable. Variables are now
j: .space 2 stored at 0x0800, 0x0802,
k: .space 2 0x0804
.text ;Start of Code section
reset: ; first instruction located at reset label
mov # SP_init, w15 ;initialize stack pointer
mov # SPLIM_init,W0
mov W0,SPLIM ;initialize stack limit reg
avalue = 2048
; i = 2048;
mov #avalue, W0 ; W0 = 2048 Instructions now
mov WREG,i ; i = 2048 perform WORD (16-bit)
; i = i + 1;
inc i ; i = i + 1 operations (the .b
; j = i
mov i,WREG ; W0 = i
qualifier is removed).
mov WREG,j ; j = W0
; j = j – 1;
dec j ; j= j – 1
; k = j + i
mov i,WREG ; W0 = i
add j,WREG ; W0 = W0+j (WREG is W0)
mov WREG,k ; k = W0
done:
goto done ;loop forever
V 0.2 67
Copyright Delmar Cengage Learning 2008. All RightsReserved.
An Alternate Solution (16-bit variables)
C Program equivalent ;Assign variables to registers
;Move variables into registers.
#define avalue 2047
;use register-to-register operations for
uint16 i,j,k;
computations;
i = avalue; // i = 2047
;write variables back to memory
i = i + 1; // i++, i = 2048
j = i; // j is 2048 ;assign i to W1, j to W2, k to W3
j = j - 1; // j--, j is 2047
k = j + i; // k = 4095
mov #2047,W1 ; W1 (i) = 2047
inc W1,W1 ; W1 (i) = W1 (i) + 1
mov W1,W2 ; W2 (j) = W1 (i)
Previous approach took 9 dec W2,W2 ; W2 (j) = W2 (j) -1
add W1,W2,W3 ; W3 (k) = W1 (i) + W2 (j)
instructions, this one took ;;write variables to memory
mov W1,i ; 0x800 (i) = W1
8 instructions. In this mov W2,j ; 0x802 (j) = W2
case, this approach is mov W3,k ; 0x804 (k) = W3
more efficient!
V 0.2 88
How long does mptst_word.s take to execute?
Ignoring the goto at the end. Instruction
Cycles
1. How many instruction mov # SP_init, W15 ?
cycles? mov # SPLIM_init,W0 ?
mov W0,SPLIM ?
2. How many clock cycles? mov #avalue, W0 ?
mov WREG,i ?
inc i ?
mov i,WREG ?
mov WREG,j ?
dec j ?
mov i,WREG ?
add j,WREG ?
mov WREG,k ?
Total ?
V 0.2 89
How long does mptst_word.s take to execute?
Ignoring the goto at the end, takes 12 instruction cycles, which
is 24 clock cycles. Instruction
Cycles
mov # SP_init, W15 1
mov # SPLIM_init,W0 1
mov W0,SPLIM 1
mov #avalue, W0 1
mov WREG,i 1
inc i 1
mov i,WREG 1
mov WREG,j 1
dec j 1
mov i,WREG 1
add j,WREG 1
mov WREG,k 1
Total 12
V 0.2 90
16-bit operations versus 8-bit
The 16-bit version of the mptst program requires the same number of
instruction bytes and the same number of instruction cycles as the 8-bit
version.
On an 8-bit processor, like the PIC18 family, the 16-bit version would take
roughly double the number of instructions and clock cycles as the 8-bit
version.
On the PIC24, a 32-bit version of the mptst program will take approximately
twice the number of instructions and clock cycles as the 16-bit version. We
will look at 32-bit operations later in the semester.
V 0.2 91
Review: Units
In this class, units are always used for physical quantity:
Time Frequency
milliseconds (ms = 10-3 s) kilohertz (kHz = 103 Hz)
microseconds (µs = 10-6 s) megahertz (MHz = 106 Hz)
nanoseconds (ns = 10-9 s) gigahertz (GHz = 109 Hz)
V 0.2 93
Some PICMicros that we have used
Features 16F87x PIC18F242 PIC24H
(Fall 2003) (Summer 2004) (Summer 2008)
Instruction 14 bits 16 bits 24 bits
width
Program 8K instr. 8K instructions ~10K instructions
memory