please dont rip this site

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:


file: /Techref/microchip/swstmt.htm, 5KB, , updated: 2003/11/30 15:36, local time: 2024/11/14 21:59, owner: DAV-MP-E62a,
TOP NEW HELP FIND: 
18.224.54.148:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://massmind.ecomorder.com/techref/microchip/swstmt.htm"> PIC Microcontoller Program Flow Context Switch Methods</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to ecomorder.com!

 

Welcome to massmind.ecomorder.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .