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=800
VMAX=10 #每秒最大圈数
PLMAX=int(1000*1000/XIFEN/VMAX) #频率,x次每S
PLMIN=int(1*1000*1000/XIFEN) #基底速度1s/圈
MC= 3750 #总脉冲数
TJJ=300 #加减速时间,单位MS
MCMCS=int((PLMIN-PLMAX)/TJJ) #每毫秒每次增加脉冲数
PLV=PLMIN #初速度初始化
mcqd = 0
jjjs=0
startmc=0
deltamc=0
def tiaosu(timer):
global stat1
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.text("timeNOW" + '-' + str(startmc), 0, 0)
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)
stat1=0
ppwf = 1000
def stt1():
global stat1
global startmc1
print(stat1)
while pushbutton1.value()==1: #制作PWM
startmc1 = time.ticks_ms()
if time.ticks_us()-startmc1>=ppwf :
startmc1=time.ticks_ms()
A4_step.value(not A4_step.value())
def key2(pushbutton2):
global stat1
time.sleep_ms(3)
if pushbutton2.value()==0:
time.sleep_ms(3)
stat1 = not stat1
stt1()
'''
def key1(pushbutton1):
time.sleep_ms(3)
if pushbutton1.value()==0:
time.sleep_ms(3)
mc_c()
def key2(pushbutton2):
global stat1
time.sleep_ms(3)
if pushbutton2.value()==0:
time.sleep_ms(3)
stat1 = not stat1
stt1()
'''
#pushbutton1.irq(key1,Pin.IRQ_FALLING)
pushbutton2.irq(key2,Pin.IRQ_FALLING)
timer.init(period= 500, mode=machine.Timer.PERIODIC, callback=tiaosu)