def write(self, port, level): if port not in self.io_list: _gpio_log.info("Port {} does not exist".format(port)) return 0 pin = Pin(port, Pin.OUT, Pin.PULL_DISABLE, 0) if level: _gpio_log.info("Write 1 to port {}".format(port)) pin.write(1) else: _gpio_log.info("Write 0 to port {}".format(port)) pin.write(0)
def write_extension(self, port, direction, pull_mode, level): if port not in self.io_list: _gpio_log.info("Port {} does not exist".format(port)) return 0 pin = Pin(port, direction, pull_mode, level) if level: _gpio_log.info("Write 1 to port {}".format(port)) pin.write(1) else: _gpio_log.info("Write 0 to port {}".format(port)) pin.write(0)
GPIO3 – 引脚号178 GPIO4 – 引脚号199 GPIO5 – 引脚号204 EC600SCN平台引脚对应关系如下: GPIO1 – 引脚号10 GPIO2 – 引脚号11 GPIO3 – 引脚号12 GPIO4 – 引脚号13 GPIO5 – 引脚号14 GPIO6 – 引脚号15 GPIO7 – 引脚号16 GPIO8 – 引脚号39 GPIO9 – 引脚号40 GPIO10 – 引脚号48 * 参数2:direction IN – 输入模式 OUT – 输出模式 * 参数3:pull PULL_DISABLE – 禁用模式 PULL_PU – 上拉模式 PULL_PD – 下拉模式 * 参数4:level 0 设置引脚为低电平 1 设置引脚为高电平 ''' gpio1 = Pin(Pin.GPIO1, Pin.OUT, Pin.PULL_DISABLE, 0) gpio1.write(1) # 设置gpio1 输出 gpio1.read() # 获取gpio的当前高低状态 # >>> 1
from machine import Pin gpio1 = Pin(GPIO1, Pin.OUT, Pin.PULL_DISABLE, 0) import utime i = 1 # GPIOn 整型。引脚号。 # 引脚对应关系如下: # GPIO1–引脚号 22 # GPIO2–引脚号 23 # GPIO3–引脚号 178 # GPIO4–引脚号 199 # GPIO5–引脚号 204 # direction 整型。 # IN 输入模式 # OUT 输出模式 # pullMode 整型。 # PULL_DISABLE 浮空模式 # PULL_PU 上拉模式 # PULL_PD 下拉模式 # level 整型。引脚电平。 # 0 设置引脚为低电平 # 1 设置引脚为高电平 while i < 100: gpio1.write(0) utime.sleep(1) gpio1.write(1) utime.sleep(1) i += 1
from machine import Pin gpio1 = Pin(Pin.GPIO1, Pin.OUT, Pin.PULL_DISABLE, 0) ret = gpio1.write(1) print(ret) ret = gpio1.read() print(ret) ret = gpio1.write(0) print(ret) ret = gpio1.read() print(ret)
* 参数2:direction IN – 输入模式 OUT – 输出模式 * 参数3:pull PULL_DISABLE – 禁用模式 PULL_PU – 上拉模式 PULL_PD – 下拉模式 * 参数4:level 0 设置引脚为低电平 1 设置引脚为高电平 ''' gpio1 = Pin(Pin.GPIO1, Pin.OUT, Pin.PULL_DISABLE, 0) if __name__ == '__main__': ''' 手动运行本例程时,可以去掉该延时,如果将例程文件名改为main.py,希望开机自动运行时,需要加上该延时, 否则无法从CDC口看到下面的 poweron_print_once() 中打印的信息 ''' utime.sleep(5) checknet.poweron_print_once() ''' 如果用户程序包含网络相关代码,必须执行 wait_network_connected() 等待网络就绪(拨号成功); 如果是网络无关代码,可以屏蔽 wait_network_connected() 【本例程可以屏蔽下面这一行!】 ''' # checknet.wait_network_connected() gpio1.write(1) # 设置 gpio1 输出高电平 val = gpio1.read() # 获取 gpio1 的当前高低状态 print('val = {}'.format(val))
class ebf_smd4805(): dev_log = None # 步进电机的参数 sm_para_step = None # 步进角度 # 控制器的参数 env_pin = None # 使能引脚 dir_pin = None # 方向引脚 pul_pwm = None # 脉冲输出引脚 ctrl_divstep = None # 细分参数,具体请参考控制器手册 def init(self, step, divstep): self.dev_log = log.getLogger("ebf_smd4805") self.env_pin = Pin(Pin.GPIO7, Pin.OUT, Pin.PULL_DISABLE, 0) self.dir_pin = Pin(Pin.GPIO6, Pin.OUT, Pin.PULL_DISABLE, 0) # 配置电机的参数 self.sm_para_step = step # 配置控制器的参数 self.ctrl_divstep = divstep def reset(self): self.env_pin.write(DISABLE_MOTOR) self.dir_pin.write(DIR_ANTI_CLOCKWISE) if self.pul_pwm is not None: self.pul_pwm.close() # 根据频率 初始化PWM def outputpwm(self, HZ, duty_cycle): # 将HZ 转化为 us 级别 cycleTime = int(1000000/HZ) highTime = int(cycleTime * duty_cycle) return highTime, cycleTime # 根据速度,设置PWM的输出 def enable_pwm(self, speed): # 1. 首先根据步进电机的步进角度,计算旋转一圈需要多少个脉冲 Count_pulse = int(360/self.sm_para_step) self.dev_log.debug("sm motor step as {0}".format(Count_pulse)) # 2. 根据控制器的细分参数,计算控制器控制步进电机旋转一圈,需要多少的脉冲 Count_pulse = int(Count_pulse * self.ctrl_divstep) # 3. 最后计算出1秒旋转speed圈,需要多少个脉冲 , 换句话说 就是频率 Count_pulse = int(Count_pulse * speed) # 4. 初始化PWM, 默认占空比%50 highTime, cycleTime = self.outputpwm(Count_pulse, 0.1) self.dev_log.debug( """config frequency is {0}HZ,cycleTime {1}us, hightime {2}us""" .format(Count_pulse, cycleTime, highTime)) self.pul_pwm = PWM(PWM.PWM2, PWM.ABOVE_10US, int(highTime), int(cycleTime)) self.pul_pwm.open() pass def disable_pwm(self): self.pul_pwm.close() pass # speed 为速度, 每秒多少圈 # Duration 为持续时间, ms # dir 表示方向 def run(self, speed, Duration, dir=DIR_CLOCKWISE): self.dir_pin.write(dir) self.dev_log.info( "Configure the motor to rotate {0} revolutions per second".format(speed)) self.enable_pwm(speed) self.env_pin.write(1) # delay for i in range(int(Duration * 4)): delay_250us() self.env_pin.write(0) self.reset() pass
''' BEARPI LED QuecPython demo ''' from machine import Pin LED = Pin(Pin.GPIO4, Pin.OUT) LED(1) LED.write(0)
class GetDeviceInfo(): ''' 温湿度,GPS数据采集 ''' def __init__(self): self.uart = UART(UART.UART2, 9600, 8, 0, 1, 0) self.w_data = bytearray({0, 0}) self.r_data = bytearray(2) self.I2C_SLAVE_ADDR_HDC1080 = 0x40 self.HDC1080_TEMPERATURE = 0x00 self.HDC1080_HUMIDITY = 0x01 self.HDC1080_CONFIGURATION = 0x02 self.HDC1080_MANUFACTURER_ID = 0xFE self.HDC1080_DEVICE_ID = 0xFF self.I2C_SLAVE_ADDR_BH1750 = 0x23 self.BH1750_POWER_ON = 0x01 self.BH1750_H_RE_MODE = 0x10 self.gpio5 = Pin(Pin.GPIO5, Pin.OUT, Pin.PULL_DISABLE, 1) self.gpio5.write(0) self.i2c_obj = I2C(I2C.I2C0, I2C.STANDARD_MODE) reg_addr = bytearray({0x02, 0}) self.i2c_obj.write(self.I2C_SLAVE_ADDR_HDC1080, reg_addr, 1, self.w_data, 2) utime.sleep_ms(10) def middle(self): global TEMPERATUR global HUMIDITY global LIGHT global GPSMSG while True: reg_addr = bytearray({0xFE, 0xFE}) self.i2c_obj.read(self.I2C_SLAVE_ADDR_HDC1080, reg_addr, 1, self.r_data, 2, 0) utime.sleep_ms(10) reg_addr = bytearray({0xFF, 0xFF}) self.i2c_obj.read(self.I2C_SLAVE_ADDR_HDC1080, reg_addr, 1, self.r_data, 2, 0) utime.sleep_ms(10) reg_addr = bytearray({0x00, 0x00}) ret = self.i2c_obj.read(self.I2C_SLAVE_ADDR_HDC1080, reg_addr, 1, self.r_data, 2, 10) if ret == 0: TEMPERATUR = ((self.r_data[0] * 256 + self.r_data[1]) / (2 ** 16)) * 165 - 40 print('T', TEMPERATUR) utime.sleep_ms(10) reg_addr = bytearray({0x01, 0x01}) ret = self.i2c_obj.read(self.I2C_SLAVE_ADDR_HDC1080, reg_addr, 1, self.r_data, 2, 10) if ret == 0: HUMIDITY = ((self.r_data[0] * 256 + self.r_data[1]) / (2 ** 16)) * 100 print('H', HUMIDITY) reg_addr = bytearray({0x01, 0x01}) self.i2c_obj.write(self.I2C_SLAVE_ADDR_BH1750, reg_addr, 1, self.w_data, 0) utime.sleep_ms(10) reg_addr = bytearray({0x10, 0x10}) self.i2c_obj.write(self.I2C_SLAVE_ADDR_BH1750, reg_addr, 1, self.w_data, 0) utime.sleep_ms(200) reg_addr = bytearray({0x00, 0x00}) ret = self.i2c_obj.read(self.I2C_SLAVE_ADDR_BH1750, reg_addr, 0, self.r_data, 2, 0) if ret == 0: LIGHT = (self.r_data[0] * 256 + self.r_data[1]) / 1.2 print('L', LIGHT) len = self.uart.any() if len > 0: buf = self.uart.read(len) gps_data = buf.decode().strip("b") # print(gps_data) try: r = ure.search("GPRMC(.+?)M", gps_data) gps_msg = r.group(0).split(",") except Exception as e: continue print("GPS", gps_msg) utime.sleep(1) def run(self): _thread.start_new_thread(self.middle, ())