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

Thread Starter

be80be

Joined Jul 5, 2008
2,072
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:
nt a = 1;
int b = 2;
int c = 3;

switch ( a ) {
case b:
  /* Code */
  break;
case c:
  /* Code */
  break;
default:
  /* Code */
  break;
}
 

philba

Joined Aug 17, 2017
959
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:
nt a = 1;
int b = 2;
int c = 3;

switch ( a ) {
case b:
  /* Code */
  break;
case c:
  /* Code */
  break;
default:
  /* Code */
  break;
}
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.
 

joeyd999

Joined Jun 6, 2011
5,285
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:
nt a = 1;
int b = 2;
int c = 3;

switch ( a ) {
case b:
  /* Code */
  break;
case c:
  /* Code */
  break;
default:
  /* Code */
  break;
}
Each case has to be a constant expression. They cannot be variables.
 

Picbuster

Joined Dec 2, 2013
1,047
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
 

Thread Starter

be80be

Joined Jul 5, 2008
2,072
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
 

xox

Joined Sep 8, 2017
838
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
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.
 

Thread Starter

be80be

Joined Jul 5, 2008
2,072
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:
unsigned char readSw = 8*sw1 + 4*sw2 + 2*sw3 + sw4;
And this don't
Code:
unsigned char readSw;
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.
 

Thread Starter

be80be

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

Thread Starter

be80be

Joined Jul 5, 2008
2,072
This is close but not right
Code:
#define sw1 sw1_PORT
#define sw2 sw2_PORT
#define sw3 sw3_PORT
#define sw4 sw4_PORT
#include "mcc_generated_files/mcc.h"
#include <stdio.h>
void getSw (void)
{
unsigned char readSw = (8*sw1 + 4*sw2 + 2*sw3 + sw4);
unsigned char Sw;
Sw = readSw;
switch(Sw)
{
    case 1: printf("sw1\n"); break;
    case 2: printf("sw2\n"); break;
    case 3: printf("sw3\n"); break;
    case 3: printf("sw4\n"); break;
}
return readSw;
}
 

xox

Joined Sep 8, 2017
838
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:
enum
{
    sw1 = 1,
    sw2 = 2,
    sw3 = 4,
    sw4 = 8
};

int get_all_switches(void)
{
    int result = 0;
    if(sw1_PORT)
        result |= sw1;
    if(sw2_PORT)
        result |= sw2; 
    if(sw3_PORT)
        result |= sw3;     
    if(sw4_PORT)
        result |= sw4;     
    return result;
}

int switch_is_enabled(int all, int which)
{
    return (all & which) == which;
}

void print_switch_state(int all, int which, char* name)
{
    char* state = NULL;
    if(switch_is_enabled(all, which))
        state = "yes";
    else
        state = "no";
    if(name == NULL)
        printf("#%d : %s\n", which, state);
    else
        printf("%s : %s\n", name, state);
}

void show_switch_states(void)
{
    int switches = get_all_switches();
/*
    Print each switch separately
*/ 
    print_switch_state(switches, sw1, "sw1");
    print_switch_state(switches, sw2, "sw2");
    print_switch_state(switches, sw3, "sw3");
    print_switch_state(switches, sw4, "sw4");
/*
    Print sw4 without specifying a "name" tag
*/ 
    print_switch_state(switches, sw4, NULL);
/*
    Check groups of switches; since addition and bitwise-OR work
    the same for powers of two, we can use + operator here...
*/     
    print_switch_state(switches, sw2 + sw4, "both sw2 and sw4");
}
To test it, just call show_switch_states().
 
Top