need help in Switch case C Program

Discussion in 'Programmer's Corner' started by Ralf, Apr 27, 2009.

  1. Ralf

    Thread Starter Active Member

    Mar 16, 2009
    31
    0
    i have a list of switch case how do i retrieve the data from the case?

    here is my program:
    void led_result (void)
    {
    switch (D1) // List of number display for LED 1
    {
    case 0:
    LATAbits.LATA1 = 0;
    LATAbits.LATA2 = 0;
    LATAbits.LATA3 = 0;
    LATAbits.LATA4 = 0;
    LATAbits.LATA5 = 0;
    LATBbits.LATB6 = 0;
    LATBbits.LATB7 = 1;
    break;

    case 1:
    LATAbits.LATA1 = 1;
    LATAbits.LATA2 = 0;
    LATAbits.LATA3 = 0;
    LATAbits.LATA4 = 1;
    LATAbits.LATA5 = 1;
    LATBbits.LATB6 = 1;
    LATBbits.LATB7 = 1;
    break;

    case 2:
    LATAbits.LATA1 = 0;
    LATAbits.LATA2 = 0;
    LATAbits.LATA3 = 1;
    LATAbits.LATA4 = 0;
    LATAbits.LATA5 = 0;
    LATBbits.LATB6 = 1;
    LATBbits.LATB7 = 0;
    break;

    case 3:
    LATAbits.LATA1 = 0;
    LATAbits.LATA2 = 0;
    LATAbits.LATA3 = 0;
    LATAbits.LATA4 = 0;
    LATAbits.LATA5 = 1;
    LATBbits.LATB6 = 1;
    LATBbits.LATB7 = 0;
    break;

    case 4:
    LATAbits.LATA1 = 1;
    LATAbits.LATA2 = 0;
    LATAbits.LATA3 = 0;
    LATAbits.LATA4 = 1;
    LATAbits.LATA5 = 1;
    LATBbits.LATB6 = 0;
    LATBbits.LATB7 = 0;
    break;

    case 5:
    LATAbits.LATA1 = 0;
    LATAbits.LATA2 = 1;
    LATAbits.LATA3 = 0;
    LATAbits.LATA4 = 0;
    LATAbits.LATA5 = 1;
    LATBbits.LATB6 = 0;
    LATBbits.LATB7 = 0;
    break;

    case 6:
    LATAbits.LATA1 = 0;
    LATAbits.LATA2 = 1;
    LATAbits.LATA3 = 0;
    LATAbits.LATA4 = 0;
    LATAbits.LATA5 = 0;
    LATBbits.LATB6 = 0;
    LATBbits.LATB7 = 0;
    break;

    case 7:
    LATAbits.LATA1 = 0;
    LATAbits.LATA2 = 0;
    LATAbits.LATA3 = 0;
    LATAbits.LATA4 = 1;
    LATAbits.LATA5 = 1;
    LATBbits.LATB6 = 1;
    LATBbits.LATB7 = 1;
    break;

    case 8:
    LATAbits.LATA1 = 0;
    LATAbits.LATA2 = 0;
    LATAbits.LATA3 = 0;
    LATAbits.LATA4 = 0;
    LATAbits.LATA5 = 0;
    LATBbits.LATB6 = 0;
    LATBbits.LATB7 = 0;
    break;

    case 9:
    LATAbits.LATA1 = 0;
    LATAbits.LATA2 = 0;
    LATAbits.LATA3 = 0;
    LATAbits.LATA4 = 0;
    LATAbits.LATA5 = 1;
    LATBbits.LATB6 = 0;
    LATBbits.LATB7 = 0;
    break;
    }

    switch (D2) // List of number display for LED 2
    {
    case 0:
    LATBbits.LATB0 = 0;
    LATBbits.LATB1 = 0;
    LATBbits.LATB2 = 0;
    LATBbits.LATB3 = 0;
    LATBbits.LATB4 = 0;
    LATBbits.LATB5 = 0;
    LATCbits.LATC7 = 1;
    break;

    case 1:
    LATBbits.LATB0 = 1;
    LATBbits.LATB1 = 0;
    LATBbits.LATB2 = 0;
    LATBbits.LATB3 = 1;
    LATBbits.LATB4 = 1;
    LATBbits.LATB5 = 1;
    LATCbits.LATC7 = 1;
    break;

    case 2:
    LATBbits.LATB0 = 0;
    LATBbits.LATB1 = 0;
    LATBbits.LATB2 = 1;
    LATBbits.LATB3 = 0;
    LATBbits.LATB4 = 0;
    LATBbits.LATB5 = 1;
    LATCbits.LATC7 = 0;
    break;

    case 3:
    LATBbits.LATB0 = 0;
    LATBbits.LATB1 = 0;
    LATBbits.LATB2 = 0;
    LATBbits.LATB3 = 0;
    LATBbits.LATB4 = 1;
    LATBbits.LATB5 = 1;
    LATCbits.LATC7 = 0;
    break;

    case 4:
    LATBbits.LATB0 = 1;
    LATBbits.LATB1 = 0;
    LATBbits.LATB2 = 0;
    LATBbits.LATB3 = 1;
    LATBbits.LATB4 = 1;
    LATBbits.LATB5 = 0;
    LATCbits.LATC7 = 0;
    break;

    case 5:
    LATBbits.LATB0 = 0;
    LATBbits.LATB1 = 1;
    LATBbits.LATB2 = 0;
    LATBbits.LATB3 = 0;
    LATBbits.LATB4 = 1;
    LATBbits.LATB5 = 0;
    LATCbits.LATC7 = 0;
    break;

    case 6:
    LATBbits.LATB0 = 0;
    LATBbits.LATB1 = 1;
    LATBbits.LATB2 = 0;
    LATBbits.LATB3 = 0;
    LATBbits.LATB4 = 0;
    LATBbits.LATB5 = 0;
    LATCbits.LATC7 = 0;
    break;

    case 7:
    LATBbits.LATB0 = 0;
    LATBbits.LATB1 = 0;
    LATBbits.LATB2 = 0;
    LATBbits.LATB3 = 1;
    LATBbits.LATB4 = 1;
    LATBbits.LATB5 = 1;
    LATCbits.LATC7 = 1;
    break;

    case 8:
    LATBbits.LATB0 = 0;
    LATBbits.LATB1 = 0;
    LATBbits.LATB2 = 0;
    LATBbits.LATB3 = 0;
    LATBbits.LATB4 = 0;
    LATBbits.LATB5 = 0;
    LATCbits.LATC7 = 0;
    break;

    case 9:
    LATBbits.LATB0 = 0;
    LATBbits.LATB1 = 0;
    LATBbits.LATB2 = 0;
    LATBbits.LATB3 = 0;
    LATBbits.LATB4 = 1;
    LATBbits.LATB5 = 0;
    LATCbits.LATC7 = 0;
    break;
    }

    switch (D3) // List of number display for LED 3
    {
    case 0:
    LATCbits.LATC0 = 0;
    LATCbits.LATC1 = 0;
    LATCbits.LATC2 = 0;
    LATCbits.LATC3 = 0;
    LATCbits.LATC4 = 0;
    LATCbits.LATC5 = 0;
    LATCbits.LATC6 = 1;
    break;

    case 1:
    LATCbits.LATC0 = 1;
    LATCbits.LATC1 = 0;
    LATCbits.LATC2 = 0;
    LATCbits.LATC3 = 1;
    LATCbits.LATC4 = 1;
    LATCbits.LATC5 = 1;
    LATCbits.LATC6 = 1;
    break;

    case 2:
    LATCbits.LATC0 = 0;
    LATCbits.LATC1 = 0;
    LATCbits.LATC2 = 1;
    LATCbits.LATC3 = 0;
    LATCbits.LATC4 = 0;
    LATCbits.LATC5 = 1;
    LATCbits.LATC6 = 0;
    break;

    case 3:
    LATCbits.LATC0 = 0;
    LATCbits.LATC1 = 0;
    LATCbits.LATC2 = 0;
    LATCbits.LATC3 = 0;
    LATCbits.LATC4 = 1;
    LATCbits.LATC5 = 1;
    LATCbits.LATC6 = 0;
    break;

    case 4:
    LATCbits.LATC0 = 1;
    LATCbits.LATC1 = 0;
    LATCbits.LATC2 = 0;
    LATCbits.LATC3 = 1;
    LATCbits.LATC4 = 1;
    LATCbits.LATC5 = 0;
    LATCbits.LATC6 = 0;
    break;

    case 5:
    LATCbits.LATC0 = 0;
    LATCbits.LATC1 = 1;
    LATCbits.LATC2 = 0;
    LATCbits.LATC3 = 0;
    LATCbits.LATC4 = 1;
    LATCbits.LATC5 = 0;
    LATCbits.LATC6 = 0;
    break;

    case 6:
    LATCbits.LATC0 = 0;
    LATCbits.LATC1 = 1;
    LATCbits.LATC2 = 0;
    LATCbits.LATC3 = 0;
    LATCbits.LATC4 = 0;
    LATCbits.LATC5 = 0;
    LATCbits.LATC6 = 0;
    break;

    case 7:
    LATCbits.LATC0 = 0;
    LATCbits.LATC1 = 0;
    LATCbits.LATC2 = 0;
    LATCbits.LATC3 = 1;
    LATCbits.LATC4 = 1;
    LATCbits.LATC5 = 1;
    LATCbits.LATC6 = 1;
    break;

    case 8:
    LATCbits.LATC0 = 0;
    LATCbits.LATC1 = 0;
    LATCbits.LATC2 = 0;
    LATCbits.LATC3 = 0;
    LATCbits.LATC4 = 0;
    LATCbits.LATC5 = 0;
    LATCbits.LATC6 = 0;
    break;

    case 9:
    LATCbits.LATC0 = 0;
    LATCbits.LATC1 = 0;
    LATCbits.LATC2 = 0;
    LATCbits.LATC3 = 0;
    LATCbits.LATC4 = 1;
    LATCbits.LATC5 = 0;
    LATCbits.LATC6 = 0;
    break;
    }
    }

    wad command i should type for getting my case result?
     
  2. Ruptor

    Active Member

    Apr 26, 2009
    44
    0
    Save the data at each case like this.
    ------------
    case 8:
    LATCbits.LATC0 = 0;
    LATCbits.LATC1 = 0;
    LATCbits.LATC2 = 0;
    LATCbits.LATC3 = 0;
    LATCbits.LATC4 = 0;
    LATCbits.LATC5 = 0;
    LATCbits.LATC6 = 0;
    D3data=0;
    break;

    case 9:
    LATCbits.LATC0 = 0;
    LATCbits.LATC1 = 0;
    LATCbits.LATC2 = 0;
    LATCbits.LATC3 = 0;
    LATCbits.LATC4 = 1;
    LATCbits.LATC5 = 0;
    LATCbits.LATC6 = 0;
    D3data=0x10;
    break;
    }
     
  3. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    From your short snippet of code one can only guess at what your function is doing but it seems to be writing to the bits (bit fields?) in several variables (or structures maybe?).

    Surely to get the results of the switch statements all you need do is examine the values of the said variables.
     
  4. Ruptor

    Active Member

    Apr 26, 2009
    44
    0
    The data is spread across several port latches so the only way is save it in a byte that represents the multiple locations like my example D3data byte.
     
  5. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    Why would he want to?

    He is setting the state of the latches depending on the value of some other data D1, D2, D3. so he knows what the state of the latches will be. All he need to do is determine the state of D1, D2 D3 and that is what the switch command is doing already.
     
Loading...