Loop algo problem


+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    2,790

    Default Loop algo problem

    I am trying to make a complex loop and I suppose I am in a wrong mind loop...

    The project need a time base. Done in a ISR that counts seconds and minutes OK.

    The request is to have a var value change according to data read from an EEPROM. That data include the time and setpoint.

    Example:

    Phase 1: For 1 minute
    change value from setpoint1 to setpoint2 and reverse every 20 seconds

    Phase 2: For 2 minutes
    change value from setpoint1 to setpoint2 and reverse every 30 seconds

    My ISR is this:
    Code:
    timer:                    'Timer 1 tick every 1us x 50000 = 50ms
        t1con.0=0             'time variable inc every 50ms
        if time<19 then       'x 20 = 1sec
            time=time+1
        else
            time=0
            if seconds>0 then 'dec seconds
                seconds=seconds-1
            else
                seconds=59
                if minutes>0 then  'dec minutes
                    minutes=minutes-1
                else
                    minutes=59 'reload minutes from settings
                endif
            endif
            IF p_seconds>0 then p_seconds=p_seconds-1
        endif
        tmr1l=$b7   'reload timer 1  with 16474
        tmr1h=$3c   '65536-49062
    t1con.0=1   'start timer 1
    
    @ INT_RETURN
    The data are read from the EEPROM using a For-Next loop in 4 byte group:

    Code:
    for addr2=start_addr to end_addr step 4
        i2cread data_pin, clk_pin, contr, addr2, [Minutes, setpoint1, setpoint2, change_seconds_period]
    Then there is this wrong loop that does not work correctly. It goes to the next phase when change_seconds_period expires.

    Code:
    temp_change_seconds_period=change_seconds_period
    value=setpoint1
    
    loop1:
            while minutes>0
                if change_seconds_period=0 then loop2
                if temp_change_seconds_period=0 then 'change over setpoints
                    value=setpoint2
                    temp_change_seconds_period=change_seconds_period 'reload change period
                endif
      loop2:
                if !but_stop then stop_prg  'test for user interaction
                if !but_pause then pause_prg  'test for user interaction
            lcdout $fe,line3,"Phase ",#phase,$fe,line4,dec3 value," ",dec3 minutes," ",dec3 temp_change_seconds_period
            wend
            
    phase=phase+1    
    next addr2
    Any help appreciated.
    Ioannis
    Last edited by Ioannis; - 30th December 2018 at 22:44.

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    1,559

    Default Re: Loop algo problem

    i think your isr is over complex

    my take
    Code:
    isr:
    
    
    stop timer
    inc counter
    is counter>19  then{
    counter =0
    seconds_changed = 1
    }
    timer = timer + preset
    start timer
    exit is
    
    
    
    
    
    
    start:
    phase=0 ;two phases 0 an 1
    phasetime=60
    phasereverstime=20
    sp=0; two setpoints 0 and 1
    phrt=20
    
    
    
    
    loop:
    
    
    if seconds_changed then
       seconds_changed =0
       if phasetime then      
           phasetime=phasetime-1
           if  phasereverstime then
             phasereverstime=phasereverstime-1
           else
             phasereverstime=phrt       
             gosub swapsetpoints
           endif 
       else      
          if phase then
              phase=0          
              phasetime=60
              phrt=20
          else
              phase=1
              phasetime=120
              phrt=30
          endif
       endif
    endif
    
    
    goto loop
    This is more entertaining than Free to Air TV

  3. #3
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,172

    Default Re: Loop algo problem

    Here, you set these two variables to be equal to each other.
    Code:
    temp_change_seconds_period=change_seconds_period
    ....
    Then, here, you are checking two of them if they are = 0 or not.
    If the first one is 0, then second one must also be 0 ; you had set them to be equal beforehand.
    So if change_seconds_period=0, then next line temp_change_seconds_period=0 will never work.

    Code:
            while minutes>0
                if change_seconds_period=0 then loop2
                if temp_change_seconds_period=0 then 'change over setpoints
                ....
    Am I wrong?
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

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

    Default Re: Loop algo problem

    Thanks both for the suggestions.

    Give time to look at your recommendations Richard.

    Sayzer, the change_seconds_period variable may or may not be zero, depending on the I2C EEPROM read. A check I think is needed there to jump over the next lines.

    Ioannis

Similar Threads

  1. Strange problem with button command and loop
    By CuriousOne in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 14th April 2013, 07:10
  2. Loop Problem please help
    By wildbilly in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 15th July 2006, 14:42
  3. digital integrator algo for bipolar sigs?
    By pwhitt in forum General
    Replies: 2
    Last Post: - 5th August 2005, 14:42
  4. A basic loop problem gumming things up
    By Ricardoco in forum General
    Replies: 1
    Last Post: - 21st November 2004, 23:20
  5. RSA algo
    By new_pic in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 23rd June 2004, 11:46

Members who have read this thread : 17

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