Jump to content

  • Log In with Google      Sign In   
  • Create Account


Why won't this C code compile?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 jdub   Members   -  Reputation: 415

Like
0Likes
Like

Posted 15 February 2013 - 05:16 PM

I am trying to compile some code to run a state machine on an embedded device for CS class.  However, this code won't compile and its got me stumped.  Got the error "error: a label can only be part of a statement and a declaration is not a statement".

 

  Any help would be appreciated.

 

while(1){
		switch(state)
		{
			case IDLE:
			
				if(USART_receive() == 's')
				{
					state = RUNNING;	
				}
							
				break;
			
			case INITIALLIZING:
			
				initialize_TIMER0();
				set_TIMER0(0b00000101, 255);
				
				state = IDLE;
				
				break;
			
			case RUNNING:
				
                                //Error occurs here <--------
				unsigned char data;
				
				if(try_receive(&data))
				{
					if(data == 's')
					{
						state = IDLE;
						USART_transmit('h');
						break;	
					}
				}
				
				if(check_TIMER0())
				{
					state = SAMPLING;
				}
				
				break;
			
			case SAMPLING:
			
				x = read_ADC(ADC_X);
				y = read_ADC(ADC_Y);
				z = read_ADC(ADC_Z);
				
				state = TRANSMITTING;
				
				break;
			
			case TRANSMITTING:
				
				//transmit x,y,z.  Wait between each transmission
				//so that the data can be effectively read
				USART_transmit((char)x);
				_delay_ms(50);
				USART_transmit((char)y);
				_delay_ms(50);
				USART_transmit((char)z);
				
				state = RUNNING;
				
				break;
			
			default:
				break;			
		}
	}

 

 


J.W.

Sponsor:

#2 Martins Mozeiko   Crossbones+   -  Reputation: 1413

Like
6Likes
Like

Posted 15 February 2013 - 05:22 PM

Variable definitions inside case must be in some scope.

Like this:

 

case RUNNING:
{ // <---- note the new scope
    unsigned char data;
    ...
}
 
case SAMPLING:
...

Edited by Martins Mozeiko, 15 February 2013 - 05:23 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS