You are on page 1of 81

Bi ging K thut Vi x l

Microcontroller Architecture
PIC16F Family
i hc Nng
Trng i hc Bch Khoa
Khoa in t Vin Thng
B mn K thut My tnh
Instructor: Phm Xun Trung
Reference Material
PIC 16F87X data sheet
PICMicro Mid-Range MCU Family Reference
Manual Available on course web page
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
MICROCONTROLLER
Programmable Chip
Same idea as PC, but
on much smaller
scale
Why a microcontroller is
needed?
Sensors
Microcontrollers
Actuators
Variety of microcontrollers
Atmel AT90S8535
Motorola 68HC11
Intel - 8051
Texas Instruments -
Microchip PIC16F84, PIC16F877
Why PIC microcontrollers?
Easy to program
Better online documantation
Introduction
What is PIC?
- A family of Harvard architecture microcontrollers made
by Microchip Technology
- Derived from the PIC1650 originally developed by
General Instrument Microelectronics Division.
- The name PIC was originally an acronym for
"Programmable Intelligent Computer(?)
Peripheral Interface Controller (?)
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 flash
memory capability
Its code is extremely efficient, allowing the PIC to run
with typically less program memory than its larger
competitors
PIC is very small and easy to implement for non-
complex problems and usually accompanies to the
microprocessors as an interface
Introduction
REVIEW
MCU Architectures
Hardvard vs Von Neumann
RISC vs CISC
Pipelining
Two Different Architectures
Harvard Architectures
Used mostly in RISC CPUs
Separate program bus and data
bus: 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 and
memory
RAM and program memory share
the same bus and the same
memory, and so must have the
same bit width
Bottleneck: Getting instructions
interferes with accessing RAM
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
Pipelining in PIC
Instruction Pipeline Flow Instruction Pipeline Flow
V d:
Pipelining for Instruction Execution
The Microchip PIC family
Baseline Architecture
PICMicrocontroller Families
PIC microcontrollers are designed using the
Harvard Architecture which includes:
Microprocessor unit (MPU)
Program memory for instructions
Data memory for data
I/O ports
Support devices such as timers
Microcontroller with
the Harvard Architecture
Characteristics
RISC CPUs
8-bit
16-bit
Number of I/O pins: 4-70
Memory types and sizes:
Flash; OTP; ROM
0.5k 256k
Speeds
All PICs require oscillators to execute
instructions:
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)
A/D converters and C/C modules
All PICs have between 0 and 16 A/D
converters with 8/10-bit resolution
8-16 bit Timers/Counters
Comparator Modules (0-2)
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 (Tcyc Tcyc = 4 * = 4 * Tclk Tclk) )
All PICs can be run from DC to their maximum specified speed: All PICs can be run from DC to their maximum specified speed:
40MHz 18Cxxx
33MHz 17C4x / 17C7xxx
20MHz 16Cxxx
10MHz 12C67x
4MHz 12C50x
Clock and Instruction Cycles
Instruction Clock Instruction 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.
The PIC Family: Program
Memory
Technology: EPROM, FLASH, or ROM
It varies in size from one chip to another.
- - examples: examples:
16bit
instructions
16384 (16k) 17C766
14bit
instructions
8192 (8k) 16F877
14bit
instructions
1024 (1k) 16C711
12bit
instructions
512 12C508
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
The PIC Family: Data Memory
PICs use general purpose PICs use general purpose File registers File registers for RAM (each for RAM (each
register is 8bits for all PICs) register is 8bits for all PICs)
- - examples: examples:
902B RAM 17C766
368B RAM + 256B of
nonvolatile EEPROM
16F877
36B RAM 16C71C
25B RAM 12C508
PIC Programming Procedure
For example: in programming an embedded PIC featuring electronically
erasable 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.
PIC Programming Procedure
S khi c bn ca mt
microcontroller.
Memory Unit
Memory Unit
Memory:
Registers:
Address:
Memory Unit
Bit:
Byte:
Data:
CPU
CPU
Processor
In a
microprocessor
system or a
microcontroller,
a single
processor block
is in charge of
all input, output,
calculations and
control.
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.
Input-output unit
Input-output unit
Input register:
Output register:
Connecting with other unit:
Time unit
Time unit
Counter:
Excute:
Watchdog
Trong trng hp chng trnh b kt, b m s
reset li vi x l khi trn.
Watchdog
Why a Watchdog timer is needed?
Excute:
Analog to Digital Converter
Analog to Digital Converter
a Analog to Digital Converter why is needed?
Excute:
PIC16F Microcontroller
Families
Architecture of the PIC16F
Buses: Communication lines for transferring data within the processor.
Oscillator: Used to drive the microprocessor, clocking data and
instructions 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 other
instructions 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 light
LED sequences (otherwise pedestrians will have 1s to cross the
road!) The PIC16F also has a hardware timer, TMR0, which we
shall consider later.
Architecture - continued
Program counter: The program counter stores the current program
position. After each instruction the program counter is incremented
automatically so that it points to the location of the next instruction or
data in memory.
Stack: The stack is used to save program counter contents when
subroutines 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 an
interrupt occurs, the first instruction to be executed will be at this
location. Interrupts are configured in the interrupt control register.
Status register: The status register is a very important register which
contains all the arithmetic status of the ALU and Reset status. The
contents of the status register are updated after certain instructions
which modify the W (working) register.
Microcontroller Features
Microcontrollers now come with a wide range of features, for
example, watchdog timers, sleep/wakeup modes, power
management, powerful I/O channels, and so on.
Watchdog timer
A watchdog timer provides a means of graceful recovery from a
system problem. This could be a program that goes into an
endless loop, or a hardware problem that prevents the program
from operating correctly.
If the program fails to reset the watchdog at some predetermined
interval, a hardware reset will be initiated. The bug may still
exist, but at least the system has a way to recover. This is
particularly useful for unattended systems.
See the CLRWDT instruction.
The PIC 16F87x Series
Microcontroller
Some 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!
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
Example: PIC16F877A
5/6 Programming pins
8 A/D channels
2 Oscillator Inputs
2 RS-232 inputs
33 I/O ports
PIC16F877A
PIC16F877A Features
High Performance RISC CPU:
Only 35 single word instructions to learn
All single cycle instructions except for program
branches, which are two-cycle
Operating speed: DC - 20 MHz clock input DC - 200
ns instruction cycle
Microprocessor Unit
Includes Arithmetic Logic Unit (ALU),
Registers, and Control Unit
Arithmetic Logic Unit (ALU)
Instruction decoder
Status register that stores flags
WREG working register
Microprocessor Unit
Registers
Program Counter (PC)
Bank Select Register (BSR)
File Select Registers (FSRs)
Control unit

PIC16F - Address Buses


Address bus
Address bus for program memory addressing
capacity:

Address bus for data memory addressing


capacity:
330_02 58
Data Bus and Control Signals
Data bus
Control signals
PIC16F877A Pin Layout
PORTA
PORTB
PORTE
PORTC PORTC
PORTD
PORTD
A
D
C

i
n
p
u
t
s
Counter
0
external
input
PIC 16F877A pin out
PIC Program Memory
The PIC16F877 8192 (8k) 14bit instructions
If interrupted, program
execution continues from
here
When the
controller is reset,
program execution
starts from here
Takes a max of 8 addresses, the
ninth address will write over the first.
PIC Memory
program memory:
Data Memory :
PIC Data Memory
The data memory is devided into 4 memory banks
The most
important
registers
have
addresses
in all the
four
banks
Special Function Register
STATUS Register
PIC Peripherals
Each peripheral has a set of SFRs to control its operation.
Different PICs have different on-board peripherals
Peripheral Features
5 Digital I/O Ports
Three timer/counter modules
Timer0
Timer1:
Timer2:
A 10-bit ADC with 8 inputs
Two Capture, Compare, PWM modules Capture is 16-bit, max. resolution is
12.5 ns
Compare is 16-bit, max. resolution is 200 ns
PWM max. resolution is 10-bit
Synchronous Serial Port (SSP) with SPI(Master mode) and I2C
(Master/Slave)
Universal Synchronous Asynchronous Receiver Transmitter (USART/SCI) with 9-
bit address detection
Parallel Slave Port (PSP) 8-bits wide, with external RD, WR and CS controls
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
PIC Peripherals: Analogue to Digital
Converter
Only available in 14bit and 16bit cores
Fs (sample rate) < 54KHz
the result is a 10 bit digital number
Can generate an interrupt when ADC conversion
is done
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 T
acq
to charge up sampling capacitor
Can take a reference voltage different from that of the controller
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)
PIC Peripherals: USART: UART
Synchronous communication: i.e., with clock signal
SPI = Serial Peripheral Interface
3 wire: Data in, Data out, Clock
Master/Slave (can have multiple masters)
Very high speed (1.6Mbps)
Full speed simultaneous send and receive (Full duplex)
I2C = Inter IC
2 wire: Data and Clock
Master/Slave (Single master only; multiple masters clumsy)
Lots of cheap I2C chips available; typically < 100kbps
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
Instruction
Bus
Most
important
register in
the PIC
must be
involved in
all
arithmetic
operations
Data Bus
Data
Memory
Instruction
Memory
PIC16F877A Instruction Set
Programming PIC 16F877A
Assembler (MPLAB)
Basic (Pic Basic Pro)
C (HITEC PICC)
Parallel port
Serial port
PIC Applications
LED Flasher
Loop:
bsf PORTB, 0
call Delay_500ms
bcf PORTB, 0
call Delay_500ms
goto Loop
PIC Applications
Button Read
Movlw 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
LCD
DEFINE OSC 4
DEFINE LCD_DREG PORTB
DEFINE LCD_DBI T 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBI T 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBI T 1
DEFINE LCD_BI TS 4
DEFINE LCD_LI NES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
LCDOUT 254, 1, " MERHABA"
LCDOUT 254, 192, " 2x16 LCD"
END
Analog to Digital Conversion
Format: ADCIN Channel,Var
Sample Program:
ABC VAR BYTE
ADCON1 = 2 ' PORTA is
analog
INPUT PORTA. 0
LOOP:
ADCIN PORTA. 0, ABC
LCDOUT 254, 1, #ABC
PAUSE 100
GOTO LOOP
Pulse Width Modulation
Format:
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

You might also like