Searching \ for 'CASE-statement?' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=case+statement
Search entire site for: 'CASE-statement?'.

Truncated match.
PICList Thread
'CASE-statement?'
1998\02\13@074049 by H.P.d.Vries

flavicon
face
Hi all,

I hope someone can help me with this, because I dont have any bright
ideas right now ;)
Is there a nifty little piece of code out there what does the same as
the "case", e/g/ "switch" statement?

I am creating a MIDI-controller board with a 16-button interface.
Depending on which button is pressed, I want to branch to another pice
of code. Who can help??

Hans

1998\02\13@075302 by Andrew Warren

face
flavicon
face
H.P. de Vries <spam_OUTH.P.d.VriesTakeThisOuTspamstud.tue.nl> wrote:

> I hope someone can help me with this, because I dont have any bright
> ideas right now ;) Is there a nifty little piece of code out there
> what does the same as the "case", e/g/ "switch" statement?
>
> I am creating a MIDI-controller board with a 16-button interface.
> Depending on which button is pressed, I want to branch to another
> pice of code. Who can help??

Hans:

There are many ways to do this... Here's how I usually manage it:

   CASE:

       XORLW   1
       BZ      W_CONTAINED_1

       XORLW   2^1
       BZ      W_CONTAINED_2

       XORLW   3^2
       BZ      W_CONTAINED_3

       etc...

This method takes 3 words (and 3 cycles) per test.  There are
faster/shorter ways, but they generally require your "cases" to be
consecutive numbers.  The method shown above will work with ANY
numbers; it could just as easily have been:

   CASE:

       XORLW   7
       BZ      W_CONTAINED_7

       XORLW   51^7
       BZ      W_CONTAINED_51

       XORLW   12^51
       BZ      W_CONTAINED_12

-Andy

=== Andrew Warren - .....fastfwdKILLspamspam@spam@ix.netcom.com
=== Fast Forward Engineering - Vista, California
=== http://www.geocities.com/SiliconValley/2499

1998\02\13@120138 by davewave

flavicon
face
Some time ago I did a similar project for my personal use. I made a MIDI
foot controller that had 10 switches (bank up/down, and 0 thru 9). It also
drove three LED numeric displays (to display program # selected). The
project used a 16C54.

The code was done very quickly, so it is somewhat sloppy. But it works! I
would be happy to share the code and schematic - let me know if
interested. Would be a good beginner project for someone to
optimize/enhance.

Dave

H.P. de Vries wrote:

{Quote hidden}

1998\02\13@140948 by Bob Shaver

flavicon
face
How about even shorter (see caveats below):

       movlw   high(case)      ;Set upper byte of PC latch
       movwf   PCLATH
       movf    KeyNumber,0
       addwf   PCL,1
Case    goto    Switch1
       goto    Switch2
       goto    ...

Caveats:
1) Only works if all possible values are sequential, starting with 0 (XOR method
works with any set of values).
2) Of course you should (must) verify that KeyNumber is within the valid range
before adding to PCL.
3) Must set PCLATH before the jump table
4) Make sure that the jump table does not cross page boundaries

On Friday, February 13, 1998 7:52 AM, Andrew Warren [SMTP:fastfwdspamKILLspamIX.NETCOM.COM]
wrote:
{Quote hidden}

1998\02\13@143808 by H.P.d.Vries

flavicon
face
Andrew Warren wrote:
{Quote hidden}

Andy,

Thanks for this one!
But what is that faster way (if you happen to know). My case-statement
would have to check W=0...9 (0x0F maybe )  so the short one should work.
What does BZ do btw. I cant find it anywhere in the MPLAB help-files.

Hans

1998\02\13@150035 by John Payson

picon face
> Caveats:
> 1) Only works if all possible values are sequential, starting with 0 (XOR
method
>  works with any set of values).
> 2) Of course you should (must) verify that KeyNumber is within the valid range
>  before adding to PCL.
> 3) Must set PCLATH before the jump table
> 4) Make sure that the jump table does not cross page boundaries

One technique that's helpful here is to round the maximum value up to the
next power of two and AND W with an appropriate mask; then pad the jump
table with something suitable for out-of-bounds conditions.  Not quite as
robust as doing a compare to detect invalid values, but faster and just as
effective at preventing jump-to-random-location syndrome.

1998\02\13@162125 by picinfo

flavicon
face
H.P. de Vries wrote:

> Hi all,
>
> I hope someone can help me with this, because I dont have any bright
> ideas right now ;)
> Is there a nifty little piece of code out there what does the same as
> the "case", e/g/ "switch" statement?
>
> I am creating a MIDI-controller board with a 16-button interface.
> Depending on which button is pressed, I want to branch to another pice
> of code. Who can help??
>
> Hans

 Hans,

You could rethink to gain a simple and fast solution. The PIC architecture
is
better at handling bits than bytes, so if you could binary code your keys
and store them in 2 register files simplicity and speed would be at your
fingertips!

E.g     bytes keys1_8, keys9_16

       case:    btfsc keys1_8,0
                   goto switch1
                   btfsc keys1_8,1
                   goto switch2
                   ...
                   btfsc keys9_16,7
                   goto switch16

Peter

1998\02\13@201409 by Alexandr A. Redchuck

flavicon
face
Hi,Andrew Warren!
13-Feb-98 04:51 you wrote about "Re: CASE-statement?"

> H.P. de Vries <KILLspamH.P.d.VriesKILLspamspamstud.tue.nl> wrote:
>
> > I hope someone can help me with this, because I dont have any bright
> > ideas right now ;) Is there a nifty little piece of code out there
> > what does the same as the "case", e/g/ "switch" statement?

> This method takes 3 words (and 3 cycles) per test.  There are
> faster/shorter ways, but they generally require your "cases" to be
> consecutive numbers.  The method shown above will work with ANY
> numbers; it could just as easily have been:
>
>     CASE:
>         XORLW   7
>         BZ      W_CONTAINED_7
>         XORLW   51^7
>         BZ      W_CONTAINED_51
>         XORLW   12^51
>         BZ      W_CONTAINED_12

I use macro:

Wswitch macro
wsw_last set 0 ;; setup variable only
  endm

case    macro val,addr
  xorlw val^wsw_last
  bz    addr
wsw_last set val
  endm

And example above look as

   Wswitch
   case  7,W_CONTAINED_7
   case 51,W_CONTAINED_51
   case 12,W_CONTAINED_12

WBR,
--
/* Alexandr Redchuk     RemoveMEReAlTakeThisOuTspamreal.kiev.ua  */

1998\02\14@033246 by Alexandr A. Redchuck

flavicon
face
Hi,John Payson!
13-Feb-98 13:48 you wrote about "Re: CASE-statement?"

> > Caveats:

> > 2) Of course you should (must) verify that KeyNumber is within the valid
range
> >  before adding to PCL.

> One technique that's helpful here is to round the maximum value up to the
> next power of two and AND W with an appropriate mask; then pad the jump
> table with something suitable for out-of-bounds conditions.  Not quite as
> robust as doing a compare to detect invalid values, but faster and just as
> effective at preventing jump-to-random-location syndrome.

 This way may be expensive in code size.

I prefer:

   movfw   state
   sublw   (stlast-stfirst)
   skpc
    clrw
   sublw   (stlast-stfirst)
   addwf PCL,f
stfirst:
   goto state1
   goto state2
   ...          ; I can add or remove  entries any time
   goto stateN  ; without changing the rest of code
stlast:
   goto INVALID   ; run-time error handler


--
/* Alexandr Redchuk     spamBeGoneReAlspamBeGonespamreal.kiev.ua  */

1998\02\14@033252 by Alexandr A. Redchuck

flavicon
face
Hi,H.P. de Vries!
13-Feb-98 20:11 you wrote about "Re: CASE-statement?"

> > > I hope someone can help me with this, because I dont have any bright
> > > ideas right now ;) Is there a nifty little piece of code out there
> > > what does the same as the "case", e/g/ "switch" statement?

[^Y]

> >     CASE:
> >         XORLW   1
> >         BZ      W_CONTAINED_1

[^Y]

> Thanks for this one!
> But what is that faster way (if you happen to know). My case-statement
> would have to check W=0...9 (0x0F maybe )  so the short one should work.

1) (0..9 and 0x0F)
 or
2) today 0..9, tomorrow 0..0x0F ?

In the second case use simply jump table as John Payson say.

First case may be transformed to second by incrementing key code.
   incf   key,w
   andlw  0x0F
0..9 shifted to 1..10,  0x0F becomes 0.
We have codes 0..10 without "holes".

> What does BZ do btw. I cant find it anywhere in the MPLAB help-files.

This is "standard" built-in macro

   btfsc STATUS,Z
    goto xxx

AFAIK, MPASM support it.

--
/* Alexandr Redchuk     TakeThisOuTReAlEraseMEspamspam_OUTreal.kiev.ua  */

1998\02\14@093914 by PIC Stuff

flavicon
picon face
> > I hope someone can help me with this, because I dont have any bright
> > ideas right now ;) Is there a nifty little piece of code out there
> > what does the same as the "case", e/g/ "switch" statement?
> >
> There are many ways to do this... Here's how I usually manage it:
>
>     CASE:
>
>         XORLW   1
>         BZ      W_CONTAINED_1
>
>         XORLW   2^1

Note how you have to duplicate the last case value here because of
the previous XOR.  Keeping lists like this intact when you add/remove
values to test for is a pain -> use a macro to do the work.

>         BZ      W_CONTAINED_2
>
>         XORLW   3^2
>         BZ      W_CONTAINED_3

Yes, this is the normal way to do it for few and/or widely separated
(e.g. ASCII) values to test for.  If the cases to test for are all
near each other, then a jump table might be useful (with code handling
unused entries of course).
I actually use some macros for this, so I can write:

     switch
     case "?",Copyright     ; xorlw "?":bz Copyright
     case "G",Go
     case "K",Kill
     case "Z",Sleep
     endswitch
     ; code continues here if no match

The switch and endswitch macros handle setting up a compile-time
variable to keep the last case value in (required because of the XOR)
Full source for this and many other macros are in
http://www.falstaff.demon.co.uk/picres.html  (see macros.i)

Frank

More... (looser matching)
- Last day of these posts
- In 1998 , 1999 only
- Today
- New search...