Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 19th January 2008, 11:24 PM   (permalink)
Default Pclath

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
GreenP is offline   Reply With Quote
Old 19th January 2008, 11:43 PM   (permalink)
Default

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
Diver300 is offline   Reply With Quote
Old 20th January 2008, 12:11 AM   (permalink)
Default Thanks Diver

Thanking you very much for that Diver, something to get my head around
GreenP
GreenP is offline   Reply With Quote
Old 20th January 2008, 07:34 AM   (permalink)
Default

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 by penoy_balut; 20th January 2008 at 07:40 AM.
penoy_balut is offline   Reply With Quote
Old 20th January 2008, 09:44 AM   (permalink)
Default

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:
http://www.piclist.com/techref/microchip/pages.htm
picasm is offline   Reply With Quote
Old 20th January 2008, 12:32 PM   (permalink)
Default

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.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline   Reply With Quote
Old 20th January 2008, 02:11 PM   (permalink)
Default

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.
Pommie is online now   Reply With Quote
Old 20th January 2008, 02:15 PM   (permalink)
Default

Quote:
Originally Posted by picasm
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:
http://www.piclist.com/techref/microchip/pages.htm
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.
Pommie is online now   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
What Value to move to PCLATH Suraj143 Micro Controllers 19 18th September 2007 04:31 AM
BANKSEL AND PCLATH DOUBTS? neelam29 Micro Controllers 2 18th February 2006 12:56 PM
Code not working..help Electrix Micro Controllers 15 26th September 2005 11:12 AM
PCLATH heida11 Micro Controllers 2 19th December 2004 12:14 PM
pclath 16f877 jijita Micro Controllers 1 13th September 2004 05:59 PM



All times are GMT. The time now is 04:53 AM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.