K210 Study Notes (4) - Output PWM

1. What is PWM?

PWM generally refers to pulse width modulation. Pulse width modulation is an analog control method that modulates the bias of the transistor base or MOS transistor gate according to the change of the corresponding load to realize the change of the transistor or MOS transistor conduction time, thereby realizing Changes in the output of a switching regulated power supply. This method can keep the output voltage of the power supply constant when the working conditions change, and it is a very effective technology for controlling the analog circuit by using the digital signal of the microprocessor. It is widely used in many fields from measurement and communication to power control and conversion.
PWM on MAIX BIT: Pulse width modulation module, PWM supported by hardware, can specify any pin (0 to 47 pins), each PWM depends on a timer, that is, when the timer is bound to the PWM function, it cannot Used as a normal timer. Because there are 3 timers, each timer has 4 channels, that is, a maximum of 12 channels of PWM waveforms can be generated at the same time

Two, use

1. Constructor

pwm = machine.PWM(tim, freq, duty, pin, enable=True)

Create a new PWM object with the specified parameters.
Parameter description:
tim: Each PWM depends on a timer to generate waveforms, so a timer object needs to be passed here, and the timer ID and channel number freq must be specified when the timer object must be
initialized : PWM waveform frequency
duty: PWM duty cycle, which refers to the percentage of the high level in the entire cycle, value: [0,100]
[pin]: PWM output pin. You can not set it, but use fm to manage pin mapping uniformly.
enable : Whether to start generating waveforms immediately, the default is True, and start generating PWM waveforms on the specified pins immediately after the object is generated

2. Method

1、

pwm.init(tim, freq, duty, pin, enable=True)

similar constructor

2、

pwm.freq(freq)

Get or set PWM frequency
Parameter description:
freq : PWM frequency, optional parameter, if no parameter is passed, the step setting will only return the current frequency value

3、

pwm.duty(duty)

Get or set PWM duty cycle
Parameter description:
duty : PWM duty cycle is optional, if no parameter is passed, the step setting will only return the current duty cycle value

4、

pwm.enable()

Enable PWM output to generate waveform immediately on the specified pin

5、

pwm.disable()

Disable PWM output, the specified pin no longer generates waveform

6、

pwm.deinit()

Log off the PWM hardware, release the occupied resources, and turn off the PWM clock

3. RGB breathing light

Breathing light, the LED light gradually brightens after power-on, and keeps for a few seconds when it reaches the brightest, and then gradually dims until it goes out. After turning off for a few seconds, it gradually turns from dark to bright, and this cycle continues. If the speed of the cycle is just in sync with human breathing, it is the legendary effect of the breathing light. This effect can be realized by PWM, and different degrees of brightness and darkness can be achieved by adjusting the duty cycle of PWM.

breathing light code

from machine import Timer,PWM
import time


io_led_blue = 14
tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM)
ch = PWM(tim, freq=500000, duty=50, pin=io_led_blue)
duty=0
dir = True
while True:
    if dir:
        duty += 10
    else:
        duty -= 10
    if duty>100:
        duty = 100
        dir = False
    elif duty<0:
        duty = 0
        dir = True
    time.sleep(0.1)
    ch.duty(duty)

After running the code online, we found that the LED light gradually brightened after the blue light was powered on, and when it reached the brightest point, it gradually dimmed until it went out.

breathing light

Analyze the code:
import Timer and PWM objects from the machine package.
Import the timer object
to define a variable io_led_blue with a value of 14, that is, Pin13/IO13. Which pin of the chip is connected to the specific LED pin, please refer to the previous development board introduction Look at the schematic diagram.
The timer ID is specified, which is timer 0 and channel 0.
By constantly changing the duty cycle to change the high level time, thereby changing the brightness of the lamp

4. PWM servo

Steering gear control:
The steering gear control generally requires a time base pulse of about 20ms, and the high level part of the pulse is generally the angle control pulse part in the range of 0.5ms~2.5ms. Taking the 180-degree angle servo as an example, the corresponding control relationship is as follows:
0.5ms------------0 degree;
1.0ms------------ 45 degrees;
1.5ms------------90 degrees;
2.0ms-----------135 degrees;
2.5ms-----------180 Spend;

the code

from machine import Timer,PWM
import time

tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM)
ch = PWM(tim, freq=50, duty=2.5, pin=15)
duty=2
ch.duty(duty)
ch.enable()
while True:
    ch.duty(duty)    
    for x in range(3):
        duty = 2.5 + x*6
        ch.duty(duty)        
        print("duty=",ch.duty())
        time.sleep(3)

The effect after running, because the conditions are limited, this mini board is used for power supply insert image description here
, the signal line of the servo is connected to PIN15, because the frequency division is between 0-100, and it is an integer, so some degrees cannot be transferred

PWM servo

Summarize

It is still necessary to learn how to use the PWM of MAIX BIT to lay the foundation for the follow-up of the gimbal.

Guess you like

Origin blog.csdn.net/Thousand_drive/article/details/123863399