+ Reply to Thread
Page 1 of 3
1 2 3 Last
Results 1 to 15 of 32

Thread: coding a GOTO or BRA to??

  1. #1
    MrDEB Excellent MrDEB Excellent MrDEB Excellent MrDEB Excellent MrDEB Excellent MrDEB Excellent
    Join Date
    Apr 2007
    Posts
    2,133

    Default coding a GOTO or BRA to??

    need to include a GOTO sub routine or BRA?
    the first routine is the sound. second routine checks for daylight on input B#?(photo transistor on BR2 is good choice?)disable PIR
    third routine sets timer into gear for 20 minutes if daylight is present(if BR2 is high or low?)
    I want to read status of photo transistor then disable PIR and enable timer if daylight is present. otherwise enable PIR and disable timer.
    need to goto sub routines as needed?
    how to do a sub routine??
    Code:
    Device = 18F1320
    Clock = 8 // 8MHz clock
    Config OSC = INTIO2, WDT = OFF, LVP = OFF
    Include "Utils.bas"
    Dim but As PORTB.0
    Dim led As PORTA.0
    
    Dim NOT_RBPU As INTCON2.7
    Dim TMR1IE As PIE1.0
    Dim TMR1IF As PIR1.0
    Dim TMR1 As TMR1L.AsWord
    Dim Speaker As PORTB.3
    Dim SpeakerTris As TRISB.3
    
    Dim Amp As PORTB.1 // turns on amp power
    Dim AmpTris As TRISB.1//turns on amp pin 9
    Dim Speed As Word
    Dim dip1 As PORTA.0
    
    Dim dip2 As PORTA.1
    
    Dim dip3 As PORTA.2
    
    Dim dip4 As PORTA.3
    
    Dim dipRD As PORTB.0
    
    //global variables
    Dim Seed As LongWord, Tone As Byte
    Dim i As Byte
    
    //half period delays = clock speed divided by 2*frequency
    Const Tones(18) As Word = (2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000,
    2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000,2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000)
    
    //interrupt routine
    Interrupt MyInt()
              T1CON.0=0 //stop timer
              TMR1=-Tones(Tone) //reset period
              T1CON.0=1 //restart timer
    If Tone=5 Then //if silence
       Speaker=0 //speaker off
    Else //otherwise
    Toggle(Speaker) //make sound
    
    EndIf
    TMR1IF=0 //clear interrupt flag
    End Interrupt
    
    Function Rand(Range As Byte) As Byte
    Dim i As Byte, feed As Bit, temp As Word
    For i = 0 To 7 //generate 8 bits
        Feed = Seed.30 Xor Seed.27 //make new bit
        Seed=Seed*2+Feed //shift seed left and add new bit
    Next
        Temp=(Seed And 255) * Range //change Rand from 0 to 255
        Rand = Temp/256 //to 0 to (Range-1)
    End Function
    
    //main code starts here
    
    
          
    
    
    
    
           OSCCON = $72            //select 8MHz internal clock
           NOT_RBPU=0              //WPUs on port B
           ADCON1=$7f              //all digital
           TRISB.0=0               //make output
           PORTB.0=1 //makes pin 8 high for dip switches
           T1CON = %10000001       //pre=1
           T1CON = %10000001 //pre=1
           Tone=5 //no sound please
           TMR1IE=1 //enable timer 1 interrupt
    Enable(MyInt) //set interrupt going
    SpeakerTris=0 //Setup Port
    Seed=$12345678 //seed random number
            TRISA =%00001111          //sets your inputs on porta 
    While(TRUE) //repeat forever
    
        If dip1=1 Then //dip1 pin 1
           Speed=25
        EndIf
        
        If dip2=1 Then // dip 2 pin 2
           Speed=50 
        EndIf
        
        If dip3=1 Then //dip3 pin 6
           Speed=75
        EndIf
        
        If dip4=1 Then //dip4 pin 
           Speed=Rand(10)*15+15    //Speed = 25 to 250 random select speed
           
        EndIf
      checks status of BR2=daylight or dark 
    If PORTB.1=0 Then //if button 1 pressed add PIR=1 here
    else
     PORTB.2=0 Then bra //jump to timer sub routine
    For i = 1 To 200 //play 20 tones
        Tone=Rand(5) //each tone is random frequency
    DelayMS(Speed) //and for 1.00 seconds
    Next //end for loop
    Else //otherwise
         Tone=5 //silence
         i=Rand(255) //make rand more random
    EndIf //end if condition
    Wend //end of while loop
    //PIR disable routine
    
    
    
    SetAllDigital      //set digital so you can read RB0 or it will always read low
    TRISA =%00000000  // if your using the junebug This is fool proof to get a led to lite
    PORTA =%00000000  // Set's it all low
    TRISB =%00000001  // Set's inputs and outputs 
    PORTB =%00000000   // Set's it all low
    INTCON2.7=0        // sets wpu on portb turns on pullups on PORTB
    OSCCON = $72            // 8 MHz clock
    While true              // this loops it so it keeps checking 
    If but = 0 Then        // checks button 1 on the junebug to see if low
        led =0                 // Turns off led
        DelayMS(10)       // small debounce delay
    ElseIf but = 1 Then  // checks button 1 on the junebug to see if high(BR2)
       led =1                 // Turns on led
       DelayMS(10)       // small debounce delay
    EndIf                     // closes your IF THEN statements 
    Wend
    
    End
    // timer sub routine
    
    
    Include "ISRTimer.bas"          
    
    // constant ID to 4 * 16 bit timers...
    Const
       Timer1 = 0,
      
       Timer4 = 3
          
    // OnTimer1 event...
    Event OnTimer1()
       Toggle(PORTB.0)//pin 8
    End Event
    Event OnTimer2()
       Toggle(PORTB.1)//pin 9
    End Event
    
    
    
    
    // activate the timer module...
    Timer.Initialize
    
    // initialise the timers - refresh every 1000Hz (1ms)...
    Timer.Items(Timer1).Interval = 50        // 50ms
    Timer.Items(Timer1).OnTimer = OnTimer1   // timer event handler
    
      // timer event handler
    Timer.Items(Timer4).Interval = 2000      // 2000ms, no event handler
                                             // about 20 minutes
    
    // enable the timers...
    Timer.Items(Timer1).Enabled = true
    
    Timer.Items(Timer4).Enabled = true
    
    // start processing all timers...
    Timer.Start
    
    // main program loop...
    While true
       // this is a polled timer, not event driven - check to see
       // if it has timeout...
       If Not Timer.Items(Timer4).Enabled Then
          Toggle(PORTB.0)//pin 8
          Toggle(PORTB.1)//pin 9
          Timer.Items(Timer4).Enabled = true
       EndIf
       Toggle(PORTB.1)//pin 8   
     
    Wend
    


  2. #2
    AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent
    Join Date
    Feb 2008
    Location
    Brooklyn, NY US
    Posts
    3,744

    Default

    im no basic guy but what does BRA and GOTO have to do with it? You wont use it if your using basic. Its auto by compiler i think.
    AtomSofts eBay Store
    AtomSoftTech: C18 TIPS & TRICKS v9 PDF

    My Name: Jason Lopez
    My BLOG | My YouTube Videos!
    My Favorite Store:
    dipmicro Electronics
    Trading and Selling...? Check out Dipmicro Trading/Selling Forum:
    Electronic Components & Tools Exchange

  3. #3
    3v0
    3v0 is offline
    3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent
    Join Date
    Jul 2006
    Location
    USA
    Posts
    6,464
    Blog Entries
    11

    Default

    Could be he is talking about GOTO and GOSUB ?
    Please post questions to the forums. PM's are for personal communication.

    BCHS/3v0's Tutorials
    Junebug USB PIC programmer kit., USB Bit Whacker,
    The 15 Minute Printed Circuit Board! (+drill time)

  4. #4
    MrDEB Excellent MrDEB Excellent MrDEB Excellent MrDEB Excellent MrDEB Excellent MrDEB Excellent
    Join Date
    Apr 2007
    Posts
    2,133

    Default yes a go sub

    have seen BRA in swordfish basic which this code is.
    wondering if I need it or ?? seeing how I basically have one main program and two sub programs
    main=the random sound that was originally triggered by the PIR but IR doesn't work in daylight(the PIR sensor takes RB1 LOW)
    sub 1 = is it daylight or night (a photo transistor takes a designated pin LOW or HIGH?
    sub 2=a timer that triggers the sound every 20 minutes if its daylight.
    I would like to use a SLEEP mode to conserve battery power as well.
    I got all three programs working seperatly but now I copied n pasted all together.

  5. #5
    be80be Excellent be80be Excellent be80be Excellent be80be Excellent be80be Excellent be80be Excellent
    Join Date
    Aug 2008
    Location
    morristown,tn
    Posts
    2,000

    Default

    There is no gosub in swordfish only a goto and it will not work the way he has his code.
    Last edited by be80be; 15th June 2009 at 12:07 AM.
    Burt

  6. #6
    be80be Excellent be80be Excellent be80be Excellent be80be Excellent be80be Excellent be80be Excellent
    Join Date
    Aug 2008
    Location
    morristown,tn
    Posts
    2,000

    Default

    Do you see a gosub here
    Code:
    absolute
    access
    addressof
    and
    as
    asm
    auto
    bit
    bitof
    boolean
    bound
    break
    byref
    byrefconst
    byte
    byval
    case
    char
     clear
    clock
    compound
    config
    const continue 
    dec
    delayms
    delayus
    device
    dim
    disable
    eeprom
    else
    elseif
    enable
    end 
    endif
     end
    select
    exit
    false
    float
    for
    function
    goto 
    high
    if 
    inc
    include
    inline
    input
    integer
    interrupt
    longint
    longword 
    
     
     low 
    mod
    module
    next
    noinline
    not
    null
    or
    output
    port 
    private 
    program
    public 
    repeat
    restore
    save
    select
    shortint 
     step 
    string
    structure
    sub
    system
    terminate
    then
    to
    toggle
    true
    type
    until
    wend
    while
    word
    xor
    
    This is what you have to work with
    Last edited by be80be; 15th June 2009 at 12:13 AM.
    Burt

  7. #7
    3v0
    3v0 is offline
    3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent
    Join Date
    Jul 2006
    Location
    USA
    Posts
    6,464
    Blog Entries
    11

    Default

    I have been saying for a while that swordfish is not Basic.

    Maybe MrDeb should step back a work through a few tutorials and learn programing/swordfish from the start.
    Please post questions to the forums. PM's are for personal communication.

    BCHS/3v0's Tutorials
    Junebug USB PIC programmer kit., USB Bit Whacker,
    The 15 Minute Printed Circuit Board! (+drill time)

  8. #8
    be80be Excellent be80be Excellent be80be Excellent be80be Excellent be80be Excellent be80be Excellent
    Join Date
    Aug 2008
    Location
    morristown,tn
    Posts
    2,000

    Default

    Read this all about goto it's in the help for swordfish
    He can do what he wants with
    The Select…Case Statement



    Conditional Jump




    if expression goto label



    The conditional jump is a special construct that can be used with a standard goto statement. For example,



    if Value <> 0 goto SkipCode

    high(LED)

    delayms(500)



    SkipCode:

    low(LED)



    Notice the difference in syntax when compared to a normal if…then statement. Firstly, no endif is required. Secondly, the then part of the statement is not present. You can of course use a goto inside a normal if…then statement, but the above form allows you to write the same thing more concisely.



    The goto statement has a nasty reputation because of its ability to jump to almost anywhere. Some people view this lack of control as very bad. Using a goto can produce what is called spaghetti code. It gets this name because with a goto infested program, drawing a line between a goto and its destination label would look like a big plate of spaghetti. Used with care, a goto statement can be useful. However, given the highly structured nature of the compiler language, a goto statement should be used sparingly and is best avoided.
    Last edited by be80be; 15th June 2009 at 12:20 AM.
    Burt

  9. #9
    be80be Excellent be80be Excellent be80be Excellent be80be Excellent be80be Excellent be80be Excellent
    Join Date
    Aug 2008
    Location
    morristown,tn
    Posts
    2,000

    Default

    Maybe MrDeb should step back a work through a few tutorials and learn programing/swordfish from the start.
    You right It Not basic it has labels like
    basic and that's it more like C but you can't write c like Statement with out dim as bunch of this and that lol

    And in swordfish if you use a goto it don't always do what you think it should.I would say that why the help file tells you this
    goto statement should be used sparingly and is best avoided.
    Last edited by be80be; 15th June 2009 at 12:30 AM.
    Burt

  10. #10
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,804

    Default

    Nearly all Basics (even VB) contain the goto function but its use is frowned upon. When you use goto you end up with spaghetti like code that is very hard to follow.

    Learn to use the more structured commands like while..wend, repeat...until, for...next, if...endif etc. Also, use correct indenting. It will make your code much easier to follow.

    Mike.

  11. #11
    MrDEB Excellent MrDEB Excellent MrDEB Excellent MrDEB Excellent MrDEB Excellent MrDEB Excellent
    Join Date
    Apr 2007
    Posts
    2,133

    Default I will try all the suggestions

    Will research in swordfish help.
    hopfully I can straighen the code out.

  12. #12
    MrDEB Excellent MrDEB Excellent MrDEB Excellent MrDEB Excellent MrDEB Excellent MrDEB Excellent
    Join Date
    Apr 2007
    Posts
    2,133

    Default work in progress --formatting suggestions?

    here is my work in progress. not pretty but still moving things around so I need no sub routines (don't want spagetti)
    the tabs etc? what is the perfered layout?
    I am attempting to comment and seperate the different routines.
    one for sound
    one for timer,
    one for daylight/disable PIR night time/enable PIR
    finding its diffcult to locate duplicate items as I copy n paste alot. lose track where I am at.
    wish the left side gutter had a search function I don't know about. It shows some but not all??
    Code:
    Device = 18F1320
    Clock = 8 // 8MHz clock
    Config OSC = INTIO2, WDT = OFF, LVP = OFF
    Include "Utils.bas"
    Include "ISRTimer.bas" 
    Dim but As PORTB.2 //input for photo diode day disable
    Dim led As PORTb.4//triggers PIC inplace of PIR
    
    Dim NOT_RBPU As INTCON2.7
    Dim TMR1IE As PIE1.0
    Dim TMR1IF As PIR1.0
    Dim TMR1 As TMR1L.AsWord
    Dim Speaker As PORTB.3
    Dim SpeakerTris As TRISB.3
    
    Dim Amp As PORTB.1 // turns on amp power
    Dim AmpTris As TRISB.1//turns on amp pin 9
    Dim Speed As Word
    Dim dip1 As PORTA.0
    //dip switch settings
    Dim dip2 As PORTA.1
    
    Dim dip3 As PORTA.2
    
    Dim dip4 As PORTA.3
    
    Dim dipRD As PORTB.0
    Const
         Timer1 = 0,
         
         Timer4 = 3
    
    //global variables
    Dim Seed As LongWord, Tone As Byte
    Dim i As Byte
    
    //half period delays = clock speed divided by 2*frequency
    Const Tones(18) As Word = (2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000,
    2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000,2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000)
    //******************************************
    //++++++++++++++++++++++++++++++++++++++++++
    //interrupt routine
    Interrupt MyInt()
              T1CON.0=0 //stop timer
              TMR1=-Tones(Tone) //reset period
              T1CON.0=1 //restart timer
    If Tone=5 Then //if silence
       Speaker=0 //speaker off
    Else //otherwise
    Toggle(Speaker) //make sound
    
    EndIf
    TMR1IF=0 //clear interrupt flag
    End Interrupt
    
    Function Rand(Range As Byte) As Byte
    Dim i As Byte, feed As Bit, temp As Word
    For i = 0 To 7 //generate 8 bits
        Feed = Seed.30 Xor Seed.27 //make new bit
        Seed=Seed*2+Feed //shift seed left and add new bit
    Next
        Temp=(Seed And 255) * Range //change Rand from 0 to 255
        Rand = Temp/256 //to 0 to (Range-1)
    End Function
    //+++++++++++++++++++++++++++++++
    //+++++++++++++++++++++++++++++++      
    
    
    //main code starts here
    
           OSCCON = $72            //select 8MHz internal clock
           NOT_RBPU=0              //WPUs on port B
           ADCON1=$7f              //all digital
           TRISB.0=0               //make output
           PORTB.0=1 //makes pin 8 high for dip switches
           T1CON = %10000001       //pre=1
           T1CON = %10000001 //pre=1
           Tone=5 //no sound please
           TMR1IE=1 //enable timer 1 interrupt
    Enable(MyInt) //set interrupt going
    SpeakerTris=0 //Setup Port
    Seed=$12345678 //seed random number
            TRISA =%00001111          //sets your inputs on porta 
    While(TRUE) //repeat forever
    
    If     dip1=1 Then //dip1 pin 1
           Speed=25
    EndIf
        
    If     dip2=1 Then // dip 2 pin 2
           Speed=50 
    EndIf
        
    If     dip3=1 Then //dip3 pin 6
           Speed=75
    EndIf
        
    If     dip4=1 Then //dip4 pin 
           Speed=Rand(10)*15+15    //Speed = 25 to 250 random select speed
           
    EndIf
    If    but = 0 Then        // checksFOR DAYLIGHT CONDITION on pin 17 B2
          led =1                 // takes pin 10 HIGH
    DelayMS(10)       // small debounce delay
            led=0     // pin 10 low
            
            
    //+++++++++++++++++
    //timer routine daylight condition
    //++++++++++++++++++
    
              
    
    // constant ID to 4 * 16 bit timers...
    //Const
         //Timer1 = 0,
         
       //  Timer4 = 3
          
    // OnTimer1 event...
    Event OnTimer1()
          PORTB.2=1//pin 17= 
          //daylight present triggers timer
    End Event
    
    // activate the timer module...
       Timer.Initialize
    
    // initialise the timers - refresh every 1000Hz (1ms)...
       Timer.Items(Timer1).Interval = 50        // 50ms
       Timer.Items(Timer1).OnTimer = OnTimer1   // timer event handler
    
      // timer event handler
      Timer.Items(Timer4).Interval = 2000      // 2000ms, no event handler
                                             // about 20 minutes// enable the timers...
       Timer.Items(Timer1).Enabled = true
    
       Timer.Items(Timer4).Enabled = true
    
    // start processing all timers...
       Timer.Start
    
    // main program loop...
    While true
       // this is a polled timer, not event driven - check to see
       // if it has timeout...
    If Not Timer.Items(Timer4).Enabled Then
          Toggle(PORTB.4)//pin 10 for testing
          Toggle(PORTB.1)//pin 9
          Timer.Items(Timer4).Enabled = true
    EndIf
       Toggle(PORTB.1)//pin 8   
     
    Wend
    
    //+++++++++++++
     // DARK ROUTINE PIR enabled
     //++++++++++++++++  
    If     PORTB.1=0 Then //PIr ENABLED = DARK pin 9
    For    i = 1 To 200 //play 20 tones
           Tone=Rand(5) //each tone is random frequency
    DelayMS(Speed) //and for 1.00 seconds
    Next //end for loop
    
    
    
    
    EndIf                     // closes your IF THEN statements 
    
    Else //otherwise
           Tone=5 //silence
           i=Rand(255) //make rand more random
    EndIf //end if condition
    Wend //end of while loop
    
    
    
    
    EndIf                     // closes your IF THEN statements 
    Wend
    
    End
    

  13. #13
    be80be Excellent be80be Excellent be80be Excellent be80be Excellent be80be Excellent be80be Excellent
    Join Date
    Aug 2008
    Location
    morristown,tn
    Posts
    2,000

    Default

    I don't see why you don't make three Functions
    1. Sound Function.
    2. PIR Function.
    3. Day time Function.
    It would make your code work a lot easier.
    Code:
    // your main code would look like this
    dim day as byte
    dim nite as byte
    dim light as byte
    dim checkPIR as byte
    dim PlaySound as byte
    
    
    while true 
          If day = light then
          PlaySound 
          elseif day = nite then
          checkPIR 
          endif
          If checkPIR = true then
          PlaySound
          endif
    wend
          
         // then just make three functions One for day and night
         // One for PlaySound
         // One for checkPIR
    
    Burt

  14. #14
    MrDEB Excellent MrDEB Excellent MrDEB Excellent MrDEB Excellent MrDEB Excellent MrDEB Excellent
    Join Date
    Apr 2007
    Posts
    2,133

    Default never occured to me

    to do it like tht. remember I am really new at this.
    I contemplated making each function a module but creating each module ??
    question=you designate a byte as light etc.
    this is assume is same as say light = 1. night = 2 etc.
    if X = 1 then its light or X = 2 its dark
    this byte syntax ???
    takes less memory code or ??
    looking at your code If day = light then PlaySound
    playsound then is same as a subroutine? just a different way to go instead of a goto or ??

    will rewrite as suggested as I have each function in separate programs.
    your method looks so much easier.

  15. #15
    be80be Excellent be80be Excellent be80be Excellent be80be Excellent be80be Excellent be80be Excellent
    Join Date
    Aug 2008
    Location
    morristown,tn
    Posts
    2,000

    Default

    It's Like this it would run the first If then as long as there lite out side.
    If it gets dark it will run the night if then and the pir to start the sound
    It's still all inline
    Last edited by be80be; 15th June 2009 at 08:45 PM.
    Burt

+ Reply to Thread
Page 1 of 3
1 2 3 Last

Similar Threads

  1. programming code 'goto $+2'
    By 16F877A in forum General Electronics Chat
    Replies: 12
    Latest: 9th November 2008, 02:55 PM
  2. PIC18F and GOTO $-6
    By yngndrw in forum Micro Controllers
    Replies: 12
    Latest: 3rd August 2007, 01:10 AM
  3. What means the instruction "goto $-1 " ?
    By Vitico in forum Micro Controllers
    Replies: 9
    Latest: 30th June 2007, 06:56 PM
  4. what has C goto do with AT89C51
    By crazytwism in forum Micro Controllers
    Replies: 9
    Latest: 13th May 2005, 05:31 AM
  5. goto $-1.............clrf LoX............ ?
    By timothyjackson in forum Micro Controllers
    Replies: 29
    Latest: 12th January 2005, 05:28 PM

Tags for this Thread