Help Getting Stuck in Case

This is a discussion on Help Getting Stuck in Case within the C Programming forums, part of the General Programming Boards category; I have written a switch state program that uses the Renesas M16C Microcontroller, the switch case program works fine however ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    Help Getting Stuck in Case

    I have written a switch state program that uses the Renesas M16C Microcontroller, the switch case program works fine however I now have to link the microcontroller with the PC via RS232. This works fine i have written the uart and I can enter pin number and change speed, time and date by sending command from PC. However When i want to change Direcation Forward or Reverse the program crashes as I think it is getting stuck in the state. Also if I send $cs# to the microcontroller is should send the Speed to the computer, however i only need to recive it once but it sends over and over again. Please can you give my any suggestions to my problems, Code is shown below. Apolgies I the code is formatted on my post however in preview it is not formatted not sure how to format it.

    Code:
    case 'd':
    	if (str[2]=='f')
    	              {
    		RAMP_DOWN(direction);
    	                direction=FORWARD;
    	                TA4=65000;
    		TABSR_4=0;
    		reload=15000;
    		TA4=reload;
    		TABSR_4=1;
    		}
    		break;
    					
    	if (str[2]=='r')
    	        {
    		RAMP_DOWN(direction);
    		direction=REVERSE;
    		TA3=65000;
    		TABSR_3=0;
    		reload=15000;
    		TA3=reload;
    		TABSR_3=1;
    	        }        
    	             break;
    
    case 'c':
    	if (str[2]=='s')
    	{
                         RS232PutString(str_RPM);
         	     RS232PutChar('\n');
          	     RS232PutChar('\r');
    	}
    break;
    Last edited by bigvanbowski; 03-24-2010 at 03:04 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    To format it, click on the edit button, then click on "Advanced" button.

    Then highlight your code, and click on the # icon, at the top of the edit window. Really makes a difference.

    It's very late, but I'm sure someone will be along after bit with some advice for you. I have none, atm.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    Quote Originally Posted by Adak View Post
    To format it, click on the edit button, then click on "Advanced" button.

    Then highlight your code, and click on the # icon, at the top of the edit window. Really makes a difference.

    It's very late, but I'm sure someone will be along after bit with some advice for you. I have none, atm.
    Thanks

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    I dont know, like you say the switch seems fine, what can be seen of it, maybe post a little more code, from what you are sayign it seems the externals may be at fault?

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    This is part of Orignal Switch case statement

    Code:
    void main(void)                
    {
        char str_RPM[]="00000";     // Buffer for RPM string
        unsigned char pin_cnt=0;    // Counter for PIN digit
        unsigned char speed_cnt=0;  // Counter for Speed digit
        _Bool direction=FORWARD;     // Direction flag
        char ENT[5]="    ";         // PIN string
        char CODE[]="0123";         // PIN number
        unsigned int set_speed=2400;// Set speed for Motor
        _Bool old = 0;              // Used to check for RPM updates
        _Bool mrun=RUNNING;               // Motor running flag
        int count=0;
    	char str[20];
    	char err;
        // Initialisation function calls
        Initialise();       // Set up CPU
        Init_Ports();       // Set up I/Os
        keyscan_4x4_init(); // Set up Keypad scan
        Init_PWM();         // Set up PWM
        Init_RPM();         // Set up RPM counting
        GLCD_Initalize();   // Set up Graphic Display  
        GLCD_ClearScreen();
    	state=ST_STARTUP;
    	Init_Uart0();
    	
        while(1)
        {
         // Start State Machine
         switch(state)
         {
          case ST_STARTUP:   // STARTUP STATE
                Calc_RPM(counter, str_RPM);//CALCULATE RPM
                GLCD_GoTo(7*FONT_WIDTH,0);
                GLCD_WriteString(MSG_WELCOME);//DISPLAY WELCOME
                GLCD_GoTo(0,2);
                GLCD_WriteString(MSG_SPEED);//DISPLAY SPEED
                GLCD_GoTo(6*FONT_WIDTH,2);
                GLCD_WriteString(str_RPM);//DISPLAY RPM
                GLCD_GoTo(12*FONT_WIDTH,2);
                if(direction==FORWARD)//DISPLAY FORWARD/REVERSE
                    GLCD_WriteString(MSG_FORW);
                else
                    GLCD_WriteString(MSG_REVE);
                // Display PIN request
                GLCD_GoTo(0,6);
                GLCD_WriteString(MSG_EPIN); 
                
                 // Check for key press    
                character=getcommand();   //command contains character of key pushed
                if (character!='X')
                {
                    ENT[pin_cnt]=character;
                    GLCD_GoTo((pin_cnt+11)*FONT_WIDTH,6);
                    GLCD_WriteChar(character); //print character to terminal I/O
                    pin_cnt++;
                }
                if (pin_cnt > 3)    // End of PIN
                {
                    pin_cnt = 0;    // Reset PIN digit counter
                    ENT[4]='\0';    // Terminate PIN string
                    if (strcmp(ENT,CODE)==0)
                    {   // PIN correct
                        state=ST_MAINMENU;
                        
                    }
                    else
                    {   // PIN incorrect
                        GLCD_GoTo((pin_cnt+11)*FONT_WIDTH,6);
                        GLCD_WriteString(MSG_CPIN); 
                
    			   }
    				GLCD_ClearScreen();
                }
    			
                break;
     
    			
    	
    
    //--------------End MOTOR START/STOP---------------------------------//  
    
    
    		case CHANGESPEEDDIR:	//CHANGE DIRECTION/SPEED STATE
    		
    			character=getcommand();   //command contains character of key pushed
    			 switch(character)
    			      {
    			         case '1':   //forward
    			                RAMP_DOWN(direction);
    			                direction=FORWARD;
    			                TA4=65000;
    			                TABSR_4=0;
    			                reload=15000;
    			                TA4=reload;
    			                TABSR_4=1;
    			                break;
    			        case '2':   // reverse
    			                RAMP_DOWN(direction);
    			                direction=REVERSE;
    			                TA3=65000;
    			                TABSR_3=0;
    			                reload=15000;
    			                TA3=reload;
    			                TABSR_3=1;
    			                break;
    				    case '3': //change speed
    							state=ST_CHSPEED;
    			                GLCD_ClearScreen();
    			                break;
    							
    			        case 'D':  //MAINMENU
    			                state=ST_MAINMENU;
    			                GLCD_ClearScreen();
    			                break;
                    
    			            }
    			break;
    
    //--------------End CHANGE DIRECTION/SPEED---------------------------------//  
    
    		case ST_CHSPEED:		//CHANGE SPEED STATE
    		 
                	GLCD_GoTo(13*FONT_WIDTH,4);
                	if(direction==FORWARD)
                	GLCD_WriteString(MSG_FORW);//DISPLAY FORWARD
               	else
               		GLCD_WriteString(MSG_REVE);	//DISPLAY REVERSE
    				GLCD_GoTo(0,7);
    				GLCD_WriteString(MSG_D_FOR_MAIN);//DISPLAY MAIN MENU				
    				
    			character=getcommand();   
    		    if (character!='X')
    		       {
    		        value[speed_cnt]=character;
    		        GLCD_GoTo((speed_cnt+11)*FONT_WIDTH,2);
    		        GLCD_WriteChar(character); //print character to terminal I/O
    		        speed_cnt++;
    		       }
    		    if (speed_cnt > 3)
    		       {
    		        speed_cnt = 0;
    		        set_speed=(value[0]-48)*1000+(value[1]-48)*100+(value[2]-48)*10+(value[3]-48);
    				GLCD_ClearScreen();
    		       }
    					
    			if(character=='D')//RETURN TO MAIN MENU
    				{		
    				state=ST_MAINMENU;
    				GLCD_ClearScreen();
    				};
    					
    		    break;
    
    //--------------End CHANGE SPEED---------------------------------//

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    This is the new switch that I use to communicate with PC, It works apart from case D and C as explained on first post, Your help would be much appreciated.

    Code:
    if(RxBuffer.Cmdcnt!=0)
    	 {
    		 err=RS232GetCmd(str);
    		 RS232PutString(str);
    	 }
    		 
    
    	if(str[0]=='$')
    		{
    			switch (str[1])
    			{
    			case 'e':
    					if (str[2]=='p')
    					{
    					ENT[0]=str[4];	
    					ENT[1]=str[5];
    					ENT[2]=str[6];
    					ENT[3]=str[7];
    					if (strcmp(ENT,CODE)==0)
    					state=ST_MAINMENU;
    					GLCD_ClearScreen();
    						
                		}
    					
    					break;
    			
    			case 's':
    					if (str[2]=='t')
    					{
    					THE_NEW_TIME[0]=str[4];
    					THE_NEW_TIME[1]=str[5];
    					THE_NEW_TIME[3]=str[7];
    					THE_NEW_TIME[4]=str[8];
    					THE_NEW_TIME[6]=str[10];
    					THE_NEW_TIME[7]=str[11];
    					}
    					
    					if (str[2]=='d')
    					{
    					THE_NEW_DATE[0]=str[4];
    					THE_NEW_DATE[1]=str[5];
    					THE_NEW_DATE[3]=str[7];
    					THE_NEW_DATE[4]=str[8];
    					THE_NEW_DATE[6]=str[10];
    					THE_NEW_DATE[7]=str[11];;
    					
    					}
    									
    					if (str[2]=='s')
    					{
    					set_speed=(str[5]-48)*1000+(str[6]-48)*100+(str[7]-48)*10+(str[8]-48);
    					GLCD_ClearScreen();
    					}
    				
    					break;
    					
    					
    			case 'd':
    					if (str[2]=='f')
    					{
    							RAMP_DOWN(direction);
    			                direction=FORWARD;
    			                TA4=65000;
    			                TABSR_4=0;
    			                reload=15000;
    			                TA4=reload;
    			                TABSR_4=1;
    			         }
    					break;
    					
    					if (str[2]=='r')
    					{
    					 		RAMP_DOWN(direction);
    			                direction=REVERSE;
    			                TA3=65000;
    			                TABSR_3=0;
    			                reload=15000;
    			                TA3=reload;
    			                TABSR_3=1;
    			        }        
    					break;
    					
    			case 'c':
    					if (str[2]=='s')
    					{
    				    RS232PutString(str_RPM);
         				RS232PutChar('\n');
          				RS232PutChar('\r');
    					}        
    					break;
    			}		
    			
    		  }

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    i dont really have time to properly look through now, but just as a thought is it possible that 'character' remains set to whatever it was last time around, until told differently, and with the program loop execution speed this leads to the switch matching the same case repeatedly, i have had something similar and needed to kind of 'flush' the input or set to a null value to stop repeated events. the switch is probably just doing what it is told, it is the event itself that needs looking at

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    It would be a lot easier to read (for all of us), if you did
    Code:
    case 'd':
        doSomeFunction();
        break;
    case 'f':
        doSomethingElse();
        break;
    With many cases, and several statements in each, a switch can get messy in a hurry.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    ok sorry, I have figured out the problems they were simple. I put a break after the first case statement in case d so no memory was allocated for the second if statment. Also the RX Buffer if statment should be carried out on all switch statment so it only carries out the action once. Thanks for peoples help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-01-2009, 08:54 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21