Python program example for writing K210 to control stepper motor

 Today I happened to see the pulse chapter of K210, so I took out the stepper motor to do a small experiment by the way, so as to consolidate the knowledge I learned. The following is the relevant introduction of K210 about pulse:

Constructor
machine.PWM(tim, freq, duty, pin, enable=True)
The PWM object is under the machine module
         [tim] K210’s PWM depends on the timer to generate waveforms
         [freq] PWM frequency
         [duty] PWM duty cycle
         [ pin] PWM output pin
         [enable] Whether to generate waveform immediately after building the object, the default is True.
Usage:
        PWM.freq(freq) sets the frequency. Returns the current frequency value without passing parameters.
        PWM.duty(duty) Set the duty cycle. Returns the current duty cycle value without passing parameters. [0-100] means duty ratio percentage
        PWM.enable() Enable PWM output.
        PWM.disable() Pauses PWM output.
        PWM.deinit() Unregister PWM    

 The following is a wiring diagram of the stepper motor:

 Then write the code for K210 as follows:

from machine import Timer, PWM   #  导入计时器模块,脉冲模块

import time      # 导入时间模块

tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM) # 创建定时器对象(定时器0,通道0,脉冲模式)
motor = PWM(tim, freq=1, duty=50, pin=10)  # 创建电机对象(定时器tim,频率=1,占空比=50%, IO口为外部10口 )

# 设置电机对象的函数(传参(频率值))
def s(x):
    motor.freq(x)    #  设置频率


        
s(4000)  #  调用函数, 此值越大步进电机的转速越快,此值越小,步进电机的噪音越大,转速越低。
       

Then power on the test, perfect control. Code can be eaten directly. If it is of any help to you, I hope to leave your little love! Thank you!

Guess you like

Origin blog.csdn.net/xingyuncao520025/article/details/130471773