I am trying to execute some commands using USART communication and reading some values using ATmega32-A. have a look at my code . My problem is i am trying to add some cases (nested switch case) but i am not getting what i want. I hope i am giving all most all information required to solve my problem.
void uniCom(void) {
switch (Command) {
/* ... */
case (muxsel):
printf(muxselection);
switch (c) {
case 1:
printf("this is mux chaneel1");
DDRB = 0b10111111;
PORTB = 0b00000000;
printf("adc Value", ReadAdc());
Command = 0;
break;
case 2:
/*-------------------*/
break;
}
Command = 0;
break;
/* ... */
default:
Command = 0;
break;
}
}
The problem is undefined c. And I don’t see any declaration of muxselection, maybe missing “” ? Now the second approach.
void selcase(void) {
unsigned char c;
printf("muxselection");
while (rx_counter0) {
c = getchar();
switch (c) {
case 1:
printf("this is mux chaneel1");
DDRB = 0b10111111;
PORTB = 0b00000000;
printf("adc Value", ReadAdc());
Command = 0;
break;
case 2:
/*-------------------*/
break;
}
}
}
void uniCom(void) {
switch (Command) {
/* ... */
case (muxsel):
printf(muxselection);
selcase();
Command = 0;
break;
/* ... */
default:
Command = 0;
break;
}
}
My problem is i am executing all the commands as i declared but i want to select some more cases in one of the main switch case command “muxsel”. for that i wrote nested switch case. if i select “muxsel” command on hyperteminal then it is printing like “muxselection” then if i enter 1 to select “case ‘1’”in second switch, nothing is printing. it is printing “command not found”. what is the problem. I want execute nested switch but i am not able to do that using above code I have tried like this also.
void selcase(void) {
unsigned char c;
printf("muxselection");
while (rx_counter0) {
c = getchar();
switch (c) {
case '1':
printf("this is mux chaneel1");
DDRB = 0b10111111;
PORTB = 0b00000000;
printf("adc Value", ReadAdc());
c= 0;
break;
case '2':
/*-------------------*/
break;
default;
break;
}
}
}
creating one function for nested switch case and calling in main switch case as shown below.
void uniCom(void) {
switch (Command) {
/* ... */
case (muxsel):
printf(muxselection);
selcase();
Command = 0;
break;
/* ... */
default:
Command = 0;
break;
}
}
this way also not working please suggest me how to overcome this problem. i want to select one of the command in main switch case such as “muxsel” after that i have select mux channels using case statement. any help appreciated.
Thanks in advance.
I have solved this problem.
OK… the code isn’t terribly clear, but I think I see your problem.
You tried to modify the code like this:
First, you don’t have
cdeclared in the scope of theuniCom()function. So that would not compile. Because you didn’t give full code I assume you know that and probably really did something like this:Which leads to the next problem. You’re asking for a char
cbut your cases are built on anint. For example, if the user enters 3, what you’re getting is the character ‘3’ or the int 51. Check the ASCII TableSo your cases are for start of header (SOH), start of text (STX), etc right now… that’s not going to work the way you wanted it to. You need to either do this:
Or do this:
Since you didn’t give your input, or how c was defined, I could be wrong… but I’m going to wager that’s your problem. By the way, make sure you have a
defaultcase at the end with a message like “bad input”, it makes this type of thing easier to catch.EDIT:
Modify the code as follows and share the result:
and