How to use the switch case statement in C# ?

Discussion in 'Programmer's Corner' started by LETITROLL, Mar 29, 2014.

  1. LETITROLL

    Thread Starter Member

    Oct 9, 2013
    218
    2
    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 .

    Code ( (Unknown Language)):
    1.  
    2. void main() {
    3.  
    4.  PORTA=0x00;
    5.  PORTB=0x00;
    6.  TRISA=0xff;
    7.  TRISB=0x00;
    8.  
    9.  while(1){
    10.  
    11.  switch(PORTA){
    12.  
    13.  
    14.  case 1:
    15.  PORTB.B0=1;
    16.  break;
    17.  delay_ms(500);
    18.  case 2:
    19.  PORTB.B1=1;
    20.  break;
    21.  delay_ms(500);
    22.  case 4:
    23.  PORTB.B2=1;
    24.  break;
    25.  delay_ms(500);
    26.  case 8:
    27.  PORTB.B3=1;
    28.  break;
    29.  delay_ms(500);
    30.  case 16:
    31.  PORTB.B4=1;
    32.  break;
    33.  delay_ms(500);
    34.  case 32:
    35.  PORTB.B5=1;
    36.  break;
    37.  delay_ms(500);
    38. }
    39. }
    40. }
     
  2. panic mode

    Senior Member

    Oct 10, 2011
    1,320
    304
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,012
    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?
     
  4. LETITROLL

    Thread Starter Member

    Oct 9, 2013
    218
    2
    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: Mar 29, 2014
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,012
    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.
     
  6. LETITROLL

    Thread Starter Member

    Oct 9, 2013
    218
    2
    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: Mar 30, 2014
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,012
    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.
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,442
    3,361
    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.
     
  9. LETITROLL

    Thread Starter Member

    Oct 9, 2013
    218
    2

    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: Apr 3, 2014
  10. fernan82

    New Member

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

    Code ( (Unknown Language)):
    1.  
    2.  switch (value)
    3.  {
    4.      case 1:
    5.          x = 0;
    6.          break;
    7.      case 2:
    8.          x = 1;
    9.          break;
    10.  }
    11.  
    is the same as:

    Code ( (Unknown Language)):
    1.  
    2.  if (value == 1)
    3.  {
    4.      x = 0;
    5.  }
    6.  else if (value == 2)
    7.  {
    8.      x = 1;
    9.  }
    10.  
    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:

    Code ( (Unknown Language)):
    1.  
    2.  LATB = 0;
    3.  while (1)
    4.  {
    5.      LATB <<= 1;
    6.  
    7.      if (!LATB)
    8.          LATB = 1;
    9.  
    10.      delay_ms(500);
    11.  
    12.  }
    13.  
    <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: Apr 20, 2014
Loading...