basics programming


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2007
    Location
    France Bourgoin Jallieu
    Posts
    2

    Default basics programming

    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

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default

    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:
    Code:
    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.
    Last edited by HenrikOlsson; - 18th February 2007 at 10:20. Reason: Forgot one thing.

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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

    Code:
            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
    Code:
            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
    Code:
            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
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Join Date
    Feb 2007
    Location
    France Bourgoin Jallieu
    Posts
    2


    Did you find this post helpful? Yes | No

    Smile Great

    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

Similar Threads

  1. Data Programming Error at 0000
    By cpatnoi in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 22nd May 2009, 03:37
  2. Problems programming
    By Lionheart in forum General
    Replies: 4
    Last Post: - 7th December 2008, 16:51
  3. PIC programming algorithm - where is it to find?
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th June 2007, 18:31
  4. Some questions for programming
    By fnovau in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2006, 16:04
  5. MELab Programming
    By tarr in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 22nd March 2006, 13:36

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts