PWM: Pulse Width Modulation
It is to periodically control the time (duty cycle) of IO pulling high and low to control the on and off of the switch tube to achieve power control, such as LED dimming, DC motor speed control, etc.
I mainly use it here to control LED dimming. We know that the frequency at which the human eye does not see the light flicker is above 50hz, so if we want our eyes to see the light without flickering, the high and low PWM should be 10ms respectively, and the off time cannot be greater than 10ms. If it is greater than this value, we will see flickering. If I don't want the switch on time to be so bright and less than 10ms, then we can only increase the frequency. By increasing the frequency, we can control the length of time the switch is on, so as to achieve power control and control the brightness of the LED without flickering.
W800 PWM usage :
Initialization: Since it has been officially packaged, initialization is very simple. You only need to call an initialization to turn on the PWM function. Through the following enumeration values, we know that up to 6 PWM channels can be turned on.
Startup: There is only one function for startup, but we must pay attention to the entry parameters. The first is to configure the PWM channel, the second is the period, that is, the duty cycle, and the third is the frequency. By changing the last two parameters, we can achieve the PWM parameters we want.
Test : Test Many students may be like me. How can we view the waveform without an oscilloscope? As a hardware developer, you can prepare some small tools. The following picture shows a simple logic analyzer I bought online. It can be used to capture PWM waveforms and analyze serial communication protocols (such as UART I2C SPI, etc.), which are very useful.
In order to facilitate viewing of the waveform, I wrote an operation: when the protocol interface receives data, the PWM value increases by 10, and the cycle continues like this. The figure below shows that my frequency is set to 1000, which is 1K, and the period is 255. Why is it 255? There is no comment on the interface, and I have not turned to the manual to explain how to set this period. Through testing, I found that this period is just an 8-bit register, and the overflow starts from the beginning, so 255 is given.
The following pictures are waveforms captured by logic analyzer software: by sending instructions, only the waveform of channel 0 is changed, and channel 1 remains unchanged. Figure 1 shows the waveform when both PWM channels are set to 0 when the power is turned on, and the frequency is exactly 1K.
Set the waveform close to 50%:
Set the waveform close to 100%:
This section will briefly talk about PWM. Knowing the role of PWM, the brightness and color temperature of the two-color LED light to be made in the next section are both based on PWM.