View previous topic :: View next topic |
Author |
Message |
Jent
Joined: 04 Feb 2012 Posts: 20
|
How to use "goto_address() and "return" |
Posted: Sat Mar 03, 2012 8:45 pm |
|
|
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.
|
|
![](templates/subSilver/images/spacer.gif) |
dezso
Joined: 04 Mar 2010 Posts: 102
|
|
Posted: Sun Mar 04, 2012 12:34 am |
|
|
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](images/smiles/icon_smile.gif) |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sun Mar 04, 2012 2:52 am |
|
|
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
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. |
|
![](templates/subSilver/images/spacer.gif) |
Geps
Joined: 05 Jul 2010 Posts: 129
|
Re: How to use "goto_address() and "return" |
Posted: Sun Mar 04, 2012 5:58 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
|