Pclath

Status
Not open for further replies.

GreenP

New Member
Hi
I am currently in the middle of a project using a PIC16f88, and have got to the stage where i think the program counter has overflowed, and it is jumping to the same place in the code all the time as opposed to where i want it to go. I have read the data sheet AN556 concerning PCLATH and don't quite understand it fully, it speaks about different pages. could someone please help me on this
Thanks
GreenP
 
For GOTOs and CALLs, the instructions contain 11 bits of the address of where the GOTO or CALL is going to.

11 bits accesses 2048 lines of code.

The PIC16F88 has 4096 lines of code, which can be seen as consisting of two halves, one with the MSB of 0 and the other with an MSB of 1. These halves are called pages. There isn't room in the instruction for the MSB, only for the other 11, so at each GOTO or CALL, the MSB comes from bit 3 of PCLATH.

Any CALL or GOTO that is in one half, that send the program counter to the other half, needs to set or clear Pclath, 3.

If you use MPLAB, you can look at the program memory to see where instuctions are. If the address is 0x000 to 0x7FF, then Pclath, 3 should be clear when GOTO or CALL sends the program counter to that address.

For instance, if there is a line at 0x600, that needs to call a routine at 0x800, the code would be:-

org 0x600
bsf pclath, 3
call 0x800
bcf pclath, 3 ;This is useful if there are any other calls later

org 0x800
;here is the subroutine
return
 
Learn C language, compilers do the banking and paging, just don't exceed the page size for a single routine and limit array to fit the banks.
 
Last edited:
There are two built-in macro commands in MPLAB called LCALL and LGOTO that will handle the long call or goto.

BUT you must be careful to always set the correct page again after a LCALL

You must also be careful on using them in conditional "skip" instructions as they contain several instructions and MPLAB won't warn you either.

There is plenty of info on the piclist site here:
 
You might also consider why you're exceeding a single 2K page?, 2K is a LOT of PIC assembler, your program has to be pretty big to exceed it.
 
As Nigel said, you would have to have written a lot of code to exceed 2k. If you do exceed it then you will get an error 306 - "Crossing page boundary -- ensure page bits are set." If you don't get that error then your problem is probably elsewhere.

Mike.
 

I wrote my own macro to handle it,
Code:
_fcall		macro	Address
        if (Address & 0x800) ==0
            bcf	PCLATH,3
	else
            bsf PCLATH,3
        endif
        call (Address & 0x7ff) | ($ & 0x1800)
        if ($ & 0x800) ==0
            bcf	PCLATH,3
	else
            bsf PCLATH,3
        endif

  endm

Note, it needs additional bits if you use 4 banks.

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…