CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

How to use "goto_address() and "return"

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Jent



Joined: 04 Feb 2012
Posts: 20

View user's profile Send private message

How to use "goto_address() and "return"
PostPosted: Sat Mar 03, 2012 8:45 pm     Reply with quote

Hi,

Can someone guide me? I do not really understand how to use "goto_address()" and "return" if I require to jump to another function.

actually I need to jump from function1(looping) to another function2(without looping), then return back to function1. Below is my simple coding... Hope can get your kindly help..

Code:

void main()
{
   int32 location;
   Int16 heading;
   set_tris_b(0b00000011);
   output_low(PIN_B2);
   output_low(PIN_B3);
   
      while(true)
      {
         heading=(HMC6352_read_heading())/10;
         lcd_init();
         printf(lcd_putc, "\fHeading degree:");
         printf(lcd_putc, "\n     %LuDE", heading);   //display the heading degree from digital compass
         delay_ms(1000);
     
         goto_address(location);
      } 
 
   int32 servo_degree(void)   
   {
      Initial:
         location=label_address(Initial);
     
      switch(heading)
      {
         
         case 000:      output_high(LED_2);
                        delay_ms(3000);break;
         case 001:      output_high(LED_2);   
                        delay_ms(3000);break; 
         case 002:      output_high(LED_2);   
                        delay_ms(3000);break;               
         case 355:      output_high(LED_3);
                        delay_ms(3000);break;
         case 356:      output_high(LED_3);
                        delay_ms(3000);break;
         case 357:      output_high(LED_3);
                        delay_ms(3000);break;               
         default:       output_low(LED_2);
                        output_low(LED_3);
                        delay_ms(3000);break;
      }
      return;
   }
   
}   


error existed in my compiler

    int32 servo_degree(void) ------->ERROR: A numerical expression must appear here.
dezso



Joined: 04 Mar 2010
Posts: 102

View user's profile Send private message

PostPosted: Sun Mar 04, 2012 12:34 am     Reply with quote

Code:
int32 servo_degree(void)

Why are you make it as int32 when not even taking anything "(void)"
It would make sense like this...

Code:
int32 servo_degree(degree)     //call as servo_degree(180):
where a degree is a 32bit number!
Also degree is 0-360 why are you need int32, int16 will be just fine.


note:
1 - goto not used in C/C++
2 - gosub has the return from sub option anyway
3 - goto cant use return, program execution just continue from the new address.
_________________
I'm could be wrong many time's, at least I know what I'm doing Smile
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Mar 04, 2012 2:52 am     Reply with quote

As dezso explained, goto_address() is of no use in regular C programming. If you don't have a very special problem and aren't familiar with low level programming details, simply forget about it.

Your intended action will be exactly achieved by just writing

Code:
servo_degree();

instead of goto_address(location)

If you need to switch between different working functions, use a case structure or functions pointers.

As servo_degree don't use a return value, it can be defined as void function.
Geps



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

Re: How to use "goto_address() and "return"
PostPosted: Sun Mar 04, 2012 5:58 am     Reply with quote

You need to take the subroutine out of the main subroutine as such:

Code:


   int32 servo_degree(void)   
   {
      Initial:
         location=label_address(Initial);
     
      switch(heading)
      {
         
         case 000:      output_high(LED_2);
                        delay_ms(3000);break;
         case 001:      output_high(LED_2);   
                        delay_ms(3000);break; 
         case 002:      output_high(LED_2);   
                        delay_ms(3000);break;               
         case 355:      output_high(LED_3);
                        delay_ms(3000);break;
         case 356:      output_high(LED_3);
                        delay_ms(3000);break;
         case 357:      output_high(LED_3);
                        delay_ms(3000);break;               
         default:       output_low(LED_2);
                        output_low(LED_3);
                        delay_ms(3000);break;
      }
      return;


void main()
{
   int32 location;
   Int16 heading;
   set_tris_b(0b00000011);
   output_low(PIN_B2);
   output_low(PIN_B3);
   
      while(true)
      {
         heading=(HMC6352_read_heading())/10;
         lcd_init();
         printf(lcd_putc, "\fHeading degree:");
         printf(lcd_putc, "\n     %LuDE", heading);   //display the heading degree from digital compass
         delay_ms(1000);
     
         goto_address(location);
      } 
 

   }
   
}   

You can then call the functions within the main subroutine.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group