PDA

View Full Version : basics programming



olivierb38
- 18th February 2007, 09:03
Hi Guys,

I begin programming,

and i still have problems with beginning.

I want to know the correct architecture of a program with multiple choices.

Example:

'The program starts here

if Choice1 goto choice1Sub
if Choice2 goto Choice2Sub
if Choice3 goto Choice3Sub
Endif

Choice1Sub:
{Sous programme}
return

Choice2Sub:
{Sous programme}
return

Choice3Sub:
{Sous programme}
return



Questions :

Will the program read all subroutines each times or he will go to subroutines only if Choice1, 2 or 3 is validated ?
Do i need to put "return" at the end of each subroutine to come back to the beginning of the program ?


Thanks


Olivier

HenrikOlsson
- 18th February 2007, 10:16
Hi,
If you use GOTO you can't use RETURN.
RETURN is only used when you jump to a routine using GOSUB.

You don't need ENDIF if your IF statement is one one line.

EDIT: Except for the above, the program would jump to choise2Sub if Choise2 was evaluated true. But even if none of the IF statements were true the program would run all three subs one after the other since you haven't prevented the program to reach the subs after the IF statements. Makes sense??

It should probably look something more like:


Start:
if Choice1 goto choice1Sub 'Jump to choise1Sub if Choise1 is=1
if Choice2 goto Choice2Sub
if Choice3 goto Choice3Sub
Goto start 'Start over

Choice1Sub:
{Sous programme}
Goto start 'Start over

Choice2Sub:
{Sous programme}
Goto Start 'Start over

Choice3Sub:
{Sous programme}
Goto Start 'Start over



/Henrik Olsson.

mister_e
- 18th February 2007, 11:20
Bonjour et bienvenue Olivier,

Yes there is many different way. If you have multiple choice on multiple var it's a little bit more coding on your side. BUT if you have multiple conditions to test on a single var, and then jump to a specific routine, you have at least 3 choices

1. few IF THEN



Choix VAR BYTE

Start:
'
' some code here
'
IF Choix=1 then gosub Routine1
IF Choix=2 then gosub Routine2
IF Choix=3 then gosub Routine3
goto start

Routine1:
'
' plah plah plah
'
return

Routine2:
'
' plah plah plah
'
return

Routine3:
'
' plah plah plah
'
return

SELECT CASE



Choix VAR BYTE

Start:
'
' some code here
'
SELECT CASE CHOIX
CASE 1
gosub Routine1
'
CASE 2
gosub Routine2
'
CASE 3
gosub Routine3
'
END SELECT

goto start

Routine1:
'
' plah plah plah
'
return

Routine2:
'
' plah plah plah
'
return

Routine3:
'
' plah plah plah
'
return


BRANCHL
but this one AS-IS, use GOTO, so, as Henrik said, you can't use RETURN without tricks



Choix VAR BYTE

Start:
'
' some code here
'
' choix= 0 1 2 3
' ' ' ' '
branchl choix,[Start,Routine1, Routine2, Routine3]
goto start

Routine1:
'
' plah plah plah
'
goto start

Routine2:
'
' plah plah plah
'
goto start

Routine3:
'
' plah plah plah
'
goto start

olivierb38
- 18th February 2007, 18:56
Now i think, i have understood architecture of my program,

Now i will try to create a sinus wave with variable duty cycle with a PIC18F458



Thanks



Olivier