logo

openmv调用外部中断为什么没有反应?

2018-12-30 15:46
N
i56s
import sensor, image, time,pyb
from pyb import Pin, ExtInt
import micropython
micropython.alloc_emergency_exception_buf(100)

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

blue = pyb.LED(3)
green = pyb.LED(2)
stop_flag = 1
def start():
    stop_flag = 0
    blue.off()
    green.on()
    print('start')

def stop():
    blue.on()
    green.off()
    print('stop')

sta = ExtInt(Pin('P0'),ExtInt.IRQ_RISING,Pin.PULL_NONE, start())
sto = ExtInt(Pin('P1'),ExtInt.IRQ_RISING,Pin.PULL_NONE, stop())

while(True):
    clock.tick()
    img = sensor.snapshot()
N
i56s

我想实现触发P0的中断时绿灯亮,P1中断触发蓝灯亮,可是运行之后却没有反应?请问这是什么原因?

2018-12-30 16:11
I
网红小智智 星瞳科技

https://docs.singtown.com/micropython/zh/latest/openmvcam/openmvcam/quickref.html#id4

建议你先运行例子。

sta = ExtInt(Pin('P0'),ExtInt.IRQ_RISING,Pin.PULL_NONE, start())

里面的参数`回调函数`,应该是函数,也就是start,而不应该是函数的结果start(),就是不应该加括号。

而且回调函数要有参数

2018-12-30 16:22
N
i56s

@kidswong999 运行例子的效果是一直打印intr,还有不太懂例子中的e是什么意思

I
网红小智智 星瞳科技

https://docs.singtown.com/micropython/zh/latest/openmvcam/library/pyb.ExtInt.html#pyb-extint

2018-12-30 16:35
N
i56s

@kidswong999 新代码如下

from pyb import Pin, ExtInt

import sensor, image, time,pyb,micropython

micropython.alloc_emergency_exception_buf(100)

sensor.reset()

sensor.set_pixformat(sensor.RGB565)

sensor.set_framesize(sensor.QVGA)

sensor.skip_frames(time = 2000)

clock = time.clock()

def callback(line):

ExtInt.disable()

print("line =", line)

ExtInt.enable()

extint = pyb.ExtInt(Pin('P0'), pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, callback)

while(True):

clock.tick()

img = sensor.snapshot()

出现错误MemoryError: memory allocation failed, heap is locked

您看是什么原因啊?

N
i56s

@kidswong999 因为之前可能没有消抖措施,所以出现RuntimeError: maximum recursion depth exceeded的错误,然后我在回调函数中加了屏蔽中断和开启中断的指令,再运行就出现上述的效果了。

from pyb import Pin, ExtInt
import sensor, image, time,pyb,micropython
micropython.alloc_emergency_exception_buf(100)

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

clock = time.clock()

def callback(line):
    ExtInt.disable()
    print("line =", line)
    ExtInt.enable()
extint = pyb.ExtInt(Pin('P0'), pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, callback)
while(True):
    clock.tick()
    img = sensor.snapshot()
I
网红小智智 星瞳科技

建议你新开一个帖子提问,你的问题和一开始已经不一样了。