1.获取板载按键引脚名称
>>> help(pyb.Pin.board) object <class 'board'> is of type type ... SW -- Pin(Pin.cpu.B3, mode=Pin.IN, pull=Pin.PULL_UP) ...
2.获取Switch类里面的方法
>>> help(pyb.Switch) object <class 'Switch'> is of type type value -- <function> callback -- <function>
3.基本用法
>>> switch = pyb.Switch() #构造一个Switch的对象 >>> switch() #等价于switch.value() False >>> switch() True >>> switch.value() #获取按键当前状态,True表示按下,False表示没有按下 False >>> switch.value() True >>> switch.callback(lambda:pyb.LED(1).toggle()) #匿名函数 >>> def toggle_led(): #定义用于回调的函数 ... if switch.value(): ... pyb.delay(10) ... if switch.value(): ... pyb.LED(2).toggle() ... while switch.value():pass ... ... ... >>> switch.callback(toggle_led) #此处用于回调的函数里面是没有括号的
获取Pin类里面的方法和相关的类
>>> help(pyb.Pin) object <class 'Pin'> is of type type init -- <function> value -- <function> off -- <function> on -- <function> irq -- <function> low -- <function> high -- <function> name -- <function> names -- <function> af_list -- <function> port -- <function> pin -- <function> gpio -- <function> mode -- <function> pull -- <function> af -- <function> mapper -- <classmethod> dict -- <classmethod> debug -- <classmethod> board -- <class 'board'> cpu -- <class 'cpu'>
>>> x1 = pyb.Pin('X1') >>> x1 Pin(Pin.cpu.A0, mode=Pin.IN) >>> x1.init(pyb.Pin.OUT) >>> x1 Pin(Pin.cpu.A0, mode=Pin.OUT) >>>
>>> x1.init(pyb.Pin.OUT_PP) #设置为推挽方式输出 >>> x1.init(pyb.Pin.OUT_OD) #设置为开漏方式输出 >>> x1.init(pyb.Pin.IN) #设置为输入 >>> x1.init(pyb.Pin.IN,pull=pyb.Pin.PULL_UP) #设置为输入并使用内部上拉电阻>>> x1.init(pyb.Pin.IN,pull=pyb.Pin.PULL_DOWN) #设置为输入并使用内部下拉电阻
>>> from pyb import Pin >>> x1 = Pin('A0') >>> x1 Pin(Pin.cpu.A0, mode=Pin.IN) >>> x1.init(Pin.OUT_PP) >>> x1 Pin(Pin.cpu.A0, mode=Pin.OUT) >>> x1.init(Pin.IN) >>> x1 Pin(Pin.cpu.A0, mode=Pin.IN) >>> x1.init(Pin.IN,Pin.PULL_UP) >>> x1 Pin(Pin.cpu.A0, mode=Pin.IN, pull=Pin.PULL_UP) >>>
>>> from pyb import Pin >>> x1 = Pin(Pin.cpu.A0,Pin.OUT_PP) >>> x1 Pin(Pin.cpu.A0, mode=Pin.OUT) >>> x1 = Pin(Pin.cpu.A0,Pin.IN,pull=Pin.PULL_UP) >>> x1 Pin(Pin.cpu.A0, mode=Pin.IN, pull=Pin.PULL_UP)
对于GPIO的输出值(1/0)我们可以控制其高低电平,而输入是不可以的(除非你设置它的内部上拉电阻和下拉电阻):
>>> x1 = Pin('X1',Pin.IN) >>> x1 Pin(Pin.cpu.A0, mode=Pin.IN) >>> x1.high() >>> x1 Pin(Pin.cpu.A0, mode=Pin.IN) >>> x1.value() 0 >>> x1.init(Pin.OUT) >>> x1 Pin(Pin.cpu.A0, mode=Pin.OUT) >>> x1.high() >>> x1.value() 1
GPIO的输入测试部分
>>> x5 = Pin('X5',Pin.IN) >>> x5.value() 0 >>> x5.value() 1 >>> x5.value() 0 >>> x5.value() 0 >>> x5.value() 0 >>> x5.value() 0 >>> x5.value() 0 >>> x5.value() 0 >>> x5.value() 1 >>> x5.value() 0 >>> x5.init(Pin.IN,pull=Pin.PULL_UP) >>> x5() 1 >>> x5() 1 >>> x5() 1 >>> x5() 1 >>> x5() 1 >>> x5() 1 >>> x5() 1 >>> x5() 1 >>> x5.init(Pin.IN,pull=Pin.PULL_DOWN) >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5.init(Pin.IN,pull=Pin.PULL_NONE) >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 1 >>> x5() 0 >>> x5() 1 >>> x5() 0
GPIO的输出测试部分
>>> x5 = Pin('X5',Pin.OUT) >>> x5 Pin(Pin.cpu.A4, mode=Pin.OUT) >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5.init(Pin.OUT_PP) >>> x5 Pin(Pin.cpu.A4, mode=Pin.OUT) >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5.high() >>> x5() 1 >>> x5.low() >>> x5() 0 >>> x5.on() >>> x5() 1 >>> x5.off() >>> x5() 0 >>> x5.init(Pin.OUT_OD) >>> x5 Pin(Pin.cpu.A4, mode=Pin.OPEN_DRAIN) >>> x5() 0 >>> x5.high() >>> x5() 1 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 0 >>> x5() 1 >>> x5() 0 >>> x5() 0 >>> x5() 1 >>> x5() 0 >>> x5() 0 >>> x5() 0
Pin.irq(handler=None, trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING), priority=1, wake=None, hard=False)
:https://www.cnblogs.com/iBoundary/p/11508565.html
对于GPIO的引脚电平状态我们可以使用value()或者直接x1()读取
af,当mode是Pin.AF_PP或Pin.AF_OD时,可以选择第二功能索引或名称
af_list()
>>> pyb.Pin.af_list(pyb.Pin('X1')) [Pin.AF1_TIM2, Pin.AF1_TIM2, Pin.AF2_TIM5, Pin.AF3_TIM8, Pin.AF7_USART2, Pin.AF8_UART4]
af()
>>> pyb.Pin.af_list(pyb.Pin('A11')) [Pin.AF1_TIM1, Pin.AF7_USART1, Pin.AF9_CAN1] >>> pyb.Pin.af(pyb.Pin('A11')) 10
SC
:https://www.st.com/resource/en/datasheet/stm32f405rg.pdf
gpio()
mode()
name()
names()
pin()
port()
>>> sw.port() 1
pull()
来源:博客园
作者:iBoundary
链接:https://www.cnblogs.com/iBoundary/p/11504172.html