Quantcast
Channel: MEL PICBASIC Forum
Viewing all articles
Browse latest Browse all 4796

Loop algo problem

$
0
0
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

Viewing all articles
Browse latest Browse all 4796

Latest Images

Trending Articles



Latest Images