Myke's Home Page

Book CD-ROM Home

File Copying/Harddrive Setup

Development Tools

Experiments

Projects

Useful Code Snippets and Macros

Introduction to Electronics

Introduction to Programming

Datasheets

PCBs

Links

McGraw-Hill Professional Publishing

"MidGoto" Experiment

Along with the confusing operation of the bank select bits in the mid-range PICmicro MCUs, the "PCLATH" register and execution "Page" selection can also be difficult for new application developers to understand. In this application, I have specified a PICmicro MCU part number with multiple pages built in to show the code that is used for jumping to addresses between pages.

The "MyLGoto" macro used within the application can be used, but the following macro is a better "general case" example:

LGOTO MACRO Label
  movwf  _wGoto               ;  Save "w" to be passed to "Label"
  movlw  HIGH Label
  movwf  PCLATH
  swapf  _wGoto, f            ;  Restore "w" without changing STATUS
  swapf  _wGoto, w
  goto   (Label & 0x07FF) | ($ & 0x01800)
 endm
              
in the macro above, I save the contents of "w" and restore them before the next address is jumped to. Note the two "swapf" instructions before the final "goto" which are taken from the PICmicro interrupt handler code shown in this book which load "w" with the contents of a register and do not change the state of the "Zero" flag in the STATUS register.

Taking a cue from the next experiment, this macro can be streamlined to:

LGOTO MACRO Label
 if ((Label & 0x0800) != 0) then
  bsf    PCLATH, 3
 else
  bcf    PCLATH, 3
 endif
 if ((Label & 0x01000) != 0) then
  bsf    PCLATH, 4
 else
  bcf    PCLATH, 4
 endif
  goto   (Label & 0x07FF) | ($ & 0x01800)
 endm
              
which will update PCLATH correctly, but not affect "W" or "STATUS" in any way. This version would probably be considered "optimal".

The source code is listed below, or it can be accessed from the CD-ROM by clicking Here.

 title  "MidGoto - Low-End Jumping Around."
;
;  In this Application, Jumps Between Device Pages is
;   Demonstrated.  
;
;
;  99.12.25 - Created for the Second Edition
;
;  Myke Predko
;
  LIST P=16c73b, R=DEC
  INCLUDE "p16c73b.inc"

;  Registers

;  Macros
MyLGoto MACRO Label
  movlw  HIGH Label
  movwf  PCLATH
  goto   Label & 0x07FF		;  Jump to Label Without Page Selections
 endm

 __CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _BODEN_OFF & _PWRTE_ON

  PAGE
;  Mainline of LowGoto

 org      0

  goto    Page0Label		;  Goto an Address Within the Same Page


Page0Label			;  Label in Page 0

  MyLGoto Page1Label

 org      0x0800
Page1Label			;  Label in Page 1
  MyLGoto Page0Label


  end
              

Click Here to look at the sixth experiment - lowGoto