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
  ; result was false (bit=0), so execute the following code.
  ... ;

Choosing between 3 or more alternatives

The ``'' 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 ``'' 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---
; 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
SWITCH_F  macro     f_label
          movf f_label,w ; read the location into w
_previous_case set  0
CASE_W         macro     case_const, case_label
          xorlw     case_const^_previous_case
          btfsc     STATUS,Z
          goto case_label
_previous_case set  case_const

18C specific



See also:

file: /Techref/microchip/swstmt.htm, 5KB, , updated: 2003/11/30 16:36, local time: 2024/6/16 18:22, owner: DAV-MP-E62a,

 ©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=""> 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?

  PICList 2024 contributors:
o List host: MIT, Site host, Top posters @none found
- Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.

Welcome to!