PIC Microcontroller: and Its Applications
PIC Microcontroller: and Its Applications
PIC Microcontroller
And its Applications
Neeru Chaudhary
Assistant Professor
Physics Dept.
P U Chd
1
These devices were originally designed for use in applications
with
• 16-bit microprocessors and computer peripherals,
• remote control transmitters,
• domestic products and
• automotive systems.
2 NC PU CHD
• A simple microcontroller consists of the
following modules:
• An Arithmetic Logic Unit (ALU)
• One or more working registers (called accumulators in the past)
for temporary storage during computations. A register is a small
block of memory, often the size of a byte, where data is stored.
• Program memory (ROM) and data memory (RAM).
• A program counter.
• An instruction register with instruction decoder.
• The control unit.
• A stack.
3 NC PU CHD
NC PU CHD
Introduction
• The PIC microcontroller was developed by General
Instruments in 1975
• PIC stands for Peripheral Interface Controller. The General
Instruments used the acronyms Programmable Interface
Controller and Programmable Intelligent Computer for the
initial PICs (PIC1640 and PIC1650).
• In 1993, Microchip Technology launched the 8-bit
PIC16C84 with EEPROM which could be programmed using
serial programming method. The improved version of
PIC16C84 with flash memory (PIC18F84 and PIC18F84A) hit
the market in 1998.
4
NC PU CHD
Classification
The 8-bit PIC microcontroller is divided into
following four categories on the basis of
internal architecture:
• Base Line PIC
• Mid-Range PIC
• Enhanced Mid-Range PIC
• PIC18
5
NC PU CHD
6
NC PU CHD
Mid-Range PIC
• Mid-Range PICs are based on 14-bit instruction
architecture and are able to work up to 20 MHz
speed.
• These controllers are available with 8 to 64 pin
packaging.
• These microcontrollers are available with different
peripherals like ADC, PWM, Op-Amps and different
communication protocols like USART, SPI, I2C (TWI),
etc. which make them widely usable microcontrollers
not only for industry but for hobbyists as well.
7
NC PU CHD
8
NC PU CHD
PIC18
9
NC PU CHD
10
NC PU CHD
PIC18 high end core devices
11
NC PU CHD
PIC24 and dsPIC 16-bit microcontrollers
12
NC PU CHD
PIC32 32-bit microcontrollers
13
Following table compares the above four categories:
14
NC PU CHD
15 NC PU CHD
NC PU CHD
16
Pipelining:
Early processors and controllers could fetch or execute a single instruction in a unit of
time. The PIC microcontrollers are able to fetch and execute the instructions in the same
unit of time thus increasing their instruction throughput. This technique is known as
instruction pipelining where the processing of instructions is split into a number of
independent steps.
17 NC PU CHD
Features:
· C Compiler Optimized Architecture with Optional
Extended Instruction Set
· 100,000 Erase/Write Cycle Enhanced Flash
· Program Memory Typical
· 1,000,000 Erase/Write Cycle Data EEPROM Memory
Typical
· Flexible oscillator option
Four Crystal modes, including High-Precision PLL for
USB
Two External Clock modes, Up to 48 MHz
Internal Oscillator: 8 user-selectable
frequencies, from 31 kHz to 8 MHz
Dual Oscillator Options allow Microcontroller and
USB module to Run at different Clock Speeds
18 NC PU CHD
Memory:
1. Flash Memory: Flash memory is used to store the program
downloaded by a user on to the microcontroller. Flash memory is
non-volatile, i.e., it retains the program even after the power is
cut-off. PIC18F4550 has 32KB of Flash Memory.
2. EEPROM: This is also a nonvolatile memory which is used to
store data like values of certain variables. PIC18F4550 has 256
Bytes of EEPROM.
3. SRAM: Static Random Access Memory is the volatile memory
of the microcontroller, i.e., it loses its data as soon as the power is
cut off. PIC18F4550 is equipped with 2 KB of internal SRAM.
19 NC PU CHD
Oscillator: The PIC18F series has flexible clock options.
An external clock of up to 48 MHz can be applied to this
series. These controllers also consist of an internal oscillator
which provides eight selectable frequency options varying
from 31 KHz to 8 MHz.
8x8 Multiplier: The PIC18F4550 includes an 8 x 8
multiplier hardware. This hardware performs the
multiplications in single machine cycle. This gives higher
computational throughput and reduces operation cycle &
code length.
20 NC PU CHD
· ADC Interface: PIC18F4550 is equipped with 13 ADC (Analog to
Digital Converter) channels of 10-bits resolution. ADC reads the analog
input, for example, a sensor input and converts it into digital value that
can be understood by the microcontroller.
· Timers/Counters: PIC18F4550 has four timer/counters. There is
one 8-bit timer and the remaining timers have option to select 8 or 16
bit mode. Timers are useful for generating precision actions, for
example, creating precise time delays between two operations.
· Interrupts: PIC18F4550 consists of three external interrupts
sources. There are 20 internal interrupts which are associated with
different peripherals like USART, ADC, Timers, and so on.
21 NC PU CHD
· EUSART: Enhanced USART (Universal Synchronous and
Asynchronous Serial Receiver and Transmitter) module is full-duplex
asynchronous system. It can also be configured as half-duplex
synchronous system. The Enhanced USART has the feature for
automatic baud rate detection and calibration, automatic wake-up
on Sync Break reception and 12-bit Break character transmit. These
features make it ideally suited for use in Local Interconnect Network
bus (LIN bus) systems.
· ICSP and ICD: PIC18F series controllers have In Circuit Serial
Programming facility to program the Flash Memory which can be
programmed without removing the IC from the circuit. ICD (In Circuit
Debugger) allows for hardware debugging of the controller while it is
in the application circuit.
22 NC PU CHD
·
SPI: PIC18F supports 3-wire SPI communication
between two devices on a common clock source. The data
rate of SPI is more than that of USART.
· I2C: PIC18F supports Two Wire Interface (TWI) or I2C
communication between two devices. It can work as both
Master and Slave device.
· USB: PIC18F supports full-speed USB with different
clock options
23 NC PU CHD
NC PU CHD
Instruction set
• Mov instructions - they copy a value from/to a file
register or literal to/from register W
• Logic and arithmetic instructions with a file
register and register W as operands
• Logic and arithmetic instructions with a literal and
register W as operands
• One operand Logic and Arithmetic instructions
24
NC PU CHD
• AN EXAMPLE OF PIC
25
RA0 To RA4 RA is a bidirectional port.
26 NC PU CHD
NC PU CHD
BLOCK DIAGRAM
27
NC PU CHD
FEATURES
• The PIC architecture is based on a configuration
known as a Harvard machine structure where
• In the PIC16F84, the program bus is 14 bits
wide, whereas the data bus is 8 bits wide.
• In addition, the PIC family is based on a
Reduced Instruction Set Computer (RISC)
• PIC16XXX devices have only 35 instructions
28
• The PIC family are fully static devices, meaning that they
preserve the contents of their registers when the clock
frequency is reduced to zero.
29 NC PU CHD
• A watchdog timer is an internal timer running independently of
the system clock. It resets the device in the event of a program
or circuit malfunction or if an unknown logical state is
encountered.
• For example, if the program hangs, the watchdog timer will time
out and reset the processor. The PIC16F84 has a watchdog timer
with a timeout period of approximately 18msec, with no
prescaler, determined by a separate internal RC oscillator.
30 NC PU CHD
NC PU CHD
Program memory
31
• The program counter (PC) is a 13 bit wide register which will
enable 8K (8 × 1024) program address locations to be
addressed.
32 NC PU CHD
• DATA MEMORY
The data memory is separated into two areas, one for the special
function registers and one for the general purpose registers.
• The memory area is further partitioned into two banks which
require bank switching in the program to correctly access
registers.
• For example, access to register PORTA requires Bank0 to be
selected, whereas Bank1 must be selected to access register
TRISB.
• Certain special function registers such as STATUS can be
accessed from either of the two banks.
33 NC PU CHD
NC PU CHD
DATA MEMORY
34
• The special function registers are
TMR0, OPTION, PCL, STATUS, FSR, PORTA, PORTB, TRISA, TRISB,
EEDATA, EECON1, EEADR, EECON2, PCLATH and INTCON
• PORTA is associated with five input/output pins, RA0 to RA4 and
their I/O status is controlled by setting or clearing bits in the
TRISA register.
• Similarly, PORTB is an eight bit wide port and its I/O status is
controlled by the TRISB register
35 NC PU CHD
• STATUS SFR
• The first three bits (STATUS<0> to STATUS<2>) are the carry (C),
digit carry (DC) and zero (Z) flags of the ALU respectively.
• Bits 5 and 6 (RP0 and RP1) are the bank selection bits.
36 NC PU CHD
• OPTION register allows the programmer to select timer settings
and other parameters.
37 NC PU CHD
• Clock source options
• There are five possible source options for the instruction cycle clock oscillator. Choosing
an option is done when the PIC program is loaded into ROM.
There are three crystal oscillator options - low power, LP, medium speed, XT and high
speed, HS.
The low power option is intended for use with crystals with frequencies up to 200kHz; the
XT
option for crystals up to 4MHz and the HS option for crystals operating up to 10MHz. The
values of the oscillation capacitors in each case is given in the data sheet [3] for the PIC
device.
• It is also possible to run the PIC using an external clock source from a square wave signal
generator on all but the RC option. The external signal is fed directly into pin 16 (OSC1)
and
must have a peak to peak voltage of 0 to 5V. Ensure that there is no voltage offset on the
signal
before applying it to the oscillator pins.
39 NC PU CHD
NC PU CHD
40
• Microprocessor Unit
• Includes Arithmetic Logic Unit (ALU), Registers,
and Control Unit
• Arithmetic Logic Unit (ALU)
• Instruction decoder
• 16-bit instructions
• Status register that stores flags
• 5-bits
41 330_02
• Microprocessor Unit
• Registers
• Program Counter (PC)
• 21-bit register that holds the Program Memory address
• Bank Select Register (BSR)
• 4-bit register used in direct addressing the Data Memory
• File Select Registers (FSRs)
• 12-bit registers used as memory pointers in indirect addressing
Data Memory
• Control unit
• Provides timing and control signals
• Read and Write operations
42 330_02
• PIC18F - Address Buses
• Address bus
• 21-bit address bus for Program Memory
• Addressing capacity: 2 MB
• 12-bit address bus for Data Memory
• Addressing capacity: 4 KB
43 330_02
• Data Bus and Control Signals
• Data bus
• 16-bit instruction/data bus for Program Memory
• 8-bit data bus for Data Memory
• Control signals
• Read and Write
44 330_02
• PIC18F452/4520 Memory
• Program Memory: 32 K
• Address range: 000000 to 007FFFH
• Data Memory: 4 K
• Address range: 000 to FFFH
• Data EEPROM
• Not part of the data memory space
• Addressed through special function registers
45 330_02
• PIC18F452/4520 Memory
46 330_02
• Data Memory with Access Bank
47 330_02
• PIC18F452 I/O Ports
• Five I/O ports
• PORT A through PORT E
• Most I/O pins are multiplexed
• Generally have eight I/O pins
• Addresses already assigned to these ports
• Each port is identified by its assigned SFR
48 330_02
• I/O Ports A and B
49 330_02
• Data Transfer
50 330_02
• MCU Support Devices
• Timers
• Capture, Compare and PWM (CCP Modules)
• Serial Communications
• Master Synchronous Serial Port (MSSP)
• Addressable USART
• A/D converter
• Parallel Slave Port (PSP)
• Data EEPROM
51 330_02
• MCU Support Devices
52
• PIC18F Special Features
• Sleep mode
• Watchdog timer (WDT)
• Code protection
• In-circuit debugger
53 330_02
NC PU CHD
Advantages
54
NC PU CHD
Limitations
• One accumulator
• Register-bank switching is required to access the entire RAM of
many devices
• Operations and registers are not orthogonal; some instructions
can address RAM and/or immediate constants, while others can
only use the accumulator
• The following stack limitations have been addressed in the PIC18
series, but still apply to earlier cores:
The hardware call stack is not addressable, so preemptive task
switching cannot be implemented
Software-implemented stacks are not efficient, so it is difficult to
generate reentrant code and support local variables
55
NC PU CHD
COMPILERS
• Microchip released their own C compilers, C18
and C30, for the line of 18F 24F and 30/33F
processors.
• MPLAB software available free of charge from
Microchip Technology
56
NC PU CHD
57
NC PU CHD
• Mobile phone
• Rice cooker
• MP3/MP4 player
• Toys
58
http://www.hobbyprojects.com/pic_tutorials/tutorial1.html
59 NC PU CHD
60 NC PU CHD
NC PU CHD
61
NC PU CHD
Useful websites
1. http://www.eg3.com
2. http://www.ARM.MCU.com
3. http://www.mcjournal.com
4. http://www.iar.com
5. http://http://www.embedded.com
6. http://www.powersoftsystems.com
62