How to use the switch case statement in C# ?

Thread Starter

LETITROLL

Joined Oct 9, 2013
218
Hello to all over here .

Am trying to make this work , it principe is simple , i have 6 push buttons on PORTA & 6 leds to light up one by one on a infinite loop .

What i know about switch case statement is that it switchs the state of a PORT
according to the condition of another port state .
For exaple here i want the leds to be lit one by one in a loop without pressing the PORTA buttons .
I know it not necessary to use the push buttons but am just trying to make the switch function work .

Here is the code text .

Rich (BB code):
void main() {
 
 PORTA=0x00;
 PORTB=0x00;
 TRISA=0xff;
 TRISB=0x00;
 
 while(1){
 
 switch(PORTA){

 
 case 1:
 PORTB.B0=1;
 break;
 delay_ms(500);
 case 2:
 PORTB.B1=1;
 break;
 delay_ms(500);
 case 4:
 PORTB.B2=1;
 break;
 delay_ms(500);
 case 8:
 PORTB.B3=1;
 break;
 delay_ms(500);
 case 16:
 PORTB.B4=1;
 break;
 delay_ms(500);
 case 32:
 PORTB.B5=1;
 break;
 delay_ms(500);
}
}
}
 

Attachments

spinnaker

Joined Oct 29, 2009
7,830
Hello to all over here .

Am trying to make this work , it principe is simple , i have 6 push buttons on PORTA & 6 leds to light up one by one on a infinite loop .

What i know about switch case statement is that it switchs the state of a PORT
according to the condition of another port state .
For exaple here i want the leds to be lit one by one in a loop without pressing the PORTA buttons .
I know it not necessary to use the push buttons but am just trying to make the switch function work .

Here is the code text .

Rich (BB code):
void main() {
 
 PORTA=0x00;
 PORTB=0x00;
 TRISA=0xff;
 TRISB=0x00;
 
 while(1){
 
 switch(PORTA){

 
 case 1:
 PORTB.B0=1;
 break;
 delay_ms(500);
 case 2:
 PORTB.B1=1;
 break;
 delay_ms(500);
 case 4:
 PORTB.B2=1;
 break;
 delay_ms(500);
 case 8:
 PORTB.B3=1;
 break;
 delay_ms(500);
 case 16:
 PORTB.B4=1;
 break;
 delay_ms(500);
 case 32:
 PORTB.B5=1;
 break;
 delay_ms(500);
}
}
}
Is there a question in there somewhere?

What is with the delay statement after each of the break statements? They won't be executed.

What compiler is there for C# on a pic?
 

Thread Starter

LETITROLL

Joined Oct 9, 2013
218
Is there a question in there somewhere?

What is with the delay statement after each of the break statements? They won't be executed.

What compiler is there for C# on a pic?
Yes its for MICROC compiler

When i start the simulation the led 5 is directly lit and stays that way even without delay witch i added just for the test.
The question is how to make all leds light up one after another in a closed loop without pressing the buttons it means controlling them by switching the PORTA state with the switch function rather than pushing buttons and using traditional if (conditions) .
:)
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
Well it is mikro C then not C#.

Sorry I still have no idea what you are asking. You said you want to use a switch and not a button. Since when is a button not a switch?

If you mean closed contact vs momentary contact (a button). The the code for sensing that switch closure will be no different.
 

Thread Starter

LETITROLL

Joined Oct 9, 2013
218
Well it is mikro C then not C#.

Sorry I still have no idea what you are asking. You said you want to use a switch and not a button. Since when is a button not a switch?

If you mean closed contact vs momentary contact (a button). The the code for sensing that switch closure will be no different.
Sorry i wasn't clear enough , but the switch am talking about is a function in C language that could also be used in MikroC since it uses the same language .

This function switches the state of a variable , in my case its a PORT that is normally manually controlled by buttons , but i want to control that port by the switch function instead .

Of course when portA switches portB acts the same .

Anyways thanks for trying to help , i will wait for other opinions .
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
I am afraid you are still not being clear. I hope someone else has better luck in figuring out what you are trying to say.

Maybe I understand, don't know. There is no such thing as a "switch function". The switch keyword what you are referencing has nothing to do with changing anything. It is nothing but a easier to read if statement.

My suggestion is that you find a basic course on C. These are very rudimentary concepts that any beginning C programmer should know.
 

MrChips

Joined Oct 2, 2009
30,824
You have a number of problems.

1) On reset, PORTA pins are analog inputs. You have to change the pins to digital by writing 0x06 to ADCON1.

2) Your PORTA pins are being pulled up to VDD and the push button grounds the pin.
Hence you have to invert your logic.

3) Make sure you AND the inverted result of PORTA with 0x3F to mask out RA7 and RA6.
 

Thread Starter

LETITROLL

Joined Oct 9, 2013
218
I have been into this for like more than 4 years my recommendation is that you discover a primary course on C. These are very standard ideas that any starting C developer should know.

Like this book for example ? Let as C

Please shall you give some book suggestions , just to get on the right road , and btw am planning to program other types of uCs like toshibas and intels .
 
Last edited:

fernan82

Joined Apr 19, 2014
26
You're misunderstanding what switch is, think of it like this:

Rich (BB code):
 switch (value)
 {
     case 1:
         x = 0;
         break;
     case 2:
         x = 1;
         break;
 }
is the same as:

Rich (BB code):
 if (value == 1)
 {
     x = 0;
 }
 else if (value == 2)
 {
     x = 1;
 }
only difference is the compiler may be able to optimize the switch better if the cases are sequential. If you want to light the LEDs one by one try something like this:

Rich (BB code):
 LATB = 0;
 while (1)
 {
     LATB <<= 1;
  
     if (!LATB)
         LATB = 1;
  
     delay_ms(500);
  
 }
<edit>Also on most PICs you should read from the PORT and write to the LAT register. Here I read from the LAT because since the port is set as output it makes no difference but writing to the PORT may not work on some PICs
 
Last edited:
Top