veerubiji
Full Member level 2
I am trying to add some cases to switch. Initial code was written by expert. I am trying to add some functions to that. I have understand the code what he wrote but now I have to add something but not able to add.
the above code is working good with all comands such as (no_cmd, info, ...) but now i want to add mux selection to the command "muxsel". I have to choose one input among 8 inputs. so i have added one more case like this.
but program running into "muxsel" case command and not excuting inside switch case. I have tried like writing another function for inside switch case and calling that function in "muxsel"case command.
even not working . when i tried with second option it is running infinite times only in muxsel case not entering into second switch case in that command. What can I do if I want to select different cases of mux in "muxsel". any help appreciated.
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 switch case as i described earlier. 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"
Thanks.
Code:
//global variables.
unsigned char state = 0;
unsigned char debug = 0;
unsigned int status = 0x0010;
unsigned int i;
//External variables
unsigned char Command;
unsigned int Param;
extern unsigned char rx_counter0;
unsigned char CompIndex; // Index in Copmare array
unsigned char Compare[COMPBUFSIZE]; // Command string tokenize
unsigned char Step
#define CMD_SIZE 8 //Number of commands
#define no_cmd 0
#define info 1
#define frq 2
#define phs 3
#define mem 4
#define clearm 5
#define db 6
#define muxsel 7
flash unsigned char * cmdList[CMD_SIZE] = {
"no_cmd", // [0] no com
"info", // [1] Displayes version date etc.
"frq", // [2] Start logging data param gives logging time in sec
"phs", // [3] Stop logging
"mem", // [4] Read Flash memory log (stopLog)
"clearm", // [5] Clear Flash memmory (stopLog)
"db", //[6] Set debug function
"muxsel" // [7] mux selection
};
void main(void) {
status = 0x0010;
init(); //Initialize controller
Command = info;
debug = 0;
while (1) {
if (rx_counter0) getCom();
if (Command) uniCom();
#asm("WDR"); //Reset WD timer
} // EOF "while(1)"
} // EOF "main(void)"
void uniCom(void) {
switch (Command) {
case (no_com):
Command = 0;
break;
case (info):
printf("\r\n\r\n");
printf("university\r\n");
printf("name\r\n");
printf("sweden\r\n");
Command = 0;
break;
case (db):
Command = 0;
break;
case (frq):
if (Param < 500) SetWGFreq(Param);
Command = 0;
break;
case (phs):
if (Param < 4095) SetWGPhase(Param);
Command = 0;
break;
case (muxsel):
printf(muxselection);
Command = 0;
break;
case (mem):
Command = 0;
break;
case (clearm):
Command = 0;
break;
default:
Command = 0;
break;
}
}
void getCom(void) {
unsigned char c;
// Read from ring-buffer and fill in Compare buffer
while (rx_counter0) { // while unread contents in ring-buffer
c = getchar(); // fetch next byte
if (CompIndex >= COMPBUFSIZE)
CompIndex = 0; // overflow protection
// Analyze char
if (c == '#') { // Manual start
CompIndex = 0;
} else if (c == '\r') { // CR continue (end of cmd without argument)
Compare[CompIndex] = '\0'; // fill in end character of comp string
break; // possible valid cmd received -> check out
} else if (c == '\n') { // New line (ignore)
// Do nothing (ignore)
} else if (c == 8) { // Backspace
if (CompIndex)
CompIndex--; // decrement index
} else if (c == 9) { // Horizontal TAB
help(); // Write out cmds
} else if (c == 27) { // ESC button
Command = 0; // Stop current command
Param = 0; // Clear argument
Step = 0;
} else {
Compare[CompIndex++] = c; // Default action: Store character
}
if (!rx_counter0)
return; // if no more data to read -> exit
}
CompIndex = 0; // reset, ready for next command
c = 1;
while (c < CMD_SIZE) { // For each command
if (strncmpf(Compare, cmdList[c], strlenf(cmdList[c])) == 0)
break;
c++;
}
if (c < CMD_SIZE) { // If match on normal commands
Command = c;
if (isdigit(Compare[strlenf(cmdList[c])])) {
Param = atoi( & Compare[strlenf(cmdList[c])]);
} else {
Param = 0;
}
printf("@%s\r\n", & Compare); //Ack command
} else {
printf("&E;1;\r\n"); // Command not found
printf("->??: '%s'\r\n", & Compare); // If no match
Command = 0;
}
}
void help(void){
unsigned char i;
printf("Commands: ");
for(i=0;i<CMD_SIZE;i++){
printf(cmdList[i]);
printf(", ");
}printf("\r\n");
}
Code:
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:
-------------------
}
Command = 0;
break;
but program running into "muxsel" case command and not excuting inside switch case. I have tried like writing another function for inside switch case and calling that function in "muxsel"case command.
Code:
void selcase(void) {
unsigned char c;
printf("muxselection");
while (rx_counter0) {
c = getchar();
getchar();
switch (c) {
---------
}
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 switch case as i described earlier. 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"
Thanks.