This example show how to perform LED dimming using PWM module for Cypress PSoC 4 Pioneer Kit.
Here is the Application Schematics:
This example uses the PWM block in a timed configuration to produce the dimming effect on the LED.
The PWM module { PWM (TCPWM mode) [v1.0] }configuration is as shown below:
In the code one needs to vary the Compare factor for the PWM to generate a DC average output that would lead to dimming of the led.
Ideally a capacitor is needed to produce the DC averge but since the LED load is not significant, its possible to directly generate the dimming on the pin connected LED.
Here is the code for this example:
#include <project.h> int main() { uint32 i = 0; //Used as a Counter uint32 op = 0; //Used to define which operation is being currently performed : 0-additions 1-subtractions // Start the PWM generation PWM_1_Start(); for(;;) { CyDelay(5); //Cycle Delay // Process the Counter Operation if(op == 0) i += 10; else i -= 10; // Change the PWM duty cycle value as per the Counter PWM_1_WriteCompare(i); // Handle Counter overflow if(i>=1000) { op = 1; // Start Decrementing as max value has already reached } else if(i<=1) { op = 0; // Start Incrementing as Min value has already reached CyDelay(800); // Delay to keep the LED off for more time } } }
After building and loading this code the Green LED on the Pioneer Kit would show the dimming effect.
Its important to note here that this dimming would show up some visible blink and may also cause EMI/EMC or RFI issues in case the output is used to control LED drivers.
The better solution would be use the PrISM module to generate precise dimming without flicker or emission issues.