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

    Default FOR...NEXT strange counting

    Hello,

    I can't figure out how the FOR...NEXT count works.

    First my code, then my question.

    Code:
    ...
    Duty VAR BYTE
    Duty = 0
    
    if duty = 0 then
       FOR Duty = 0 TO 254
          HPWM 1, Duty, 1000
          lcdout Dec Duty 'First read of "Duty"
          PAUSE 5
       NEXT
       lcdout Dec Duty 'Second read of "Duty"
    endif
    ...
    At the end of the FOR...NEXT execution, first read of Duty = 254. Right after the "NEXT" command, the second read of the variable Duty = 255.

    Why is this!?
    Last edited by flotulopex; - 7th October 2006 at 17:05.
    Roger

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Increse PAUSE 5 to PAUSE 500, it counts perfectly.
    Why you have 255 on the second LCDOUT?

    Code:
    1   FOR Duty = 0 TO 254
    2      HPWM 1, Duty, 1000
    3      lcdout Dec Duty 'First read of "Duty"
    4      PAUSE 500
    5   NEXT
    Incrementation of Duty variable is done @ line #5, The condition to exit the for-next loop is done @ line #1. If >254, exit the loop. 255>254?
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

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


    Did you find this post helpful? Yes | No

    Default

    I'm using this routine to switch ON the backlight of my LCD display, gradually but quite quickly (around 1 second). If I "PAUSE 500", it will takes ages to have the backlight fully on.

    As stated in the MicroCode help file, the max "duty" value can be 255. If I want to have the backlight fully ON, I should have 255.

    If the condition is "FOR Duty = 0 to 255", Duty will turn back to value 0 (zero) after the "NEXT" since it has been declared as a BYTE variable.

    Maybe, the best thing to do is to declare Duty as WORD sized...

    Nevertheless, the strange thing is that the Duty variable gets incremented by 1 when it exists the FOR...NEXT cycle.
    Roger

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi flotul,

    It won't matter that the Duty variable ends up at 0 when it wraps around. The HPWM will stay at the last DutyCycle it was set to "Inside" the FOR/Next loop. So you can go the full 0-255.

    It appears that you are using the "if duty = 0 then" statement to make the backlight only ramp-up once, when power is applied. Which also means that you have this section of code within the Main Loop of the program.

    If you take it out of the main loop and put it up at the top of the program, that section of code will only run once.
    <br>
    DT

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

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,115


    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

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