8/10/2019 PIC Microcontroller Introduction
1/81
Bi ging Kthut Vi x l
Microcontroller Architecture
PIC16F Family
i hc Nng
Trngi hc Bch Khoa
Khoain t Vin ThngB mn K thut My tnh
Instructor: Phm Xun Trung
8/10/2019 PIC Microcontroller Introduction
2/81
Reference Material
PIC 16F87X data sheet PICMicro Mid-Range MCU Family Reference
Manual Available on course web page
8/10/2019 PIC Microcontroller Introduction
3/81
The Microchip PIC ucontrollers
http://www.microchip.com Characteristics
types; speeds; I/O pins; Analog to Digital
Converters; Capture/Compare modules
Programming (MPLab)
Instruction set
Implementations and Examples
8/10/2019 PIC Microcontroller Introduction
4/81
MICROCONTROLLER
Programmable Chip Same idea as PC, but
on much smaller
scale
8/10/2019 PIC Microcontroller Introduction
5/81
Why a microcontroller isneeded?
Sensors Microcontrollers Actuators
8/10/2019 PIC Microcontroller Introduction
6/81
Variety of microcontrollers
Atmel AT90S8535Motorola 68HC11
Intel - 8051
Texas Instruments -Microchip PIC16F84, PIC16F877
8/10/2019 PIC Microcontroller Introduction
7/81
Why PIC microcontrollers?
Easy to programBetter online documantation
8/10/2019 PIC Microcontroller Introduction
8/81
Introduction
What is PIC?
- A family of Harvard architecture microcontrollers madeby Microchip Technology
- Derived from the PIC1650 originally developed byGeneral Instrument Microelectronics Division.
- The name PIC was originally an acronym for
"Programmable Intelligent Computer(?)Peripheral Interface Controller (?)
8/10/2019 PIC Microcontroller Introduction
9/81
Introduction
Why PIC is popular? low cost ,wide availability with high clock speed
availability of low cost or free development tools
Only 37 instructions to remember
serial programming and re-programming with flashmemory capability
Its code is extremely efficient, allowing the PIC to runwith typically less program memory than its largercompetitors
PIC is very small and easy to implement for non-
complex problems and usually accompanies to themicroprocessors as an interface
8/10/2019 PIC Microcontroller Introduction
10/81
Introduction
8/10/2019 PIC Microcontroller Introduction
11/81
REVIEW
MCU Architectures
Hardvard vs Von Neumann
RISC vs CISC Pipelining
8/10/2019 PIC Microcontroller Introduction
12/81
Two Different Architectures
Harvard Architectures
Used mostly in RISC CPUs
Separate program bus and databus: can be of different widths
For example, PICs use: Data memory (RAM): a small
number of 8bit registers
Program memory (ROM): 12bit,14bit or 16bit wide (in EPROM,FLASH, or ROM)
Von-Neumann Architecture
Used in: 80X86 (CISC PCs)
Only one bus between CPU andmemory
RAM and program memory sharethe same bus and the samememory, and so must have the
same bit width Bottleneck: Getting instructions
interferes with accessing RAM
8/10/2019 PIC Microcontroller Introduction
13/81
RISC vs. CISC
Reduced Instruction Set
Computer (RISC)
Used in: SPARC, ALPHA,
Atmel AVR, etc.
Few instructions
(usually < 50) Only a few addressing
modes
Executes 1 instruction in 1
internal clock cycle (Tcyc)
Complex Instruction Set
Computer (CISC)
Used in: 80X86, 8051,
68HC11, etc.
Many instructions
(usually > 100) Several addressing modes
Usually takes more than 1
internal clock cycle (Tcyc)
to execute
8/10/2019 PIC Microcontroller Introduction
14/81
Pipelining in PIC
Instruction Pipeline FlowInstruction Pipeline Flow
8/10/2019 PIC Microcontroller Introduction
15/81
V d:
Pipelining for Instruction Execution
8/10/2019 PIC Microcontroller Introduction
16/81
The Microchip PIC family
8/10/2019 PIC Microcontroller Introduction
17/81
Baseline Architecture
8/10/2019 PIC Microcontroller Introduction
18/81
PICMicrocontroller Families
PIC microcontrollers are designed using theHarvard Architecture which includes:
Microprocessor unit (MPU)
Program memory for instructions
Data memory for data
I/O ports
Support devices such as timers
8/10/2019 PIC Microcontroller Introduction
19/81
Microcontroller withthe Harvard Architecture
8/10/2019 PIC Microcontroller Introduction
20/81
Characteristics
RISC CPUs 8-bit
16-bit
Number of I/O pins: 4-70
Memory types and sizes:
Flash; OTP; ROM
0.5k 256k
8/10/2019 PIC Microcontroller Introduction
21/81
Speeds
All PICs require oscillators to executeinstructions:
Internal* (low speeds, up to 8 MHz)
External (high speeds, up to 40 MHz)
Instructions are executed at least at
oscillator speed (4 clocks/instruction)
(*Note: not all PICs have internal oscillators)
8/10/2019 PIC Microcontroller Introduction
22/81
A/D converters and C/C modules
All PICs have between 0 and 16 A/Dconverters with 8/10-bit resolution
8-16 bit Timers/Counters
Comparator Modules (0-2)
8/10/2019 PIC Microcontroller Introduction
23/81
The PIC Family: Speed
Can use crystals, clock oscillators, or even an RC circuit.Can use crystals, clock oscillators, or even an RC circuit. Some PICs have a built in 4MHz RC clock, Not very accurate,Some PICs have a built in 4MHz RC clock, Not very accurate,
but requires no external components!but requires no external components!
Instruction speed = 1/4 clock speed (Instruction speed = 1/4 clock speed (TcycTcyc = 4 *= 4 * TclkTclk))
All PICs can be run from DC to their maximum specified speed:All PICs can be run from DC to their maximum specified speed:
40MHz18Cxxx
33MHz17C4x / 17C7xxx
20MHz16Cxxx
10MHz12C67x
4MHz12C50x
8/10/2019 PIC Microcontroller Introduction
24/81
Clock and Instruction Cycles
Instruction ClockInstruction Clock Clock from the oscillator enters a microcontroller via OSC1 pin where internal circuit of a
microcontroller divides the clock into four even clocks Q1, Q2, Q3, and Q4 which do not overlap. These four clocks make up one instruction cycle (also called machine cycle) during which one
instruction is executed.
Execution of instruction starts by calling an instruction that is next in string.
Instruction is called from program memory on every Q1 and is written in instruction register on Q4.
Decoding and execution of instruction are done between the next Q1 and Q4 cycles. On the
following diagram we can see the relationship between instruction cycle and clock of the oscillator
(OSC1) as well as that of internal clocks Q1-Q4. Program counter (PC) holds information about the address of the next instruction.
8/10/2019 PIC Microcontroller Introduction
25/81
The PIC Family: ProgramMemory
Technology: EPROM, FLASH, or ROM
It varies in size from one chip to another.
-- examples:examples:
16bit
instructions
16384 (16k)17C766
14bitinstructions8192 (8k)16F877
14bit
instructions
1024 (1k)16C711
12bitinstructions
51212C508
8/10/2019 PIC Microcontroller Introduction
26/81
Family Core Architecture Differences
The PIC Family: Cores 12bit cores with 33 instructions: 12C50x, 16C5x
14bit cores with 35 instructions: 12C67x,16Cxxx
16bit cores with 58 instructions: 17C4x,17C7xx
Enhanced 16bit cores with 77 instructions: 18Cxxx
8/10/2019 PIC Microcontroller Introduction
27/81
The PIC Family: Data Memory
PICs use general purposePICs use general purpose File registersFile registers for RAM (eachfor RAM (each
register is 8bits for all PICs)register is 8bits for all PICs)
-- examples:examples:
902B RAM17C766
368B RAM + 256B of
nonvolatile EEPROM
16F877
36B RAM16C71C
25B RAM12C508
8/10/2019 PIC Microcontroller Introduction
28/81
PIC Programming Procedure
For example: in programming an embedded PIC featuring electronicallyerasable programmable read-only memory (EEPROM). The essential
steps are:
Step 1: On a PC, type the program, successfully compile it and then
generate the HEX file.
Step 2: Using a PIC device programmer, upload the HEX file into the PIC.
This step is often called "burning".
Step 3: Insert your PIC into your circuit, power up and verify the program
works as expected. This step is often called "dropping" the chip. If it isn't,you must go to Step 1 and debug your program and repeat burning and
dropping.
8/10/2019 PIC Microcontroller Introduction
29/81
PIC Programming Procedure
8/10/2019 PIC Microcontroller Introduction
30/81
8/10/2019 PIC Microcontroller Introduction
31/81
Memory Unit
8/10/2019 PIC Microcontroller Introduction
32/81
Memory Unit
Memory:
Registers:
Address:
8/10/2019 PIC Microcontroller Introduction
33/81
8/10/2019 PIC Microcontroller Introduction
34/81
CPU
8/10/2019 PIC Microcontroller Introduction
35/81
CPU
8/10/2019 PIC Microcontroller Introduction
36/81
8/10/2019 PIC Microcontroller Introduction
37/81
8/10/2019 PIC Microcontroller Introduction
38/81
Input-output unit
Those locations we've just added are called"ports".
There are several types of ports : input, output or
bidiectional ports. When working with ports, first of all it is necessary
to choose which port we need to work with, and
then to send data to, or take it from the port.
8/10/2019 PIC Microcontroller Introduction
39/81
Input-output unit
8/10/2019 PIC Microcontroller Introduction
40/81
Input-output unit
Input register:
Output register:
Connecting with other unit:
8/10/2019 PIC Microcontroller Introduction
41/81
Time unit
8/10/2019 PIC Microcontroller Introduction
42/81
Time unit
Counter:
Excute:
8/10/2019 PIC Microcontroller Introduction
43/81
Watchdog
Trong trng hp chng trnh b kt, b m sreset li vi x l khi trn.
8/10/2019 PIC Microcontroller Introduction
44/81
Watchdog
Why a Watchdog timer is needed?
Excute:
8/10/2019 PIC Microcontroller Introduction
45/81
Analog to Digital Converter
8/10/2019 PIC Microcontroller Introduction
46/81
Analog to Digital Converter
a Analog to Digital Converter why is needed?
Excute:
8/10/2019 PIC Microcontroller Introduction
47/81
PIC16F Microcontroller
Families
8/10/2019 PIC Microcontroller Introduction
48/81
Architecture of the PIC16F
Buses: Communication lines for transferring data within the processor.
Oscillator: Used to drive the microprocessor, clocking data andinstructions in the processor.
Timing: The PIC has an internal divide by 4 whereby 4 oscillator pulses
form one clock pulse. This makes instruction times easy to calculate.
Most instructions (except calls and returns and otherinstructions involving jumps and branches) take one clock
cycle, so with a 4MHz oscillator (divided by 4), instructions
take 1s.
The calculation of execution times is important. We will use
timed delay subroutines in the laboratory to slow down traffic lightLED sequences (otherwise pedestrians will have 1s to cross the
road!) The PIC16F also has a hardware timer, TMR0, which we
shall consider later.
8/10/2019 PIC Microcontroller Introduction
49/81
Architecture - continuedProgram counter: The program counter stores the current programposition. After each instruction the program counter is incrementedautomatically so that it points to the location of the next instruction ordata in memory.
Stack: The stack is used to save program counter contents whensubroutines are called. The PIC16F has an 8-level stack.
Reset vector: On power-up or reset, the PIC16F will go to program
memory and begin executing instructions sequentially.Interrupt vector: In the PIC16F, this points to 0x04, so that if aninterrupt occurs, the first instruction to be executed will be at thislocation. Interrupts are configured in the interrupt control register.
Status register: The status register is a very important registerwhich
contains all the arithmetic status of the ALU and Reset status. Thecontents of the status register are updated after certain instructions
which modify the W (working) register.
8/10/2019 PIC Microcontroller Introduction
50/81
Microcontroller FeaturesMicrocontrollers now come with a wide range of features, forexample, watchdog timers, sleep/wakeup modes, powermanagement, powerful I/O channels, and so on.
Watchdog timer
A watchdog timer provides a means of graceful recovery from asystem problem. This could be a program that goes into an
endless loop, or a hardware problem that prevents the programfrom operating correctly.
If the program fails to reset the watchdog at some predeterminedinterval, a hardware reset will be initiated. The bug may still
exist, but at least the system has a way to recover. This isparticularly useful for unattended systems.
See the CLRWDT instruction.
8/10/2019 PIC Microcontroller Introduction
51/81
The PIC 16F87x SeriesMicrocontrollerSome of the characteristics of the PIC 16F87x series
High performance, low cost, for embedded applications Only 35 instructions
Each instruction is exactly one word long and is
executed in 1 cycle (except branches which take two
cycles )
4K words (14bit) flash program memory
192 bytes data memory (RAM) + 128 bytes EEPROM
data memory
Eight level deep hardware stack
Internal A/D converter, serial port, digital I/O ports,
timers, and more!
8/10/2019 PIC Microcontroller Introduction
52/81
Example: PIC 16F877A
8 kbytes of FLASH Program Memory
368 bytes of Data Memory (RAM)
256 bytes of EEPROM Data Memory
33 input or output pins
20 MHz operating speed(200 ns instruction
cycle)
Max. 25 mA current from an output pin
8/10/2019 PIC Microcontroller Introduction
53/81
Example: PIC16F877A
5/6 Programming pins
8 A/D channels
2 Oscillator Inputs
2 RS-232 inputs
33 I/O ports
8/10/2019 PIC Microcontroller Introduction
54/81
PIC16F877A
8/10/2019 PIC Microcontroller Introduction
55/81
8/10/2019 PIC Microcontroller Introduction
56/81
8/10/2019 PIC Microcontroller Introduction
57/81
Microprocessor Unit Registers
Program Counter (PC)
Bank Select Register (BSR)
File Select Registers (FSRs)
Control unit
8/10/2019 PIC Microcontroller Introduction
58/81
PIC16F - Address Buses
Address bus
Address bus for program memory addressing
capacity:
Address bus for data memory addressing
capacity:
330_02 58
8/10/2019 PIC Microcontroller Introduction
59/81
Data Bus and Control Signals
Data bus
Control signals
8/10/2019 PIC Microcontroller Introduction
60/81
PIC16F877A Pin Layout
PORTA PORTB
PORTE
PORTC PORTC
PORTD
PORTD
ADCinputs
Counter
0
external
input
8/10/2019 PIC Microcontroller Introduction
61/81
PIC 16F877A pin out
8/10/2019 PIC Microcontroller Introduction
62/81
PIC Program Memory The PIC16F877 8192 (8k) 14bit instructions
If interrupted, programexecution continues from
here
When thecontroller is reset,
program execution
starts from here
Takes a max of 8 addresses, the
ninth address will write over the first.
8/10/2019 PIC Microcontroller Introduction
63/81
PIC Memory
program memory:
Data Memory :
8/10/2019 PIC Microcontroller Introduction
64/81
PIC Data Memory
The data memory is devided into 4 memory banks
The most
important
registershave
addresses
in all the
four
banks
8/10/2019 PIC Microcontroller Introduction
65/81
Special Function RegisterSTATUS Register
8/10/2019 PIC Microcontroller Introduction
66/81
PIC Peripherals
Each peripheral has a set of SFRs to control its operation.
Different PICs have different on-board peripherals
8/10/2019 PIC Microcontroller Introduction
67/81
8/10/2019 PIC Microcontroller Introduction
68/81
PIC Peripherals: Ports (Digital I/O)
Ports are basically digital I/O pins which exist in all PICs
The PIC16F877A have the following ports:
PORT A has 6 bit wide, Bidirectional
PORT B,C,D have 8 bit wide, Bidirectional
PORT E has 3 bit wide, Bidirectional
Ports have 2 control registers
TRISx
PORTx
Pin functionality overloaded with other features
Most pins have 25mA source/sink thus it can drive LEDs directly
8/10/2019 PIC Microcontroller Introduction
69/81
8/10/2019 PIC Microcontroller Introduction
70/81
PIC Peripherals: Analogue to Digital Converter
The A/D module has four registers. These registers are: A/D Result High Register (ADRESH)
A/D Result Low Register (ADRESL)
A/D Control Register0 (ADCON0) A/D Control Register1 (ADCON1)
Multiplexed 8 channel inputs Must wait Tacq to charge up sampling capacitor
Can take a reference voltage different from that of the controller
8/10/2019 PIC Microcontroller Introduction
71/81
PIC Peripherals: USART: UART
Serial Communications Peripheral:Universal Synch./Asynch. Receiver/Transmitter
Interrupt on TX buffer empty and RX buffer full
Asynchronous communication: UART (RS-232C serial)
Can do 300bps - 115kbps
8 or 9 bits, parity, start and stop bits, etc.
Outputs 5V so you need a RS232 level converter (e.g.,MAX232)
8/10/2019 PIC Microcontroller Introduction
72/81
8/10/2019 PIC Microcontroller Introduction
73/81
PIC Peripherals: Timers
Available in all PICs.
generate interrupts on timer overflow.
Some 8bits, some 16bits, some have prescalers and/or
postscalers
Can use external pins as clock in/clock out
(ie, for counting events or using a different Fosc)
PIC16F877A Block Diagram
8/10/2019 PIC Microcontroller Introduction
74/81
PIC16F877A Block Diagram
InstructionBus
Most
important
register in
the PIC
must be
involved in
all
arithmetic
operations
Data Bus
Data
Memory
Instruction
Memory
8/10/2019 PIC Microcontroller Introduction
75/81
PIC16F877A Instruction Set
8/10/2019 PIC Microcontroller Introduction
76/81
Programming PIC 16F877A
Assembler (MPLAB)
Basic (Pic Basic Pro)
C (HITEC PICC)
Parallel port
Serial port
8/10/2019 PIC Microcontroller Introduction
77/81
PIC Applications
LED FlasherLoop:
bsf PORTB, 0
call Delay_500ms
bcf PORTB, 0
call Delay_500msgoto Loop
8/10/2019 PIC Microcontroller Introduction
78/81
PIC Applications
Button ReadMovlw 0
movwf TRISD, f
bsf TRISD, 2
Loop:
btfsc PORTD, 2
goto light
goto No_light
Light:
bsf PORTB,0
goto Loop
No_light:bcf PORTB,0
goto Loop
8/10/2019 PIC Microcontroller Introduction
79/81
LCDDEFINE OSC 4DEFINE LCD_DREG PORTBDEFINE LCD_DBI T 4DEFINE LCD_RSREG PORTBDEFINE LCD_RSBI T 0DEFINE LCD_EREG PORTBDEFINE LCD_EBI T 1DEFINE LCD_BI TS 4DEFINE LCD_LI NES 2
DEFINE LCD_COMMANDUS 2000DEFINE LCD_DATAUS 50
LCDOUT 254, 1, "MERHABA"LCDOUT 254, 192, "2x16 LCD"
END
8/10/2019 PIC Microcontroller Introduction
80/81
Analog to Digital ConversionFormat: ADCIN Channel,Var
Sample Program:
ABCVAR BYTEADCON1 = 2 ' PORTA is
analog
INPUT PORTA. 0
LOOP:ADCIN PORTA. 0, ABC
LCDOUT 254, 1, #ABC
PAUSE 100
GOTO LOOP
8/10/2019 PIC Microcontroller Introduction
81/81
Pulse Width ModulationFormat:
HPWM Channel,Dutycycle,Frequency
Sample Program:
DEFINE CCP1_REG PORTC'Hpwm 1 pin port
DEFINE CCP1_BI T 2 'Hpwm 1 pin bit
HPWM 1, 64, 1000
' Send a 25% duty cycle PWM signal at
1kHz
END