|
|
View previous topic :: View next topic |
Author |
Message |
williamgpaz
Joined: 06 Oct 2011 Posts: 1
|
Problem about functions |
Posted: Thu Oct 06, 2011 6:59 pm |
|
|
I need very useful help. I'm doing a program with the pic 18f452 and I need to call a function from another function but I can't do it, it shows me recursion not permitted.
Code: |
void manual()
{
lcd_putc('\f');
lcd_gotoxy(1,1);
printf(lcd_putc,"1.Iluminacion");
delay_ms(500);
lcd_gotoxy(1,2);
printf(lcd_putc,"2.Puertas");
delay_ms(500);
while(TRUE)
{
k=kbd_getc();
x=k-48;
if(k!=0){
switch (x){
case 1:
lcd_putc('\f');
lcd_gotoxy(1,1);
printf(lcd_putc,"*****Cuartos*****");
delay_ms(500);
lcd_gotoxy(1,2);
printf(lcd_putc," 1 2 3 4 ");
delay_ms(500);
main();
break;
case 2:
lcd_putc('\f');
lcd_gotoxy(1,1);
printf(lcd_putc,"****Puertas****");
delay_ms(500);
lcd_gotoxy(1,2);
printf(lcd_putc," 1 2 ");
delay_ms(500);
motores();
break;
}
}
}
}
void main()
{
lcd_init();
kbd_init();
port_b_pullups (TRUE);
lcd_putc('\f');
lcd_gotoxy(1,1);
printf(lcd_putc,"1.Manual");
delay_ms(500);
lcd_gotoxy(1,2);
printf(lcd_putc,"2.Automatico");
delay_ms(500);
while(1)
{
k=kbd_getc();
x=k-48;
if(k!=0){
if(x==1){
manual();
}
}
}
}
|
Please someone how teach me how to call a function, maybe I need to put declarations first. |
|
|
ünloco
Joined: 02 Oct 2011 Posts: 12 Location: Tunisia
|
|
Posted: Fri Oct 07, 2011 3:31 am |
|
|
try "return;" instead of "main();" _________________ for(;;); |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Fri Oct 07, 2011 9:07 am |
|
|
It looks like you're trying to use function calls like a goto statement. When you call a function you will branch out of your existing place in your code and execute the function that was just called. When that function has finished it's job the program will resume from where the function was called.
In your code you enter main() and then call manual(). Inside manual() you call main() which in turn will call manual() which will call main() which will call manual() and so on and so on, forever. You can't do this. This is called 'recursion'.
Instead of trying to call main() inside of manual(), just let the switch() statement exit and manual() will reach the end of it's code and you will be returned back to where manual() was called within main(). In other words, if none of the switch() cases match the variable being evaluated then the switch() will simply exit. Make sure you also have a default: statement in your switch(). Read up on switch() statements a bit.
One other point, you have a while(true) statement inside of manual(). This will keep your code inside of manual() forever and will never return back to main(). Remember, you call a function, when that function is finished it exits and your code continues from where the function was called.
Clear as mud?
Ronald |
|
|
|
|
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
|