FOR...NEXT strange counting


Closed Thread
Results 1 to 14 of 14

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default Repeat...until

    I have my LCD-backlight controlled with two buttons; this is why it is in the main loop.

    The HPWM command accepts the duty value from 0 to 255.

    Actually, when the FOR...NEXT cycle is completed with a count up to 255, the value "wraps" from 255 to 0.

    But I track this value to prevent a second press on the same button (it's not nice to see the backlight switched OFF instantaneously and dimm up again when it was already ON - looks like a bug to the user).

    To get the HPWM at full range, I would count from 0 to 254 in the FOR...NEXT cycle and, when completed, add one more command to set the duty value to 255. This is not very "clean".

    Actually I found another way around. I use REPEAT...UNTIL. Now it works.

    I was just suprised to see how the FOR...NEXT cycle affects the count.
    Roger

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


    Did you find this post helpful? Yes | No

    Default

    Since the for-next loop counting from 0 to 255 and the HPWM is inside the loop, where is the problem? At the end HPWM will be at 100% (255), the for-next will wrap to 0 since you have byte variable ant the loop will exit.

    Thats how a for-next works in the first place. Always exits when variable will pass the upper limit.

    Did I miss anything?

    Ioannis

  3. #3
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default

    I need to get a "HPWM 1, 255, 1000" final command with a Duty value of 255.
    Code:
    ...
    Duty VAR BYTE
    Duty = 0
    
    FOR Duty = 0 TO 255
       HPWM 1, Duty, 1000
       LCDOUT $FE, 2, DEC Duty   'First read of Duty
    NEXT
    LCDOUT $FE, $C0, DEC Duty    'Second read of Duty
    ...
    When this FOR...NEXT loop is completed, the HPWM command will be correct ("HPWM 1, 255, 1000" - see first read of Duty) BUT Duty's value will be 0 (see second read of Duty).

    Even if the count is completed (Duty has reached 255), passing the last NEXT command will increment Duty by 1 and make it a 0.

    This is what happens with my 16F88 programmed with PBP. I never expected such a result.

    In my program, I use Duty as a reference to check if my LCD-backlight is fully OFF (Duty = 0) or if it is fully ON (Duty = 255).

    If Duty is 0, I'm allowed to press only button 'A'; if Duty's value is 255, I'm allowed to press only button 'B'.

    In other words, if Duty is 0, I don't want to allow a second press on button 'A'; I will only accept button 'B' and vice-versa if Duty is 255.

    The solution I found now is this one:
    Code:
    ...
    SUBROUTINE:
        REPEAT
            Duty = Duty + 1
            HPWM 1, Duty, 1000
        UNTIL Duty = 255
        RETURN
    ...
    Roger

  4. #4
    Join Date
    Feb 2003
    Posts
    432


    Did you find this post helpful? Yes | No

    Default

    Alternatively you could add the following line

    FOR Duty = 0 TO 255
    HPWM 1, Duty, 1000
    LCDOUT $FE, 2, DEC Duty 'First read of Duty
    NEXT
    Duty = Duty-1
    LCDOUT $FE, $C0, DEC Duty 'Second read of Duty

    Which would put it back the the final value inside the For/Next loop

    If you are also dimming down then do the opposite

    FOR Duty = 255 TO 0 STEP -1
    HPWM 1, Duty, 1000
    LCDOUT $FE, 2, DEC Duty 'First read of Duty
    NEXT
    Duty = Duty+1
    LCDOUT $FE, $C0, DEC Duty 'Second read of Duty
    Keith

    www.diyha.co.uk
    www.kat5.tv

  5. #5
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default

    Thank you keithdoxey,

    It is not about to find a solution to make it work; it is about how the FOR...NEXT loop counts.

    There are lots of differents solutions to solve my problem and I wonder how many people noticed this stange loop counting behaviour.
    Roger

  6. #6
    Join Date
    Feb 2003
    Posts
    432


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by flotulopex
    It is not about to find a solution to make it work; it is about how the FOR...NEXT loop counts.
    I think it is the same in any language.

    you specify the start and end points for the count and optionally the step size.

    the count starts at the specified start value then adds (or subtracts) the step value. It is then tested to see if it is within the range specified at which point the actions are executed. Once outside the specfied range it exits the for/next loop. This means that the value has to have changed from the last time the loop code was executed.
    Keith

    www.diyha.co.uk
    www.kat5.tv

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


    Did you find this post helpful? Yes | No

    Default

    Hi Flotul

    As I stated at #6 there is no strange behaviour. It is just the way for-next loops work in every version of Basic. Inside the loop the result is absolutely correct. Outside is expected to be +1 or -1 depending on the step of counting.

    I see nothing strange here.
    Other way to make a loop like While-Went or Repeat-Until use different counting methods.

    While-wend for example, first checks, then executes. For-Next, first executes then checks.

    Ioannis

Similar Threads

  1. COUNT is not counting again
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 19th June 2009, 04:52
  2. Remain counting while sending out a pulse
    By ultiblade in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 10th January 2007, 15:51
  3. Strange behaviour from PIC16F877 on TMR0
    By mikebar in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 19th August 2006, 01:31
  4. continious counting process (capture)
    By asynch in forum General
    Replies: 1
    Last Post: - 17th February 2006, 07:42
  5. Strange ASM file
    By barkerben in forum General
    Replies: 2
    Last Post: - 29th November 2004, 18:54

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