Unit 2
Unit 2
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.
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.
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.
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.
(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.
• 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.
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.
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.
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
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.
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,
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.
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
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.
Number
12
C1OUT: Comparator 1 output
12 VSS Ground
13
15 RC0/T1OSO/T13CKI RC0: I/O pin of PORTC, PIN 0
14
D+: USB differential plus line (input/output).
31 VSS Ground
15
DA AN12: Analog input 12
16
39 RB6/KBI2/PGC RB6: I/O pin of PORTB, PIN 6
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
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
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.
ADDLW instruction
ADDLW K
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:
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.
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.
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.
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:
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
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.
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:
1. TRIS REGISTER
TRIS is a data direction register as input or output. Each PORT has its own TRIS 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.
TRISB.RB0=1;
TRISB.RB0=0;
2. PORT REGISTER:
This register is used to read or write data from ports. Each port has one port 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
PORTB.RB0=0;
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 –
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.
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.
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 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,
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.
• Parallel communication
• Serial communication
Parallel Communication
32
Serial Communication
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
• 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
34
• Total bits (8-bit data, 1 stop bit):
o 1 start + 8 data + 1 stop = 10 bits
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.
• 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:
This banking system is essential for efficiently handling large amounts of internal memory using
8-bit instructions.
This architecture ensures fast instruction execution and supports the high-speed RISC design of
PIC18.
Types of Registers
Registers in PIC18F are divided into two main categories:
38
Special Function Registers (SFRs):
This system allows the microcontroller to efficiently manage memory and registers while
executing instructions.
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.
. 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.
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