Salted fish ZTMR example-key usage

Salted fish ZTMR example-key usage

On the pyboard, there is a user button. MicroPython has pre-defined the key class, the key can be used like this:
define the key

sw = pyb.Switch() 

Read key status

sw() 

Define key callback function

sw.callback(lambda:pyb.LED(1).toggle()) 

Disable key callback function

sw.callback(None) 

More complex use of callback function (flip LED3 after pressing the button) click the test button to see the effect

import pyb

sw = pyb.Switch() 
pyb.LED(3).on()

def f():
 pyb.LED(4).toggle()

sw.callback(f)

Of course, it can also be used directly as GPIO:

import pyb from Pin

sw=Pin("X17", Pin.IN, Pin.PULL_UP)
sw()

The on-board interrupt button can be understood as an external button. The usage is as follows
.
Call the external interrupt module

from pyb import ExtInt

Define interrupt

from pyb import Pin, ExtInt

pyb.ExtInt(pin, mode, pull, callback)    #定义中断 
'''
pin 
	中断使用的GPIO,可以是pin对象或者已经定义GPIO的名称  
mode 
	ExtInt.IRQ_RISING                上升沿 
	ExtInt.IRQ_FALLING               下降沿 
	ExtInt.IRQ_RISING_FALLING        上升下降沿 
pull 
	pyb.Pin.PULL_NONE                无 
	pyb.Pin.PULL_UP                  上拉电阻 
	pyb.Pin.PULL_DOWN                下拉电阻
callback
	回调函数 
'''

Rising edge, falling edge: The rising edge is equal to the instant when it is connected to give an instant signal, which is equivalent to the power-on signal. The falling edge is equivalent to a momentary signal at the moment of disconnection, which is equivalent to a power-off signal.
Insert picture description here
The rising edge pulse is equivalent to you pressing the switch. The moment the switch is turned on, there is no input signal afterwards. The falling edge pulse is the moment the switch is released and the switch is off. No signal is input.

Pull-up and pull-down resistors: Pull-up is to clamp an uncertain signal to a high level through a resistor, and the resistor also acts as a current limiter. The same is true for pull-down, which also clamps the uncertain signal to a low level through a resistor.
Pull-up is the input current to the device, and pull-down is the output current; the strength is only the resistance of the pull-up resistor, there is no strict distinction; for non-collector (or drain) open-circuit output circuit (such as ordinary gate circuit) provides current The ability to sum voltage is limited, and the function of the pull-up resistor is mainly to output the current channel for the open collector output type circuit.

callback = lambda e: print("0")          #定义按键回调
function Explanation
extint.disable () Disable interrupt
extint.enable () Allow interrupt
extint.line () Returns the line number of the interrupt map
extint.swint () Software triggered interrupt
ExtInt.regs () Interrupt register value

Case 1 The button outputs the defined characters in the serial tool

# main.py -- put your code here!


from pyb import Pin    #引入引脚
from pyb import ExtInt #引入中断

callback = lambda e: print("xianyu")   #定义按键回调触发输出"xianyu"


sw2 = ExtInt(Pin('X18'), ExtInt.IRQ_FALLING, Pin.PULL_UP, callback) #点击中断按键返回xianyu

Insert picture description here
Example 2 Button control LED

# main.py -- put your code here!


from pyb import Pin    #引入引脚
from pyb import ExtInt,LED #引入中断

#控制LED状态  toggle()翻转LED,状态是开,变为关;原来是关,变为开。
callback = lambda e: LED(2).toggle()    


#点击按钮返回
sw2  = ExtInt(Pin('X18'), ExtInt.IRQ_FALLING, Pin.PULL_UP, callback) 
Published 166 original articles · 22 praises · 10,000+ views

Guess you like

Origin blog.csdn.net/weixin_45020839/article/details/105436407