PDA

View Full Version : Is this a case for CASE?



jmgelba
- 9th August 2012, 21:40
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?

HenrikOlsson
- 9th August 2012, 22:13
Hi,
Sure, you can use Select Case. Perhaps something like

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.

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.

aratti
- 10th August 2012, 06:58
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.