Unit Ii: PIC Microcontrollers
Unit Ii: PIC Microcontrollers
Unit Ii: PIC Microcontrollers
Unit II
UNIT II
PIC Microcontrollers
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
Low - end PIC Architectures :
Microchip PIC microcontrollers are available in various types. When PIC
microcontroller MCU was first available from General Instruments in early 1980's, the
microcontroller consisted of a simple processor executing 12-bit wide instructions
with basic I/O functions. These devices are known as low-end architectures. They
have limited program memory and are meant for applications requiring simple
interface functions and small program & data memories. Some of the low-end device
numbers are
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 memory type is indicated by an alphabet.
C = EPROM
F = Flash
RC = Mask ROM
Popularity of the PIC microcontrollers is due to the following factors.
1.
Speed: Harvard Architecture, RISC architecture, 1 instruction cycle = 4 clock
cycles.
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.
1
Embedded Systems
Unit II
4.
A watch dog timer (user programmable) resets the processor if the
software/program ever malfunctions and deviates from its normal operation.
5.
PIC microcontroller has four optional clock sources.
o
Low power crystal
o
Mid range crystal
o
High range crystal
o
RC oscillator (low cost).
6.
Programmable timers and on-chip ADC.
7.
Up to 12 independent interrupt sources.
8.
Powerful output pin control (25 mA (max.) current sourcing capability per pin.)
9.
EPROM/OTP/ROM/Flash memory option.
10. I/O port expansion capability.
11. Free assembler and simulator support from Microchip at www.microchip.com
CPU Architecture:
The CPU uses Harvard architecture with separate Program and Variable (data)
memory interface. This facilitates instruction fetch and the operation on
data/accessing of variables simultaneously.
Embedded Systems
Unit II
USAR
T SPI /
I2C
USART
SPI /
I2 C
USART
SPI /
I2 C
Embedded Systems
Unit II
Interrupt
Sources
12
15
Instruction
Set
35
35
Device
16C74A
16F877
PIC Microcontroller Clock
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.
Embedded Systems
Unit II
Architecture of PIC16C74A
Embedded Systems
Unit II
Embedded Systems
Unit II
PCLATH Register
(Program Counter Latch, address = 0AH, 8AH)
PCLATH is a 8-bit register which can be used to decide the upper 5bits of the
program counter. PCLATH is not the upper 5bits of the program counter.
PCLATH can be read from or written to without affecting the program
counter. The upper 3bits of PCLATH remain zero and they serve no purpose.
When PCL is written to, the lower 5bits of PCLATH are automatically loaded
to the upper 5bits of the program counter, as shown in the figure.
Embedded Systems
Unit II
Embedded Systems
Unit II
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 poweron.
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.
Embedded Systems
Unit II
10
Embedded Systems
Unit II
11
Embedded Systems
Unit II
2. Indirect Addressing
Indirect unlike direct addressing does not take an address from an instruction but
derives it from IRP bit of STATUS and FSR registers. Addressed location is accessed
via INDF register which in fact holds the address indicated by a FSR. In other words,
any instruction which uses INDF as its register in reality accesses data indicated by a
FSR register. Let's say, for instance, that one general purpose register (GPR) at
address 0Fh contains a value of 20. By writing a value of 0Fh in FSR register we will
get a register indicator at address 0Fh, and by reading from INDF register, we will get
a value of 20, which means that we have read from the first register its value without
accessing it directly (but via FSR and INDF). It appears that this type of addressing
does not have any advantages over direct addressing, but certain needs do exist during
programming which can be solved smoothly only through indirect addressing.
Such examples include sending a set of data via serial communication, working with
buffers and indicators (which will be discussed further in a chapter with examples), or
erasing a part of RAM memory (16 locations) as in the following instance.
12
Embedded Systems
Unit II
Reading data from INDF register when the contents of FSR register is equal to zero
returns the value of zero, and writing to it results in NOP operation (no operation).
Reading from EEPROM Memory
Setting the RD bit initializes transfer of data from address found in EEADR register to
EEDATA register. As in reading data we don't need so much time as in writing, data
taken over from EEDATA register can already be used further in the next instruction.
Sample of the part of a program which reads data in EEPROM, could look something
like the following:
After the last program instruction, contents from an EEPROM address zero can be
found in working register w.
Writing to EEPROM Memory
In order to write data to EEPROM location, programmer must first write address to
EEADR register and data to EEDATA register. Only then is it useful to set WR bit
which sets the whole action in motion. WR bit will be reset, and EEIF bit set
following a writing what may be used in processing interrupts. Values 55h and AAh
are the first and the second key whose disallow for accidental writing to EEPROM to
13
Embedded Systems
Unit II
occur. These two values are written to EECON2 which serves only that purpose, to
receive these two values and thus prevent any accidental writing to EEPROM
memory. Program lines marked as 1, 2, 3, and 4 must be executed in that order in even
time intervals. Therefore, it is very important to turn off interrupts which could change
the timing needed for executing instructions. After writing, interrupts can be enabled
again.
Example of the part of a program which writes data 0xEE to first location in
EEPROM memory could look something like the following:
NOTE: It is recommended that WREN be turned off the whole time except when
writing data to EEPROM, so that possibility of accidental writing would be
minimal. All writing to EEPROM will automatically clear a location prior to writing a
new.
14
Embedded Systems
Unit II
Embedded Systems
Unit II
Description
Clear bit b of register f
Set bit b of register f
Clear working register W
Clear f
Move literal 'k' to W
Move W to f
Move f to F or W
Swap nibbles of f, putting result in F or W
And literal value into W
And W with F and put the result in W or F
And W with F and put the result in W or F
inclusive-OR literal value into W
inclusive-OR W with f and put the result in
F or W
Exclusive-OR literal value into W
Exclusive-OR W with f and put the result in
F or W
Add the literal value to W and store the
result in W
Add W to f and store the result in F or W
Subtract the literal value from W and store
the result in W
Subtract f from W and store the result in F
or W
Copy f into F or W; rotate F or W left
through the carry bit
Copy f into F or W; rotate F or W right
through the carry bit
Test 'b' bit of the register f and skip the next
instruction if bit is clear
Test 'b' bit of the register f and skip the next
16
Instructio
n Cycles
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1/2
1/2
Embedded Systems
Unit II
The instruction is executed in one instruction cycle, i.e., 4 clock cycles. The
activities in various clock cycles are as follows.
17
Embedded Systems
Example-2:
goto K
Operand:
Operation:
Unit II
Encoding:
18
Embedded Systems
Unit II
19
Embedded Systems
Unit II
20
Embedded Systems
Unit II
21
Embedded Systems
Unit II
22
Embedded Systems
Unit II
23
Embedded Systems
Unit II
Prescaler
Value
24
Embedded Systems
T1CKP T1CKPS0
S1
1
1
1
0
0
1
0
0
Unit II
1:8
1:4
1:2
1:1
Embedded Systems
Unit II
Example Program
Reading 16bit of free running Timer 1
movf TMR1H
;
read high byte
movwf TMPH
;
store in TMPH
movf TMR1L
;
read low byte
movwf TMPL
;
store in TMPL
movf TMR1H, W
;
read high byte in W
subwf TMPH, W
;
subtract 1 st read with 2 nd read
btfsc STATUS, Z
;
and check for equality
goto next ;
; if the high bytes differ, then there is an overflow
; read the high byte again followed by the low byte
movf TMR1H, W
;
read high byte
movwf TMPH
movf TMR1L, W
;
read low byte
movwf TMPL
next : nop
Timer 2 Overview
Embedded Systems
Unit II
27
Embedded Systems
Unit II
Fig 21.5 Schematic diagram showing the interrupt logic for PIC
Embedded Systems
Unit II
Timer 1
PWM
Timer 2
CCP1CON Register (Address 17H )
CCP2CON Register is exactly similar to CCP1CON register. CCP2CON Register
address is 1DH. CCP1CON controls CCP module1 where as CCP2CON controls
CCP Module2.
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
0100=Capture mode, every falling edge
0101=Capture mode, every rising edge
0110=Capture mode, every 4 th rising edge
0111=Capture mode, every 16 th rising edge
1000=Compare mode, set output on match (CCP1IF bit is set)
1001=Compare mode, clear output on match (CCP1IF bit is set)
1010=Compare mode, generate software interrupt on match (CCP1IF bit is set, CCP1
pin unaffected)
1011=Compare mode, trigger special event (CCP1IF bit is set;CCP1 resets Tmr1;
CCP2 resets TMR1 and starts A/D conversion if A/D module is Enabled)
11XX=PWM mode.
Capture Mode (CCP1):
Capture Mode captures the 16-bit value of TMR1 into CCPR1H:CCPR1L register
pair in response to an event occurring on RC2/CCP1 pin. Capture Mode for CCP2 is
exactly similar to that of CCP1.
29
Embedded Systems
Unit II
30
Embedded Systems
Unit II
31
Embedded Systems
Unit II
A PWM output as shown has a time period. The time for which the output stays high
is called duty cycle.
PWM Period
The PWM period is specified by writing to PR2 register. The PWM period can be
calculated using the following formula:
32
Embedded Systems
Unit II
33
Embedded Systems
Unit II
If the PWM duty cycle is longer than the PWM period, then the CCP1 pin will not be
cleared.
PWM Period and duty cycle calculation
Example:
Desired PWM frequency = 78.125 kHz
f osc = 20MHz
TMR2 Prescalar = 1
Find the maximum resolution of duty cycle that can be used with a 78.124 kHz
frequency and 20 MHz oscillator.
Embedded Systems
Unit II
PR2 value
Prescaler 1
Prescaler 4
Prescaler 16
10 bit
1024
255
19.53 KHz
4.88 kHz
1.22 kHz
10 bit
1000
249
20kHz
5kHz
1.25kHz
8 bit
256
63
78.125kHz
19.53kHz
4.88kHz
6 bit
64
15
312.5kHz
78.125kHz
19.53kHz
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
8 analog input channels
An analog multiplexer
A sample and hold circuit for signal on the selected input channel
Alternative clock sources for carrying out conversion
Adjustable sampling rate
Choice of an internal or external reference voltage
Interrupt to microcontroller on end of conversion
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.
35
Embedded Systems
Unit II
36
Embedded Systems
Unit II
37
Embedded Systems
Unit II
RA0 RA1
RA2
RA5
RA3
RE0
A
A
A
A
A
A
D
A
A
A
A
D
D
D
A
A
A
A
D
D
D
A
VREF
A
VREF
A
VREF
D
A
A
D
D
D
D
D
A
A
D
D
D
D
D
A
A
A
A
A
A
D
38
A
A
D
D
D
D
D
VDD
RA3
VDD
RA3
VDD
RA3
-
Embedded Systems
Unit II
Fig 23.6 Schematic diagram of A/D convertor analog inputs and reference
voltage
Steps for A/D conversion
39
Embedded Systems
Unit II
Embedded Systems
Unit II
Org 004H
Movf ADRES, W ; Result of A/D conversion in W
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.
Serial Peripheral Interface (SPI)
Inter Integrated Circuit (I2C)
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:
SSPCON (Synchronous Serial Port Control Register), Address: 14H
SSPSTAT(Synchronous Serial Port status Register), Address: 94H
Serial Peripheral Interface (SPI)
Port-C three pins, viz., RC5/SDO, RC4/SDI and RC3/SCK/SCL are mainly used for
SPI mode. In addition, one Port-A pin, viz., RA5/ /AN4 is used for slave select.
The schematic block diagram of SPI is shown in the figure
41
Embedded Systems
Unit II
42
Embedded Systems
Unit II
43
Embedded Systems
Unit II
Embedded Systems
Unit II
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
45
Embedded Systems
Unit II
Port configurations
Fig 25.4 gives the configurations various registers for inputs parallel port
46
Embedded Systems
Unit II
realization.
47
Embedded Systems
Unit II
Embedded Systems
Unit II
START and STOP conditions are unique and they never happen within a data
transfer.
Data Communication Protocol:
In I2C communication both 7-bit and 10-bit slave addressing are possible. In 7-bit
addressing mode 128 slaves can be interfaced with a single master. Similarly, in
10-bit addressing mode, 1024 slaves can be interfaced with the master. We will
discuss here 7-bit addressing mode only. 10-bit addressing mode is similar to 7-bit
addressing except from the fact that the number of address bits is more.
Following a 'start' condition, the master sends a 7-bit address of the slave on SDA
line. The MSB is sent first. After sending 7-bit address of the slave peripheral, a
R/ (8th bit) bit is sent by the master. If R/ bit is '0', the following byte (after the
acknowledgement bit) is written by the master to the addressed slave peripheral. If
R/ =1, the following byte (after the acknowledgement bit) has to be read from the
slave by the master.
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.
R/
Embedded Systems
Unit II
read by the master. If R/ is 1, the subsequent data are to be read by the master. If
R/ = 0, the subsequent data are to be written by the master to the addressed
slave. It has to be noted that the slave address is sent first, following a 'start'
condition. The addressed slave responds by acknowledging and gets ready for data
transfer.
If data has to be read from a specific address of the slave device, the master sends
the 7-bit address of the slave first following a 'start' condition. R/ bit is sent as
'low'. The addressed slave acknowledges by pulling the ACK line low. The master
then sends the 8-bit internal address of the slave from which data has to be read.
The slave acknowledges. Since R/ bit was initially 0, the master is in the write
mode. To change this to read mode, the 'start' condition is again generated
followed by 7-bit address of the slave with R/ = 1. The slave acknowledges. The
slave then sends data from previously specified internal address to the master. The
master acknowledges by pulling ACK bit low. The data transfer stops when the
master does not acknowledge the data reception and a 'stop' condition is generated.
Embedded Systems
Unit II
Embedded Systems
Unit II
call TRBYTE
bsf TRBUF, 7
; Generate NO ACK
call RCVBYTE
movwf DATARD
call STOP
return
The 'START' subroutine initializes I2C bus and then generates START
condition on the I 2C bus. START1 bypasses the initialization of I 2C.
START:
movlw 3BH
;enables I2C master mode
by programming SSPCON
movwf SSPCON
bcf PORTC, SDA
; drive SDA low when it
is an o/p
movlw TRISC
;set indirect pointer to
TRISC
movwf FSR
START1:
bsf INDF, SDA
; SDA=1
bsf INDF , SCL
; SCL=1
call DELAY
; Generates a suitable
; SDA=0
; Generate a suitable
;SCL=0
;SDA=0
; SCL=1
; Generate a suitable
delay
delay
STOP:
delay
bsf INDF, SDA
;SDA=1
return
The subroutine 'TRBYTE' send out the byte available in w. It returns with Z=1
if ACK occurs. It returns with Z=0 if NOACK occurs.
TRBUF is an 8-bit RAM variable used for temporary storage. The bits are
shifted to carry flag (C) and the carry bit transmitted successively. Data transfer
is complete when all 8-bits are transmitted. Setting C = 1 initially sets an index
52
Embedded Systems
Unit II
rlf
TRBUF, F
movf RBUF,F
btfss STATUS, Z
call out_bit
; Send a bit available in C
btfss STATUS, Z
goto TR_1
call
in_bit
; Get the ACK bit in RCBUF<0>
movlw 01H
;
andwf RCBUF, W ; Store the complement of ACK bit in Z flag
return
The RCVBYTE subroutine receives a byte from I2 C into W using a RAM
variable RCBUF buffer.
Call RCVBYTE with bit 7 of TRBUF clear for ACK
Call RCVBYTE with bit 7 of TRBUF set for NOACK
RCBUF is an 8-bit RAM variable used for recieving the data. the bit is recieved
in the RCBUF<0> and is rotated successively through RCBUF as shown. The
reception ends when all 8-bits are recieved.
RCVBYTE:
movlw 01H
movwf RCBUF
; Keep an index for 8-bits to be recieved.
RCV_1:
rlf
RCBUF, F
call In_bit
btfss STATUS, C
53
Embedded Systems
Unit II
goto RCV_1
rlf
TRBUF, F
call Out_bit
movf RCBUF,w
return
The out_bit subroutine transmits carry bit, then clears the carry bit.
Out_bit:
bcf INDF, SDA
btfsc STATUS, C
bsf INDF, SDA
; Send carry bit
bsf INDF, SCL
call DELAY
bcf INDF, SCL
bcf STATUS,C
; Clear carry bit
return
The in_bit subroutine receives one bit into bit-0 of RCBUF.
In_bit:
bsf INDF,SDA
bsf INDF, SCL
bcf RCBUF, 0
btfsc PORTC, SDA
; Check SDA line for data bit
bsf RCBUF, 0
bcf INDF, SCL
return
Example of I 2 C interfacing
DAC interfacing on I 2 C bus:
MAX518 is a dual 8-bit Digital to Analog Converter (DAC) with I2C interface.
The address of the device is selectable through two pins AD1 and AD0 . This
device works in I2C slave mode. The connection diagram is shown as follows.
54
Embedded Systems
Unit II
For
the
present
connection
AD1
=
0
The
device
address
is
Three bytes are sent to output an analog voltage.
First byte (Address of the DAC and R/ bit )
55
and
AD0
010
1
1101
Embedded Systems
Unit II
Third byte
(The 8-bit digital data(B) to be converted to analog voltage)
B
Analog output voltage = V DD x B/256
Special Features
ICSP
In Circuit Serial
Programming
WDT
BOR
POR
Power on reset
PWRT
PoWeR up Time
OST
SLEEP
PIC microcontroller
sleepmode
WDT
If your software goes haywire then this timer resets the processor. To stop the
reset the well behaved software must periodically issue the CLRWDT instruction
to stop a resert. The WDT runs using its own oscillator. It runs during sleep and
shares Timer 0 prescaler.
POR
56
Embedded Systems
Unit II
OST
Oscillator Startup Timer delays for 1024 oscillator cycles after PWRT (if PWRT is
enabled) ensuring that the oscillator has started and is stable. It is automatic and
only used for crystal oscillator modes and is active after POR or wake from sleep.
SLEEP
Sleep mode (or low power consumption mode) is entered by executing the 'SLEEP'
command. The device can wake from sleep caused by an external reset, Watch
Dog Timer timeout, INT pin RB port change or peripheral interrupt.
57
Embedded Systems
Unit II
TRISE:
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
58
Embedded Systems
Unit II
59