# 在这里添加Python代码 from microbit import *
Pin_All=[pin5,pin8,pin11,pin12]
#转速(ms) 数值越大转速越慢 最小值1.8ms speed=5
STEPER_ROUND=512 #转动一圈(360度)的周期 ANGLE_PER_ROUND=STEPER_ROUND/360 #转动1度的周期
def SteperWriteData(data): count=0 for i in data: Pin_All[count].write_digital(i) count+=1 def SteperFrontTurn(): global speed
SteperWriteData([1,1,0,0]) sleep(speed)
SteperWriteData([0,1,1,0]) sleep(speed)
SteperWriteData([0,0,1,1]) sleep(speed)
SteperWriteData([1,0,0,1]) sleep(speed)
def SteperBackTurn(): global speed
SteperWriteData([1,1,0,0]) sleep(speed)
SteperWriteData([1,0,0,1]) sleep(speed)
SteperWriteData([0,0,1,1]) sleep(speed)
SteperWriteData([0,1,1,0]) sleep(speed)
def SteperStop(): SteperWriteData([0,0,0,0])
def SteperRun(angle): global ANGLE_PER_ROUND
val=ANGLE_PER_ROUND*abs(angle) if(angle>0): for i in range(0,val): SteperFrontTurn() else: for i in range(0,val): SteperBackTurn()
SteperStop()
if __name__=='__main__': while True: SteperRun(360) |