Home     Contact     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS     Terms of services     Privacy policy  
   
 Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science   



The Program Memory Organization

The Program Memory is the place where the user can write the assembly code of his program. Therefore, this is usually the bigest in size memory of the PIC. The PIC 16F88 device, has 4K of Program Memory. Strangely, it can allocate up to 8K of memory, but only the first 4K are physical memory. If the user calls a memory position higher than 4K, it will loop back to the beginning of the program memory (0x00).



The Reset Vector

There are two positions in the Program Memory that you need to know. The first one is the reset vector. The reset vector is the position where the program counter will go to after a reset (or a power on). This position is as expected at position 0x00, at the very beginning of the program memory.



The Interrupt Vector

The other very important position to know is the Interrupt Vector. This is the position where the program counter will go to when an interrupt occurs. The interrupts will be covered extensively later on. But in order to get an idea of what interrupts are, i shall give you an example. Suppose that the program counter is the paper carrier of a vintage type writer. While you type letters, the whole carrier shifts to left, one position every time. The letters are supposed to be your assembly code. Each letter corresponds to one instruction. The carrier will not shift left infinitely. There is a specific point where it has reached it's most left position and needs to be sent back. This position is the hypothetical interrupt and the interrupt vector is the most-right position of the carrier. So, as you type letters (instructions), when the interrupt occurs, no matter what instruction follows or is already executed, the carrier (program counter) shall return to the interrupt vector!

In real life, an interrupt example is the RB0/INT. If this interrupt is enabled, then every time the RB0 pin is HIGH, the program counter goes to the interrupt vector. This interrupt vector is located at the position 0x04, shortly after the reset vector.

          org 0x00 ;Here is the Reset Vector. Here the program starts
          GOTO Start

          org 0x04 ;Here is the Interrupt Vector
          .
          .
          .
          
Start
          .
          .
          .

The above example demonstrates how to start a program. Right on the reset vector, we usually put a GOTO instruction to a position after the Interrupt Vector. That is because we may want to use the Interrupt Vector, and therefore we need to keep it clear of code. You will notice the keyword ORG. This keyword tells to our assembler the position that the following instructions should be placed. So, the first one tells to MASM to put the GOTO Start instruction right on the 0x00 position. The second one, the ORG 0x04, is the position of the interrupt vector. It does not moatter if you do not feel comfortable with the interrupts. You are just on the beginning and this is normal. The interrupts will be discussed in details during the intermediate lessons.




The Program Counter (PC), the PCL and PCLATH registers

The Program Counter is a 13-bit wide register that keeps the position of the program while in execution. The PC will have the value 0x00 (reset Vector) after a reset or a power on of the PIC. Then, after an execution of an instruction, the PC will increase it's position by 1. The instruction that is located to this position will be the next to be executed. Because of it's 13-bit width, the PC can get values up to 8K, but as said before, only the first 4K are physical memory.

The program Counter is composed by two registers. The first one is the PCL. This is a readable and writable register that carries the first 8 bits of the Program Counter. This register can be directly accessed and is located in the third position of all four banks (0x02, 0x82, 0x102 and 0x182). The other 5 bits are NOT readable. the can be indirectly written though through the PCLATH register.

The knowledge provided above shall be useful to you when you start making computed GOTOs. A computed GOTO is a programming technique, that someone can implement data tables. For example, if you add 0x01 to the PCL, then the first element of the table is accessed, if you add 0x02, the second element is accessed and so on. The limitation is that only 256 elements can be accessed each time, as the PCL is an 8-bit register. For longer tables, you need to access the PCLATH.




Program Memory Paging

This is a very important topic and needs of your full attention. I have spent hours and hours of sense-less debugging because i was not aware of the Program Memory Paging that the PIC uses.

Let's take thing from the beginning. The problem occurs when the user does a GOTO or a CALL. These instruction can carry up to 11-bit memory position. But our memory width is 13-bits. In other words, a CALL or GOTO instruction can send the PC from position 0 to position 2047 (2K). But our PIC has 4K (4095) of physical memory! How can a GOTO or a CALL access the whole memory and not just the first 2K of it?

The job is done manually. And this is the point that needs your attention. Microchip calls this "Program Memory Paging". This is nothing else than manually setting the 2 MSB of the PCLATH register that the 11-bit GOTO and CALL instructions cannot reach. An elegant name for the "manually setting the PCLATH bits".

The PCLATH is a 5-bit width register. It carries the bits 8 through 12 of the Program Counter. But the CALL and GOTO instructions can carry up to 11-bits of memory location. So, the 2 MSB of the Program Counter that happens to be also the 2 MSB of the PCLATH (bits 4 and 3) needs to be changed manually. There are 4 obvious pages in the program memory:

Page
Number
PCLATH
bit 4
PCLATH
bit 3
Start
Position
End
Position
0 0 0 0x000 0x7FF
1 0 1 0x800 0xFFF
2 1 0 0x1000 0x17FF
3 1 1 0x1800 0x1FFF

And why is this so important for you? Suppose that you are writing a program that it is smaller than 2 Kbytes. Your whole program is located within the first page (page 0) and only. So, any GOTO and CALL will be FROM page 0 TO page 0. Everything will work fine. Now suppose that your program is longer than 2K. And suppose that in page 0 there is a GOTO (or CALL) to a program location that belongs to page 1. What will happen? The GOTO instruction will have an affect to the first 11 bits of the program counter. So, if the source (the GOTO position) is the position 0x100 in the program memory, and the target of the GOTO is the program memory 0x900, look what happens:

    The Program Counter at the location of the GOTO is:
    0x100 = 00 00100000000 b

    The Program Counter at the target of the GOTO should be:
    0x900 = 01 00100000000 b

    I have kept the highest 2-bits of the PC separated in purpose for you to see the difference.

The GOTO instruction is just 11-bits width, so the GOTO to location 0x900, will actually affect ONLY the 11 first bits of the Program Counter. And actually, it shall be sent to '00100000000' instead of '01 00100000000'. The 2 MSB of the Program Counter shall be kept untouched! The final position of the GOTO 0x900, instead of the 0x900 will be the 0x100 again!

And what can be done for this? There are basically two ways to avoid such errors. The first one is the most obvious. You can manually set the PCLATH 2 MSB bits. So, the correct way of the above GOTO command should be as follows:

          org 0x100            ;You are in position 0x100 (Page 0)
          bcf PCLATH,4         ;PCLATH bit 4 is 0
          bsf PCLATH,3         ;PCLATH bit 4 is 1, The page 1 is selected for the next GOTO
          GOTO TargetPosition  ;This GOTO is done in Page 1 as selected above
          .
          .
          .
          org 0x900            ;You are in position 0x900 (page 1)
TargetPosition
          .
          .

Another way of selecting pages, that i prefer the most, is to use the MASM 'pagesel' keyword. By using this keyword, the appropriate page selection is done automatically, according to the position of the label that follows this keyword. The above example could be written as follows:

          org 0x100                ;You are in position 0x100 (Page 0)
          pagesel TargetPosition   ;The appropriate page (page 1) is selected!
          GOTO TargetPosition      ;This GOTO is done in Page 1 as selected above
          .
          .
          .
          org 0x900                ;You are in position 0x900 (page 1)
TargetPosition
          .
          .

The page selection NEEDS TO BE DONE each and every tome you want to change the PC with a GOTO or a CALL instruction, and the target position is in a different page tfrom the page that the GOTO or CALL was located. When i make such GOTOs or CALL, i usually call them FAR GOTO and FAR CALL, to indicate that the target is in different page.







Confirm your knowledge

There is an online test to check your knowledge on this page. You may reveal the test with the following button:






Previous page ---- Next page



Go back to the book contents

Go to the discussion forum of this book





 

Comments

  Name

  Email (shall not be published)

  Website

Notify me of new posts via email


Write your comments below:
BEFORE you post a comment:You are welcome to comment for corrections and suggestions on this page. But if you have questions please use the forum instead to post it. Thank you.


      

  • At 14 March 2014, 8:34:26 user Giorgos Lazaridis wrote:   [reply @ Giorgos Lazaridis]
    • @Vanderlei If you change the bank during the interrupt routine then definitely save it. Actually it is always a good practice to save contex during interrupts. Microchip provides short routines in the datasheet to save and restore all necessary daya like status register, banks, W register...


  • At 14 March 2014, 1:01:22 user Vanderlei wrote:   [reply @ Vanderlei]
    • Hi,
      Suppose you're on page 1 and an interruption occurs.
      Do I have to save the pclath in the beggining of ISR?
      And restore it before return?
      thanks,
      Vanderlei


  • At 11 January 2013, 10:02:27 user Giorgos Lazaridis wrote:   [reply @ Giorgos Lazaridis]
    • @Shafi sorry i only program in assembly. But as far as i know, C MUST take care of all paging problems.


  • At 9 January 2013, 10:22:40 user Shafi wrote:   [reply @ Shafi]
    • I am facing similar problem with pic18f4620. Whenever i increase the number of source code lines (>4090Kb), my program do not respond as it should. It becomes unstable and seems to be executed from begining. How can i set pagign in mikroC are in config bits.
      Regards


  • At 7 November 2012, 15:31:17 user Heywood Garth wrote:   [reply @ Heywood Garth]
    • An elegant name for the "manually setting the PCLATH bits".

      Thank God! Thank You! Finally after all the searching, reading, blogging, and attempts to understand the CRYPTIC microchip explanations, a common sense explanation of a obvious design limitation in the Pic. Now I can get on with my life, you have dispelled the last grey area of my assembly langauge programming experience! Thankyou again for you eloquent words and I know time spent on this trail. You are not alone.
      Thankyou so much!
      Sincerley,
      Heywood Garth
      atragonus@yahoo.com
      garthmesmerizer.com


  • At 4 April 2012, 20:04:54 user Ali wrote:   [reply @ Ali]
    • Thanks so much for the explanation. It really helped me .


  • At 21 July 2011, 14:48:17 user Kammenos wrote:   [reply @ Kammenos]
    • @Stefan somehow you redirected the MPASM directory to something RGB... So you need to point this directory to the directory that MPASM exists. Search in your hdd for MPASM and you will find it. Then paste this location (directory AND file) to the "location" textbox


  • At 20 July 2011, 20:21:18 user Stefan wrote:   [reply @ Stefan]
    • I have a problem in MPASM,here it's the error that I get:
      [url=http://pic.mk/?v=mpasm1311193442.][img]http://pic.mk/thumbs/mpasm1311193442.[/img][/url]
      I use MPLAB IDE v.7.60
     






    No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise without the prior written permission of the author.

    Read the Disclaimer


    All trademarks used are properties of their respective owners.
    Copyright © 2007-2009 Lazaridis Giorgos.
    All rights reserved.






     HOT in heaven!


  • Disclaimer
  • Book Contents
  • Discussion forum

  • Basics
  • What will you need
  • Choosing the right PIC
  • The MPLAB
  • Getting familiar with the MPLAB environment
  • Creating a new project
  • Open and close projects
  • Creating new files and including them in the project
  • Your very first assembly program
  • Compile a program and transfer to the PIC
  • Section 1: Beginner's theory
  • Memory Organization
  • The Data Memory Organization
  • The Program Memory Organization
  • The instructions
  • General knowledge about instructions
  • Value Loading Instructions
  • Program Flow Instructions
  • Mathematic Instructions
  • Logic Function Instructions
  • Bit Orientated Instructions
  • Byte Orientated Instructions
  • Miscellaneous Instructions
  • The Basic Special Function Registers
  • The Status Register
  • The Option_Reg Register
  • The TRIS and PORT registers
  • Beginner's PIC Tutorials
  • How to use our PIC Tutorials
  • A Pushbutton turning an LED on and off
  • A Simple LED Flasher
  • Interfacing Multiple Switches - The internal Pull-Up resistors
  • An LED Sequencer
  • Interface a Single 7seg Digit
  • Interface Multiple 7seg Digits
  • A 3-digits Decimal Counter
  • A Clever Button
  • Section 2: Intermediate theory
  • Instruction Cycle Duration and Calculated Delays
  • The Timer Modules - Timer0
  • The Timer Modules - Timer1
  • The Timer Modules-Timer2



  • NEW in heaven!



    New Theory: AC electric motor working principle






     Contact     Forum     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS   

    Site design: Giorgos Lazaridis
    © Copyright 2008
    Please read the Terms of services and the Privacy policy