利用中断方式控制树梅派的GPIO
来源:互联网 发布:全民奇迹挂机软件 编辑:程序博客网 时间:2024/06/08 07:37
Using Interrupt Driven GPIO
In the previous post, a program keeps executing a while loop checking to see if a button has been pressed. This is called polling, and it's not very efficient because the program can't do anything else while waiting for the button to be pressed. It would be better if the program could do something else while it was waiting.
This can be achieved using interrupt driven IO. An interrupt is a signal generated by a peripheral device that lets the processor know that an event has happened. The RPi GPIO library makes this easy. You simply need to use the add_event_detect function to specify what type of event you're waiting for, and add_event_callback to specify what should happen when the event occurs.
In this exercise I used the same circuit as in the previous exercise, and I used the code below:
#!/usr/bin/env pythonimport timeimport RPi.GPIO as GPIO# handle the button eventdef buttonEventHandler (pin): print "handling button event" # turn the green LED on GPIO.output(25,True) time.sleep(1) # turn the green LED off GPIO.output(25,False)# main functiondef main(): # tell the GPIO module that we want to use # the chip's pin numbering scheme GPIO.setmode(GPIO.BCM) # setup pin 23 as an input # and set up pins 24 and 25 as outputs GPIO.setup(23,GPIO.IN) GPIO.setup(24,GPIO.OUT) GPIO.setup(25,GPIO.OUT) # tell the GPIO library to look out for an # event on pin 23 and deal with it by calling # the buttonEventHandler function GPIO.add_event_detect(23,GPIO.FALLING) GPIO.add_event_callback(23,buttonEventHandler,100) # turn off both LEDs GPIO.output(25,False) GPIO.output(24,True) # make the red LED flash while True: GPIO.output(24,True) time.sleep(1) GPIO.output(24,False) time.sleep(1) GPIO.cleanup()if __name__=="__main__": main()
This program tells the GPIO library to look out for an event on pin 23 of the GPIO header. The event to look out for is a falling edge, where the voltage goes from 1 to 0. This example detects the GPIO.FALLING event, but it's also possible to detect GPIO.RISING and GPIO.BOTH. The function named buttonEventHandler is registered as the callback for the event.
The program then goes round the while loop making the red LED blink. When the button is pressed, buttonEventHandler is called, and the green LED is turned on for one second. The event is handled by a different thread, so while the green button is on, the red light continues to blink uninterrupted.
If your program doesn't need to do anything while its waiting for an event, you can call GPIO.wait_for_edge() which will make your program sleep until an event wakes it up. If your program no longer needs to wait for an event, you can unregister it by calling GPIO.remove_event_detect().
See also: https://code.google.com/p/raspberry-gpio-python/wiki/Inputs
- 利用中断方式控制树梅派的GPIO
- 利用GPIO的命令控制树梅派的GPIO
- 利用DM642的定时器0通过查询方式控制GPIO口
- 三种方式控制NanoPi2的GPIO
- sysfs方式控制GPIO
- at91sam926X的GPIO中断
- 用中断的方式都控制LED
- 利用中断方式通讯的测试程序
- 通过sysfs方式控制GPIO
- 高通 linux 3.0 配置及控制GPIO的方式
- Omap3530 的GPIO中断设置
- Omap3530 的GPIO中断设置
- DSP28335的GPIO外设中断
- iMX257的GPIO中断配置
- s3c2440的GPIO控制
- BeagleBone的GPIO控制
- 树梅派 GPIO控制
- gpio的工作方式
- (8)const 限定符
- 数据结构实验之二叉树二:遍历二叉树
- Matlab产生特定范围的随机数矩阵
- ubuntu 安装flash plaer
- hdoj2036 改革春风吹满地——叉积
- 利用中断方式控制树梅派的GPIO
- Java多线程之线程安全与同步实例
- 特殊压缩的解压方法
- 运算符重载
- cocos2dx-3.12 解决android不能监听键盘高度事件
- Linux上使用github简介
- request转发与response重定向(面试题)
- Java中的方法的重写与重载
- CODEVS 1017乘积最大