Loop issues


Closed Thread
Results 1 to 19 of 19

Thread: Loop issues

Hybrid View

  1. #1
    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

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,132


    Did you find this post helpful? Yes | No

    Default Re: Loop issues

    1. RMW issue here:

    greenport = 0 'provides a pulse to measure for diagnostics to check timer is actually running
    greenport= 1
    greenport = 0

    I think you will not see the greenport change because of RMW issue. It depends on the capacitive load of the port though.

    2. SetTimer turns off the timer.

    So the rest of the commands after this line

    Gosub SetTimer ' Set the Timer for next Interrupt

    I guess are not executed.

    The Interrupt routine should be less loaded also.

    If time allows I will try to see more on your code.

    Ioannis

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


    Did you find this post helpful? Yes | No

    Default Re: Loop issues

    Ioannis, thank you for your reply. I do actually see a pulse and have been using it to measure changes I've been making, although it is always fairly constant at about 1ms pulse to pulse. I'll assume this is because I'm turning off the timer rather than letting it run. Interestingly though, it does stop when seconds = runtime. Its just that seconds aren't 1000ms long :-D Uff just checked, 68uS between pulses, not 1ms as previously stated.
    Last edited by jmgelba; - 28th September 2021 at 10:48.

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,643


    Did you find this post helpful? Yes | No

    Default Re: Loop issues

    There are many issues



    starting here
    1.
    Code:
    disable ;  missing
    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   ;  never a good look  in a isr
    If RunningFlag=1 then ' If timing actually enabled then continue
    thousandths=thousandths+1 ;////   the isr  is set to 10mS   wtf
    if thousandths>999 then 
    thousandths=0
    Hundredths=Hundredths+1
    If Hundredths>99 then  ; what how
    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    you should never do this in an interrupt 
    endif
    
    
    Resume
    enable ;lmissing


    you cannot call a isr as a subroutine . there is no return
    2.
    Code:
    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
    3.
    the timer timer 1 is never turned on

    4.
    'INTCON.7=1 ' Enable Global Interrupts no do not do this

    this works
    Code:
    #CONFIG  CONFIG  OSC=INTIO1, FSCM=ON, IESO=ON, PWRT=OFF, BOR=ON, BORV=27, WDT=ON
      CONFIG  WDTPS=512, MCLRE=ON, STVR=ON, LVP=OFF, DEBUG=OFF, CP0=OFF, CP1=OFF
      CONFIG  CPB=OFF, CPD=OFF, WRT0=OFF, WRT1=OFF, WRTC=OFF, WRTB=OFF, WRTD=OFF
      CONFIG  EBTR0=OFF, EBTR1=OFF, EBTRB=OFF
    #ENDCONFIG
    OSCCON = 110011
    DEFINE OSC 8
    DEFINE WRITE_INT 1
    'Include "modedefs.bas"
        DEFINE DEBUG_REG PORTA
        DEFINE DEBUG_BIT 0      ;  if not used for pwr  
        DEFINE DEBUG_BAUD 9600
        DEFINE DEBUG_MODE 0     
        pause 2000
        Debug "Start",13 ,10  
    
    
    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
    
    
    Seconds var word
    comp var bit
    
    
     adcon1=$7f
    'adcon0.0 = 0
    'adcon1.3 = 1
    'adcon1.4 = 1
    'adcon1.5 = 1
    'adcon1.6 = 1
    TRISA = 111000
    TRISB = 010011
    PortB.5 = 0
    PortB.2 = 0
    T1CON=0
    TMR1H=$B1     ;10mS
    TMR1L=$e7
    lata.0=0   ;debug
    
    
     pause 1000
        Debug "Start",13 ,10  
    comp = 0
    redport = 0
    greenport = 0
    blueport = 0
    enable1 = 0
    enable2 = 0
    'ended = 0
    runningflag = 0
    thousandths = 0
    
    
    Seconds = 0
    
    
    
    
    
    
    blueport = 1 'testing outputs
    pause 200
    blueport = 0
    greenport = 1
    pause 200
    greenport = 0
    
    
    T1CON.0=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&19) == 18 then
        runtime = 10
    elseif (PortB&19) == 19 then
        runtime = 90
    elseif (PortB&19) == 1 then
        runtime = 120
    endif
    
    
    'Debug "runtime ",dec runtime," ",dec portb,13 ,10  
    ''write 5, runtime
    'pause 2000
    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:
        Debug "main1",dec runtime,13 ,10 
        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
    '        Debug "start",dec runtime,13 ,10 
            runningflag = 1 'and sets a flag to show timer is running
            T1CON.0=1 ; timer on
            enable1 = 1
            enable2 = 1
            blueport = 1
            comp = 0
            while runningflag: wend  
            enable1 = 0
            enable2 = 0
            greenport = 1
            blueport = 0
            comp = 1
            redport = 0
        endif
        if comp==1 then
        WHILE  SSW = 0 : wend  
        greenport = 0
        Debug "DUNN",dec runtime,13 ,10 
        ENDIF
        goto main
            
    goto main1
    
    
    end
    
    
        disable
    Starttimer:
        lata.1=1 ;debug to clock  time
        T1CON.0=0
        TMR1H=$B1+TMR1H     ;10mS
        TMR1L=$e7 +TMR1L
        if STATUS.0 THEN  TMR1H =TMR1H +1
        PIR1.0=0 ' Reset TMR1's Interupt Flag
        T1CON.0=1
        thousandths=thousandths+10
        if thousandths>999 then
            thousandths=0
            Seconds=Seconds+1
        endif
        if (seconds = runtime) || (ssw = 1) then
            runningflag = 0
            T1CON.0=0
        endif
        lata.1=0;debug to clock  time
        Resume
        enable
    End


    my view of schematic
    Name:  Screenshot 2021-09-28 194517.jpg
Views: 1061
Size:  145.6 KB
    Last edited by richard; - 28th September 2021 at 10:58.
    Warning I'm not a teacher

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


    Did you find this post helpful? Yes | No

    Default Re: Loop issues

    Wow Richard, thanks for that! I guess I was way off. I used Melanie's Olympic Timer as the basis for my code and she included some of the things that you have advised against such as adding the SetTimer subroutine call in the Starttimer routine and INTCON.7=1.

    I tried your code and needed to change a few things.

    OSCCON = 110011
    TRISA = 111000
    TRISB = 010011

    became

    OSCCON = %01110011
    TRISA = %11111000
    TRISB = %00010011

    in order to compile.

    If you open the lid while its running and close it again, then push the start button, it doesn't turn on the outputs and start timing again. It does jump back to main, it will jump in to main1 and put the red led on solid, but doesn't run main1.
    If the switch is in the 60 second run position it runs for about 10 seconds - which I saw you had runtime = 10 so I changed that.
    In the 90 second position it runs for 90 seconds - perfect.
    In the 120 second position it ends almost instantly. I noticed this would happen sometimes in my code too. 100 and less and it works each time. I don't know what this phenomena is.
    Last edited by jmgelba; - 28th September 2021 at 15:01.

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