Arvin Baba
Arvin Baba

Reputation: 11

Is it possible to have more than 2kword code in PIC16F877A?

I Have problem with coding PIC16F877A. GOTO andCALL can only manage 2kword addressing. Some compilers like MikroE use the PCLATH register to work with more than 2kword of code, but when I active interrupt everything messed up and result is randomly reset or my code execute wrongly!

Q: Is it possible to have more than 2kword code and also interrupt ?

When I cut my code and my code is below 2kword and all code is in PAGE0 with interrupt everything is ok. but more than one PAGE (more than 2kword) my code not working properly

Upvotes: 1

Views: 69

Answers (1)

Mike
Mike

Reputation: 4288

Q: Is it possible to have more than 2kword code and also interrupt ?

Yes!!

If you want to work with more than 2k in your controller and handle interrupts you had to select the page with the PCLATH register. And don't forget to save the PCLATH register in your ISR so you could switch back to the correct page when you finished your ISR.

Rst_Vek   code 0x00                    ;Reset address 
      CLRF      PCLATH                 ;
      GOTO      init                   ;

Int_Vek  code 0x04                     ;ISR vector
TMR1_IRQ
      MOVWF     W_safe                 ;save W register
      SWAPF     STATUS,W               ;save Carry with SWAP 
      banksel S_safe
      MOVWF     S_safe                 ;
      MOVFF     PCLATH, Buffer_PCLATH  ;save PCLATH 
      pagesel IRQ
      GOTO      IRQ                    ;could be on a different page know
init:     ...                          ;start of main code

...

Page_2   code 0x0800
IRQ:
      
      ... do ISR stuff
      banksel S_safe
      MOVFF     Buffer_PCLATH, PCLATH   ;PCLATH back
      SWAPF     S_safe,W                ;STATUS back with SWAP
      MOVWF     STATUS                  ;
      SWAPF     W_safe,F                ;
      SWAPF     W_safe,W                ;W back
      RETFIE              

      ;ISR end

But all of this is only an issue if you work with assembler. If you work with a compiler the context saving is handled by the compiler. See here for your MikroE compiler.

Upvotes: 0

Related Questions