Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

verifying assumption on PCLATH

Status
Not open for further replies.

qtommer

Member
i just found out the phenomenon of the requirement of page addressing should my 16F exceed2K of program memory which is the case now. I exceeded 2K but I will definitely not exceed 4K so my code will only deal with Page 0 and Page 1.

I looked at the program memory chart at the 877a datasheet and page 1 spans from 0005h to 7FFh while page 2 spans from 800h to FFFh. Does this mean that for each time i implement a call /goto which is located in page 1, I have to execute the following?

(@page1 , PCLATH <4:3> is 1:0 - except for the very last address on page 1 which is 1:1)

Code:
org       somewhere_on_page0

;now I want to call a routine from page 1

bsf        PCLATH,4
bcf        PCLATH,3

call       routine on page 1

;next line of code is when called routine returns
;do i have to reset the page back to 0 again?


Is my assumption of what I read in the datasheet correct?

thanks alot!:)
 
To call a routine in page 1 you need to set bit 3 of PCLATH - not 4 and, yes, you need to reset it when you return. The bits in PCLATH are the next bits of the address and so the last location of page 1 is still 0:1 (not 1:0 which is page 2).

One thing to watch for,
Do not put a jump at the beginning of your interrupt as it may jump to the wrong page.
Save and restore PCLATH in your interrupt and set it to zero if you make any jumps or calls in your interrupt.

Mike.
 
thank you for your reply

supposing i called a routine in page 1 and this routine in page 1 calls back a routine within page 0, is it okay to once again alter the PCLATH within the routine on page 1 to go back to page 0?

Code:
;;;;;;;;;;;;;;PAGE 0;;;;;;;;;;;;;;;;;;;;;;;;;
routineX  (on page0)
{..

.}
return

main: 
bsf PCLATH,3
call routine on page 1
;;;;;;;;;;;;;;;;;;;PAGE1;;;;;;;;;;;;;;;;;;;;;;
routine on page1
bcf PCLATH,3
call routineX on page 0
return

i know this is a bit wierd but my code is quite long and Routine X is a routine which is often called throughout the program. Thanks!=)
 
Hi,

PCLATH is a continuation of your memorys address, just that the PCL of that chip cannot hold the full address on its own.

If you call out of a subroutine in page 1 to a routine in page 0 then that routine will need to change pclath back to page 0 and then back to page1 at the end to allow the call to return to page1; but I would assume that routine is also being called by code within page 0 ..?
Better then to place a renamed copy of that routine in Page 1 to avoid such problems.

Better still, look for a chip like the 18F series eg 18f4520 where you do not have to bother with PCLath - think some of the latest 16F might also do this.
 
There is no need to change back before a return as the stack holds the full address. The only reason to change PCLATH is if you are going to jump (call) a different bank. If you have a routine in bank 1 that calls page zero then you need to set PCLATH back to zero. Just think of it as being transferred to the program counter whenever a goto or call is executed or a write to PCL happens.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top