上两章我们讲解了在树莓派上如何点亮一个LED灯,这一章我们讲解一下按键以及事件中断。 一、bcm2835 #include <bcm2835.h> #include <stdio.h> #define KEY 20 int main(int argc, char **argv) { if (!bcm2835_init())return 1; bcm2835_gpio_fsel(KEY, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_set_pud(KEY, BCM2835_GPIO_PUD_UP); printf("Key Test Program!!!!\n"); while (1) { if(bcm2835_gpio_lev(KEY) == 0) { printf ("KEY PRESS\n") ; while(bcm2835_gpio_lev(KEY) == 0) bcm2835_delay(100); } bcm2835_delay(100); } bcm2835_close(); return 0; } 编译并执行,按下按键会看到窗口显示”KEY PRESS”,按Ctrl+C结束程序。 gcc –Wall key.c –o key –lbcm2835 sudo ./key 注:(1)bcm2835_gpio_fsel(KEY, BCM2835_GPIO_FSEL_INPT);设置管脚为输入模式 (2)bcm2835_gpio_set_pud(KEY, BCM2835_GPIO_PUD_UP);设置为上拉模式 (3) bcm2835_gpio_lev(KEY);读取管脚状态 二、wiringPi #include <stdio.h> #include <wiringpi.h> char KEY = 28; int main() { if (wiringPiSetup() < 0)return 1 ; pinMode (KEY,INPUT); pullUpDnControl(KEY, PUD_UP); printf("Key Test Program!!!\n"); while(1) { if (digitalRead(KEY) == 0) { printf ("KEY PRESS\n") ; while(digitalRead(KEY) == 0) delay(100); } delay(100); } } 编译并执行,按下按键会看到窗口显示”KEY PRESS”,按Ctrl+C结束程序。 gcc –Wall key.c –o key –wiringPi sudo ./key 注:(1)pinMode (KEY,INPUT);设置管脚为输入模式 (2)pullUpDnControl(KEY, PUD_UP);设置为上拉模式 (3) digitalRead(KEY);读取管脚状态 通过中断的方式编程 /* Interrupt.c * you can build this like: * gcc -Wall Interrupt.c -o Interrupt -lwiringPi * sudo ./Interrupt */ #include <stdio.h> #include <wiringPi.h> #define button 28 char flag = 0; void myInterrupt() { flag ++; } int main() { if(wiringPiSetup() < 0)return 1; pinMode(button,INPUT); pullUpDnControl(button,PUD_UP); if(wiringPiISR(button,INT_EDGE_RISING,&myInterrupt) < 0) { printf("Unable to setup ISR \n"); } printf("Interrupt test program\n"); while(1) { if(flag) { while(digitalRead(button) ==0); printf("button press\n"); flag = 0; } } } 编译并执行 gcc –Wall Interrupt.c –o Interrupt -lwirngPi sudo ./Interrupt 注:(1)wiringPiISR(button,INT_EDGE_FALLING,&myInterrupt);设置中断下降沿触发,myInterrupt为中断处理函数。 三、python #!/usr/bin/python # -*- coding:utf-8 -*- import RPi.GPIO as GPIO import time KEY = 20 GPIO.setmode(GPIO.BCM) GPIO.setup(KEY,GPIO.IN,GPIO.PUD_UP) while True: time.sleep(0.05) if GPIO.input(KEY) == 0: print("KEY PRESS") while GPIO.input(KEY) == 0: time.sleep(0.01) 执行程序,按下按键会看到窗口显示”KEY PRESS”,按Ctrl+C结束程序。 sudo python key.py 注:(1)GPIO.setup(KEY,GPIO.IN,GPIO.PUD_UP) 设置管脚为上拉输入模式 (2)GPIO.input(KEY) 读取管脚值 通过中断模式编程 #!/usr/bin/python # -*- coding:utf-8 -*- import RPi.GPIO as GPIO import time KEY = 20 def MyInterrupt(KEY): print("KEY PRESS") GPIO.setmode(GPIO.BCM) GPIO.setup(KEY,GPIO.IN,GPIO.PUD_UP) GPIO.add_event_detect(KEY,GPIO.FALLING,MyInterrupt,200) while True: time.sleep(1) 注:(1)def MyInterrupt(KEY): 定义中断处理函数 (2) GPIO.add_event_detect(KEY,GPIO.FALLING,MyInterrupt,200) 增加事件检测,下降沿触发,忽略由于开关抖动引起的小于200ms的边缘操作。 关于树莓派事件中断编程请参考:http://www.guokr.com/post/480073/focus/1797650173/ |
关键词: 树莓派
微雪官网|产品资料|手机版|小黑屋|微雪课堂. ( 粤ICP备05067009号 )
GMT+8, 2019-12-6 03:16 , Processed in 0.019622 second(s), 19 queries .
Powered by Discuz! X3.2 © 2001-2013 Comsenz Inc & Style Design