C I am not but I'm learning trying to figure a menu out and the best way to go.

Discussion in 'Embedded Systems and Microcontrollers' started by be80be, Oct 6, 2017.

  1. be80be

    Thread Starter AAC Fanatic!

    Jul 5, 2008
    1,900
    394
    I was thinking I have 4 button test box for input and going to use the LCD for outputing menu
    would select case be the best way what get's me about C is the using printf
    Code (Text):
    1. nt a = 1;
    2. int b = 2;
    3. int c = 3;
    4.  
    5. switch ( a ) {
    6. case b:
    7.   /* Code */
    8.   break;
    9. case c:
    10.   /* Code */
    11.   break;
    12. default:
    13.   /* Code */
    14.   break;
    15. }
     
  2. philba

    Active Member

    Aug 17, 2017
    959
    303
    A switch statement is similar to a series of if then else if then else if... I prefer a switch statement because it is more readable, especially when the number of cases is more than 3 or so. Though, typically it results in the same number of instructions. Depending on your compiler, YMMV. Really more of a personal preference.

    I often use a switch statement to build a finite state machine. But that's the subject of a much long discussion.
     
  3. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    4,230
    6,389
    Each case has to be a constant expression. They cannot be variables.
     
  4. nerdegutta

    Moderator

    Dec 15, 2009
    2,660
    919
    Don't forget to handle switch bounce. :)

    I prefer switch - case.
     
  5. Picbuster

    Well-Known Member

    Dec 2, 2013
    980
    126
    I solved this the following way:
    Or all switches to an interrupt
    Read all switches Result= 8*sw1 + 4*sw2 + 2*sw3 + sw4
    allowing you to combine knobs ( eq sw4 + sw8)
    State machine
    switch (Result)
    {
    case 1:
    return;
    .
    .
    Case 15:
    return;

    }

    Picbuster
     
  6. be80be

    Thread Starter AAC Fanatic!

    Jul 5, 2008
    1,900
    394
    I'm reading the sw on port change it works well. There seems to be solid readings
    Just learning xc8 and you don't no how it is working you get errors with there sample code let alone something i write LOL
     
  7. xox

    Member

    Sep 8, 2017
    351
    74
    What microcontroller are you developing this for? With a little more info (and preferably some code) we may be able to help you get this off the ground.
     
  8. be80be

    Thread Starter AAC Fanatic!

    Jul 5, 2008
    1,900
    394
    I'm using the 16f18855 Not had much time to mess with this. My main problem I don't think is writing the code more them crazy errors I get
    with xc8 and mplab-X
    This works
    Code (Text):
    1. unsigned char readSw = 8*sw1 + 4*sw2 + 2*sw3 + sw4;
    And this don't
    Code (Text):
    1. unsigned char readSw;
    2. readSw = 8*sw1 + 4*sw2 + 2*sw3 + sw4;
    My thinking is I made a variable readSw
    mplab said i made 2 and don't like it.
     
  9. xox

    Member

    Sep 8, 2017
    351
    74
    The second initialization looks perfectly fine, can you post a more complete state of the code?
     
  10. be80be

    Thread Starter AAC Fanatic!

    Jul 5, 2008
    1,900
    394
    Well I started with this but I got figure how to cast the readSw to SW
    Code (Text):
    1. unsigned char readSw = (8*sw1 + 4*sw2 + 2*sw3 + sw4);
    2. switch(SW)
    3. {
    4.     case 1; printf("sw1\n"); break;
    5.     case 2; printf("sw2\n"); break;
    6.     case 3; printf("sw3\n"); break;
    7.     case 3; printf("sw4\n"); break;
    8. }
     
  11. be80be

    Thread Starter AAC Fanatic!

    Jul 5, 2008
    1,900
    394
    This is close but not right
    Code (Text):
    1. #define sw1 sw1_PORT
    2. #define sw2 sw2_PORT
    3. #define sw3 sw3_PORT
    4. #define sw4 sw4_PORT
    5. #include "mcc_generated_files/mcc.h"
    6. #include <stdio.h>
    7. void getSw (void)
    8. {
    9. unsigned char readSw = (8*sw1 + 4*sw2 + 2*sw3 + sw4);
    10. unsigned char Sw;
    11. Sw = readSw;
    12. switch(Sw)
    13. {
    14.     case 1: printf("sw1\n"); break;
    15.     case 2: printf("sw2\n"); break;
    16.     case 3: printf("sw3\n"); break;
    17.     case 3: printf("sw4\n"); break;
    18. }
    19. return readSw;
    20. }
     
  12. xox

    Member

    Sep 8, 2017
    351
    74
    Okay, so I'm assuming that those swX_PORT variables are binary (on/off) values automatically updated by the processor then? If so then something like this should work:

    Code (Text):
    1. enum
    2. {
    3.     sw1 = 1,
    4.     sw2 = 2,
    5.     sw3 = 4,
    6.     sw4 = 8
    7. };
    8.  
    9. int get_all_switches(void)
    10. {
    11.     int result = 0;
    12.     if(sw1_PORT)
    13.         result |= sw1;
    14.     if(sw2_PORT)
    15.         result |= sw2;
    16.     if(sw3_PORT)
    17.         result |= sw3;    
    18.     if(sw4_PORT)
    19.         result |= sw4;    
    20.     return result;
    21. }
    22.  
    23. int switch_is_enabled(int all, int which)
    24. {
    25.     return (all & which) == which;
    26. }
    27.  
    28. void print_switch_state(int all, int which, char* name)
    29. {
    30.     char* state = NULL;
    31.     if(switch_is_enabled(all, which))
    32.         state = "yes";
    33.     else
    34.         state = "no";
    35.     if(name == NULL)
    36.         printf("#%d : %s\n", which, state);
    37.     else
    38.         printf("%s : %s\n", name, state);
    39. }
    40.  
    41. void show_switch_states(void)
    42. {
    43.     int switches = get_all_switches();
    44. /*
    45.     Print each switch separately
    46. */
    47.     print_switch_state(switches, sw1, "sw1");
    48.     print_switch_state(switches, sw2, "sw2");
    49.     print_switch_state(switches, sw3, "sw3");
    50.     print_switch_state(switches, sw4, "sw4");
    51. /*
    52.     Print sw4 without specifying a "name" tag
    53. */
    54.     print_switch_state(switches, sw4, NULL);
    55. /*
    56.     Check groups of switches; since addition and bitwise-OR work
    57.     the same for powers of two, we can use + operator here...
    58. */    
    59.     print_switch_state(switches, sw2 + sw4, "both sw2 and sw4");
    60. }
    To test it, just call show_switch_states().
     
    be80be likes this.
Loading...