3 Replies Latest reply on Jun 13, 2017 2:47 PM by e.pratt_1639216

    PWM Glitch when Dimming

    karim.sal

      Hello,

         

      I have a working code for a capsense button project. If  the user presses the capsense button, the LED will slowly get brighter and when the button is pressed again the LED gets dimmer. The problem is when the LED  nears turning off I get this sudden increase in brightness and then it turns off. Any idea why this is the case?

         

      I am using CY8C4024LQI-S411

         

      Attached is the project files

         

      Here is a video link for the switch in action( notice the glitch at the end of the video) 

         

      https://www.youtube.com/watch?v=83Qj79vqrhI

         

      Any help is appreciate it, 

         

      Thank you in advance

        • 1. Re: PWM Glitch when Dimming
          e.pratt_1639216

          You are reading the PWM_ReadCompare() multiple times, it could be returning different values (theoretically) each time you read it.

             

          Have you tried varying the Brightness Resolution to see how the code behaves? (smaller/larger test values)

             

          Also, iirc, you can't disable an interrupt while inside the interrupt (calling ISR_PWM_Stop()), so refactoring that might yield results, but that is just a shot in the dark :)

             

          Also, PWM_WriteCompare(0); might be telling the PWM to toggle the output on EVERY SINGLE cycle, rather than being "off"; This would cause a 50% duty cycle and the brightness increase. Try disabling/turning off the PWM instead, rather than setting it to 0.

          • 2. Re: PWM Glitch when Dimming
            karim.sal

            Hello e.pratt, thank you for your reply.  

               

            I did vary the brightness resolution but that changes how fast the LED change in brightness. 

               

            I used  PWM_Stop() instead of ISR_PWM_Stop() to turn the LED off however I can't use  PWM_Stop() when the LED is in full

               

            brightness because If I do that it just turns off the output when I need the output to stay in full brightness. 

               

            Why would PWM_WriteCompare(0) cause a 50% duty cycle?  I would appreciate it if you can elaborate little more. 

               

            If  compare is 0 and period is 30000 the output should stay at zero. 

               

            I am still having the same issue even if I don't disable the interrupt inside the interrupt. Any other ideas?

               

            Thanks again

            • 3. Re: PWM Glitch when Dimming
              e.pratt_1639216

              Yeah, you won't be able to use the PWM_Stop() when the LED is "full", but you can use it at the low-level brightness to prevent the sudden spike in brightness when it reaches the bottom of the brightness values. (Since you are stepping the brightness, just check if it is "0" to call PWM_Stop() or "1" to call PWM_Start() to turn the PWM back on once it is no longer zero brightness).

                 

              Why would PWM_WriteCompare(0) cause a 50% duty cycle?  I would appreciate it if you can elaborate little more. 

                 

              If  compare is 0 and period is 30000 the output should stay at zero. 

                 

              Hmmm, I was thinking the compare was the period. I'm not sure what I was thinking with respect to that, just ignore my idiotic comment :)

                 

              I would give a shot at determining which values of the PWM compare cause the LED to be brighter than the value above them (since that is the issue you are seeing).

                 

              Also, I would replace the PWM_ReadCompare(); values with a single PWM_ReadCompare() value at the beginning of the ISR followed by usage of a variable holding the value read in. Checking bounds of the value to make sure it is always between 0 and 30000 might be a good idea too while you are debugging stuff.

                 

              Another cause of the brightness spike could be bouncing on the capsense button (unlikely I know :) )

                 

              Try just setting fixed values of the PWM compare to see what values cause what physical brightness to see if you can narrow down which values are the issue.