Documente Academic
Documente Profesional
Documente Cultură
P R I N C I P L E S A N D A P P L I C AT I O N S O F M I C R O C O N T R O L L E R S
LECTURE #3
BY
SANIRA LASANTHA
ICBT KANDY CAMPUS
ACKNOWLEDGEMENT
History of PIC
In 1988 General Instruments sold off their microelectronics division to Arizona
Microchip Technology.
After Microchip took over they came up with a new range of micro controllers
based on a Peripheral Interface Controller (PIC) that General Instruments had
introduced in mid 1970s which was based on the Harvard Architecture.
Their base range PIC16C5XX and currently PIC10FXXX and PIC12CXXX families
have 33 instructions and 12-bit program store with parallel ports and 8-bit
timer/counter.
The execution unit was 8-bit to match the 8-bit data store.
Set 5
Set 5
Objectives
Understand the mid-range Harvard-based Microchip PIC
microcontroller architecture
Be able to interpret the Status register bits that control memory paging
and hold the C, DC and Z flags
Know how to manipulate the contents of the Program Counter in
conjunction with the PCLATCH special purpose register
Recognise the interaction between the clock phases and the internal
sequence of micro-operations.
Set 5
PIC16F84
From a software point of view, all members of a single family of PIC MCUs are
identical. What usually differ are the extended I/O facilities.
In 1994 the PIC16C83/4 were introduced to the mid range family. They became
popular because they were the first PIC micro controllers to use EEPROM.
Set 5
Set 5
Fetch Unit
The fetch unit is primarily concerned
with fetching instructions from
Program store down to the Pipeline.
Set 5
Program Store
Central to the fetch unit is the Program Store.
On power-on the operating program has to be available in Program
Store.
Program Memory is some kind of ROM.
Most PIC MCUs use some kind of EEPROM.
Set 5
Program Counter
PC points to the instruction being fetched at any instance.
The 13-bit register normally increments after each fetch.
However, instructions such as goto that will cause execution of the
program to jump to another part of the Program store.
Thus the PCs normal up count can be overridden.
In addition the programmer has access to the PC
Although with the 13-bit register 213 = 8192 addresses can be
accessed, in PIC16F84, only the lower 10 bits (1010=1024) are actually
connected.
The first instruction is always at h000. This is known as the reset
vector.
Set 5
Set 5
10
Pipeline
Two 14-bit registers implement the pipeline.
Top of the pipeline holds the instruction that has just been fetched
from the Program Store. (nth fetch)
The bottom register feeds the decoder circuits and is the instruction
that is in the process of being executed. (n-1th fetch)
This pipeline action assumes that the instruction execution sequence
is linear.
For instructions that requires a jump to another part of the Program
store, instruction currently at the top of the pipeline needs to be
replaced by the far instruction.
This process is known as flushing and adds an extra machine cycle to
the execution time.
Set 5
11
Instruction Decoder
Instruction decoder uses logic circuitry to decode each field of the 14-bit instruction
and gate the appropriate addresses and data to the correct execution units circuitry
and configure the ALU.
ALL PIC MCU families have and integral oscillator that generates the internal timerelated sequences of micro-operations commanded by the Instruction decoder.
The timing element is typically an external quartz crystal connected across the OSC1
and OSC2 pins, and this determines the clock frequency fOSC.
Mid range devices are limited to an upper frequency of 20MHz, but some early
members were limited to 4 to 10MHz.
Set 5
12
Instruction Decoder
Four oscillator pulses are used as part of decoding logic to activate
internal processes in time dependent sequences.
Set 5
13
Set 5
14
Increment the Program Counter and copy onto the Program store address bus.
Q4:
Read the instruction code off program stores data bus into Instruction register 1
and at the same time move the previous instruction down the Pipeline into
Instruction register 2, where it is presented to the Instruction decoder.
Set 5
15
Stack
Eight 13-bit registers are stacked together and are connected to the
program counter.
Set 5
16
Execute Unit
The 8-bit execute unit is responsible for reading a datum from the Data
store or literal datum from the instruction and processing this byte as
commanded by the Instruction decoder using the ALU.
The outcome is placed either in the Working register or back in the Data
store, overwriting the original datum. (again this is done as indicated by
the instruction and decoded by the ID)
Set 5
17
Set 5
18
ALU
Set 5
Outcome of the former case can be directed back to the Data store if destination byte is 0 or in
to the Working register if destination byte is 1.
19
Status Register
Associated with the ALU is the Status register which holds three flag bits
used to tell the software something about the outcome from an
instruction.
For instance, if there was a carry-out from an addition or if the out come
is zero.
Set 5
20
Set 5
21
Carry Flag
Bit 0 of the status register is the C flag. This primarily holds the carry out from
the last addition operation. Subtraction operations activate this bit as the
complement of the borrow out.
Example:
24-12=12 with borrow = 1
12-24=88 with borrow = 0
C also functions as the input/output bit for the rotate instructions.
The label (R/W ?) Indicates that this bit can be read from or written to and has
an uncertain value on Power on reset; its value does not alter on any other type
of reset.
Set 5
22
This hold the carry out from the lower nybble to upper nybble; that is
from bit 3 to bit 4.
In the same manner DC holds the complement of the borrow out from
bit 3 to bit 4.
Knowledge of carry activity between upper nybble and lower nybble is
useful when dealing with BCD data.
Set 5
23
Zero Flag
Bit 2 of the Status register is the Z flag. This is set whenever the outcome
of the instruction is zero, otherwise it is cleared.
Set 5
24
Set 5
25
In order to understand this, let us look at the Data store in a little more
detail.
Set 5
26
Set 5
27
There are two type of Files in this cabinet. Some of these files have
special significance. These are known as special-purpose registers. SPRs
are used to control and monitor the state of the microcontroller and its
peripheral devices.
Example:
Set 5
28
RP0
The remaining files can be given relevant names by the programmer and used
for general-purpose storage.
The PIC16F84 has 68 of these General Purpose Registers (GPRs) addressed from
File h0C through File h4F.
We have seen earlier that only seven bits are reserved for the address in the 14
bit instruction. Which means only 27 addressed can be accessed.
In order to overcome this restriction PIC16F84 uses an 8th bit called Register
Page 0 (RP0). With this bit, we could go up to 256 addresses. When RP0 is 0 then
Bank 0 is enabled for access and when RP0 is 1 then bank 1 is accessed.
Set 5
29
Most of the more commonly used SPRs are also mirrored. For example
the status register.
Set 5
30
Indirect Addressing
Normal Direct addressing carries the address of the operand as an integral part of the
instruction.
In embedded computing, where program is stored in ROM this address becomes fixed
which cannot be modified at runtime.
An alternative would be to hold the address in File Select Register (FSR) at File 04 in the
Data store.
Whenever the data store address is b0000000 8-bit contents of the FSR is switched to
the Data stores address bus.
With indirect addressing the operand location is no longer fixed. It is a variable in the File
Select Register (FSR). Therefore the location can be altered in runtime.
Set 5
31
The File Select Register hold the 8-bit Indirect address used when the
instruction actually refers to the null address.
Set 5
32
Timer
All PIC MCU families have at least a basic timer/counter known as Timer0
(TMR0). The read/write TMR0 counter register at File 1 can be clocked from the
outside world via the T0CKI (Timer 0 ClocK In) pin, which is shared with the RA4
Port A pin.
Alternatively the source can be the internal Q4 phase clock which is one quarter
of the crystal frequency.
Either clock source can be frequency divided by a 8-bit Prescaler Counter.
The divide ratio is controlled by the lower three prescaling bits of the Option
register at file h81 labeled PS2:PS1:PS0.
Set 5
33
More on Timer
If the lower 3 bits (PS2:PS1:PS0) were 111 the ratio is then 2PS+1 which means
27+1 = 28 = 256 the counter will increment at f/256 where f is the source
frequency.
The prescaler register can be disconnected by setting the bit 3 of OPTION_REG
to 1. This will give a direct connection between the pulse source and the counter.
Set 5
34
More on Timer
If the prescaler is assigned to the timer then the watchdog circuit will
eventually timeout after approximately 18ms. With PSA set to 1, 2PS *
18ms Watchdog timeouts are required before the processor is reset.
If the watchdog timer times out, TO bit in the status register will be
cleared.
Set 5
35
It may be preset to any byte value by moving data from W, and read at
any time. When it overflows from hff to h00 it sets the T-ID in the
INTCON (INTerrupt CONtrol) register. This may be used to generate an
interrupt.
Set 5
36
T0CS (Timer 0 Clock Select) at bit 5 allows you to select the internal clock or a
transition at T0CKI pin.
Set 5
37
Program Counter
Occasionally it may be necessary for a program to modify the PC. To
allow for this, the lower byte of the PC is directly accessible as the
Program Counter Low (PCL) byte SPR.
The Program Counter LATch High register (PCLATH) will not alter the top
byte of the PC, but when the PCL is written to, the new state of the
PCLATH simultaneously becomes the new top half of the 13-bit PC.
Set 5
38
Program Counter
Set 5
39
The Program Counter LATch High byte SPR is a holding register carrying
data destined to be loaded in to the top half of the PC. This occurs at the
same time as the data is written to PCL register, allowing a simultaneous
updating of the complete 13 bits.
Set 5
40
Port A has five I/O lines mapped into the Data store address space at File 5.
(RA4,.,RA0)
Port B has 8 I/O lines at File 6. (RB7,,RB0)
Each port can be configured for input or output.
Two registers in Bank 1 (TRISA and TRISB) are the corresponding data
direction registers for Port A and Port B.
TRIS stands for TRIState.
Set 5
41
Set 5
42
Data EEPROM
PIC16F84 has 64 bytes of non-volatile EEPROM memory.
This is not part of the volatile Data store, and is accessed via SPRs as a
peripheral device.
Any byte can be addressed and then read from or written to via the
EEDATA register, as addressed by the EEADR register and controlled by
the EECON1 and EECON2 control file registers.
Set 5
43
Set 5
44
Interrupts
Interrupt control register at File h08 holds the mask and the
status bits controlling the response of the MCU interrupts. Operation of
interrupts will be discussed later.
Set 5
45
Thank You
Set 5
46