PIC Microcontoller Program Flow Switch Methods

People use ``switch'' to mean 2 very different things.

choosing between 2 alternatives: if...then...else...endif

I assume you've already evaluated the condition and put the result in some bit somewhere (very often the Z or C bit of the STATUS byte). Then implementing the standard ``if...then...else...endif'' code looks like

  ; is the result true ?
  btfss result_byte,result_bit
  goto else
  ; then:
  ; result was true (bit=1), so execute following code.
  ... ;
  goto endif
else:
  ; result was false (bit=0), so execute the following code.
  ... ;
endif:



Choosing between 3 or more alternatives

The ``switch...case...default'' from C, or the ``case..of'' from Pascal, can choose any one of dozens of alternatives. Here's how this is implemented on the PIC.

The best way (least programming effort) I've found so far is the ``select...case...endcase...endselect'' set of macros from Karl Lunt (1999-05)

Tom Hartman says:

...I came up with a method for a "switch" statement, its a little faster than average because it doesn't reload the variable for each case. Each case is 3 clocks if not equal, 4 clocks if equal. Code can appear between cases, but be careful not to alter the contents of w.
; Switch, case macros:
; Typical usage:
; #define CONSTANT1 1
; #define CONSTANT2 2
; SWITCH_F     file_register
; CASE_W  CONSTANT1, do_case_1
; CASE_W  CONSTANT2, do_case_2
;
; --- Or where W is already the variable---
; SWITCH_W
; CASE_W  CONSTANT1, do_case_1
; CASE_W  CONSTANT2, do_case_2
;--------------
SWITCH_W  macro
_previous_case set  0         ; W already contains the switch variable
          endm
;--------------
SWITCH_F  macro     f_label
          movf f_label,w ; read the location into w
_previous_case set  0
          endm
;--------------
CASE_W         macro     case_const, case_label
          xorlw     case_const^_previous_case
          btfsc     STATUS,Z
          goto case_label
_previous_case set  case_const
          endm
;------------------------------------------------------------------

18C specific

Comments:

Archive:

See also: