Unit 1 Architecture of Pic 16cXX
Unit 1 Architecture of Pic 16cXX
PIC stands for Peripheral Interface Controller given by Microchip Technology to identify its single-chip microcontrollers. These
devices have been very successful in 8-bit microcontrollers. The main reason is that Microchip Technology has continuously
upgraded the device architecture and added needed peripherals to the microcontroller to suit customers' requirements. The
development tools such as assembler and simulator are freely available on the internet at www.microchip.com .
12C5XX
16C5X
16C505
Mid range PIC Architectures
Mid range PIC architectures are built by upgrading low-end architectures with more number of peripherals, more number of registers
and more data/program memory. Some of the mid-range devices are
16C6X
16C7X
16F87X
Program
C
F
RC = Mask ROM
memory
type
is
indicated
by
=
=
an
alphabet.
EPROM
Flash
1.
2.
Instruction set simplicity: The instruction set consists of just 35 instructions (as opposed to 111 instructions for 8051).
3.
Power-on-reset and brown-out reset. Brown-out-reset means when the power supply goes below a specified voltage (say
4V),
it
causes
PIC
to
reset;
hence
malfunction
is
avoided.
A watch dog timer (user programmable) resets the processor if the software/program ever malfunctions and deviates from
its normal operation.
4.
5.
6.
7.
Powerful output pin control (25 mA (max.) current sourcing capability per pin.)
8.
9.
Data Memory is also known as Register File. Register File consists of two components.
1.
2.
Device
Program
Memory
(14bits)
Data RAM
(bytes)
I/O
ADC
Pins
16C74A
4K EPROM
192
33
16F877
8K Flash
368 (RAM)
256 (EEPROM)
33
Timers
8/16
bits
CCP
(PWM)
USART
SPI / I2C
8 bits x
2/1
8 channels
USART
SPI / I2C
10 bits x 2/1
8 channels
USART
SPI / I2C
Device
Interrupt
Sources
Instruction
Set
16C74A
12
35
16F877
15
35
Most of the PIC microcontrollers can operate upto 20MHz. One instructions cycle
(machine cycle) consists of four clock cycles.
Fig 17.1 Relation between instruction cycles and clock cycles for PIC
microcontrollers
Instructions that do not require modification of program counter content get
executed in one instruction cycle.
Although the architectures of various midrange 8 - bit PIC microcontroller are not
the same, the variation is mostly interns of addition of memory and peripherals. We
will discuss here the architecture of a standard mid-range PIC microcontroller,
16C74A. Unless mentioned otherwise, the information given here is for a PIC
16C74A microcontroller Chip.Architecture of PIC16C74A
Fig 17.3
W register
STATUS Register
The STATUS register is a 8-bit register that stores the status of the processor. This
also stores carry, zero and digit carry bits.
STATUS - address 03H, 83H
It can be noted that some of the special purpose registers are available both in
Bank-0 and Bank-1. These registers have the same value in both banks. Changing
the register content in one bank automatically changes its content in the other
bank.
Port Structure and Pin Configuration of PIC 16C74A
As mentioned earlier, there is a large variety of PIC microcontrollers. However, the
midrange architectures are widely used. Our discussion will mainly confine to
PIC16C74A whose architecture has most of the required features of a mid-range PIC
microcontroller. Study of any other mid-range PIC microcontroller will not cause
much variation from the basic architecture of PIC 16C74A ..
PIC 16C74A has 5 I/O Ports. Each port is a bidirectional I/O port. In addition, they
have the following alternate functions.
In addition to I/O pins, there is a Master clear pin (MCLR) which is equivalent to reset
in 8051. However, unlike 8051, MCLR should be pulled low to reset the micro
controller. Since PIC16C74Ahas inherent power-on reset, no special connection is
required with MCLR pin to reset the micro controller on power-on.
There are two VDD pins and two VSS pins. There are two pins (OSC1 and OSC2) for
connecting the crystal oscillator/ RC oscillator. Hence the total number of pins with a
16C74A is 33+7=40. This IC is commonly available in a dual-in-pin (DIP) package.
Every instruction fits in a single 14-bit word. In addition, every instruction also executes in a
single cycle, unless it changes the content of the Program Counter. These features are due to
the fact that PIC micro controller has been designed on the principles of RISC (Reduced
Instruction Set Computer) architecture.
Instruction set:
Mnemonics
Description
Instructi
on Cycles
bcf f, b
bsf f, b
clrw
clrf f
Clear f
movlw k
movwf f
Move W to f
movf f, F(W)
Move f to F or W
swapf f, F(W)
andlw k
andwf f, F(W)
andwf f, F(W)
iorlw k
iorwf f, F(W)
xorlw k
xorwf f, F(W)
addlw k
addwf f, F(W)
sublw k
subwf f, F(W)
rlf f, F(W)
rrf f, F(W)
btfsc f, b
1/2
btfss f, b
1/2
decfsz f, F(W)
1/2
incfcz f, F(W)
goto label
call label
retrun
retlw k
retie
enable interrupt
clrwdt
sleep
nop
No operation
Encoding of instruction:
As has been discussed, each instruction is of 14-bit long. These 14-bits contain both op-code
and the operand. Some examples of instruction encoding are shown here.
Example-1:
bcf f, b
Operands:
0 f 127
0b7
Encoding:
The instruction is executed in one instruction cycle, i.e., 4 clock cycles. The activities in various
clock cycles are as follows.
Example-2:
goto K
Operand:
Operation:
Encoding:
PC <10:0>
PCLATH <4:3>
PC <12:11>
Since this instruction requires modification of program Counter, it takes two instruction cycles
for execution.
Q-Cycle activities are shown as follows.
Select Bank-0
Clears the data latch
Select Bank-1
W
03H ( data direction )
Set RA0-RA3 as outputs, RA4-RA5 as inputs
Port-B
Port-B is an 8-bit bidirectional I/O port. The data direction in Port-B is controlled by TRISB
register. Setting a bit in TRISB register puts the corresponding output in high impedance input
mode. When a bit in TRISB is made zero, the corresponding pin in Port-B outputs the content of
the latch (output mode).
Each port pin has a weak internal pull-up that can be enabled by clearing bit
of OPTION
register (bit-7). When a pin is configured in the output mode, the weak pull-up is automatically
turned off. Internal pull-up is used so that we can directly drive a device from the pins.
PIC 16C74A has three modules, viz., Timer-0, Timer-1 and Timer-2. Timer-0 and Timer-2 are 8-bit timers. Timer-1 is a 16-bit timer.
Each timer module can generate an interrupt on timer overflow.
Timer-0 Overview:
The timer-0 module is a simple 8-bit UP counter. The clock source can be either the internal clock (f osc /4) or an external clock. When
the clock source is external, the Timer-0 module can be programmed to increment on either the rising or falling clock edge. Timer-0
module has a programmable pre-scaler option. This pre-scaler can be assigned either to Timer-0 or the Watch dog timer, but not to
both.
The Timer-0 Counter sets a flag T0IF (Timer-0 Interrupt Flag) when it overflows and can cause an interrupt at that time if that
interrupt source has been enabled, (T0IE = 1), i.e., timer-0 interrupt enable bit = 1.
OPTION Register Configuration :
Option Register (Addr: 81H) Controls the prescaler and Timer -0 clock source. The following OPTION register configuration is for
clock source = fosc /4 and no Watchdog timer.
The pre-scalar can be used either with the Timer-0 module or with the Watchdog timer. The pre-scalar is available for Timer-0 if the
pre-scalar assignment bit PSA in the OPTION register is 0. Pre-scalar is a programmable divide by n counter that divides the
available clock by a pre-specified number before applying to the Timer-0 counter.
imer - 1 Module
Timer 1 module is a 16-bit timer/counter consisting of two 8-bit registers (TMR1H and TMR1L)
which are readable and writable. The TMR1 register pair (TMR1H:TMR1L) increments from
0000H to FFFFH and rolls over to 0000H. The TMR1 interrupt, if enabled, is generated on
overflow, which sets the interrupt flag bit TMR1IF (bit-0 of PIR1 register). This interrupt can be
enabled/disabled by setting/clearing TMR1 interrupt enable bit TMR1IE (bit-0 of the PIE1
register).
The operating and control modes of Timer1 are determined by the special purpose register
T1CON.
Various bits of T1CON register are given as follows:-
Timer1 ON bit
0 = stops Timer 1;
1 = Enables Timer 1
T1OSCEN:
Select bits
Prescaler Value
T1CKPS T1CKPS
1
0
1
1:8
1:4
1:2
1:1
Fig 21.2
Operation of Timer 1
As a timer (TMR1CS = 0). In the timer mode, Timer 1 increments in every instruction
cycle. The timer 1 clock source is
. Since the internal clock is selected, the timer is
always synchronized and there is no further need of synchronization.
As a counter (TMR1CS = 1). In the counter mode, external clock input from the pin
RCO/T1CKI is selected.
Timer 2 Overview
Fig 21.3
Timer 2 is an 8 - bit timer with a pre-scaler and a post-scaler. It can be used as the PWM time
base for PWM mode of capture compare PWM (CCP) modules. The TMR2 register is readable
and writable and is cleared on device reset.
The input clock (
) has a pre-scaler option of 1:1, 1:4 or 1:16 which is selected by bit 0
and bit 1 of T2CON register respectively.
The Timer 2 module has an 8bit period register (PR2). Timer-2 increments from 00H until it is
equal to PR2 and then resets to 00H on the next clock cycle. PR2 is a readable and writable
register. PR2 is initailised to FFH on reset.
The output of TMR2 goes through a 4bit post-scaler (1:1, 1:2, to 1:16) to generate a TMR2
interrupt by setting TMR2IF.
Fig 21.5
PIC16C74A has two CCP Modules. Each CCP module contains a 16 bit register (two 8-bit registers) and can operate in one of the
three modes, viz., 16-bit capture, 16-bit compare, or up to 10-bit Pulse Width Modulation (PWM). The details of the two modules
(CCP1 and CCp2) are given as follows.
CCP1 Module:
CCP1 Module consists of two 8-bit registers, viz., CCPR1L (low byte) and CCPR1H (high byte). The CCP1CON register controls the
operation of CCP1 Module.
CCP2 Module:
CCP2 Module consists of two 8 bit registers, viz., CCPR2L (Low byte) and CCPR2H (high byte). The CCP1CON register controls
the operation of CCP2 Module.
Both CCP1 and CCP2 modules are identical in operation with the exception of the operation of special event trigger.
The following table shows the timer resources for the CCP Mode.
CCP Mode
Timer Used
Capture
Timer 1
Compare
Timer 1
PWM
Timer 2
Bit
5-4:
CCP1X CCP1Y: PWM least significant bits. These bits are of no use in Capture mode. In PWM Mode, these bits are the two Lsbs of
the PWM duty cycle. The eight Msbs are found in CCPR1L. Thus the PWM mode operates in 10-bit mode.
Bit
3-0:
CCP1M3:CCP1MO
(CCP1
Mode
select
bits)
0000=Capture/Compare/PWM
Mode
off
ADC Module
An analog-to-digital converter (ADC) converts an analog signal into an equivalent digital number. PIC 16C74A has an inbuilt ADC
with the following features -
8-bit conversion
An analog multiplexer
A sample and hold circuit for signal on the selected input channel
Port A and Port E pins are used for analog inputs/reference voltage for ADC. In A/D conversion, the input analog voltage is digitized
and an equivalent digital output is generated as shown in the figure.
Most of mid range PIC microcontrollers include a Synchronous Serial Port (SSP) Module. The discussion in this section is relevant
to PIC16C74A only. SSP Module section can be configured in either of the following two modes.
Either of these modes can be used to interconnect two or more PIC chips to each other using a minimal number of wires for
communication. Alternatively, either can be used to connect a PIC microcontroller to a peripheral chip. When I 2C mode is selected,
the peripheral chip must also have an I 2C interface. On the other hand, the SPI mode provides the clock and serial data lines for
direct connection to shift registers. This leads to increased I/O interface capability and an arbitrary number of I/O devices can be
connected to a PIC microcontroller. SPI can also achieve data rate significantly higher than I2C. Both the communication methods
are synchronous, i.e., the data transfer is synchronized with an explicit clock signal.
Two special purpose registers control the synchronous serial port (SSP) operations. These registers are:
The SPI port requires RC3/SCK pin to be an output that generates the clock signal used by the external shift registers. When SPI is
configured in the slave mode, RC3/SCK pin works as the input for the clock.
When a byte is written to SSPBUF register, it is shifted out of RC5/SDO pin in synchronous with the emitted clock pulses on
RC3/SCK pin. The MSB of SSPBUF is the first bit to appear on RC5/SDO pin. Simultaneously, the same write to SSPBUF also
initiates the 8 bit data reception into SSPBUF of whatever appears on RC4/SDI pin at the time of rising edges of the clock on SCK
pin. Hence shifting-in and shifting-out of data occur simultaneously.
SSPIF interrupt flag is cleared by the user software if already in the set mode. The interrupt is enabled. Any write to SSPBUF
initiates the data transfer, i.e., transmission and reception. The clock pulses (8 clock pulses) are output through SCK pin. The data is
received through SDI. When CKP=1 (SSPCON<4>), data changes at SDO at negative clock transition and is read through SDI at
positive clock transition. The idle state of clock is high. If CKP=0, data appears at SDO at positive clock transition and is read
through SDI at negative clock transition. The idle state of the clock is low. These are shown in the following diagrams.
Though SPI is a serial communication interface, it can be used to realize multiple output parallel ports and multiple input parallel ports.
We will consider this realization of an output parallel port and an input port separately.
Parallel Output Port Realization
A parallel 8-bit output port can be realized through SPI with the help of a shift register chip (74HC595) as shown in Fig 25.1. RC5/SD0
pin outputs serial data while RC3/SCK oin outputs the serial clock. Since input data transfer is not required, port pin RC4/SDI is used to
latch the shift register data to the output pins of the shift register. Hence RC4 is configured as an output pin.
Fig 25.1 PIC connection (in SPI mode) with a shift register
When an 8-bit data is written to SSPBUF, the data is shifted out of RC5/SD0 pin. With CKP = 1, the data is stable at the positive
transition but changes at the negative transition. The shift shifts the data at the positive clock transition. After 8 clock pulses, all 8-bits
are shifted in the shift register. The completion of data transfer is indicated by SSPIF interrupt flag becoming ' 1' . The interrupt service
routine make RC4 ' 1' , thus latching the 8-bit data to the output of the shift register. The configuration of various registers are shown in
Fig 25.2
Port configurations
Fig 25.3 Realization of an 8-bit parallel input port with PIC in SPI mode.
Port configurations
Fig 25.4 gives the configurations various registers for inputs parallel port realization.
I 2C stands for Inter-Integrated circuit. I 2C communication is a two wire bi-directional interface for connecting one or more master
processors with one or more slave devices, such as an EEPROM, ADC, RAM, LCD display, DAC, etc. I 2C interface requires two
open drain I/O pins, viz. SDA (Serial Data) and SCL (Serial Clock).
The reason for open drain connection is that the data transfer is bi-directional and any of the devices connected to the I 2C bus can
drive the data line (SDA). The serial clock line (SCL) is usually driven by the master. Since SDA and SCL pins are open drain pins,
external pull-up resistances are required for operation of I 2C bus.
A typical I2C bus showing the connection of multi-master and multi-slave configuration is shown in the following figure.
1.
2.
Data in SDA line is stable during clock (SCL) high. A new bit is initiated at the negative clock transition after a specified hold
time.
3.
4.
An acknowledgement bit (0) is driven by the receiver after the end of reception. If the receiver does not acknowledge, SDA
line remains high (1).
I2C bus transfer consists of a number of byte transfers within a START condition and either another START condition or a STOP
condition. During the idle state when no data transfer is taking place, both SDA and SCL lines are released by all the devices and
remains high. When a master wants to initiate a data transfer, it pulls SDA low followed by SCL being pulled low. This is called
START condition. Similarly, when the processor wants to terminate the data transfer it first releases SCL (SCL becomes high) and
then SDA. This is called a STOP condition. START and STOP conditions are shown in the diagram as follows.
After sending the 7-bit address of the slave, the master sends the address (usually 8 bit) of the internal register of the slave
wherefrom the data has to be read or written to. The subsequent access is automatically directed to the next address of the internal
register.
The following diagrams give the general format to write and read from several peripheral internal registers.
Alternately, the indirect pointer FSR can have the address of TRISC and the required bit setting and bit clearing can be done
indirectly.
SCL
SDA equ 4
equ
The instruction bsf INDF, SDA will release the SDA line(as RC4/SDA pin is configured as an input, hence tristated), letting the
external pullup resister pull it high or some I 2C Slave device/Chip pull it low.
When FSR is used for indirect addressing, care should be taken to restore FSR value when subroutine is completed and the
program returns to the main line program.
I 2 C Subroutine
SDA
SCL equ 3
equ
The following subroutine DATA_OUT transfers out three bytes, i.e., ADDRDEV, ADDR8, and DATAWRTE
DATA_OUT:
call
START
movf
ADDRDEV,
W
call
;
;
Sends
TRBYTE
7-bit
Generate
peripheral
start
address
;
with
condition
R/
=0
Transmit
PIC Microcontroller offers a mechanism by which an 8-bit parallel bidirectional data transfer can be achieved between a PIC
Microcontroller and a PC. PIC Microcontroller's Port-D and Port-E are used in this data transfer. For this data transfer, Port-D of PIC
Microcontroller is configured as a Parallel slave Port (PSP) by setting bit-4 of TRISE Register. The pins of Port-E function as control
pins
(
,
and
)
for
data
The connection diagram between PC and the PIC Microcontroller in PSP Mode is shown below.
ADCON1, ADD: 9F H
TRISE:
transfer.
This register plays a crucial role in PSP configuration and control. The lower three bits control the data direction of PortE. the upper
four bits are used in conjunction with parallel slave port as shown here.
TRISE, ADD: 89 H
As explained, PSP Mode facilitates bidirectional 8-bit parallel data transfer. After ADCON1<b2-b0> and TRISE<b4, b2-b0> bits are
set by the user program, PORTD and PORTE are configured for PSP. When PC wants to write an 8-bit data to PIC, it addresses the
PIC microcontroller and the I/O address decoding circuit makes
go low selecting the PIC chip. PC also makes
(I/O write)
pin low and floats the data through its data bus (b7-b0). The data is written to PORTD and IBF flag in TRISE Register is set
indicating that a byte is waiting at PORTD input buffer to be read by the PIC. Simultaneously PSPIF flag bit of PIR1 register is set
and an interrupt is generated if PSPIE, PEIE and GIE bits have been set (i.e., the peripheral PSP interrupt is enabled.). After the
data is read from PORTD, IBF bit automatically becomes zero; however PSPIF bit has to be cleared by software. If a second byte is
written by the PC before the first byte is read, the second byte is lost and the IBOV flag in TRISE register is set indicating this loss.
Similarly a byte can also be read by the PC from the PIC microcontroller. When PIC writes a byte to PORTD, OBF flag is set
indicating that the byte is waiting to be read by the PC. When the PC reads this bytes, OBF flag in TRISE Register is automatically
cleared and the interrupt flag bit PSPIF is set indicating that the byte has been read by the PC from PIC microcontroller.