0% found this document useful (0 votes)
3 views37 pages

Unit 1 - Embedded Processor

description of embedded processor

Uploaded by

gerald
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
0% found this document useful (0 votes)
3 views37 pages

Unit 1 - Embedded Processor

description of embedded processor

Uploaded by

gerald
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

Embedded Processor

UNIT I ​ PIC MICROCONTROLLER – ARCHITECTURE

1. PIC Architecture
●​ Overview: Peripheral Interface Controller (PIC) microcontrollers are based on the
Harvard architecture with separate program and data memory, offering faster
operation and efficient performance.
●​ Key Topics to Explore:
○​ Harvard vs. Von Neumann Architecture.
○​ Features of PIC microcontrollers (e.g., pipelining, instruction set).
○​ Memory layout and configuration.
○​ Peripherals such as timers, ADCs, and communication interfaces.
●​ Resources:
○​ Books:
■​ "PIC Microcontroller and Embedded Systems" by Muhammad Ali
Mazidi.
○​ Online Tutorials:
■​ Microchip's official website for datasheets and design resources.
■​ TutorialsPoint: PIC Architecture.

PIC Architecture: A Detailed Description

Overview

PIC microcontrollers, developed by Microchip Technology, are known for their compact size,
versatility, and robust performance. The architecture of PIC microcontrollers is based on the
Harvard architecture, which separates program memory and data memory. This design
enables simultaneous access to both memory types, significantly improving operation speed
and efficiency. PIC microcontrollers are widely used in embedded systems due to their
simplicity, low power consumption, and rich feature set.

Key Topics in PIC Architecture


PICmicro MID-RANGE MCU FAMILY
Figure 4-2: General Mid-range PICmicro Block Diagram

13 Data Bus 8 PORTA


EPROM Program Counter
RA0
Program RA1
Memory RAM RA2
up to 8 Level Stack File RA3
8K x 14 (13-bit) Registers RA4
up to RA5
368 x 8
Program 14
Bus RAM Addr (1) 9 PORTB
RB0/INT
Addr MUX RB1
Instruction reg
RB2
Direct Addr 7 Indirect RB3
8 Addr
RB4
FSR reg RB5
RB6
STATUS reg RB7
8 PORTC
RC0
RC1
3 RC2
Power-up MUX
RC3
Timer
RC4
Instruction Oscillator RC5
Decode & Start-up Timer ALU RC6
Control RC7
Power-on
Reset 8
PORTD
Timing Watchdog RD0
Generation Timer W reg
RD1
OSC1/CLKIN Brown-out RD2
OSC2/CLKOUT Internal Reset (2)
RC clock (2) RD3
RD4
RD5
RD6
RD7
PORTE
MCLR VDD, VSS RE0
RE1
RE2
RE3
RE4
RE5
Timer0 Timer1 Timer2 A/D RE6
RE7

PORTF
RF0
RF1
RF2
Synchronous RF3
CCPs Comparators USARTs
Serial Port RF4
RF5
RF6
RF7

Other Parallel
Modules Slave Port LCD Drivers
PORTG
RG0
RG1
RG2
RG3
RG4
Data EEPROM
Voltage up to RG5
Reference 256 x 8 RG6
RG7

Peripheral Modules (Note 3) General Purpose I/O


(Note 3)

Note 1: The high order bits of the Direct Address for the RAM are from the STATUS register.
2: Not all devices have this feature, please refer to device data sheet.
3: Many of the general purpose I/O pins are multiplexed with one or more peripheral module functions.
The multiplexing combinations are device dependent.

DS31004A-page 4-4  1997 Microchip Technology Inc.


1. Harvard vs. Von Neumann Architecture

●​ Harvard Architecture:
○​ Features separate buses for program instructions and data, allowing parallel
access.
○​ Faster execution since instructions and data can be fetched simultaneously.
○​ Commonly used in high-performance applications like microcontrollers and
DSPs.
○​ Example in PIC: Instructions are fetched from program memory while data is
accessed in RAM.
●​ Von Neumann Architecture:
○​ Uses a single memory space for both program instructions and data.
○​ Execution is slower due to sequential memory access (fetch-decode-execute
cycle).
○​ Simpler design, typically used in general-purpose processors.

Comparison: The separation in the Harvard architecture makes PIC microcontrollers


well-suited for real-time embedded applications requiring high-speed operations, unlike Von
Neumann systems.

2. Features of PIC Microcontrollers

PIC microcontrollers are rich in features that make them versatile for various applications:

●​ RISC Architecture:
○​ Reduced Instruction Set Computing (RISC) ensures simpler and faster
instruction execution, typically in one cycle.
●​ Pipelining:
○​ Instruction pipelining allows overlapping of instruction fetch and execution
phases, enabling one instruction per cycle throughput.
●​ Instruction Set:
○​ A compact instruction set, typically 35 instructions for mid-range devices,
ensures ease of programming and efficient execution.
●​ Wide Range of Families:
○​ PIC microcontrollers include families like PIC10, PIC12, PIC16, PIC18, and
PIC32, catering to applications from basic control tasks to high-performance
processing.

3. Memory Layout and Configuration

PIC microcontrollers have a well-organized memory structure:

●​ Program Memory:
○​ Stores instructions (firmware).
○​ Typically implemented as Flash memory, allowing reprogrammability.
●​ Data Memory:
○​ Consists of RAM for temporary data and registers.
○​ Includes EEPROM or Flash for non-volatile data storage in some models.
●​ Special Function Registers (SFRs):
○​ Dedicated registers used for controlling peripherals and internal operations.
●​ Banking Mechanism:
○​ PIC microcontrollers use memory banks to extend accessible RAM, controlled
via bank-select bits.

4. Peripherals in PIC Microcontrollers

PIC microcontrollers are equipped with various peripherals to enhance functionality:

●​ Timers:
○​ Provide precise time delays and event counting.
○​ Include features like watchdog timers (WDT) for system reliability.
●​ Analog-to-Digital Converters (ADCs):
○​ Convert analog signals into digital data, supporting a range of resolutions
(e.g., 10-bit, 12-bit).
●​ Communication Interfaces:
○​ Support for UART, SPI, I2C, and USB protocols.
○​ Enable connectivity with external devices and networks.
●​ PWM Modules:
○​ Pulse Width Modulation (PWM) for motor control, power regulation, and
signal generation.
●​ Comparators and Op-Amps:
○​ Facilitate analog signal processing and comparisons.
●​ Interrupt System:
○​ Offers precise control over asynchronous events, critical for real-time
systems.

PIC microcontrollers, leveraging the Harvard architecture, offer a blend of efficiency, speed,
and versatility. Their features, such as pipelining, compact instruction sets, and rich
peripheral support, make them ideal for a wide range of embedded applications, including
industrial automation, consumer electronics, and IoT devices. Understanding their memory
structure, architecture, and peripheral capabilities is essential for designing optimized
systems.
2. RISC Architecture

●​ Overview: PIC microcontrollers use RISC (Reduced Instruction Set Computing),


which simplifies instructions for faster execution.
●​ Key Topics:
○​ Characteristics of RISC: Fewer instructions, single-cycle execution.
○​ Comparison between RISC and CISC.
○​ Examples of RISC in PIC microcontrollers.
●​ Resources:
○​ Books:
■​ "Microcontroller Theory and Applications with the PIC18F" by M.
Rafiquzzaman.
○​ Online Resources:
■​ IEEE papers on RISC architecture for embedded systems.
■​ TutorialsPoint: RISC and CISC Comparison.

RISC Architecture: A Detailed Description

Overview

Reduced Instruction Set Computing (RISC) is a processor design philosophy that simplifies
the instruction set to improve execution speed and efficiency. PIC microcontrollers adopt
RISC architecture, enabling faster operation by executing most instructions in a single cycle.
This streamlined approach makes them ideal for real-time embedded applications where
speed and power efficiency are critical.

Key Topics in RISC Architecture

1. Characteristics of RISC Architecture

RISC architecture focuses on simplicity and efficiency, characterized by the following


features:

●​ Fewer Instructions:
○​ A limited set of simple instructions reduces complexity.
○​ Each instruction typically performs a fundamental operation like load, store, or
add.
●​ Single-Cycle Execution:
○​ Most instructions are executed in a single clock cycle, improving throughput.
○​ Supported by hardware features like pipelining.
●​ Uniform Instruction Format:
○​ Instructions have a fixed length and format, simplifying decoding and
execution.
○​ Reduces the need for complex decoding logic.
●​ Load/Store Architecture:
○​ Data manipulation occurs only in registers, not directly in memory.
○​ Separate instructions handle data transfer between memory and registers.
●​ Efficient Pipelining:
○​ The simplicity of RISC instructions allows efficient pipelining, where multiple
instructions are processed simultaneously at different stages.
●​ Large Number of General-Purpose Registers:
○​ Facilitates faster data access as most operations occur in registers rather
than memory.
●​ Hardwired Control:
○​ Uses hardwired logic instead of microcode for instruction control, reducing
execution time.

2. Comparison Between RISC and CISC

Feature RISC (Reduced Instruction Set CISC (Complex Instruction Set


Computing) Computing)

Instruction Set Few, simple instructions. Many, complex instructions.

Execution High, as most instructions Slower, due to multi-cycle


Speed execute in one cycle. instructions.

Instruction Fixed, simplifying decoding. Variable, increasing decoding


Length complexity.

Memory Access Limited to load/store instructions. Instructions can directly


manipulate memory.

Hardware Simplified, allowing faster clock More complex due to microcoded


Complexity speeds. instructions.

Energy High, due to faster execution and Lower, as complex instructions


Efficiency simpler operations. require more power.

Examples PIC, ARM, and MIPS processors. x86 processors (Intel, AMD).

RISC Advantage in PIC: The simplicity of RISC enables PIC microcontrollers to excel in
real-time applications where deterministic performance and low power consumption are
critical.

3. Examples of RISC in PIC Microcontrollers

PIC microcontrollers demonstrate RISC principles in several ways:

●​ Compact Instruction Set:


○​ Mid-range PIC microcontrollers (e.g., PIC16 series) typically use a
35-instruction set.
○​ Simplified instructions like MOVWF (move to register) and ADDWF (add to
register) are optimized for embedded tasks.
●​ Single-Cycle Execution:
○​ Instructions like arithmetic, logic, and branch operations are executed in one
clock cycle.
○​ Exceptions include branch instructions, which may take two cycles.
●​ Efficient Pipelining:
○​ Instruction pipelining ensures a steady flow of instructions to the processor.
○​ While one instruction executes, the next is fetched and decoded.
●​ Dedicated Load/Store Instructions:
○​ Data transfer between memory and registers uses explicit instructions like
MOVF and MOVWF, ensuring efficiency.
●​ Simple Addressing Modes:
○​ PIC uses basic addressing modes, such as direct, indirect, and immediate,
reducing decoding complexity.
●​ Low Power Consumption:
○​ RISC architecture contributes to the energy-efficient operation of PIC
microcontrollers, making them suitable for battery-powered devices.

RISC architecture plays a pivotal role in the performance and efficiency of PIC
microcontrollers. Its emphasis on a simplified instruction set, single-cycle execution, and
efficient pipelining ensures high-speed operation and low power consumption. These
features make PIC microcontrollers ideal for embedded applications requiring real-time
responsiveness, reliability, and energy efficiency.
3. Program Memory Organization

●​ Overview:
○​ Program memory stores the firmware and is generally implemented as Flash
memory in PIC.
○​ Memory organization varies between families (e.g., PIC16, PIC18).
○​ Topics: Memory addressing, program counters, stack memory.
●​ Resources:
○​ Books:
■​ "PIC Microcontrollers: An Introduction to Microelectronics" by Martin
Bates.
○​ Datasheets:
■​ Microchip data sheets for specific PIC families (e.g., PIC16F877A,
PIC18F4550).

Program Memory Organization in PIC Microcontrollers

Overview

Program memory in PIC microcontrollers is a critical component that stores the firmware or
machine code executed by the processor. It is typically implemented as Flash memory,
which allows reprogrammability and ensures non-volatile storage. The memory organization
differs across PIC families, such as PIC10, PIC16, PIC18, and PIC32, reflecting variations in
size, addressing, and features. Understanding the organization of program memory is
essential for optimizing code and managing resources efficiently.

Key Topics in Program Memory Organization

1. Memory Addressing

●​ Harvard Architecture:
○​ Program memory is separate from data memory, enabling parallel instruction
fetch and data manipulation.
●​ Instruction Word Size:
○​ The size of an instruction word depends on the PIC family:
■​ PIC10/PIC12/PIC16: 12-bit or 14-bit instructions.
■​ PIC18: 16-bit instructions.
■​ PIC32: 32-bit instructions.
●​ Linear Addressing:
○​ Program memory is addressed linearly, starting from address 0x0000.
○​ The memory size (and hence the maximum address) varies between families:
■​ PIC16: 1K to 8K words.
■​ PIC18: Up to 2M words.
■​ PIC32: Much larger capacities for advanced applications.
●​ Configuration Words:
○​ Reserved memory locations in program memory are used for device-specific
configurations (e.g., oscillator settings, watchdog timer).
PIC18F2455/2550/4455/4550
5.0 MEMORY ORGANIZATION 5.1 Program Memory Organization
There are three types of memory in PIC18 enhanced PIC18 microcontrollers implement a 21-bit program
microcontroller devices: counter which is capable of addressing a 2-Mbyte
program memory space. Accessing a location between
• Program Memory
the upper boundary of the physically implemented
• Data RAM memory and the 2-Mbyte address will return all ‘0’s (a
• Data EEPROM NOP instruction).
As Harvard architecture devices, the data and program The PIC18F2455 and PIC18F4455 each have
memories use separate busses; this allows for con- 24 Kbytes of Flash memory and can store up to 12,288
current access of the two memory spaces. The data single-word instructions. The PIC18F2550 and
EEPROM, for practical purposes, can be regarded as PIC18F4550 each have 32 Kbytes of Flash memory
a peripheral device, since it is addressed and accessed and can store up to 16,384 single-word instructions.
through a set of control registers.
PIC18 devices have two interrupt vectors. The Reset
Additional detailed information on the operation of the vector address is at 0000h and the interrupt vector
Flash program memory is provided in Section 6.0 addresses are at 0008h and 0018h.
“Flash Program Memory”. Data EEPROM is
The program memory maps for PIC18FX455 and
discussed separately in Section 7.0 “Data EEPROM
PIC18FX550 devices are shown in Figure 5-1.
Memory”.

FIGURE 5-1: PROGRAM MEMORY MAP AND STACK FOR PIC18F2455/2550/4455/4550 DEVICES

PIC18FX455 PIC18FX550
PC<20:0> PC<20:0>
CALL, RCALL, RETURN, 21 CALL, RCALL, RETURN, 21
RETFIE, RETLW, CALLW, RETFIE, RETLW, CALLW,
ADDULNK, SUBULNK ADDULNK, SUBULNK
Stack Level 1 Stack Level 1
• •
• •
• •
Stack Level 31 Stack Level 31

Reset Vector 0000h Reset Vector 0000h

High Priority Interrupt Vector 0008h High Priority Interrupt Vector 0008h
Low Priority Interrupt Vector 0018h Low Priority Interrupt Vector 0018h

On-Chip
Program Memory On-Chip
Program Memory
5FFFh
6000h
User Memory Space

User Memory Space

7FFFh
8000h

Read ‘0’ Read ‘0’

1FFFFFh 1FFFFFh
200000h 200000h

© 2006 Microchip Technology Inc. Preliminary DS39632C-page 57


2. Program Counter (PC)

The Program Counter (PC) is a special-purpose register that holds the address of the next
instruction to be executed.

●​ Incremental Progression:
○​ The PC increments sequentially after each instruction fetch.
○​ For branch, jump, and call instructions, the PC is updated to reflect the target
address.
●​ Size of the Program Counter:
○​ The width of the PC determines the addressable range of program memory:
■​ PIC16: 13 bits (addressing up to 8K program memory words).
■​ PIC18: 21 bits (addressing up to 2M words).
■​ PIC32: Larger program counters for expanded memory support.
●​ Reset Vector:
○​ On a reset, the PC is initialized to the reset vector (e.g., 0x0000 for PIC16).

3. Stack Memory

The stack in PIC microcontrollers is tightly integrated with program memory and is used
primarily for managing return addresses during subroutine calls and interrupts.

●​ Hardware Stack:
○​ A hardware-implemented stack stores return addresses.
○​ PIC16: Fixed-depth stack (e.g., 8 levels), not addressable by software.
○​ PIC18: Larger, software-accessible stack.
●​ Stack Overflow:
○​ In families like PIC16, stack depth is limited. Exceeding the stack capacity
can cause overwriting, leading to program instability.
●​ Interrupt Handling:
○​ When an interrupt occurs, the current PC value is pushed onto the stack,
allowing the program to return correctly after servicing the interrupt.

Program Memory Layout

The structure of program memory in PIC microcontrollers typically includes:

1.​ Reset Vector:


○​ The starting address where the program execution begins after a reset.
○​ Located at 0x0000 in most PIC microcontrollers.
2.​ Interrupt Vector Table:
○​ Contains addresses for interrupt service routines (ISRs).
○​ Multiple vectors are available in advanced families like PIC18 and PIC32.
3.​ User Program Space:
○​ Main area for storing application code and instructions.
4.​ Configuration Bits:
○​ Located in high memory or reserved areas.
○​ Used for device-specific settings (e.g., oscillator type, code protection).
Program Memory Organization Across PIC Families

●​ PIC16:
○​ 14-bit instructions.
○​ Fixed-size stack (e.g., 8 levels).
○​ Limited program memory size (up to 8K words).
●​ PIC18:
○​ 16-bit instructions.
○​ Larger, software-accessible stack.
○​ Extended addressing with high-capacity program memory (up to 2M words).
●​ PIC32:
○​ 32-bit instructions.
○​ Advanced program memory features, such as memory-mapped peripherals
and large storage capacities.

Program memory organization in PIC microcontrollers is designed to balance simplicity and


efficiency, with variations across families to cater to different application requirements. Key
aspects like memory addressing, program counters, and stack memory are integral to
understanding how PIC devices execute firmware. Mastery of these concepts enables
developers to optimize performance, handle interrupts effectively, and make the most of the
device's capabilities.
4. Branch, Call, and Time Delay Loop

● Overview:
○ Branch instructions: Conditional and unconditional jumps.
○ Call instructions: Subroutine handling.
○ Time delay loops: Techniques to generate precise delays.
● Resources:
○ Books:
■ "Designing Embedded Systems with PIC Microcontrollers" by Tim
Wilmshurst.
○ Online Guides:
■ MikroElektronika tutorials for PIC assembly.
■ Practical examples of delay loops on platforms like [Link].

Branch, Call, and Time Delay Loop in PIC Microcontrollers

Overview

PIC microcontrollers rely on branch and call instructions for program flow control and time
delay loops for precise timing. These instructions and techniques are essential in embedded
systems, enabling conditional execution, subroutine handling, and delay generation for
various real-time applications.

Key Topics

1. Branch Instructions

Branch instructions control the program flow by modifying the program counter (PC). They
are categorized into:

a. Unconditional Jumps

● Instruction: GOTO (Go To Address)


● Functionality:
○ The GOTO instruction updates the program counter to the specified address,
causing the program to jump unconditionally to that address.

Example:

GOTO 0x0020 ; Jump to the instruction at address 0x0020

b. Conditional Jumps

● Instruction Examples:
○ BTFSC: Bit Test, Skip if Clear
○ BTFSS: Bit Test, Skip if Set
○ DECFSZ: Decrement File Register, Skip if Zero
○ INCFSZ: Increment File Register, Skip if Zero
● Functionality:
○ Conditional jumps evaluate a condition, and if the condition is true, the next
instruction is skipped.

Example:

BTFSS STATUS, Z ; Check if Zero flag is set

GOTO NotZero ; If not set, jump to NotZero

Use Case: Branch instructions are widely used in loops, decision-making, and interrupt
handling.

2. Call Instructions

Call instructions manage subroutines, enabling code modularity and reuse.

● Instruction: CALL
● Functionality:
○ The CALL instruction saves the current program counter value (return
address) onto the stack and jumps to the specified subroutine address.
○ After completing the subroutine, the RETURN instruction retrieves the saved
address from the stack and resumes execution.

CALL Subroutine ; Call the subroutine at "Subroutine"


...
Subroutine:
; Subroutine code here
RETURN ; Return to the instruction after the CALL

Key Features:

● Stack Usage:
○ The hardware stack in PIC microcontrollers temporarily stores the return
address.
○ Stack depth limitations must be considered to avoid overflow (e.g., 8 levels in
PIC16).
● Nested Subroutines:
○ Nested calls are supported, but each consumes a stack level.
● Interrupt Handling:
○ Interrupts can trigger a call to the interrupt service routine (ISR), which must
end with a RETFIE (Return from Interrupt).
3. Time Delay Loops

Time delay loops are used to generate precise delays in embedded systems by leveraging
the predictable execution time of instructions.

a. Basic Delay Loop

● Concept:
○ A simple loop iterates a known number of times, consuming clock cycles to
create a delay.

Example:​

MOVLW 0xFF ; Load WREG with 255


MOVWF Counter ; Store value in Counter
DelayLoop:
DECFSZ Counter, F ; Decrement Counter, skip if zero
GOTO DelayLoop ; Repeat until Counter = 0

Execution:

● Each iteration consumes a fixed number of clock cycles.


● Total delay = (Number of Iterations) × (Instruction Cycle Time).

b. Nested Delay Loop

● Concept:
○ Nested loops extend delay duration by adding a second counter.

Example:​

MOVLW 0xFF ; Outer loop counter


MOVWF OuterCounter
OuterLoop:
MOVLW 0xFF ; Inner loop counter
MOVWF InnerCounter
InnerLoop:
DECFSZ InnerCounter, F ; Decrement inner counter
GOTO InnerLoop ; Repeat inner loop
DECFSZ OuterCounter, F
GOTO OuterLoop ; Repeat outer loop

c. Instruction Cycle Time

● Delay calculations depend on the oscillator frequency:


d. Accurate Delays

● For precise timing, consider additional cycles consumed by branch instructions and
adjust loop counters accordingly.

e. Timer-Based Delays (Alternative to Loops)

● Timers in PIC microcontrollers are more efficient for longer or precise delays, freeing
up the CPU for other tasks.

Branch instructions (GOTO, BTFSS, etc.) and call instructions (CALL, RETURN) are
fundamental for controlling program flow and implementing modular code in PIC
microcontrollers. Time delay loops provide a simple way to generate delays but require
careful calculation based on clock frequency and instruction timing. For more precise or
extended delays, hardware timers are preferred. Mastery of these concepts is essential for
efficient and reliable embedded system design.
5. PIC I/O Port Programming
● Overview:

○ Configuring GPIO pins for input/output.


○ Using registers like TRISx and LATx.
○ Advanced configurations: Analog input, interrupts.
● Resources:
○ Books:
■ Mazidi's "PIC Microcontroller and Embedded Systems."
○ Datasheets:
■ Specific device datasheets for I/O port configuration.
○ Online Tools:
■ MPLAB X IDE: Code examples for I/O programming.

PIC I/O Port Programming: A Detailed Description

Overview

Input/Output (I/O) ports in PIC microcontrollers enable interaction with external devices.
These ports can be configured as input or output through specific registers. Features like
analog input and interrupt-on-change expand their versatility, making them integral to
embedded systems. Understanding how to configure and program these ports is essential
for efficient hardware interfacing.

Key Topics in PIC I/O Port Programming

1. Configuring GPIO Pins for Input/Output

I/O pins in PIC microcontrollers are grouped into ports, labeled as PORTA, PORTB, PORTC,
etc. Each pin can function as either an input or output, determined by the Data Direction
Register.

● TRISx Register:
○ Determines the direction of each pin in port x:
■ 1 = Input
■ 0 = Output

Example:​
MOVLW 0x0F ; Set lower 4 pins as inputs (TRIS = 1) and upper 4
pins as outputs (TRIS = 0)
MOVWF TRISB ; Configure PORTB

● Reading Input Pins:


○ Use the PORTx register to read the state of input pins.

Example:​
MOVF PORTB, W ; Read the state of PORTB into WREG

● Writing to Output Pins:


○ Use the Latch Register (LATx) for writing output values.

Example:​
MOVLW 0xAA ; Load pattern 10101010
MOVWF LATB ; Write pattern to PORTB
2. Using Registers for I/O

a. TRISx Registers

● Control the data direction of each pin in port x.


● A 1 in a bit position makes the corresponding pin an input, while 0 makes it an
output.

b. LATx Registers (Latch Registers)

● Used to write data to output pins.


● Writing to LATx avoids potential read-modify-write issues that can occur with PORTx.

c. PORTx Registers

● Used for reading the state of pins in port x.


● Reflects the actual voltage levels on the pins.

Register Example:

For configuring PORTA as an output port and toggling its pins:

CLRF TRISA ; Configure all PORTA pins as output (TRIS = 0)


MOVLW 0xFF ; Load all 1s
MOVWF ; Set all PORTA pins high
LATA NOP ; No operation (delay)
CLRF LATA ; Clear all PORTA pins (set low)

3. Advanced Configurations

a. Analog Input

Many GPIO pins on PIC microcontrollers are multiplexed with Analog-to-Digital Converter
(ADC) inputs. To use a pin as analog input:

1. Configure the ANSELx Register:


○ Sets the pin as analog or digital.
○ 1 = Analog mode, 0 = Digital mode.

Example:​
MOVLW 0x01 ; Configure RA0 as analog input
MOVWF ANSELA

2. Configure the ADC module:


○ Select the analog channel corresponding to the pin.
○ Start the ADC conversion process.

b. Interrupt-on-Change (IOC)

● Some GPIO pins support interrupt-on-change, generating an interrupt when the


pin's state changes.
● Steps to configure:
1. Enable the IOC feature for the desired pin using the IOCx registers.
Example:​
BSF IOCAN, 0 ; Enable negative edge interrupt for RA0
BSF IOCAP, 1 ; Enable positive edge interrupt for RA1

2. Enable global and peripheral interrupts.


3. Write an Interrupt Service Routine (ISR) to handle the event.

c. Pull-Up Resistors

● Internal pull-up resistors can be enabled for input pins, reducing the need for external
components.
● Configured using the WPUx (Weak Pull-Up) registers.

Common Programming Scenarios

1. Blinking an LED

● Configure a pin as output and toggle its state.

CLRF TRISA ; Configure PORTA as output


Loop:
BSF LATA, 0 ; Set RA0 high (turn LED on)
CALL Delay ; Call delay subroutine
BCF LATA, 0 ; Set RA0 low (turn LED off)
CALL Delay ; Call delay subroutine
GOTO Loop ; Repeat

2. Reading a Button

● Configure a pin as input and read its state.

BSF TRISB, 0 ; Configure RB0 as input


Loop:
BTFSC PORTB, 0 ; Check if button is pressed (RB0 = 1)
GOTO ButtonPressed
GOTO Loop
ButtonPressed:
; Perform some action
GOTO Loop

3. Using Interrupt-on-Change

● Detects a state change on a pin.

MOVLW 0x01 ; Enable IOC for RB0


MOVWF IOCBP ; Positive edge interrupt for RB0
BSF INTCON, IOCIE ; Enable IOC interrupts
BSF INTCON, GIE ; Enable global interrupts
; ISR (Interrupt Service Routine)
ISR:
BTFSC IOCBF, 0 ; Check if RB0 caused the interrupt
; Handle the interrupt
BCF IOCBF, 0 ; Clear the interrupt flag
RETFIE
Conclusion

PIC microcontroller I/O port programming provides flexibility in interfacing with external
devices. By using registers like TRISx, LATx, and ANSELx, GPIO pins can be configured for
digital or analog input/output. Advanced features such as interrupt-on-change and internal
pull-ups enhance functionality. Understanding these concepts is crucial for designing efficient
and responsive embedded systems.

6. AL Instructions and Programs


● Overview:
○ Arithmetic and Logical (AL) instructions: ADDWF, SUBWF, ANDWF, etc.
○ Writing programs for arithmetic operations and decision-making.
● Resources:
○ Books:
■ "Programming and Customizing the PIC Microcontroller" by Myke
Predko.
○ Online Resources:
■ Assembly language programming tutorials.
■ Microchip forums for AL instruction examples.

Arithmetic and Logical (AL) Instructions and Programs in PIC


Microcontrollers

Overview

Arithmetic and Logical (AL) instructions form the core of data manipulation in PIC
microcontrollers. These instructions perform operations like addition, subtraction, and bitwise
logical functions, enabling arithmetic calculations and decision-making tasks. Mastery of
these instructions and their programming is essential for effective embedded system design.

Key Topics

1. Arithmetic Instructions

a. Common Arithmetic Instructions

● ADDWF (Add WREG and File Register):


○ Adds the contents of the WREG (Working Register) and a file register.
○ The result is stored in either WREG or the file register, based on the
destination bit.

Example:​
ADDWF F, W ; Add WREG and F, store result in WREG

○​
● SUBWF (Subtract WREG from File Register):
○ Subtracts the contents of WREG from a file register.
○ The result is stored based on the destination bit.

Example:​
SUBWF F, F ; Subtract WREG from F, store result in F

○​
● INCF (Increment File Register):
○ Increments the value of a file register by 1.

Example:​
INCF F, F ; Increment F, store result in F
○​
● DECF (Decrement File Register):
○ Decrements the value of a file register by 1.

Example:​
DECF F, F ; Decrement F, store result in F

○​

b. Status Flags for Arithmetic Instructions

Arithmetic operations affect certain status flags in the STATUS register:

● C (Carry): Set if a carry-out occurred in addition or borrow in subtraction.


● DC (Digit Carry): Set if a carry occurs from the lower nibble.
● Z (Zero): Set if the result is zero.

2. Logical Instructions

a. Common Logical Instructions

● ANDWF (Logical AND WREG and File Register):


○ Performs a bitwise AND operation between WREG and a file register.

Example:​
ANDWF F, W ; Perform AND operation, store result in WREG

○​
● IORWF (Inclusive OR WREG and File Register):
○ Performs a bitwise OR operation between WREG and a file register.

Example:​
IORWF F, F ; Perform OR operation, store result in F

○​
● XORWF (Exclusive OR WREG and File Register):
○ Performs a bitwise XOR operation between WREG and a file register.

Example:​
XORWF F, F ; Perform XOR operation, store result in F

b. Bit Manipulation Instructions

● BSF (Bit Set in File Register):


○ Sets a specific bit in a file register.

Example:​
BSF F, 2 ; Set bit 2 in F


● BCF (Bit Clear in File Register):
○ Clears a specific bit in a file register.

Example:​
BCF F, 2 ; Clear bit 2 in F

○​
● BTFSC (Bit Test, Skip if Clear):
○ Skips the next instruction if a specified bit is clear.

Example:​
BTFSC F, 2 ; Skip if bit 2 in F is clear

○​
● BTFSS (Bit Test, Skip if Set):
○ Skips the next instruction if a specified bit is set.

Example:​
BTFSS F, 2 ; Skip if bit 2 in F is set

Writing Programs Using AL Instructions

1. Arithmetic Operations

Adding Two Numbers


MOVLW 0x12 ; Load first number into WREG
MOVWF NUM1 ; Store WREG in NUM1
MOVLW 0x34 ; Load second number into WREG
ADDWF NUM1, W ; Add NUM1 and WREG, store result in WREG

Subtracting Two Numbers


MOVLW 0x20 ; Load 20 into WREG
MOVWF NUM1 ; Store WREG in NUM1
MOVLW 0x10 ; Load 10 into WREG
SUBWF NUM1, W ; Subtract WREG from NUM1, store result in
WREG

2. Logical Operations

AND Operation
MOVLW 0x0F ; Load mask 00001111 into WREG
ANDWF PORTB, F ; Apply mask to PORTB

OR Operation
MOVLW 0xF0 ; Load mask 11110000 into WREG
IORWF PORTB, F ; Apply mask to PORTB

3. Decision-Making Using AL Instructions

Check if a Number is Zero


MOVF NUM1, W ; Load NUM1 into WREG
BTFSC STATUS, Z ; Check if Zero flag is set
GOTO IsZero ; If set, jump to IsZero
GOTO NotZero ; If not, continue to NotZero

Set LED Based on Condition


MOVF PORTA, W ; Read input from PORTA
BTFSS WREG, 0 ; Check if bit 0 is set
BSF LATB, 0 ; Turn on LED (set bit 0 in LATB)
BCF LATB, 0 ; Turn off LED (clear bit 0 in LATB)

Conclusion

Arithmetic and Logical (AL) instructions in PIC microcontrollers provide powerful tools for
performing calculations, bit manipulations, and decision-making tasks. By combining these
instructions with conditional and branch operations, developers can create efficient programs
for complex embedded applications. Understanding these instructions is fundamental to
mastering PIC programming.

7. PIC Bank Switching, Table Processing, Macros, and Modules


● Bank Switching:
○ Registers are divided into banks; switching is required in small-memory
models.
● Table Processing:
○ Storing and accessing data tables in program memory.
● Macros and Modules:
○ Using reusable code for modular programming.
● Resources:
○ Books:
■ Tim Wilmshurst's "Designing Embedded Systems with PIC
Microcontrollers."
○ Datasheets:
■ Detailed explanations in Microchip's PIC datasheets.
○ Online Examples:
■ Tutorials from EmbeddedLab.

PIC Bank Switching, Table Processing, Macros, and Modules

Overview

PIC microcontrollers, especially those with small-memory models like the PIC16 and PIC18
families, utilize techniques like bank switching, table processing, macros, and modules to
manage memory, handle data, and optimize code. Understanding these features is vital for
efficient program design, code reusability, and effective memory usage.

Key Topics
1. Bank Switching in PIC Microcontrollers

Overview

● Bank Switching is a mechanism to access specific blocks of memory, as the total


data memory exceeds the accessible range of the file select register (FSR).
● Registers and RAM in PIC microcontrollers are divided into banks, and switching is
required to access registers or variables in different banks.

How It Works

● Each bank contains 128 bytes (PIC16) or 256 bytes (PIC18) of data memory.
● The Bank Select Register (BSR) in PIC18 or bits in the STATUS register in PIC16
determine which bank is active.

Key Registers

1. STATUS Register (PIC16):


○ Bits RP0 and RP1 select the active bank:
■ RP1 RP0 = 00: Bank 0
■ RP1 RP0 = 01: Bank 1
■ RP1 RP0 = 10: Bank 2
■ RP1 RP0 = 11: Bank 3
Example:​
BCF STATUS, RP0 ; Select Bank 0
BSF STATUS, RP1 ; Select Bank 2

2. BSR Register (PIC18):


○ 4 bits of the BSR register select the active bank.

Example:​
MOVLW 0x02 ; Select Bank 2
MOVWF BSR

Programming Example
CLRF TRISB ; Select Bank 0 to configure PORTB as output
BSF STATUS, RP0 ; Switch to Bank 1
CLRF ANSELB ; Configure PORTB as digital (Bank 1)
BCF STATUS, RP0 ; Return to Bank 0

2. Table Processing

Overview

● Table Processing is used to store and access constant data, such as lookup tables
or predefined values, in program memory.
● In PIC microcontrollers, table data is often stored in Flash memory.

Accessing Data Tables

● Table Read Instructions (PIC18):


○ TBLRD: Reads a byte from the program memory.

Example:​
MOVLW HIGH(Table) ; Load the high byte of the address
MOVWF TBLPTRH ; Set high byte of table pointer
MOVLW LOW(Table) ; Load the low byte of the address
MOVWF TBLPTRL ; Set low byte of table pointer
TBLRD* ; Read the data at the address
MOVWF DATA ; Store the read data in DATA

● Table Addressing:
○ Tables are addressed using a table pointer register (TBLPTR).
○ The read or write operation accesses the data pointed to by the TBLPTR.

Programming Example
Table:
DW 0x12, 0x34, 0x56, 0x78 ; Define a table of constants
ReadTable:
MOVLW HIGH(Table) ; Load high byte of table address
MOVWF TBLPTRH ; Set TBLPTR high byte
MOVLW LOW(Table) ; Load low byte of table address
MOVWF TBLPTRL ; Set TBLPTR low byte
TBLRD* ; Read the first byte
MOVWF DATA ; Store the data in DATA

3. Macros and Modules

a. Macros

● Definition:
○ Macros are reusable code blocks defined using the #define directive in
assembly language or #macro in higher-level languages like MPLAB XC8.
○ They simplify repetitive tasks and enhance code readability.

Example Macro Definition:​


#define ToggleLED
BCF LATB, 0
BSF LATB, 0
Usage in Program:​
CALL ToggleLED ; Toggle LED using the macro

● Advantages:
○ Reduces repetitive coding.
○ Improves maintainability.

b. Modules

● Definition:
○ Modules are separate code files containing functions or subroutines,
designed for reusability and modular programming.
○ They are typically combined during the linking process.
● Creating a Module:

Main Program File ([Link]):​


INCLUDE "my_module.asm" ; Include the module
CALL MySubroutine ; Call a subroutine from the module
Module File (my_module.asm):​
MySubroutine:
; Subroutine code here
RETURN


● Advantages:
○ Encourages modular design.
○ Simplifies debugging and testing.

Summary

● Bank Switching: Facilitates access to extended memory in PIC microcontrollers with


small-memory models using the STATUS or BSR register.
● Table Processing: Efficiently handles constant data stored in program memory,
enabling lookup tables and fixed data handling.
● Macros: Simplify repetitive tasks and improve code readability by defining reusable
code blocks.
● Modules: Promote modular programming by organizing reusable subroutines and
functions into separate files.

Understanding these techniques allows developers to write efficient, organized, and scalable
code for PIC-based embedded systems.
8. PIC Configuration Registers

● Overview:
○ Configuration registers control system-level features like oscillator settings,
watchdog timers, and power modes.
○ Example: CONFIG directive in MPLAB X IDE.
● Resources:
○ Books:
■ "PIC Microcontrollers: Know It All" by Lucio Di Jasio.
○ Online Tutorials:
■ Microchip's online documentation for configuration bits.

PIC Configuration Registers

Overview

Configuration registers in PIC microcontrollers are special memory locations that define
system-level settings. These settings include the type of oscillator, enabling/disabling
watchdog timers, power-up timers, and other key features. They are set at compile-time
using configuration directives and are critical for customizing the microcontroller's operation
to suit the application's requirements.

Key Topics

1. Purpose of Configuration Registers

Configuration registers control various hardware settings, including:

● Oscillator Selection: Choosing between internal or external oscillators.


● Watchdog Timer (WDT): Enabling/disabling the WDT for system reliability.
● Power-Up Timer (PWRTE): Adding a delay during startup to stabilize the power
supply.
● Code Protection: Preventing unauthorized access to program memory.
● Brown-Out Reset (BOR): Protecting the system from unstable voltage conditions.
● Low-Voltage Programming (LVP): Configuring the microcontroller for in-circuit
programming.

2. Using Configuration Registers

Setting Configuration Registers

In PIC microcontrollers, configuration registers are typically defined using the #pragma
config directive in MPLAB X IDE for C programming or the CONFIG directive in assembly
language.

Example (C Programming in MPLAB X IDE)


#pragma config FOSC = INTOSC // Internal oscillator
#pragma config WDTE = OFF // Watchdog Timer disabled
#pragma config PWRTE = ON // Power-up Timer enabled
#pragma config MCLRE = ON // Master Clear Reset enabled
#pragma config CP = OFF // Code protection disabled
#pragma config BOREN = ON // Brown-out Reset enabled

Example (Assembly Language)


CONFIG FOSC = INTOSC ; Internal oscillator
CONFIG WDTE = OFF ; Watchdog Timer disabled
CONFIG PWRTE = ON ; Power-up Timer enabled
CONFIG MCLRE = ON ; Master Clear Reset enabled
CONFIG CP = OFF ; Code protection disabled
CONFIG BOREN = ON ; Brown-out Reset enabled

Common Configuration Options

1.​ Oscillator Settings:


○​ FOSC: Defines the oscillator source.
■​ Example: FOSC = XT (Crystal oscillator).
○​ Options include INTOSC, XT, HS, and LP.
2.​ Watchdog Timer (WDT):
○​ Enables or disables the WDT, which resets the system if the program stalls.
○​ Example: WDTE = ON (WDT enabled).
3.​ Power-Up Timer (PWRTE):
○​ Delays the start of the program for a fixed time to stabilize the power supply.
○​ Example: PWRTE = ON.
4.​ Code Protection (CP):
○​ Prevents external access to the program memory.
○​ Example: CP = OFF (Code protection disabled).
5.​ Brown-Out Reset (BOR):
○​ Resets the microcontroller when voltage drops below a threshold.
○​ Example: BOREN = ON.
6.​ Low-Voltage Programming (LVP):
○​ Enables programming with reduced voltage levels.
○​ Example: LVP = OFF (Low-voltage programming disabled).

3. Default Configuration

Each PIC microcontroller has default configuration settings, typically specified in the
datasheet. These defaults can be overwritten using the #pragma config or CONFIG
directives to meet the specific application requirements.

4. Viewing and Editing Configuration Bits

●​ MPLAB X IDE:
○​ The IDE provides a graphical interface to set configuration bits without writing
code manually.
○​ Steps:
1.​ Open the Configuration Bits window from the IDE menu.
2.​ Use dropdowns to set desired options (e.g., oscillator, WDT).
3.​ The IDE generates corresponding #pragma config directives in the
source code.

5. Example Configuration for a Practical Application

Application Scenario: Using an internal oscillator with WDT for system reliability.

#pragma config FOSC = INTOSC // Use internal oscillator


#pragma config WDTE = ON // Enable watchdog timer
#pragma config PWRTE = ON // Enable power-up timer
#pragma config MCLRE = OFF // Disable external reset pin
#pragma config CP = OFF // Disable code protection
#pragma config BOREN = ON // Enable brown-out reset

6. Programming Notes

●​ Importance of Configuration Bits:


○​ Improper settings can render the microcontroller inoperable (e.g., selecting a
non-existent oscillator).
●​ Testing:
○​ Always verify configuration settings with a test program before deploying.

Conclusion

Configuration registers provide essential control over system-level features in PIC


microcontrollers. By carefully configuring options like the oscillator, WDT, and power-up
timers, developers can customize the microcontroller to meet the application's needs. Using
directives like #pragma config or CONFIG simplifies the process and ensures the system
operates reliably and efficiently.
9. PIC Hardware Connection
●​ Overview:
○​ Connecting external devices (e.g., sensors, actuators).
○​ Circuit design considerations: Pull-up resistors, decoupling capacitors, crystal
oscillators.
●​ Resources:
○​ Books:
■​ "Embedded C Programming and the Microchip PIC" by Barnett, Cox,
and O'Cull.
○​ Online Tools:
■​ Fritzing for visualizing hardware connections.

PIC Hardware Connection


Overview

Establishing proper hardware connections for PIC microcontrollers is crucial for interfacing
external devices like sensors, actuators, and communication peripherals. Effective circuit
design ensures reliable operation and minimizes issues like noise, power instability, or
erroneous signals.

Key Topics
1. Connecting External Devices

a. Sensors

●​ Analog Sensors: Connected to analog input pins (e.g., ANx pins).


○​ Example: Temperature sensor (LM35) connected to an ADC pin.
○​ Design Considerations:
■​ Ensure correct voltage levels (e.g., 0–5V for a 5V system).
■​ Use decoupling capacitors to filter noise.
■​ Configure the ADC module in the firmware.
●​ Digital Sensors: Connected to GPIO pins.
○​ Example: Infrared sensor for obstacle detection.
○​ Design Considerations:
■​ Match voltage levels between the sensor and the PIC.
■​ Use pull-up or pull-down resistors if required by the sensor.

b. Actuators

●​ Motors: Requires driver circuits (e.g., L298 or MOSFETs).


○​ Design Considerations:
■​ Protect the microcontroller using diodes to handle back EMF from
motors.
■​ Use PWM pins to control motor speed.
●​ Relays: Used for high-power devices.
○​ Design Considerations:
■​ Use a transistor to drive the relay.
■​ Place a flyback diode across the relay coil to prevent voltage spikes.
c. Communication Devices

●​ UART: Connects devices like Bluetooth modules or GPS receivers.


○​ Example: TX and RX pins for serial communication.
○​ Use voltage level shifters if interfacing with 3.3V devices.
●​ I2C/SPI: Interfaces sensors and displays.
○​ Design Considerations:
■​ Pull-up resistors are required for I2C lines (SCL and SDA).
■​ Match clock speed with the peripheral for SPI.

2. Circuit Design Considerations

a. Pull-Up and Pull-Down Resistors

●​ Pull-Up Resistors: Connected between a GPIO pin and VCC.


○​ Ensures the pin reads a stable HIGH state when not driven by an external
signal.
○​ Example: 10kΩ pull-up resistor for a push-button input.
●​ Pull-Down Resistors: Connected between a GPIO pin and GND.
○​ Ensures the pin reads a stable LOW state when not driven.
○​ Less common in PIC systems but useful in specific scenarios.

b. Decoupling Capacitors

●​ Placed near the microcontroller’s power pins to stabilize voltage and filter noise.
●​ Typical values: 0.1 µF ceramic capacitor.
●​ Design Rule:
○​ Place capacitors as close as possible to the VDD and VSS pins.

c. Crystal Oscillators

●​ Used to provide a stable clock source for the PIC microcontroller.


●​ Design Considerations:
○​ Connect the crystal oscillator to the OSC1 and OSC2 pins.
○​ Add load capacitors (typically 22pF) based on the crystal's specifications.
○​ Avoid routing high-speed signal traces near the oscillator circuit to minimize
interference.

3. Power Supply Design

●​ Voltage Regulators: Ensure a stable voltage supply (e.g., 7805 for 5V systems).
●​ Bypass Capacitors: Use capacitors (e.g., 10 µF electrolytic and 0.1 µF ceramic) to
smoothen voltage fluctuations.
●​ Battery Backup: For critical systems, include a battery backup circuit to maintain
operation during power loss.
4. Example Hardware Setup

Basic Circuit for PIC16F877A

1.​ Power Supply:


○​ Connect VDD to +5V and VSS to GND.
○​ Place a 0.1 µF decoupling capacitor between VDD and VSS.
2.​ Oscillator Circuit:
○​ Use a 20 MHz crystal oscillator.
○​ Connect the oscillator between OSC1 and OSC2 with two 22pF capacitors to
GND.
3.​ Reset Circuit:
○​ Connect a 10kΩ pull-up resistor to the MCLR pin.
○​ Add a push-button to MCLR for manual reset.
4.​ LED Output:
○​ Connect an LED with a 330Ω resistor to a GPIO pin (e.g., PORTB.0).
○​ Program the pin as an output in firmware.
5.​ Push-Button Input:
○​ Connect a push-button to PORTB.1 with a 10kΩ pull-up resistor to VDD.
6.​ UART Communication:
○​ Connect TX and RX pins of the PIC to a serial device (e.g., PC or Bluetooth
module).
○​ Use a MAX232 IC if RS-232 voltage levels are required.

5. Advanced Considerations

●​ Noise Reduction:
○​ Use grounded planes in PCB design.
○​ Keep high-speed signals (e.g., PWM) away from analog lines.
●​ Protection Circuits:
○​ Add TVS diodes for ESD protection.
○​ Use series resistors (100Ω) on GPIO lines to limit current.
●​ ISP (In-System Programming):
○​ Include an ICSP header for programming the microcontroller without
removing it from the circuit.
○​ Connect MCLR, PGC, PGD, VDD, and GND to the ICSP programmer.

Conclusion

Proper hardware connections for PIC microcontrollers involve careful planning of power,
signal, and peripheral interfaces. Incorporating design elements like pull-up resistors,
decoupling capacitors, and crystal oscillators ensures reliable operation. Understanding
these connections and considerations is critical for robust and efficient embedded system
design.
10. ROM Loaders
●​ Overview:
○​ Loading firmware into the PIC microcontroller's ROM.
○​ Tools like MPLAB IPE, PICkit, or ICD.
●​ Resources:
○​ Books:
■​ "Programming 16-bit Microcontrollers in C" by Lucio Di Jasio.
○​ Online Guides:
■​ Microchip's tutorials on using PICkit programmers.

ROM Loaders for PIC Microcontrollers


Overview

A ROM loader is a software or hardware tool that enables the loading of firmware (program
code) into the ROM (Read-Only Memory) or Flash memory of a PIC microcontroller. The
firmware, usually compiled into a hex file, contains the machine code that tells the
microcontroller how to operate. This process is crucial for preparing the microcontroller for
use in embedded applications, as the PIC microcontroller doesn’t have pre-installed software
and needs to be programmed with the user's application code.

Tools like MPLAB IPE, PICkit, and ICD are commonly used to load firmware into the
microcontroller’s ROM. These tools provide easy and reliable methods for programming,
verifying, and debugging PIC microcontrollers, making them essential in the development
and deployment of embedded systems.

Key Topics
1. The ROM Loading Process
The general process for loading firmware into the ROM (Flash memory) of a PIC
microcontroller involves the following steps:

1.​ Developing the Firmware:


○​ The firmware is first written in a high-level language like C or Assembly using
an Integrated Development Environment (IDE) like MPLAB X IDE.
○​ After the program is written, the code is compiled into a hex file (Intel Hex
format) that can be loaded into the microcontroller.
2.​ Choosing the Loading Tool:
○​ The appropriate hardware programmer/debugger tool must be selected to
load the firmware into the microcontroller. The most common tools include
MPLAB IPE, PICkit, and ICD.
3.​ Connecting the Microcontroller:
○​ The microcontroller is connected to the loading tool (e.g., PICkit 3, PICkit 4,
or ICD 3) via the In-Circuit Serial Programming (ICSP) interface, which
allows the microcontroller to be programmed while it is still in the application
circuit.
4.​ Programming the Firmware:
○​ Using the selected tool (e.g., MPLAB IPE), the hex file is loaded into the
microcontroller’s Flash memory. The tool transmits the program data to the
microcontroller's memory.
5.​ Verification:
○​ After the firmware is loaded, a verification step is often performed to ensure
that the firmware has been correctly written to the microcontroller’s ROM.
This involves reading back the memory and comparing it with the original hex
file.
6.​ Debugging (Optional):
○​ Some tools, such as PICkit and ICD, also support debugging features,
allowing developers to step through the code and test the firmware in
real-time.

2. Tools for Loading Firmware

a. MPLAB IPE (Integrated Programming Environment)

●​ Overview: MPLAB IPE is a software tool provided by Microchip that allows


developers to program their PIC microcontroller directly from a PC. MPLAB IPE
supports a variety of programming tools, such as PICkit and ICD, and facilitates the
loading of firmware into the microcontroller’s Flash memory.
●​ Key Features:
1.​ Supports a wide range of PIC devices: MPLAB IPE is compatible with
many PIC microcontrollers across different families, including PIC10, PIC16,
PIC18, PIC24, and dsPIC.
2.​ Works with different programmers/debuggers: It supports PICkit 3, PICkit
4, ICD 3, and other Microchip tools.
3.​ Firmware loading: Allows loading compiled hex files into the microcontroller.
4.​ Device programming and verification: MPLAB IPE can read the current
contents of a microcontroller’s memory and compare it with the programmed
file to verify the programming process.
●​ How to Use MPLAB IPE:
1.​ Open MPLAB IPE and connect the programmer (e.g., PICkit 3) to the PIC
microcontroller.
2.​ Select the correct device family and specific microcontroller in the tool.
3.​ Load the hex file generated by the compiler (e.g., MPLAB X IDE).
4.​ Click the Write button to load the firmware into the microcontroller.
5.​ Click the Verify button to ensure the contents of the ROM match the hex file.

b. PICkit (e.g., PICkit 3, PICkit 4)

●​ Overview: PICkit is a family of low-cost programmers/debuggers developed by


Microchip for programming and debugging PIC microcontrollers. PICkit 3 and
PICkit 4 are the most commonly used models.
●​ Key Features:
1.​ PICkit 3: Provides ICSP support for programming and debugging.
2.​ PICkit 4: Faster than PICkit 3, with improved debugging capabilities, and
supports more advanced microcontrollers.
3.​ Programming and Debugging: These tools allow for both programming
and debugging of firmware in real-time.
4.​ Low-Voltage Programming (LVP): Supports programming at low voltages
(typically 3.3V).
●​ Steps for Using PICkit:
1.​ Connect the PICkit to the PIC microcontroller via the ICSP interface.
2.​ Open MPLAB IPE or MPLAB X IDE.
3.​ Select the correct device and load the compiled hex file.
4.​ Hit Write to program the microcontroller.
5.​ Optionally, use the debugging features (if supported).

c. ICD (In-Circuit Debugger)

●​ Overview: The ICD is an advanced programmer/debugger tool that provides both


programming and real-time debugging capabilities. It is typically used for more
complex applications requiring advanced debugging features.
●​ Key Features:
1.​ Real-time Debugging: The ICD supports breakpoints, variable monitoring,
and stepping through code.
2.​ ICSP support: Like the PICkit, the ICD uses the ICSP interface for
programming the microcontroller.
3.​ MPLAB X IDE Integration: Works seamlessly with MPLAB X IDE for
debugging and programming.
●​ Steps for Using ICD:
1.​ Connect the ICD to the target microcontroller via ICSP.
2.​ Open MPLAB X IDE and select the microcontroller.
3.​ Load the hex file into the IDE.
4.​ Program the microcontroller by clicking the Program button.
5.​ Use debugging features to step through the code and monitor variables.

3. Programming Modes

There are several modes for loading firmware into a PIC microcontroller:

●​ In-Circuit Serial Programming (ICSP):


○​ The most common method used to load firmware into the PIC. It allows the
microcontroller to be programmed while it is still in the application circuit.
○​ Uses the MCLR, PGC, PGD, VDD, and VSS pins for programming.
●​ Low-Voltage Programming (LVP):
○​ Allows programming of the microcontroller at reduced voltages (often 3.3V)
without the need for high voltage levels.
○​ Available for certain PIC microcontroller families and can be enabled or
disabled through the configuration bits.
●​ High-Voltage Programming:
○​ Used primarily for older PIC microcontrollers or in production environments
where high voltage is required for programming.

4. Example of Loading Firmware using MPLAB IPE

1.​ Prepare Firmware:


○​ Write and compile the firmware using MPLAB X IDE.
○​ The output will be a hex file (e.g., [Link]).
2.​ Setup MPLAB IPE:
○​ Open MPLAB IPE and select the PICkit 3 or PICkit 4 programmer from the
drop-down menu.
○​ Connect the PICkit to the target microcontroller’s ICSP pins.
3.​ Load the Hex File:
○​ Click on Browse and select the compiled hex file.
○​ Verify the selected device is the correct PIC microcontroller model.
4.​ Write to the Microcontroller:
○​ Click Write to program the firmware into the microcontroller’s Flash memory.
○​ After programming, click Verify to ensure the firmware was written correctly.

Conclusion

ROM loaders like MPLAB IPE, PICkit, and ICD are essential tools for programming and
debugging PIC microcontrollers. These tools facilitate the process of transferring firmware
into the microcontroller’s ROM (Flash memory), ensuring that the device runs the desired
program. Understanding the tools and processes for loading firmware is crucial for
successful embedded system development and deployment.

You might also like