Menu

Not fully functioning HPWM

Winch
2022-02-17
2022-02-19
  • Winch

    Winch - 2022-02-17

    Try to make an HPWM signal with Arduino Uno.
    In the example program the signal runs from 0 to 255 and then again from 255 to 0.
    But he doesn't carry out the last assignment? So going from 255 to 0 doesn't seem to work. I see the LED slowly increasing after which it should actually slowly decrease again.
    Just for the sake of completeness, the LED goes up and at the maximum the LED goes out and then goes up again.
    What could be the problem? I don't see any error in the code so far.

    #chip mega328p, 16
    
        '#1
        'Set the CLOCK Source as AVRTC0 relates to PWM2
        #define AVRTC0
    
        '#2
        'Set the AVR PWM Channel as enabled by defining this constants.  AVRCHAN2 equates OC0B
        #define AVRCHAN2
    
        '#3
        'Set the PWM pin to output mode.  OC0B on assigned to portD.0
        DIR PORTD.5 out
        Dim Bright as byte
    
        'Main code
        'freq 40khz
        'variable duty from 0% to 100%
        do
            'Turn up brightness over 2.5 seconds
            For Bright = 0 to 255
                '#4 - Call the method with the correct PWM channel
                HPWM 2, 40, Bright
                wait 15 ms
            next
    
            wait 1 s
    
            'Turn down brightness over 2.5 seconds
            For Bright = 255 to 0
                '#4 - Call the method with the correct PWM channel
                HPWM 2, 40, Bright
                wait 15 ms
            next
    
            wait 1 s
    
        loop
    
    end
    
     
    • Anobium

      Anobium - 2022-02-17

      Please use the For Next loop as follows:

      For Bright = 255 to 0 step -1

      This is the resolution.

       
  • Anobium

    Anobium - 2022-02-17

    What version of the compiler? open the asm file, look at the first line and post to this chat.

     
  • kent_twt4

    kent_twt4 - 2022-02-17

    Copied program, tried out with UNO, and works as intended. Compiled with 98.07 rc19

    P.S. If you could post and attach the .asm file, I could check against rc19 for differences.

     

    Last edit: kent_twt4 2022-02-17
    • Anobium

      Anobium - 2022-02-17

      @kent_twt4 Can you try the latest build ?

       
      • kent_twt4

        kent_twt4 - 2022-02-17

        Version 0.99.01 1/29/2022 from downloads?

         
  • kent_twt4

    kent_twt4 - 2022-02-17

    OK, V99.01 not handling the for/next correctly for dimming led .
    The endpoint of "0" not being compared, but instead "255" :-(!
    So have to wait till rollover to repeat first loop.

    P.S. just a guess that the compiler is grabbing wrong parameter or excluded from using "0"?

     

    Last edit: kent_twt4 2022-02-17
    • Anobium

      Anobium - 2022-02-17

      Who tested the for-next loop.... :-)

      Try adding #Define USELEGACYFORNEXT to the code - does this resolve? It is a workaround NOT a fix.

       
  • kent_twt4

    kent_twt4 - 2022-02-17

    I don't want to add define for all my dozens and dozens of programs. Why not make it so
    '#Define USENEWFORNEXT' So I don't have to go back and change them all???

     
    • Anobium

      Anobium - 2022-02-17

      The reason to fix for-next was to resolve the huge number of issues with the existing code. It was riddled with bugs and caused many issues.

      This issue came about because of the lack of testing. I tested 1000s of variants and somehow this condition was not tested. My error I guess.

       
  • kent_twt4

    kent_twt4 - 2022-02-17

    Here are the asm files for V99.01 and V98.07 rc19 respectively

    EDIT : yes the legacy define for next works. The attach asm file is pre-define.

     

    Last edit: kent_twt4 2022-02-17
    • Anobium

      Anobium - 2022-02-17

      Use the workaround to prove resolves the issue - so, this works. Good.

      Someone needs to fix the compiler.

       
  • Anobium

    Anobium - 2022-02-17

    I am sorry. The code is doing as expected.

    For Bright = 255 to 0 This is POSTIVE increment of 1, as BRIGHT is a byte it will be assgned 255 then 0 and exit.

    The code is working as expected.

    The legacy is INCORRECT and should be relied on.

    Use For Bright = 255 to 0 step -1 if this is the intended action.

     
  • kent_twt4

    kent_twt4 - 2022-02-17

    OK back on me, step -1 it is.

    Somehow the old for next was letting me get away with stuff. Need to change Help example. Step -1 confirmed correct on hardware with 99.01

     
    • Anobium

      Anobium - 2022-02-18

      Thank you.

      I have just updated all the Help. There were many places where STEP -1 was missing. It is now updated and published online.

       
  • Anobium

    Anobium - 2022-02-18

    I have also updated many, many demo files that did not have the correct usage to step -integer. I did not update last year.. clearly.

     
  • Winch

    Winch - 2022-02-18

    Okay the solution with "step -1 " works fine and is the solution for me.
    Thank you!

     
    • Anobium

      Anobium - 2022-02-18

      @winch - thank you for spotting. The demos and Help has not been corrected to reflect the improvements in the compiler.

      :-)

       
  • stan cartwright

    stan cartwright - 2022-02-18

    What's the difference between a for-next loop and a do-loop in speed and memory usage please?

    dim count1 as Byte
    for count1=255 to 0
    next
    ;
    count1=255
    do until count1=0
      count1---
    loop
    
     
    • Anobium

      Anobium - 2022-02-18

      You figure it out.....

      For newbees the for-next is expected to work, and, it is expected to work as programmed (it was not in the past)
      Newbees may not know about that do loop.

       
      • stan cartwright

        stan cartwright - 2022-02-18

        True, for-next is more common, do loop was not in zx basic or anything else at the time , when did it become a basic command? It's not used in any 80's machines that ran basic as the os.
        Thing is, is it faster than do-loop? I guess no-one tested.
        How to. A for next inside an another for next and the same for a do loop inside another do loop and use the millis function at the start and end?

         
        • Anobium

          Anobium - 2022-02-19

          Hijacking a thread Stan... create a new thread on the performance question

           

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.