100% found this document useful (1 vote)
33 views42 pages

Unit 2

The document provides an overview of PIC microcontroller architecture, including its features, families, and programming model. It contrasts microcontrollers with microprocessors, highlights the criteria for selecting a microcontroller, and details the architecture of the PIC18 series, including its memory structure and I/O ports. Additionally, it discusses the functionalities of various components such as timers, ADCs, and communication interfaces within the PIC microcontroller.

Uploaded by

sandeep.jadhav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
33 views42 pages

Unit 2

The document provides an overview of PIC microcontroller architecture, including its features, families, and programming model. It contrasts microcontrollers with microprocessors, highlights the criteria for selecting a microcontroller, and details the architecture of the PIC18 series, including its memory structure and I/O ports. Additionally, it discusses the functionalities of various components such as timers, ADCs, and communication interfaces within the PIC microcontroller.

Uploaded by

sandeep.jadhav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit- 2 PIC Microcontroller Architecture

Introduction to microcontroller, Criteria for selection of microcontroller, Features and architecture,


Comparison of PIC 18 series microcontrollers; Details of Pins, Pin Diagram PIC 18 internal Architecture:
ALU, EEPROM, RAM, IO Ports, Timer, ADC, Serial port PIC18 microcontroller programming model, Bus
architecture Registers of PIC18F, Interrupts of PIC18F Program memory and data memory organization

Introduction to Microcontroller
Introduction
A microcontroller is a compact integrated circuit designed to perform a specific control-oriented
task. It consists of a CPU, memory, and input/output peripherals on a single chip.
Microcontrollers are widely used in embedded systems where dedicated control and real-time
operation are required.

Microprocessor vs Microcontroller
A microprocessor contains only the central processing unit and requires external memory and
peripherals. A microcontroller integrates CPU, memory, and I/O ports on a single chip.
Microprocessors are used in general-purpose computing systems, whereas microcontrollers are
mainly used in embedded applications. Microcontrollers consume less power and are cost-
effective for control-based systems.

Embedded System
An embedded system is a specialized computing system designed to perform a specific function.
It uses a microcontroller as its core component. Examples include washing machines, microwave
ovens, digital thermometers, traffic control systems, and industrial automation equipment.

Features of a Microcontroller

1
A microcontroller provides on-chip program memory, data memory, input/output ports, timers
and counters, interrupt handling, serial communication interfaces, and low-power operating
modes. These features make it suitable for real-time control applications.

Introduction to PIC Microcontrollers


PIC microcontrollers are developed by Microchip Technology. PIC stands for Peripheral
Interface Controller. These microcontrollers follow RISC and Harvard architectures, offering
high-speed operation, low power consumption, and efficient instruction execution.

PIC Microcontroller Families


PIC microcontrollers are available in different families. The PIC16 family is an 8-bit
microcontroller commonly used for learning and basic embedded applications. The PIC18 family
is an enhanced 8-bit microcontroller with improved instruction set, larger memory, and advanced
peripherals suitable for complex embedded systems.

Architecture of PIC Microcontroller


The PIC microcontroller architecture consists of a central processing unit, program memory, data
memory, input/output ports, timers, analog-to-digital converter, interrupt controller, and serial
communication modules. The integration of these components on a single chip enables efficient
embedded system design.

2
Harvard Architecture
PIC microcontrollers use Harvard architecture, where program memory and data memory are
stored separately. This allows simultaneous access to instructions and data, resulting in faster
execution and improved system performance.

RISC Architecture
PIC microcontrollers are based on Reduced Instruction Set Computer architecture. They use a
small set of simple instructions, allowing faster instruction execution and efficient pipelining.

Criteria for selection of microcontroller


There are five major 8-bit microcontrollers. They are: Freescale Semiconductor's (formerly Motorola)
68HC08/68HC11, Intel's 8051, Atmel's AVR, Zilog's Z8, and PIC from Microchip Technology. Each of the
above micro-controllers has a unique instruction set and register set; therefore, they are not compatible
with each other. Programs written for one will not run on the others.

With all these different microcontrollers, what criteria do designers consider in choosing one? Three
criteria in choosing microcontrollers are as follows: (1) meeting the computing needs of the task at hand
efficiently and cost effectively; (2) availability of software and hardware development tools such as
compilers, assemblers, debuggers, and emulators; and (3) wide availability and reliable sources of the
microcontroller.

Criteria for choosing a microcontroller

1. The first and foremost criterion in choosing a microcontroller is that it must meet the task at hand
efficiently and cost effectively. In analyzing the needs of a microcontroller-based project, we must first
see whether an 8-bit, 16-bit, or 32-bit microcontroller can best handle the computing needs of the task
most effectively. Among other considerations in this category are:

(a) Speed. What is the highest speed that the microcontroller supports?

(b) Packaging. Does it come in a 40-pin DIP (dual inline package) or a QFP (quad flat package), or some
other packaging format? This is important in terms of space, assembling, and prototyping the end
product.

(c) Power consumption. This is especially critical for battery-powered products.

(d) The amount of RAM and ROM on the chip.

(e) The number of I/O pins and the timer on the chip.

3
(f) Ease of upgrade to higher-performance or lower-power-consumption versions.

(g) Cost per unit. This is important in terms of the final cost of the product in which a microcontroller is
used. For example, some microcontrollers cost 50 cents per unit when purchased 100,000 units at a
time.

2. The second criterion in choosing a microcontroller is how easy it is to develop products around it. Key
considerations include the availability of an assembler, debugger, a code-efficient C language compiler,
emulator, technical support, and both in-house and outside expertise. In many cases, third-party vendor
(i.c., a supplier other than the chip manufacturer) support for the chip is as good as, if not better than,
support from the chip manufacturer.

3. The third criterion in choosing a microcontroller is its ready availability in needed quantities both now
and in the future. For some designers this is even more important than the first two criteria. Currently,
of the leading 8-bit micro-controllers, the 8051 family has the largest number of diversified (multiple
source) suppliers. (Supplier means a producer besides the originator of the microcontroller.) In the case
of the 8051, which was originated by Intel, several companies also currently produce (or have produced
in the past) the 8051.

Features of PIC Microcontroller


PIC microcontrollers are widely used in embedded systems due to their rich set of features that
make them efficient and reliable for control applications.

• On-Chip CPU: Central processing unit integrated on the chip for computation and
control.
• Program Memory (ROM/Flash): Stores the user program; Flash memory allows easy
reprogramming.
• Data Memory (RAM & SFR): Stores temporary data and special function registers for
peripheral control.
• I/O Ports: Multiple digital and analog input/output pins to interface with sensors,
displays, and actuators.
• Timers and Counters: Built-in modules for event counting, time delays, and generating
PWM signals.
• Analog-to-Digital Converter (ADC): Converts analog signals from sensors into digital
form for processing.
• Serial Communication: UART, SPI, and I²C modules for communication with other
devices or microcontrollers.
• Interrupt System: Supports external and internal interrupts for real-time event handling.
• Low Power Operation: Sleep modes and idle modes for battery-operated applications.
• RISC Architecture: Fewer instructions, simple execution, and faster instruction cycle.
• Harvard Architecture: Separate program and data memory for simultaneous access and
improved speed

4
PIC Microcontrollers Architecture
The PIC microcontroller is based on RISC architecture. Its memory architecture
follows the Harvard pattern of separate memories for program and data, with
separate buses.

5
1. Memory Structure
The PIC architecture consists of two memories: Program memory and Data
memory.

PIC microcontroller program ROM

In microcontrollers, the ROM is used to store programs and for that reason it is
called program or code ROM. Although the PIC18 has 2M (megabytes) of
program (code) ROM space, not all family members come with that much ROM
installed. The program ROM size can vary from 4K to 128K at the time of this
writing, depending on the family member. The PIC18 program ROM is available
in different memory types, such as flash, OTP, and masked, all of which have
different part numbers. Note that although different flavors of the PIC18 exist in
terms of speed and amount of on-chip RAM/ROM, they are all compatible with
each other as far as the instructions are concerned. This means that if you write
your program for one, it will run on any of them regardless of the chip number.

PIC microcontroller with UV-EPROM

Some of the PIC microcontrollers use UV-EPROM, for on-chip program ROM. To
use these kinds of chips for development requires access to a PROM burner, as
well as a UV-EPROM eraser to erase the contents of ROM. The window on the UV-
EPROM chip allows the UV light to erase the ROM. The problem with the UV-
EPROM is that it takes around 20 minutes to erase the chip before it can be
programmed again. This has led Microchip to introduce a flash version of the PIC
family. At this time flash is replacing the UV-EPROM altogether.

PIC18Fxxx with flash

Many PIC18 chips have on-chip program ROM in the form of flash memory. The
flash version uses the letter F in the part number to indicate that the on-chip
ROM is flash. PIC18F458 is an example of PIC18 with flash ROM. The flash
version is ideal for fast development because flash memory can be erased in
seconds compared to the 20 minutes or more needed for the UV-EPROM version.
For this reason, the PIC18F has been used in place of the UV-EPROM to eliminate
the waiting time needed to erase the chip, thereby speeding up the development
time

6
OTP version of the PIC

OTP (one-time-programmable) versions of the PIC are also available from


Microchip. PIC16C432 chip uses OTP for program ROM. Contrast the PIC16C432
and PIC18F252. The letter C indicates the OTP ROM, while the letter F is for the
flash. The flash version is typically used for product development. When a
product is designed and absolutely finalized, the OTP version of the PIC is used
for mass production because it is cheaper than flash in terms of price per unit.
The problem with the OTP is that you cannot reprogram it if you want to modify
your program.

Masked version of PIC

Microchip Corporation provides a service in which you can send in your program
and they will burn the program into the PIC chip during the fabrication process
of the chip. This chip is commonly referred to as masked PIC, which is one of the
stages of IC fabrication. Masked PIC is the cheapest of all types, if the unit
numbers are high enough. This is because there is a minimum order masked
version of the PIC microcontrollers.

PIC microcontroller data RAM and EEPROM

While ROM is used to store program (code), the RAM space is for storage. The
PIC18 has a maximum of 4096 bytes (4K) of data RAM space. Not all of the family
members come with that much RAM. The data RAM size for the PIC18 varies
from 256 bytes to 4096 bytes. the data RAM space has two components: General-
Purpose RAM (GPR) and Special Function Registers (SFRs). Because the SFRs are
fixed and every microcontroller must have them, it is the GPR's size that varies
from chip to chip. The RAM GPR space is used for read/write scratch pad and
data manipulation and is divided into banks of 256 bytes each, The GPR size
given for the PIC18 is always a multiple of 256 bytes. In some of the PIC18 family
members, we also have a small amount of EEPROM to store critical data that does
not need to be changed very often. While every PIC18 must have some data RAM
for scratch pad, the EEPROM is optional, so not all versions of the PIC18 come
with EEP-ROM. EEPROM is used mainly for storage of critical data,

The special function registers consist of control registers to control different


operations of the chip resources like Timers, Analog to Digital Converters, Serial
ports, I/O ports, etc. For example, the TRISA register whose bits can be changed
to alter the input or output operations of the port A.

7
The general-purpose registers consist of registers that are used to store
temporary data and processing results of the data. These general-purpose
registers are each 8-bit registers.

Working Register: It consists of a memory space that stores the operands for each
instruction. It also stores the results of each execution.
Status Register: The bits of the status register denotes the status of the ALU
(arithmetic logic unit) after every execution of the instruction. It is also used to
select any one of the 4 banks of the RAM.
File Selection Register: It acts as a pointer to any other general-purpose
register. It consists of a register file address, and it is used in indirect addressing.
Another general-purpose register is the program counter register, which is a 13-
bit register. The 5 upper bits are used as PCLATH (Program Counter Latch) to
independently function as any other register, and the lower 8-bits are used as the
program counter bits. The program counter acts as a pointer to the instructions
stored in the program memory.

EEPROM: It consists of 256 bytes of memory space. It is a permanent memory


like ROM, but its contents can be erased and changed during the operation of the
microcontroller. The contents into EEPROM can be read from or written to, using
special function registers like EECON1, EECON, etc.

2. I/O Ports
PIC16 series consists of five ports, such as Port A, Port B, Port C, Port D, and Port
E.

Port A: It is a 16-bit port, which can be used as an input or output port based on
the status of the TRISA register.
Port B: It is an 8-bit port, which can be used as both an input and output port. 4 of
its bits, when used as input, can be changed upon interrupt signals.
Port C: It is an 8-bit port whose operation (input or output) is determined by the
status of the TRISC register.
Port D: It is an 8-bit port, which apart from being an I/O port, acts as a slave port
for connection to the microprocessor bus.
Port E: It is a 3-bit port that serves the additional function of the control signals to
the A/D converter.

8
3. Timers
PIC microcontrollers consist of 3 timers, out of which the Timer 0 and Timer 2
are 8-bit timers and the Time-1 is a 16-bit timer, which can also be used as
a counter.
4. A/D Converter
The PIC Microcontroller consists of 8-channels, 10-bit Analog to Digital
Converter. The operation of the A/D converter is controlled by these special
function registers: ADCON0 and ADCON1. The lower bits of the converter are
stored in ADRESL (8 bits), and the upper bits are stored in the ADRESH register.
It requires an analog reference voltage of 5V for its operation.
5. Oscillators
Oscillators are used for timing generation. PIC microcontrollers consist of
external oscillators like crystals or RC oscillators. In the case of crystal
oscillators, the crystal is connected between two oscillator pins, and the value of
the capacitor connected to each pin determines the mode of operation of the
oscillator. The different modes are low-power mode, crystal mode, and the high-
speed mode. In the case of RC oscillators, the value of the Resistor and Capacitor
determines the clock frequency. The clock frequency ranges from 30 kHz to 4
MHz.
6. CCP module:
A CCP module works in the following three modes:
Capture Mode: This mode captures the time of arrival of a signal, or in other
words, captures the value of the Timer1 when the CCP pin goes high.
Compare Mode: It acts as an analog comparator that generates an output when
the timer1 value reaches a certain reference value.
PWM Mode: It provides pulse width modulated output with a 10-bit resolution
and programmable duty cycle.
Other special peripherals include a Watchdog timer that resets the
microcontroller in case of any software malfunction and a Brownout reset that
resets the microcontroller in case of any power fluctuation and others. For a
better understanding of this PIC microcontroller, we are giving one practical
project which uses this controller for its operation.

9
Comparison of PIC 18 series microcontrollers

10
Details of Pins, Pin Diagram

PIC18F4550 belongs to ‘PIC18F’ family of microcontrollers. PIC18F4550 is one of popular


Microcontrollers from the microchip technology, comes with a High-Performance, Enhanced
flash, USB Microcontroller with nano-Watt-Technology. PIC18F4550 comes in various packages
like DIP, QPF and QPN and can be selected according to the project requirement.

11
PIC18F4550 Pinout Configuration

It is a 40 pin device as shown in PIC18F4550 pin diagram. There are so many features
for a controller the manufacturer cannot provide that many I/O pins. So many pins of
controller have multiple features. These features can be enabled through programming
depending on requirement. We will describe each pin functions briefly below.

Pin Pin Name Description

Number

1 MCLR/VPP/RE3 MCLR: Master Clear(RESET) Input

VPP: programming voltage input

RE3: I/O pin of PORTE, PIN 3

2 RA0/AN0 RA0: I/O pin of PORTA, PIN 0

AN0: Analog input 0

3 RA1/AN1 RA1: I/O pin of PORTA, PIN 1

AN1: Analog input 1

4 RA2/AN2/VREF-/CVREF RA2: I/O pin of PORTA, PIN 2

AN2: Analog input 2

VREF-: A/D reference voltage (low) input

CVREF: Analog comparator reference output

5 RA3/AN3/VREF+ RA3: I/O pin of PORTA, PIN 3

AN3: Analog input3

VREF+: A/D reference voltage (high) input

6 RA4/T0CKI/C1OUT/RCV RA4: I/O pin of PORTA, PIN 4

T0CKI: Timer0 external clock input

12
C1OUT: Comparator 1 output

RCV:External USB transceiver RCV input

7 RA5/AN4/SS/HLVDIN/C2OU RA5: I/O pin of PORTA, PIN 5

T AN4: Analog input 4

SS: SPI slave select input

HLDVIN: High/Low-Voltage Detect input

C2OUT: Comparator 2 output

8 RE0/AN5/CK1SPP RE0: I/O pin of PORTE, PIN 0

AN5: Analog input 5

CK1SPP: SPP clock 1 output

9 RE1/AN6/CK2SPP RE1: I/O pin of PORTE, PIN 1

AN6: Analog input 6

CK2SPP: SPP clock 2 output

10 RE2/AN7/OESPP RE2: I/O pin of PORTE, PIN 2

AN6: Analog input 7

OESPP : SPP Enabled output

11 VDD Positive Power Supply (+5V)

12 VSS Ground

13 OSC1/CLKI OSC1:Oscillator pin 1

CLKI: External clock source input

14 OSC2/CLKO/RA6 OSC2: Oscillator pin 2

CLKO: clock source output

RA6: I/O pin of PORTA, PIN 6

13
15 RC0/T1OSO/T13CKI RC0: I/O pin of PORTC, PIN 0

T1OSO :Timer1 oscillator output

T13CKI: Timer1/Timer3 external clock input

16 RC1/T1OSI/CCP2/UOE RC1: I/O pin of PORTC, PIN 1

T1OSI: Timer1 oscillator input

CCP2:Capture 2 input/Compare 2 output/PWM2 output

UOE: External USB transceiver OE output

17 RC2/CCP1/P1A RC2: I/O pin of PORTC, PIN 2

CCP1: Capture 1 input/Compare 1 output/PWM1 output.

P1A :Enhanced CCP1 PWM output, channel A

18 VUSB VUSB:Internal USB 3.3V voltage regulator output

19 RD0/SPP0 RD0: I/O pin of PORTD, PIN 0

SPP0: Streaming Parallel Port data

20 RD1/SPP1 RD1: I/O pin of PORTD, PIN 1

SPP1: Streaming Parallel Port data

21 RD2/SPP2 RD2: I/O pin of PORTD, PIN 2

SPP2: Streaming Parallel Port data

22 RD3/SPP3 RD3: I/O pin of PORTD, PIN 3

SPP3: Streaming Parallel Port data

23 RC4/D-/VM RC4: I/O pin of PORTC, PIN 4

D-: USB differential minus line (input/output)

VM: External USB transceiver VM input

24 RC5/D+/VP RC5: I/O pin of PORTC, PIN 5

14
D+: USB differential plus line (input/output).

VP: External USB transceiver VP input

25 RC6/TX/CK RC6: I/O pin of PORTC, PIN 6

TX: EUSART asynchronous transmit

CK: EUSART synchronous clock (see RX/DT).

26 RC7/RX/DT/SDO RC7: I/O pin of PORTC, PIN 7

RX: EUSART asynchronous receive

DT: EUSART synchronous data (see TX/CK).

SDO: SPI data out

27 RD4/SPP4 RD4: I/O pin of PORTD, PIN 4

SPP4: Streaming Parallel Port data

28 RD5/SPP5/P1B RD5: I/O pin of PORTD, PIN 5

SPP5: Streaming Parallel Port data

P1B: Enhanced CCP1 PWM output, channel B

29 RD6/SPP6/P1C RD6: I/O pin of PORTD, PIN 6

SPP6: Streaming Parallel Port data

P1C: Enhanced CCP1 PWM output, channel C

30 RD7/SPP7/P1D RD7: I/O pin of PORTD, PIN 7

SPP7: Streaming Parallel Port data

P1D: Enhanced CCP1 PWM output, channel D

31 VSS Ground

32 VDD Positive Power Supply (+5V)

33 RB0/AN12/INT0/FLT0/SDI/S RB0: I/O pin of PORTB, PIN 0

15
DA AN12: Analog input 12

INT0: External interrupt 0

FLT0: Enhanced PWM Fault input (ECCP1 module)

SDI: SPI data in

SDA: I2C data I/O

34 RB1/AN10/INT1/SCK/SCL RB1: I/O pin of PORTB, PIN 1

AN10: Analog input 10

INT1: External interrupt 1

SCK: Synchronous serial clock input/output for SPI mode

SCL: Synchronous serial clock input/output for I2C mode

35 RB2/AN8/INT2/VMO RB2: I/O pin of PORTB, PIN 2

AN8: Analog input 8

INT2: External interrupt 2

VMO: External USB transceiver VMO output

36 RB3/AN9/CCP2/VPO RB3: I/O pin of PORTB, PIN 3

AN9: Analog input 9

CCP2: Capture 2 input/Compare 2 output/PWM2 output

VPO: External USB transceiver VPO output

37 RB4/AN11/KBI0/CSSPP RB4: I/O pin of PORTB, PIN 4

AN11: Analog input 11

KBI0: Interrupt-on-change pin

CSSPP: SPP chip select control output

38 RB5/KBI1/PGM RB5: I/O pin of PORTB, PIN 5

KBI1: Interrupt-on-change pin

PGM: Low-Voltage ICSP Programming enable pin

16
39 RB6/KBI2/PGC RB6: I/O pin of PORTB, PIN 6

KBI2: Interrupt-on-change pin

PGC: In-Circuit Debugger and ICSP programming clock pin.

40 RB7/KBI3/PGD RB7: I/O pin of PORTB, PIN 7

KBI3: Interrupt-on-change pin

PGD: In-Circuit Debugger and ICSP programming data pin.

PIC 18 internal Architecture:

ALU

The Arithmetic Logic Unit (ALU) is the core functional block of the PIC18 CPU, responsible
for performing arithmetic and logic operations. It works closely with the WREG register,
file registers, and status register, enabling fast execution of instructions.

Functions of ALU
Arithmetic Operations

• Addition (ADD): Adds contents of WREG and a file register.


• Subtraction (SUB): Subtracts file register from WREG.
• Increment/Decrement (INC, DEC): Increases or decreases the value of a register.
• Addition/Subtraction with Carry (ADDC, SUBC): Useful in multi-byte operations.
• Complement (COMF): Computes 1’s complement of a register.

Logic Operations

• AND, OR, XOR: Performs bitwise operations on WREG and file registers.
• Bit Testing/Setting/Clearing (BSF, BCF, BTFSC, BTFSS): Directly manipulates
specific bits.
• Shift Operations (RLF, RRF, SWAPF): Rotate left/right and nibble swap operations.

17
WREG register

In the CPU, registers are used to store information temporarily. That information could be a byte
of data to be processed, or an address pointing to the data to be fetched. In the PIC there is only
one data type: 8-bit. The 8 bits of a register are shown in the diagram below. These range from
the MSB (most-significant bit) D7 to the LSB (least-significant bit) D0. With an 8-bit data type,
any data larger than 8 bits must be broken into 8-bit chunks before it is processed.

D7 D6 D5 D4 D3 D2 D1 DO
The 8-bit WREG register is the most widely used register in the PIC micro-controller. WREG
stands for working register, as there is only one. The WREG register is the same as the
accumulator in other microprocessors. The WREG register is used for all arithmetic and logic
instructions.

MOVLW instruction

MOVLW instruction moves 8-bit data into the WREG register.

It has the following format:

MOVLW K ;move literal value K Into WREG

18
K is an 8-bit value that can range from 0-255 in decimal, or 00-FF in hex. The L stands for
literal, which means, literally, a number must be used.

Notice that in MOVLW, the letter L (literal) comes first and then the letter W (WREG), which
means "move a literal value to WREG," the destination.

MOVLW 25H ;move value 25H into WREG (WREG 25H)

MOVLW 87H ;load 87H into WREG (WREG 87H)

ADDLW instruction

The ADDLW instruction has the following format:

ADDLW K

ADD literal value K to WREG

The ADD instruction tells the CPU to add the literal value K to register WREG and put the result
back in the WREG register.

Notice that in ADDLW, first comes the letter L (literal) and then the letter W (WREG), which
means "add a literal value to WREG," the destination. To add two numbers such as 25H and
34H, one can do the following:

MOVLW 25H ;load 25H into WREG

ADDLW 34H ;add value 34 to W(W W+ 34H)

Executing the above lines results in WREG = 59H (25H +34H = 59H)

EEPROM
EPROM (erasable programmable ROM) and UV-EPROM

EPROM was invented to allow changes in the contents of PROM after it is burned. In EPROM,
one can program the memory chip and erase it thousands of times. This is especially necessary
during development of the prototype of a microprocessor-based project. A widely used EPROM
is called UV-EPROM, where UV stands for ultraviolet. The only problem with UV-EPROM is
that erasing its contents can take up to 20 minutes.
To program a UV-EPROM chip, the following steps must be taken:
1. Its contents must be erased. To erase a chip, remove it from its socket on the system board and
place it in EPROM erasure equipment to expose it to UV radiation for 15-20 minutes.

19
2. Program the chip. To program a UV-EPROM chip, place it in the ROM burner (programmer).
3. Place the chip back into its socket on the system board.

• EEPROM stands for Electrically Erasable Programmable Read-Only


Memory (EEPROM) is a non-volatile data memory. Data EEPROM is used mainly to
store information (data) which is changing frequently and also used for long-term storage
of data.
• We can store the data while an application is running. So if there is a power failure
(supply off), data will never get lost unlike volatile RAM and we can recollect the data
stored in EEPROM.
• It is basically used for the application such as Automatic Electric Meter Reading where
the reading needs to be store for continuous calculation and also if a power failure occurs
then needs to store current reading also.
• PIC18F4550 has in-built 256 Bytes of data EEPROM. Its address ranges from 0h-FFh.
• The default content of EEPROM data memory is 0xff.

EEPROM has 4 different registers

1. EEADR: Register holds the 8-bit address of the EEPROM location which we want to
access. It can address up to a maximum of 256 Bytes.
2. EEDATA: Register holds the 8-bit data for read/write operation.
3. EECON1: It is the EEPROM Control register which needs to be configured for
Read/Write operation.
4. EECON2: It is not a physical register. It is used in memory erase and write sequence.

EEPROM (electrically erasable programmable ROM)


EEPROM has several advantages over EPROM, such as the fact that its method of erasure is
electrical and therefore instant, as opposed to the 20-minute erasure time required for UV-
EPROM. In addition, in EEPROM one can select the byte to be erased, in contrast to UV-
EPROM, in which the entire contents of ROM are erased. The main advantage of EEPROM is
that one can program and erase its contents while it is still in the system board. It does not
require physical removal of the memory chip from its socket. In other words, unlike UV-
EPROM, EEP-ROM does not require an external erasure and programming device. To utilize
EEPROM fully, the designer must incorporate the circuitry to program the EEP-ROM into the
system board. In general, the cost per bit for EEPROM is much higher than for UV-EPROM

20
RAM
The PIC microcontroller has many other registers in addition to the WREG register. They are
called data memory space to distinguish them from program (code) memory space. The data
memory space in PIC is a read/write (static RAM) memory. In the PIC microcontroller literature,
the data memory is also called the file register.

File register (data RAM) space allocation in PIC

The file register is read/write memory used by the CPU for data storage, scratch pad, and
registers for internal use and functions. As with WREG, we can perform arithmetic and logic
operations on many locations of the file register data RAM. The PIC microcontrollers' file
register size ranges from 32 bytes to several thousand bytes depending on the chip. Even within
the same family, the size of the file register data RAM varies from chip to chip. Notice that the
file register data RAM has a byte-size width, just like WREG The file register data RAM in PIC
is divided into two sections:

(a) Special Function Registers (SFR), and

(b) General-Purpose Registers (GPR).

SFRS (Special Function Registers)

The Special Function Registers (SFRs) are dedicated to specific functions such as ALU status,
timers, serial communication, I/O ports, ADC, and so on. The function of each SFR is fixed by
the CPU designer at the time of design because it is used for control of the microcontroller or
peripheral. The PIC SFRs are 8-bit registers. The number of locations in the file register set aside
for SFR depends on the pin numbers and peripheral functions supported by that chip. That
number can vary from chip to chip even among members of the same family. Some have as few
as 7 (8-pin PIC12C508 with no on-chip analog-to-digital converter) and some have over a
hundred (40-pin PIC18F458 with on-chip analog-to digital converter). For example, the more
timers we have in a PIC chip, the more SFR registers we will have

GPR (General-Purpose Registers or RAM)

The general-purpose registers are a group of RAM locations in the file register that are used for
data storage and scratch pad. Each location is 8 bits wide and can be used to store any data we
want as long as it is 8-bit. Again, the number of registers can vary from chip to chip, even among
members of the same family. In the PIC controllers, the space that is not allocated to the SFRs
typically is used for general-purpose registers. That means in a PIC chip with a thousand-byte

21
file register, no more than 100 bytes are used for SFRs and the rest are used for general-purpose
registers. A larger GPR size means more difficulties in managing these registers if you use
Assembly language programming.

22
IO port
INPUT OUTPUT PORTS OF PIC18FXX

PIC 18FXX has five ports which are used for connection with external devices. It has a total of
40 pins. Out of these 40 pins, 34 pins can be used as input-output pins. The Ports are denoted by
A, B, C, D and E. Size of each port is different.

Port A consists of seven pins named as RAO-RA6

Port B consists of eight pins named as RB0-RA7

Port C consists of eight pins named as RCO-RC7

Port D consists of eight pins named as RD0-RD7

Port E consists of three pins named as RE0-RE2

Some pins of the I/O ports are multiplexed with an alternate function. If a pin is used as any
other function then it may not be used as a general purpose I/O pin

REGISTERS:

Each port has three registers for its operation.

TRIS register (data direction register)

PORT register (read and write data on the port terminals)

LAT register (output latch)

1. TRIS REGISTER

TRIS is a data direction register as input or output. Each PORT has its own TRIS register.

PORT A has TRISA register

PORT B has TRISB register

PORT C has TRISC register

PORT D has TRISD register

PORT E has TRISD register

To use all port bits of PORT B as output, the instruction used is TRISB=0X00;

To use all port bits of PORT B as input, the instruction used is TRISB-OXFF;. TRISB=0

23
If a particular bit in TRIS register is set to 1, then the port pin is used as Input and if the bit in
TRIS register is set to 0, then the port pin is used as an output pin.

To use RBO pin of PORT B as input, Instruction used is

TRISB.RB0=1;

To use RBO pin of PORT B as output, Instruction used is

TRISB.RB0=0;

2. PORT REGISTER:

This register is used to read or write data from ports. Each port has one port register.

PORT A has PORTA register

PORT B has PORTB register

PORT C has PORTC register

PORT D has PORTD register

PORT E has PORTE register

For example, if PORT B is used as Output port and we want to write data FFH on port B, then
use instruction

PORTB=0XFF;

If PORT B issued as an input port then to read a data from all 8 bits of PORTB, use

val=PORTB PORTB; // val is variable where value of PORT B will be stored

To write data at single port pin instruction used is

PORTB.RB0=0;

To read data from the single port pin instruction used is

1-PORTB.RB0; // J is variable where the value of PORT B terminal RBO will be stored

[Link] REGISTER

LAT register is associated with an I/O pin. Each port has one Latch register like LATA, LATB,
LATC, LATD, LATE.

24
Latch Read:

A read of the LAT register returns the values held in the port output latches, instead of the values
on the I/O pins. A. read-modify-write operation on the LAT register which is associated with an
I/O port, avoids the possibility of writing the input pin values into the port latches.

Latch Write:

A write to the LAT register has the same effect as a write to the PORT register. A write to the
PORT register writes the data value to the port latch. Similarly, a write to the LAT register writes
the data value to the port latch.

Ports are not only used for simple I/O, but also can be used other functions –

ADC (analog-to-digital conversion) , Timers , Oscillator Input , Interrupts , Serial


communication , Capturing and Generating PWM Signals , Programming the PIC

25
26
27
Timer-
The PIC18 has two to five timers depending on the family member. They are referred to as
Timers 0, 1, 2, 3, and 4. They can be used either as timers to generate a time delay or as counters
to count events happening outside the micro-controller.

PROGRAMMING TIMERS 0 AND 1

Every timer needs a clock pulse to tick. The clock source can be internal or external. If we use
the internal clock source, then 1/4th of the frequency of the crystal oscillator on the OSCI and
OSC2 pins (Fosc/4) is fed into the timer. Therefore, it is used for time delay generation and for
that reason is called a timer. By choosing the external clock option, we feed pulses through one
of the PIC18's pins: this is called a counter.

Basic registers of the timer

Many of the PIC18 timers are 16 bits wide. Because the PIC18 has an 8-bit architecture, each 16-
bit timer is accessed as two separate registers of low byte (TMRxL) and high byte (TMRxH).
Each timer also has the TCON (timer control) register for setting modes of operation.

Timer 0 registers and programming

Timer can be used as an 8-bit or a 16-bit timer. The low-byte register is called TMROL (Timer)
low byte) and the high-byte register is referred to as TMROH (Timer) high byte). These registers
can be accessed like any other special function registers.

28
For example, the instruction "MOVWF TMROL" moves the value in WREG into TMROL, the
low byte of Timer0.

These registers can also be read like any other register. For example,

"MOVFF TMROL, PORTB" copies TMROL (low byte of Timer0) to PORTB.

TOCON (Timer0 control) register

Each timer has a control register, called TCON, to set the various timer operation modes.
TOCON is an 8-bit register used for control of Timer0. The bits for TOCON are shown in Figure
9-2.

29
Timer1 programming

Timer1 is a 16-bit timer, and its 16-bit register is split into two bytes, referred to as TMRIL
(Timer1 low byte) and TMR1H (Timer1 high byte). See Figure 9-8. Timer1 can be programmed
in 16-bit mode only and unlike Timer0, it does not support 8-bit mode. Timer1 also has the
TICON (Timer 1 control) regis-ter in addition to the TMRIIF (Timer1 interrupt flag). The
TMR1IF flag bit goes HIGH when TMR1H:TMR1L overflows from FFFF to 0000. Timer1 also
has the prescaler option, but it only supports factors of 1:1, 1:2, 1:4, and 1:8. See Figure 9-9 for
the Timer1 block diagram and Figure 9-10 for TICON register options. The PIR1 register
contains the TMR1IF flags. See Figure 9-11.

30
• Timer3 - Timer3 consists of two 8-bit registers TMR2H and TMR2L. - Timer3 can
choose to use either the internal (instruction cycle clock) or external signal as the clock
source. - The block diagram of Timer3 is quite similar to that of Timer1. - Reading

31
TMR3L will load the high byte of Timer3 into the TMR3H register. - Timer3 operation is
controlled by the T3CON register.

Timer4 - Only available to the PIC18F8X2X and PIC6X2X devices. - The block diagram of
Timer4 is shown in Figure 8.9. - The value of TMR4 is compared to PR4 in each clock cycle. -
When the value of TMR4 equals that of PR4, TMR4 is reset to 0. - The contents of T4CON are
identical to those of T2CON.

ADC-
The PIC18 A/D Converter - The PIC18 has a 10-bit A/D converter.
- The number of analog inputs varies among difference PIC18 devices.
- The A/D converter has the following registers:
• A/D Result High Register (ADRESH) • A/D Result Low Register (ADRESL)
• A/D Control Register 0 (ADCON0)
• A/D Control Register 1 (ADCON1)
• A/D Control Register 2 (ADCON2)
- The contents of these registers vary with the PIC18 members. - Early PIC18 (PIC18FXX2)
members have only ADCON0 and ADCON1 registers.

Serial port PIC18 microcontroller programming model


Data Transfer Methods

• Parallel communication
• Serial communication

Parallel Communication

• Transfers multiple bits at once


• Uses 8 or more wires
• High speed for short distance
• Expensive and bulky
• Not suitable for long distance

32
Serial Communication

• Transfers one bit at a time


• Uses single data line
• Low cost
• Reliable for long distance

Half- and full-duplex transmission

In data transmission, if the data can be both transmitted and received, it is a duplex transmission.
This is in contrast to simplex transmissions such as with printers, in which the computer only
sends data. Duplex transmissions can be half or full duplex, depending on whether or not the data
transfer can be simultaneous.

If data is transmitted one way at a time, it is referred to as half duplex. If the data can go both
ways at the same time, it is full duplex. Of course, full duplex requires two wire conductors for
the data lines (in addition to the signal ground), one for transmission and one for reception, in
order to transfer and receive data simultaneously.

33
Asynchronous Serial Communication

• Data is transmitted one bit at a time over a single data line


• Sender and receiver must follow the same protocol
• Used mainly for character-oriented transmission
• Examples: UART, RS-232

Need for Data Framing

• Received data is only 0s and 1s


• Framing helps identify:
o Start of data
o End of data
o Number of bits per character

Start and Stop Bits

• Each character is sent independently


• Start bit
o Always 1 bit
o Logic 0 (Low) → called Space
• Stop bit
o 1 or 2 bits
o Logic 1 (High) → called Mark

Data Framing Structure

• Format:
o Start bit (0)
o Data bits (7 or 8 bits)
o Stop bit(s) (1)
• LSB is transmitted first
• MSB is transmitted last

Example: ASCII ‘A’

• ASCII ‘A’ = 41H = 0100 0001


• Transmission order:
o Start bit → D0 → D1 → … → D7 → Stop bit

34
• Total bits (8-bit data, 1 stop bit):
o 1 start + 8 data + 1 stop = 10 bits

Data Transfer Rate


• Measured in bps (bits per second)
• Also referred to as baud rate (modem terminology)
• Baud ≠ bps when one signal change transmits multiple bits
• In conductor wires, bps ≈ baud → terms used interchangeably
• Typical transfer rates:
o Early IBM PC/XT: 100 , 9600 bps
o Modern PCs: up to 56 Kbps
o Asynchronous serial communication: up to 100,000 bps

RS232 Standard
• Developed by Electronics Industries Association (EIA), 1960
• Versions: RS232A (1963), RS232B (1965), RS232C (1969)
• Widely used serial I/O standard for PCs and devices
• Not TTL compatible:
o Logic 1 → -3V to -25V
o Logic 0 → +3V to +25V
o Voltage range -3V to +3V → undefined

35
RS232 and Microcontrollers
• Requires voltage conversion for TTL devices
• MAX232 IC used as a line driver
o Converts TTL → RS232 voltage levels and vice versa
• Common connection: DB-25 connector
o DB-25P → Male plug
o DB-25S → Female socket

36
Bus Architecture of PIC18 Microcontroller
The PIC18 microcontroller uses a modified Harvard architecture that separates program
memory and data memory into distinct buses. This separation allows the CPU to fetch
instructions and read/write data simultaneously, which improves execution speed and overall
efficiency.

Overview
The bus system in PIC18 consists of:

• Program Memory Bus: Transfers instructions from Flash program memory to the CPU.
• Data Memory Bus: Transfers data between RAM (both GPRs and SFRs) and the CPU.
• Instruction Bus: Carries the opcode to the instruction decoder.

This structure ensures that instruction fetches do not interfere with data operations, allowing
high-speed execution.

CPU and ALU Interaction


The CPU communicates with registers, RAM, and peripherals via the data bus. The Working
Register (WREG) acts as an accumulator for the ALU, allowing arithmetic and logic operations
to be performed efficiently. The STATUS register is updated based on ALU operations to
reflect conditions such as zero, carry, or overflow.

Register File Access


The PIC18 internal memory is organized into General Purpose Registers (GPRs) and Special
Function Registers (SFRs):

• SFRs control peripherals such as timers, ADC, UART, and I/O ports.
• GPRs store temporary data, intermediate results, and variables.

The registers are accessed through banks, with the Bank Select Register (BSR) controlling the
currently active bank. An Access Bank exists to allow faster access to frequently used registers
without switching banks.

37
Memory Banking
The banked memory system extends the accessible RAM beyond the 8-bit address range:

• Each bank has 256 bytes.


• BSR selects the bank for indirect memory access.
• The Access Bank provides direct access to important SFRs and a portion of GPRs.

This banking system is essential for efficiently handling large amounts of internal memory using
8-bit instructions.

Bus Operation Summary


• Instruction Fetch: Program memory bus sends instructions to CPU.
• Data Access: Data bus reads/writes registers or RAM as directed by CPU instructions.
• Simultaneous Operation: Separation of program and data buses allows instruction fetch
and data read/write to happen at the same time.

This architecture ensures fast instruction execution and supports the high-speed RISC design of
PIC18.

Registers of PIC18F Microcontroller


Registers are special memory locations inside the PIC18F microcontroller used to store data,
control information, and status flags. They form the interface between the CPU, ALU, RAM,
and peripherals, enabling the microcontroller to execute instructions efficiently and control
hardware.

Types of Registers
Registers in PIC18F are divided into two main categories:

General Purpose Registers (GPRs):

• Store temporary data and variables used during program execution.


• Hold intermediate results of arithmetic and logical operations.
• Organized into banks, which are selected using the Bank Select Register (BSR).
• Part of the GPRs is available in the Access Bank for faster access without switching
banks.

38
Special Function Registers (SFRs):

• Memory-mapped registers that control hardware peripherals and CPU operations.


• Each SFR has a dedicated function, such as controlling timers, ADC, UART, EEPROM,
I/O ports, or system configuration.
• SFRs can be directly accessed through instructions.

Important Special Function Registers


• WREG (Working Register): Acts as the accumulator for the ALU. All arithmetic and
logic operations pass through WREG.
• STATUS Register: Holds CPU status flags (Zero, Carry, Digit Carry, Overflow) and
bank selection bits.
• PORTx, TRISx, LATx Registers: Manage digital I/O pins; PORTx reads pin values,
TRISx sets direction, LATx controls output latches.
• TMRx Registers: Control Timer0, Timer1, Timer2, and Timer3 operations.
• ADCONx Registers: Manage ADC configuration, channel selection, conversion clock,
and result format.
• RCSTA, TXSTA, SPBRG Registers: Control UART/EUSART communication and
baud rate.
• EECONx, EEDATA, EEADR Registers: Handle EEPROM read/write operations.
• BSR (Bank Select Register): Selects the active RAM bank for indirect addressing.

Register File Organization


The PIC18F uses a banked memory structure to extend the 8-bit addressable range:

• Each bank contains 256 bytes.


• The Bank Select Register (BSR) selects which bank is active.
• The Access Bank includes frequently used SFRs and part of GPRs for direct access
without bank switching.

This system allows the microcontroller to efficiently manage memory and registers while
executing instructions.

ALU and Register Interaction


• The ALU interacts with WREG and file registers.
• Data from a GPR or SFR can be loaded into WREG for computation.

39
• ALU results can be stored back into registers.
• STATUS register flags update automatically based on ALU operations, providing
information about zero, carry, overflow, etc.

Program memory and data memory organization


The program memory map is shown in Figure. All PIC18F devices have a 21-bit program
counter and hence are capable of addressing 2Mbytes of memory space. User memory space on
the PIC18F452 microcontroller is 00000H to 7FFFH

. Accessing a nonexistent memory location (8000H to 1FFFFFH) will cause a read of all 0s. The
reset vector, where the program starts after a reset, is at address 0000.

40
Addresses 0008H and 0018H are reserved for the vectors of high-priority and low-priority
interrupts respectively, and interrupt service routines must be written to start at one of these
locations. The PIC18F microcontroller has a 31-entry stack that is used to hold the return
addresses for subroutine calls and interrupt processing. The stack is not part of the program or
the data memory space. The stack is controlled by a 5-bit stack pointer which is initialized to
00000 after a reset. During a subroutine call (or interrupt) the stack pointer is first incremented,
and the memory location it points to is written with the contents of the program counter. During
the return from a subroutine call (or interrupt), the memory location the stack pointer has pointed
to is decremented. The projects in this book are based on using the C language. Since subroutine
and interrupt call/return operations are handled automatically by the C language compiler, their
operation is not described here in more detail. Program memory is addressed in bytes, and
instructions are stored as two bytes or four bytes in program memory. The least significant byte
of an instruction word is always stored in an even address of the program memory. An
instruction cycle consists of four cycles: A fetch cycle begins with the program counter
incrementing in Q1. In the execution cycle, the fetched instruction is latched into the instruction
register in cycle Q1. This instruction is decoded and executed during cycles Q2, Q3, and Q4. A
data memory location is read during the Q2 cycle and written during the Q4 cycle.

Data Memory Organization

The data memory map of the PIC18F452 microcontroller is shown in Figure 2.4. The data
memory address bus is 12 bits with the capability to address up to 4Mbytes. The memory in
general consists of sixteen banks, each of 256 bytes, where only 6 banks are used. The
PIC18F452 has 1536 bytes of data memory (6 banks 256 bytes each) occupying the lower end of
the data memory. Bank switching happens automatically when a high-level language compiler is
used, and thus the user need not worry about selecting memory banks during programming. The
special function register (SFR) occupies the upper half of the top memory bank. SFR contains
registers which control operations such as peripheral devices, timers/ counters, A/D converter,
interrupts, and USART. Figure 2.5 shows the SFR registers of the PIC18F452 microcontroller.

41
42

You might also like