Page 1 of 1
C switch case Vs PB Select Case
Posted: Tue Dec 29, 2009 2:25 pm
by Baldrick
Just wondering if any of the C ppl here can tell me of a way to be able to use Case in C with multiple values so I can type less.
e.g. Sudeo code in PB We can do something like:
Code: Select all
ADCValue(channel_number)=ADRESH;
Select ADCValue(0)
Case 130 To 145 ;4k7
ProcedureReturn 1;
Case 175 To 195 ;6k8
ProcedureReturn 2;
EndSelect
In C I am at the moment doing this something like:
Code: Select all
ADCValue[channel_number]=ADRESH;
switch (ADCValue[0]){
Case 130: Case 131: Case 132: Case 133: Case 134: Case 135: Case 136: Case 137:
Case 138: Case 139: Case 140: Case 141: Case 142: Case 143: Case 144: Case 145: //4k7
Return 1;
Break;
Case 175: Case 176: Case 177: Case 178:Case 179: Case 180: Case 185: Case 186: Case 187:
Case 188: Case 189: Case 190: Case 191: Case 192: Case 193: Case 194: Case 195: //6k8
Return 2;
Break;
};
So just wondering if any1 knows a way to use any similar 'To' statement in C something like the way it works in PB?
Re: C switch case Vs PB Select Case
Posted: Tue Dec 29, 2009 3:03 pm
by Fred
Better use standard 'if' 'else if' construct in your case, it will be faster and easier to read.
Re: C switch case Vs PB Select Case
Posted: Tue Dec 29, 2009 3:31 pm
by Perkin
@Baldrick
Did you realise that the check for 181->184 is missing in the C version?
Re: C switch case Vs PB Select Case
Posted: Tue Dec 29, 2009 3:35 pm
by Baldrick
Thanks Fred.
Currently I have used 8 bits of a 10 bit resolution ADC converter by dropping the 2 low order bits in the function register, so I might see if I can do a little bitshifting on my left over high order bits to turn my resolution down from 8 bits to 6 also which might make my coding a little easier to work with & hopefully should keep within my desired needs with this project.
Re: C switch case Vs PB Select Case
Posted: Tue Dec 29, 2009 3:41 pm
by Baldrick
Perkin wrote:@Baldrick
Did you realise that the check for 181->184 is missing in the C version?
No matter Perkin, it is only a rough seudo code pulled from a microcontroller project & quickly even more roughed up in the PB editor to try & help explain what I would have liked to do with the C code.
Guess I am just made a little lazy by the ease of PB these days....

Re: C switch case Vs PB Select Case
Posted: Tue Dec 29, 2009 3:48 pm
by Perkin
Guess I am just made a little lazy by the ease of PB these days....

I know what that's like

Re: C switch case Vs PB Select Case
Posted: Thu Dec 31, 2009 2:40 am
by Airr
Could you do something like this?
Code: Select all
if (ADCValue[0]>=130 && ADCValue[0]<=145) {
Return 1;
}
AIR.
Re: C switch case Vs PB Select Case
Posted: Thu Dec 31, 2009 7:16 am
by Baldrick
Airr wrote:Could you do something like this?
Code: Select all
if (ADCValue[0]>=130 && ADCValue[0]<=145) {
Return 1;
}
AIR.
That certainly looks like a good way to simplify things for me. I will give it a go. I have a reasonably stable proto board working now, which I will recode completely soon & will try this in the new code.
fwiw, I just simply bitshifted my analogue to digital resolution down to 6 bits which has worked wonders also.
It is just a pity that we cannot do some of the neat little tricks in C such as the Case X to Y thing which works so well for me in PB..
Re: C switch case Vs PB Select Case
Posted: Sat May 08, 2010 3:48 am
by Baldrick
lol, np. In my situation I have stuck with using the switch case routine & currently I have moved forward enough to be at a stage where I am now in the process of getting a 1st run prototype production of my micrcocontroller pcb's manufactured. So it's all good.

Re: C switch case Vs PB Select Case
Posted: Wed May 12, 2010 8:26 pm
by Avatar Engineer
Most C compilers (and Pascal, etc) can only use a constant as a case.
Therefore any complex conditions to share a few case statements needs
a prior function to convert conditions to a constant.
If you think of "switch" like a rotary switch, it helps to overcome
the despair of being unable to condense the source code lines.
peace.