Extend:bitⅡ
来自Labplus盛思维基百科
Tangliufeng(讨论 | 贡献)2018年1月16日 (二) 16:15的版本
概述
microbit拓展板,支持4路电机驱动、4路舵机驱动和3颗级联全彩RGB灯珠ws2812。内置声音传感器、光线传感器、无源蜂鸣器。
技术参数
- 支持4路电机驱动
- 支持4路舵机驱动
- 拓展microbit引脚p0/1/2/5/8/11、I2C、UART
- 级联3颗全彩RGB灯珠ws2812
- 内置光线、声音传感器、无源蜂鸣器
- 最大负载电流:1.2A
使用教程
引脚定义
micro:bit引脚 | |
---|---|
无源蜂鸣器 | P0 |
光线传感器 | P1 |
声音传感器 | P2 |
全彩RGB灯 | P16 |
注意:如要拓展使用P0/P1/P2,要取下跳线帽断开蜂鸣器、光线、声音传感器与microbit连接!
Python示例
示例测试各模块功能:
1.上电蜂鸣器发出1KHz,持续1秒声音
2.RGB灯依次红绿蓝显示
3.串口打印输出声音和光线采样数据
4.4路电机驱动正反转和4路舵机180度摆动,间隔1秒
''' extend bitⅡ driver how to user this driver 1. user initPCA9685() init driver. 2. driver device: a. servo user servo() b. motor use motorRun() c. step motor use setStepper() ''' from microbit import * import music import neopixel PCA9685_ADDRESS = 0x40 #device address #define register MODE1 = 0x00 MODE2 = 0x01 SUBADR1 = 0x02 SUBADR2 = 0x03 SUBADR3 = 0x04 PRESCALE = 0xFE LED0_ON_L = 0x06 LED0_ON_H = 0x07 LED0_OFF_L = 0x08 LED0_OFF_H = 0x09 ALL_LED_ON_L = 0xFA ALL_LED_ON_H = 0xFB ALL_LED_OFF_L = 0xFC ALL_LED_OFF_H = 0xFD STP_CHA_L = 2047 STP_CHA_H = 4095 STP_CHB_L = 1 STP_CHB_H = 2047 STP_CHC_L = 1023 STP_CHC_H = 3071 STP_CHD_L = 3071 STP_CHD_H = 1023 ''' # Servos define S1 = 0x01 S2 = 0x02 S3 = 0x03 S4 = 0x04 # Motors define M2 = 0x1 M1 = 0x2 M4 = 0x3 M3 = 0x4 ''' # Steppers define STP_M1 = 0x1 STP_M2 = 0x2 FORWARD = 0 REVERSAL = 1 # device init def initPCA9685(): i2c.write(PCA9685_ADDRESS, bytearray([MODE1, 0x00])) setFreq(50) for idx in range(0, 16, 1): setPwm(idx, 0 ,0) #initialized = true # function: motorRun() # @param motor: M1A, M1B, M2A, M2B # @param speed1: -255 - 255 0 is stop def MotorRun(Motors, speed): speed = speed * 16 # map 255 to 4096 if (speed >= 4096): speed = 4095 if (speed <= -4096): speed = -4095 if (Motors <= 4 and Motors > 0): pp = (Motors - 1) * 2 pn = (Motors - 1) * 2 + 1 if (speed >= 0): setPwm(pp, 0, speed) setPwm(pn, 0, 0) else : setPwm(pp, 0, 0) setPwm(pn, 0, -speed) # function: motorRun() # @param Servos: S1, S2, S3, S4 # @param degree: 0-180 def Servo(Servos, degree): # 50hz: 20,000 us v_us = (degree * 1800 / 180 + 600) # 0.6 ~ 2.4ms value = int(v_us * 4096 / 20000) setPwm(Servos + 7, 0, value) #set frequency def setFreq(freq): #Constrain the frequency prescaleval = int(25000000/(4096*freq)) - 1 #prescale = prescaleval; #Math.Floor(prescaleval + 0.5) i2c.write(PCA9685_ADDRESS, bytearray([MODE1])) oldmode = i2c.read(PCA9685_ADDRESS, 1) newmode = (oldmode[0] & 0x7F) | 0x10 # sleep i2c.write(PCA9685_ADDRESS, bytearray([MODE1, newmode])) # go to sleep i2c.write(PCA9685_ADDRESS, bytearray([PRESCALE, prescaleval])) # set the prescaler i2c.write(PCA9685_ADDRESS, bytearray([MODE1, oldmode[0]])) sleep(4) i2c.write(PCA9685_ADDRESS, bytearray([MODE1, oldmode[0] | 0xa1])) # function: setPwm() def setPwm(channel, on, off): if (channel >= 0 and channel <= 15): buf = bytearray([LED0_ON_L + 4 * channel, on & 0xff, (on >> 8) & 0xff, off & 0xff, (off >> 8) & 0xff]) i2c.write(PCA9685_ADDRESS, buf) # function: setStepper() # for 2 phase 4 line step motor # @param stpMotors : STP_M1, STP_M2 # @param dir: FORWARD REVERSAL # @param speed: 25 - 200 def setStepper(stpMotors, dir, speed): spd = speed #print(spd) setFreq(spd) if (stpMotors == 1): #step motor 1 if (dir): # forward rotating setPwm(0, STP_CHA_L, STP_CHA_H) setPwm(1, STP_CHB_L, STP_CHB_H) setPwm(2, STP_CHC_L, STP_CHC_H) setPwm(3, STP_CHD_L, STP_CHD_H) else: # reversal rotating setPwm(3, STP_CHA_L, STP_CHA_H) setPwm(2, STP_CHB_L, STP_CHB_H) setPwm(1, STP_CHC_L, STP_CHC_H) setPwm(0, STP_CHD_L, STP_CHD_H) elif (stpMotors == 2): #step motor 2 if (dir): setPwm(4, STP_CHA_L, STP_CHA_H) setPwm(5, STP_CHB_L, STP_CHB_H) setPwm(6, STP_CHC_L, STP_CHC_H) setPwm(7, STP_CHD_L, STP_CHD_H) else: setPwm(7, STP_CHA_L, STP_CHA_H) setPwm(6, STP_CHB_L, STP_CHB_H) setPwm(5, STP_CHC_L, STP_CHC_H) setPwm(4, STP_CHD_L, STP_CHD_H) #test code initPCA9685() # setStepper(STP_M1, REVERSAL, 80) # test step motor np = neopixel.NeoPixel(pin16, 3) music.pitch(1000,1000) for i in range(0,3): np[i]=(255,0,0) np.show() sleep(1000) for i in range(0,3): np[i]=(0,255,0) np.show() sleep(1000) for i in range(0,3): np[i]=(0,0,255) np.show() sleep(1000) np.clear() t =running_time() flag=0 while True: print('light:',pin1.read_analog(),' Sound',pin2.read_analog()) sleep(20) if (running_time() - t) > 2000: t = running_time() flag= not flag if flag: MotorRun(1, 255) MotorRun(2, 255) MotorRun(3, 255) MotorRun(4, 255) #servo test for i in range(1, 5, 1): Servo(i, 180) pin5.write_digital(1) pin8.write_digital(1) pin11.write_digital(1) else: MotorRun(1, -255) MotorRun(2, -255) MotorRun(3, -255) MotorRun(4, -255) for i in range(1, 5, 1): Servo(i, 0) pin5.write_digital(0) pin8.write_digital(0) pin11.write_digital(0)