from machine import Pin, PWM, SoftI2C
import time
import machine 
from ssd1306 import SSD1306_I2C  

i2c = SoftI2C(sda = Pin(12), scl = Pin(13)) 
oled = SSD1306_I2C(128, 64, i2c, addr=0x3C)
timer = machine.Timer(0)
 
pushbutton1=Pin(5,Pin.IN,Pin.PULL_UP)
pushbutton2=Pin(18,Pin.IN,Pin.PULL_UP)
A4_step = Pin(4,Pin.OUT)
A4_dir = Pin(2,Pin.OUT)


XIFEN=3200
VMAX=10  #每秒最大圈数
PLMAX=int(1000*1000/XIFEN/VMAX)  #频率,x次每S
PLMIN=int(10*1000*1000/XIFEN)  #基底速度10s/圈
MC= 15000  #总脉冲数
TJJ=300   #加减速时间,单位MS
MCMCS=int((PLMIN-PLMAX)/TJJ)  #每毫秒每次增加脉冲数
PLV=PLMIN  #初速度初始化
mcqd = 0
jjjs=0
deltamc=0
def tiaosu(timer):
    global PLV 
    global startmc
    global deltamc
    oled.fill(0)
    oled.text("PLMIN" + '-' + str(PLMIN), 0, 10)
    oled.text("jjjs" + '-' + str(jjjs), 0, 20)
    oled.text("time" + '-' + str(deltamc), 0, 30)
    oled.text("plv" + '-' + str(PLV), 0, 40)
    oled.text("PLMAX" + '-' + str(PLMAX), 0, 50)
    oled.show()


def mc_c(): 
    global PLMAX
    global PLMIN
    global PLV
    global mcqd  
    global MCMCS 
    global jjjs
    global syjs
    global bjs
    global bbb
    global startmc
    global deltamc
    bjs=0
    jjjs=0
    PLV=PLMIN
    mcqd=1
    startmc = time.ticks_ms()   
    for i in range(1, MC+1 , 1): 
        if  pushbutton2.value()==0:
            print("break")
            break        
        elif pushbutton2.value()==1:
            syjs=MC-jjjs
            if PLV > PLMAX and i< MC/2 :#加速阶段
                PLV=PLV-MCMCS  
                jjjs=jjjs+1  
            elif i==syjs+1 or bjs==1:
                bjs=1
                PLV=PLV+MCMCS
            A4_step.value(1)
            time.sleep_us(PLV)
            A4_step.value(0)                
    deltamc=time.ticks_diff(time.ticks_ms(),startmc)
    bjs=0
    mcqd=0
    print(PLMIN)
    print(PLMAX)

def key1(pushbutton1):
    time.sleep_ms(3)        
    if pushbutton1.value()==0: 
        time.sleep_ms(3)   
        mc_c()




pushbutton1.irq(key1,Pin.IRQ_FALLING)         

timer.init(period= 500, mode=machine.Timer.PERIODIC, callback=tiaosu)
Loading
ssd1306
A4988