[Allwinner ClassA20 class library Analysis] 5. Use of the PWM class, classa205.pwm
This section describes how to use the PWM class. Download The classa20class library at https://github.com/tjcfeng/classa20.
PWM, another typical output function, is a very effective technology that uses the digital output of the processor to control analog circuits. It mainly includes the concepts of cycle, pulse width and duty cycle. The pulse width is the time of the High-level output, and the duty cycle is the percentage of the low-level output. The sum of the high and low time is the cycle, so we mainly set the pulse width and duty cycle values (of course, some basic settings have been encapsulated or use the default values ).
Let's take a look at the class definition first. Open the PWM unit file:
Similar to GPIO units, there are also two classes for use. But there are some differences here. Let's talk about the TPWM class first.
The TPWM class is used in the same way as TGPIO, and can be directly created, but its implementation method is different. The PWM port and the IO port are reused. It is another function of the IO port, so the TPWM class inherits the TGPIO class. The inherited TPWM class internally calls some functions of the TGPIO class to set the corresponding IO port to the PWM output, and sets some basic attributes, therefore, you do not need to consider too much during use.
The TPWMGROUP class is different. It is a singleton class, that is, it can only be created once in the same program and is created for the first time, the created instance is used in other places, and you do not need to manually release it. It is automatically released when the program exits. This is done to avoid the impact of multiple programs on one PWM channel operation. Of course, it is impossible to completely avoid (unless the lock is applied, but the dependency on the operating system is relatively large). It mainly serves as a prompt.
Okay, let's get started. Self-made artifacts appear again.
Ignore the buttons above and only check the LED. Unlike the PH24 on the last banana Pi, this LED is driven by a transistor, which can be brightened Based on the PWM frequency. When the PWM output is low, the voltage difference is usually formed to make the LED light, and the power is usually off.
The hardware connection is self-made and not universal, so I will not describe it. I only look at the PWM line. For convenience, I connected it to PWM1, that is, the IO port PI3 (the purple line ).
After the preparation is complete, start to write the program.
Pascal's with can omit the object name. The actual operation is to specify the PWM channel and set the pre-division, cycle, and duty cycle. You can see the change in the LED flashing frequency after the change.
The flickering process is dynamic. If you do not take a picture, you will not be able to see the effect after you take the picture :-).
Let's take a look at the use of the TPWMGROUP class to achieve the same effect as above, but the code should be written as follows:
The amount of code is increased a lot, but it is more flexible to use and determined based on your own needs.
If you are interested, try motor control or implement a breathing lamp.
Download the classa20class library at https://github.com/tjcfeng/classa20.