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 16:36, local time: 2010/7/30 10:28, owner: DAV-MP-E62a,
TOP NEW HELP FIND: 
38.107.191.112:LOG IN
©2010 PLEASE DON'T RIP! DO: LINK / DIGG! / MAKE!

 ©2010 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!
<A HREF="http://www.piclist.org/techref/microchip/swstmt.htm"> PIC Microcontoller Program Flow Context Switch Methods</A>

Did you find what you needed?

 
FREE PCBs!
Enter the PICList design contest to win a free PCB from olimex.com!
 
miSim DE is an excellent, portable and powerful IDE for developing PIC applications.
The only consistant, simple to use yet powerful development environment. It simulates real-world devices via virtual component "plugins" (LED,LCD,key,motor,TV,etc) in real time, has a syntax highlighting editor, macro assembler and disassembler. Regular updates and third-party plugins keep this software ahead of any other PIC IDE.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .