• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Microcontroller Tips

Microcontroller engineering resources, new microcontroller products and electronics engineering news

  • Products
    • 8-bit
    • 16-bit
    • 32-bit
    • 64-bit
  • Applications
    • 5G
    • Automotive
    • Connectivity
    • Consumer Electronics
    • EV Engineering
    • Industrial
    • IoT
    • Medical
    • Security
    • Telecommunications
    • Wearables
    • Wireless
  • Learn
    • eBooks / Tech Tips
    • EE Training Days
    • FAQs
    • Learning Center
    • Tech Toolboxes
    • Webinars/Digital Events
  • Resources
    • Design Guide Library
    • DesignFast
    • LEAP Awards
    • Podcasts
    • White Papers
  • Videos
    • EE Videos & Interviews
    • Teardown Videos
  • EE Forums
    • EDABoard.com
    • Electro-Tech-Online.com
  • Engineering Training Days
  • Advertise
  • Subscribe

PIC 16F and register banking explained

July 27, 2016 By Aimee Kalnoskas 2 Comments

by Jon Wilder

Microchip PICAs some of you have noticed, the internal RAM in the PIC 16F appears to be arranged in “banks”. Each bank holds 128 RAM registers total. The first 128 RAM locations are in bank 0, the next 128 RAM locations are in bank 1, so on and so forth.

But hold on…is this REALLY the case? If it is, then why is it that when we indirect address via the FSR pointer register that we can access 256 RAM locations without having to bank select?

Because this isn’t what’s really going on.

iRAM on a PIC 16F is a contiguous space. It starts at address 0x000 and ends at 0x1FF. However, some locations within this space are unimplemented. The datasheet states that iRAM is divided up into 4 banks of 128 registers each. Why is this?

The reason is because there is a limitation in the instructions that the PIC uses to address iRAM locations (basically all of the instructions which have an “f” in them). Byte-oriented instructions which address the registers are in the format of –

Code (text):
PIC 16F

Bit-oriented operations (bsf, bcf, btfss and btfsc) are in the format of –

Code (text):
PIC 16F 2
The opcode is the instruction itself. In the first table, DST is the destination bit that tells the PIC whether to store the result in W (0) or in the file register (1) who’s address is in bits AD0-AD6. In the second table, the 4 MSBs are the instruction, the 3 “BIT” bits are the bit that the instruction will be executed on (0-7), while AD0-AD6 are the address in the file where the bit to be manipulated resides.

Notice AD0-AD6…that’s only 7 address bits!

Basically, instructions which directly address the iRAM register locations can only provide 7 of the address bits. Since 7 bits can only count to 0x7F (decimal 127), this limits the instruction itself to only access 128 RAM locations on its own…hence the limitation.

So how can we overcome the limitation so that we can address the entire iRAM space? With the addition of two more bits of course, but where would they come from?

Simple…the STATUS register!

We’ve all heard them referred to as the “bank select” bits but this is just a convention that Microchip decided to use for its “register banking” concept. Bits RP1 and RP0 (Register Page 1 and Register Page 0 respectively) serve as the upper 2 address bits for instructions which directly address the iRAM locations.

So say for instance we wanted to move a value in W to register TRISA, which has iRAM address 0x85 (b’010000101). The instruction could not supply the leading “01” in the address, so these two bits must come from register bits RP0 and RP1. So prior to writing the contents of W to register TRISA, we must first execute these two instructions –

bsf STATUS,RP0
bcf STATUS,RP1

This makes bit RP1 a 0 while making bit RP0 a 1, which gives us the leading “01” in TRISA’s address.

Now we want to write to PORTA, which resides at iRAM address 0x05 (b’000000101′). Now the leading two bits must be cleared to access location 0x05 –

bcf STATUS,RP0
bcf STATUS,RP1

This clears both of our RP bits, giving us the two leading zeros at the beginning of our instruction, while the remaining 7 bits come from the instruction itself.

With indirect addressing using the FSR register, we only need 1 more bit. This is because the FSR is an 8 bit register so it can address up to 256 iRAM locations on its own. Its 9th bit comes from bit IRP (Indirect Register Page) in the STATUS register.

With a more conventional processor like the Intel 8051, a mov instruction is 8 bits long, followed by 1 or 2 more bytes that provide the address(es) that the mov instruction is being executed on. With an instruction set of this nature, no iRAM ‘banking’ is required. But since the PIC is a RISC processor with a single 14 bit instruction word, there will be limits to what can be done and how things must be done, and this is one of them.

If we were to use the FSR to indirectly address register TRISA, we could simply load the FSR with the value of 0x85, load W with the immediate value to write to TRISA, then load the value in W into the INDF register without having to bank select. This is because register FSR is an 8 bit register and can supply 8 of the 9 address bits on its own. However, we must ensure that bit IRP in the STATUS register is clear prior to doing this otherwise we would end up writing to iRAM register address 0x185 instead of 0x085.

So there you have it…PIC 16F register banking explained.

Filed Under: 8-bit, Products Tagged With: electro-tech-online, microchip, microchiptechnology

Reader Interactions

Comments

  1. Brian says

    November 8, 2017 at 5:24 pm

    “Not sure what Microchip was thinking”, but “PIC12” and “PIC14” are terms used to describe CPU architecture, and “PIC12” and “PIC16” devices might be PIC12 or PIC14 architecture. “Clear as mud!”

    The register bank scheme described here is a good explanation for PICs with a PIC14 instruction set and architecture.The PIC16F505 (and some others) Register Bank Scheme is different, the device has a PIC12 architecture but has extended it using code-page selection bits in the STATUS register and Register Bank selection using the upper bits of the FSR register. As the FSR only requires 5-bits to address each bank, the upper 3bits serve as Bank Selection both for Indirect Addressing and for Direct Addressing. So- you need to track the current value of the FSR to know which register is actually being accessed in each instruction.This is an easier task when writing in a high-level language, but can complicate things when debugging in MPLAB. “as I learned!”

    Reply
  2. usman says

    April 24, 2020 at 6:13 am

    not a neat way to access memory

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Featured Contributions

Five challenges for developing next-generation ADAS and autonomous vehicles

Securing IoT devices against quantum computing risks

RISC-V implementation strategies for certification of safety-critical systems

What’s new with Matter: how Matter 1.4 is reshaping interoperability and energy management

Edge AI: Revolutionizing real-time data processing and automation

More Featured Contributions

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center

EE ENGINEERING TRAINING DAYS

engineering
“bills
“microcontroller
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

RSS Current EDABoard.com discussions

  • Elektronik devre
  • Powering a USB hub: safely distributing current from a shared power supply
  • RF-DC rectifier impedance matching
  • How can I get the frequency please help!
  • 12VAC to 12VDC 5A on 250ft 12AWG

RSS Current Electro-Tech-Online.com Discussions

  • 100uF bypass Caps?
  • Fuel Auto Shutoff
  • Actin group needed for effective PCB software tutorials
  • how to work on pcbs that are thick
  • compatible eth ports for laptop

DesignFast

Design Fast Logo
Component Selection Made Simple.

Try it Today
design fast globle

Footer

Microcontroller Tips

EE World Online Network

  • 5G Technology World
  • EE World Online
  • Engineers Garage
  • Analog IC Tips
  • Battery Power Tips
  • Connector Tips
  • DesignFast
  • EDA Board Forums
  • Electro Tech Online Forums
  • EV Engineering
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips

Microcontroller Tips

  • Subscribe to our newsletter
  • Advertise with us
  • Contact us
  • About us

Copyright © 2025 · WTWH Media LLC and its licensors. All rights reserved.
The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media.

Privacy Policy