Functions in Pic Basic Pro


Results 1 to 7 of 7

Threaded View

  1. #7
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    For your first part – when I have a large program and want to use a function-like appraoch, I do something like this.

    Code:
    ' Variables for use inside functions
    temp1 var word
    temp2 var word
    temp3 var word
    
    ' for Divide Function
    Numerator var temp1
    Denominator var temp2
    DivAnswer var temp3
    
    ' for Square Function
    tobesquared var temp1
    SqrAnswer var temp2
    
    Main:
    
    ' Do lots of stuff here
    
    Numerator = 45
    Denominator = 5
    Gosub Divide
    Serout, Portb.1, 2, (#DivAnswer)
    
    ' More stuff here
    
    tobesquared= 9
    Gosub Squared
     Serout, Portb.1, 2, (#SqrAnswer)
    
    ' Do more stuff
    
    Goto Main
    
    ' Divide Function
    Divide:
    DivAnswer = Numerator/Demonimator
    Return
    
    ' Squared Function
    Squared:
    SqrAnswer = tobesquared * tobesquared
    Return
    In this manner, I can use names that make sense but reuse the globals each time.

    For your second part try something like this:

    Code:
    ...
    counter var byte
    compare var bit
    counter = 0
    
    GOTO Main ' jump over subroutine (to wherever I want, even line 100)
     'Init ABIC Suroutine
    INTABIC:
    
    LOW DIN
    LOW SCLK
    HIGH SCLK
    HIGH DIN
    LOW SCLK
    LOW DIN
    RETURN
    
    Main:
    for counter = 7 to 0 step -1
    	if config_page_1.0[counter] Then
    		HIGH DIN
    	else
    		LOW DIN
    	ENdif
    	HIGH SCLK
    	LOW SCLK
    next counter 
    
    GOSUB INITABIC
    
    for counter = 7 to 0 step -1
    	if config_page_3.0[counter] Then
    		HIGH DIN
    	else
    		LOW DIN
    	ENdif
    	HIGH SCLK
    	LOW SCLK
    next counter 
    
    GOSUB INITABIC
    
    etc.
    
    GOTO Main
    END
    Last edited by paul borgmeier; - 4th April 2007 at 23:02. Reason: minor type in code -
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

Similar Threads

  1. Looking at pic basic pro symbol values in mplab 8.15a simulator
    By ukemigrant in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 16th May 2009, 13:18
  2. I want to buy PIC BASIC PRO...
    By simransingh in forum General
    Replies: 2
    Last Post: - 30th October 2007, 17:13
  3. PIC BASIC or PIC BASIC PRO
    By kirkmans in forum USB
    Replies: 3
    Last Post: - 20th April 2007, 00:52
  4. Replies: 5
    Last Post: - 17th January 2006, 19:26
  5. How to use 93C46 Type EEPROM using PIC Basic PRo
    By in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 1st April 2003, 04:07

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