Jon Wilder
Active Member
If I'm understanding this correctly, the contents of PCLATH only get written to PCH in the event of a write to PCL?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Hey Jon. Just want to clarify some things for you... When you execute a normal instruction, PCL is advanced by 1, as you probably know. If PCL is at 255, it will rollover to 0 and PCLATH will be advanced by 1. Both PCL and PCLATH are writable registers.
This means that if we add for example 10 to PCL and store it in itself, PCL will advance by 10. If PCL was at around 255, it will rollover, but because this is an ADD instruction, PCLATH is not affected. This can leave you somewhere bad in code. To avoid this, it is routine to check if an overflow will occur and if it will manually increment PCLATH.
Hope this helped,
Mike
This is confusing.
Correct me if I'm wrong but as I thought I understood it -
PCL is incremented by 1 per instruction. Once it reaches 0xFF (255), it rolls over and increments PCH by 1. But PCLATH's contents do not change unless you write data to it.
However, when you add say 10 to PCL, PCL advances by 10 and the current contents of PCLATH are loaded into PCH simultaneously, which overwrites the current data in PCH. If PCH was incremented once already but PCLATH was never written to and is still in its default state, this would reset PCH to 0x00 once the current contents of PCLATH are loaded into PCH and cause the program counter to jump to an unknown location on the first 255 lines of code.
Is this correct?
birdman0_o said:PCH is PCLATH, they aren't two separate registers.
I apologize! I personally struggled with this table stuff last year and actually gave up on it because I could never get it to work. I think I see why now!
Indeed PCLATH and PCH are two different monsters. Apparently the only time when PCLATH is important is when you have more than 2kbytes of code OR when you add to PCL.
In the first case, the program counter steals 2 bits from PCLATH whenever its doing a goto instruction. With less than 2kb of code this will be 00, so no PCLATH adjusting is necessary.
In the 2nd case, it steals PCLATH (5 bits) to finish off the 'computed goto'. Therefore whenever implementing a table, unless you are on page 0 (which is where I always put my table, didn't know why it worked until now) you MUST preload PCLATH with the HIGH address of your table!
Extra precaution is needed for long tables!
I apologize once again, but I think now we both understand what is going on![]()
Instead of 'incrementing' PCLATH load it with the right value with MOVLW HIGH Table and replace Table with the name of yours...If you do this for every table, you won't need to decrease PCLATH again after.