Is this a case for CASE?


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305

    Default Is this a case for CASE?

    I have 2 input pins that can be either both off, either one on, or both on. Each combination needs a specific number applied to it.
    both off = 0
    portb.6 on = 2
    portb.7 on = 10
    portb.6 and portb.7 on = 19

    So if both pins where high variable SETPOINT = 19.

    I've tried if then statements but I cant get them to work.
    I tried while wend hoping that while the pin or combo is high the correct value is used but I cant get that to work either.

    Any ideas?

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: Is this a case for CASE?

    Hi,
    Sure, you can use Select Case. Perhaps something like
    Code:
    Select Case (PortB & %11000000)
    
    Case 192
    	SetPoint = 19
    Case 128
    	SetPoint = 10
    Case 64
    	SetPoint = 2
    Case 0
    	SetPoint = 0
    End Select
    But I think IF-THEN will compile to less code but execute a bit slower since it will test all four conditions even if it finds a match in the first one.
    Code:
    PinStates = PortB & %11000000
    If PinStates = 192 THEN SetPoint = 19
    If PinStates = 128 THEN SetPoint = 10
    If PinStates = 64 THEN SetPoint = 2
    If PinStates = 0 THEN SetPoint = 0
    As with most things there are several ways of doing it and these two are likely not the only ones.

    /Henrik.

  3. #3
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: Is this a case for CASE?

    Code:
    Setpoint  VAR byte [4]
    
    Setpoint[0]=0
    Setpoint[1]=2
    Setpoint[2]=10
    Setpoint[3]=19
    
    
    Main_Loop:
    
    Setpoint[PortB>>6]
    
    Goto Main_Loop
    This is an additional solution without If/Then or Select Case.

    Cheers

    Al.
    All progress began with an idea

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts