Loop issues


Closed Thread
Results 1 to 19 of 19

Thread: Loop issues

Hybrid View

  1. #1
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: Loop issues

    Just for diagnostics, try putting "goto main1" as the very first line in your "main:" If it goes there, then we need to look at something else. If not, there's something wrong with main1:.

  2. #2
    Join Date
    Feb 2013
    Posts
    1,132


    Did you find this post helpful? Yes | No

    Default Re: Loop issues

    Side question - these nesting of IF-THEN are used to avoid usage of AND/OR operators?

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,653


    Did you find this post helpful? Yes | No

    Default Re: Loop issues

    Side question - these nesting of IF-THEN are used to avoid usage of AND/OR operators?

    or even a little binary arithmetic

    Code:
    if PortB.0 = 0 then
    if PortB.1 = 1 then
    if PortB.4 = 1 then
    runtime = 60
    endif
    endif
    endif
    
    
    if PortB.0 = 1 then
    if PortB.1 = 1 then
    if PortB.4 = 1 then
    runtime = 90
    endif
    endif
    endif
    
    
    if PortB.0 = 1 then
    if PortB.1 = 0 then
    if PortB.4 = 0 then
    runtime = 120
    endif
    endif
    endif
    shrinks to
    Code:
    if PortB&19 == 18 then
    runtime = 60
    elseif PortB&19 == 19 then
    runtime = 90
    elseif PortB&19 == 1 then
    runtime = 120
    endif

    with regard to the loop

    if SSW = 1 then ???????????? what is ssw

    snippets are a total failure as usual

    could be the pin if its a pin is still set to analog
    Last edited by richard; - 24th September 2021 at 07:35.
    Warning I'm not a teacher

  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,139


    Did you find this post helpful? Yes | No

    Default Re: Loop issues

    Code:
    main1:
    redport = 0
    blueport = 1
    pause 500
    redport = 0
    blueport = 0
    
    goto main1
    
    
    The problem is that this portion of code..
    
    if SSW = 0 then
    redport = 1
    goto main1
    endif
    in this part of the code, if SSW=0, the redport will be on for a very narrow time frame. I doubt that you will be able to see it. And then it will loop for ever in the main1.

    Ioannis
    Last edited by Ioannis; - 25th September 2021 at 20:41.

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,653


    Did you find this post helpful? Yes | No

    Default Re: Loop issues

    in this part of the code, if SSW=0, the redport will be on for a very narrow time frame. I doubt that you will be able to see it. And then it will loop for ever in the main1.

    whatever redport is , might be a bit var that could never be seen

    the snippet is useless , so many things undefined
    Warning I'm not a teacher

  6. #6
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: Loop issues

    Here's the entire code.
    Redport = 0 was a typo and should have been = 1. The loop worked once I disabled the external oscillator requirements for timer1.
    18F1320 using internal 8MHz oscillator (because I have hundreds of them from a past project! :-) )

    So this code is almost there functionally but has a couple of problems.

    The project is to run some high power LED's for either 60, 90, 120 seconds. There is a lid that has to be closed (SSW=1) before it will allow the LED's to be turned on. If the lid is open, it will just sit there and flash redport until the lid is closed. At this point with lid closed, the redport indicator led will be on solid and it'll move into main1.
    The main LED's get turned on with a momentary push button switch by the user. The timer should start, count up until Runtime is reached, switch off the LED's (enable1 and enable2 = 0) turn on greenport which is a green "done" indicator LED, and it should sit there until the lid is opened again.
    On lid open it should return to the first loop and flash redport until the lid is closed again. While the timer is counting and the main LED's are on it has to monitor for the lid being opened and shut off the main LED's and return to the first loop with the counter ready to start at Runtime.

    So that said here is what works:
    It sets up correctly, will load either 60, 90 or 120 in to Runtime, will sit and flash redport with the lid open.
    It will go in to main1 with a solid redport and wait for the pushbutton input (Startswitch)
    It will turn on the timer, count and when done will turn off the main LED's and turn on greenport and sit there until the lid is lifted (SSW=1)

    What isn't working is:
    Its not counting to the correct number in Runtime. I use greenport in the timer loop to basically have a pulse to look at on the scope showing that the timer is indeed running, and it is. The pulses are approx 1ms apart but they are only running 50% of the time. So say 100 pulses 1ms apart (I haven't counted the actual number yet) then nothing for 100ms then 100 pulses 1ms apart etc etc.
    Currently it runs for about 630ms if Runtime = 60, about 900ms if Runtime = 90 and never turns off if Runtime = 120. I added the thousandths to see if I could increase the on time and it doesn't respond, staying on all the time. When the lid is lifted it does interesting things and gives a wonderful flashing combination of the red green and blue indicator LED's. Clearly I'm setting up the timer wrong but I'm not sure what I'm doing wrong at this point and could use some fresh eyes.
    Last edited by jmgelba; - 28th September 2021 at 01:24.

  7. #7
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: Loop issues

    OSCCON = %01110011
    DEFINE OSC 8
    DEFINE WRITE_INT 1
    Include "modedefs.bas"

    SSW var PortA.7
    redport var PortB.5
    blueport var PortB.7
    greenport var PortB.6
    enable1 var PortB.2
    enable2 var PortB.3
    runtime var word
    RunningFlag var BIT
    Startswitch var PortA.3
    ended var bit
    Thousandths var word
    Hundredths var word
    Seconds var word
    comp var bit


    adcon0.0 = 0
    adcon1.3 = 1
    adcon1.4 = 1
    adcon1.5 = 1
    adcon1.6 = 1
    TRISA = %11111000
    TRISB = %00010011
    PortB.5 = 0
    PortB.2 = 0
    T1CON = %00000100

    comp = 0
    redport = 0
    greenport = 0
    blueport = 0
    enable1 = 0
    enable2 = 0
    ended = 0
    runningflag = 0
    thousandths = 0
    Hundredths = 0
    Seconds = 0

    Gosub SetTimer

    blueport = 1 'testing outputs
    pause 2000
    blueport = 0
    greenport = 1
    pause 2000
    greenport = 0

    On Interrupt goto Starttimer
    PIE1.0=1 ' Enable TMR1 Interrupts
    INTCON.6=1 ' Enable all unmasked Interrupts
    INTCON.7=1 ' Enable Global Interrupts



    main:

    if PortB.0 = 0 then
    if PortB.1 = 1 then
    if PortB.4 = 1 then
    runtime = 60
    endif
    endif
    endif

    if PortB.0 = 1 then
    if PortB.1 = 1 then
    if PortB.4 = 1 then
    runtime = 90
    endif
    endif
    endif

    if PortB.0 = 1 then
    if PortB.1 = 0 then
    if PortB.4 = 0 then
    runtime = 120
    endif
    endif
    endif

    write 5, runtime

    if SSW = 1 then
    redport = 1
    pause 200
    redport = 0
    pause 200
    goto main
    endif

    if SSW = 0 then
    redport = 1
    goto main1
    endif

    goto main

    'main code loop

    Enable
    main1:

    if ssw = 1 then 'checks to see if lid is closed, if not,
    goto main 'go back to first loop and wait
    endif 'if lid is closed, continue on to next requirement

    if SSW = 0 and startswitch = 1 then 'starts the first run of the timer if lid
    ended = 0 'is closed and push button has been pressed
    runningflag = 1 'and sets a flag to show timer is running
    GOSUB StartTimer
    endif

    if runningflag = 1 then 'checks to see if timer was started and
    gosub starttimer 'if true loops for next cycle
    endif

    goto main1
    disable




    completed:
    T1CON.0=0
    enable1 = 0
    enable2 = 0
    greenport = 1
    redport = 0
    blueport = 0
    runningflag = 0
    comp = 1
    if ssw = 1 then
    greenport = 0
    goto main
    endif
    goto completed
    return

    '
    ' Subroutine Loads TMR1 values
    ' ============================
    SetTimer:
    T1CON.0=0 ' Stop the Clock
    TMR1H=$B1
    TMR1L=$e7
    PIR1.0=0 ' Reset TMR1's Interupt Flag
    Return

    ' Subroutine to stop the timer, reset, and shutoff outputs
    Stoptimer:
    T1CON.0=0
    TMR1H=$B1
    TMR1L=$E7
    PIR1.0=0
    enable1 = 0
    enable2 = 0
    thousandths = 0
    Hundredths = 0
    Seconds = 0
    redport = 0
    blueport = 0
    greenport = 1
    runningflag = 0
    ended = 1
    'gosub settimer
    Pause 1000
    return


    Starttimer:
    redport = 1 'output to show timer is running
    blueport = 1
    greenport = 0 'provides a pulse to measure for diagnostics to check timer is actually running
    greenport= 1
    greenport = 0
    enable1 = 1 'turns on both LED channels
    enable2 = 1
    Gosub SetTimer ' Set the Timer for next Interrupt
    If RunningFlag=1 then ' If timing actually enabled then continue
    thousandths=thousandths+1
    if thousandths>999 then
    thousandths=0
    Hundredths=Hundredths+1
    If Hundredths>99 then
    Hundredths=0
    Seconds=Seconds+1
    write 10, seconds 'just to see if it turns off at runtime seconds
    ' Increment the Seconds

    if seconds = runtime then
    gosub stoptimer
    goto completed
    endif
    endif
    endif
    endif

    if ssw = 1 then
    gosub stoptimer
    goto main
    endif

    Resume

    End

Similar Threads

  1. Do Loop
    By skybox in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 27th December 2012, 00:45
  2. Issues with a feedback loop.
    By jmgelba in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th June 2012, 18:54
  3. if ... then loop
    By lerameur in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 9th November 2010, 23:08
  4. Help with loop
    By wildbilly in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd January 2007, 16:59
  5. While LOOP
    By actionplus in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 5th March 2004, 14:59

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