树莓派系列教程9:按键

上两章我们讲解了在树莓派上如何点亮一个LED灯,这一章我们讲解一下按键以及事件中断。

一、bcm2835

01 #include

02 #include

03

04 #define KEY 20

05 int main(int argc, char **argv)

06 {

07 if (!bcm2835_init())return 1;

08 bcm2835_gpio_fsel(KEY, BCM2835_GPIO_FSEL_INPT);

09 bcm2835_gpio_set_pud(KEY, BCM2835_GPIO_PUD_UP);

10 printf("Key Test Program!!!!\n");

11 while (1)

12 {

13 if(bcm2835_gpio_lev(KEY) == 0)

14 {

15 printf ("KEY PRESS\n") ;

16 while(bcm2835_gpio_lev(KEY) == 0)

17 bcm2835_delay(100);

18 }

19 bcm2835_delay(100);

20 }

21 bcm2835_close();

22 return 0;

23 }

编译并执行,按下按键会看到窗口显示”KEY PRESS”,按Ctrl+C结束程序。

1 gcc –Wall key.c –o key –lbcm2835

2

3 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

01 #include

02 #include

04 char KEY = 29;

05

06 int main()

07 {

08 if (wiringPiSetup() < 0)return 1 ;

09 pinMode (KEY,INPUT);

10 pullUpDnControl(KEY, PUD_UP);

11 printf("Key Test Program!!!\n");

12 while(1)

13 {

14 if (digitalRead(KEY) == 0)

15 {

16 printf ("KEY PRESS\n") ;

17 while(digitalRead(KEY) == 0)

18 delay(100);

19 }

20 delay(100);

21 }

22 }

编译并执行,按下按键会看到窗口显示”KEY PRESS”,按Ctrl+C结束程序。

1 gcc –Wall key.c –o key –wiringPi

2

3 sudo ./key

注:(1)pinMode (KEY,INPUT);设置管脚为输入模式

(2)pullUpDnControl(KEY, PUD_UP);设置为上拉模式

(3)digitalRead(KEY);读取管脚状态

通过中断的方式编程

01 #include

02 #include

03

04 #define button 29

05 char flag = 0;

06 void myInterrupt()

07 {

08 flag ++;

10

11 int main()

12 {

13 if(wiringPiSetup() < 0)return 1;

14 if(wiringPiISR(button,INT_EDGE_FALLING,&myInterrupt) < 0)

15 {

16 printf("Unable to setup ISR \n");

17 }

18 printf("Interrupt test program\n");

19 while(1)

20 {

21 if(flag)

22 {

23 while(digitalRead(button) ==0);

24 printf("button press\n");

25 flag = 0;

26 }

27 }

28 }

29

编译并执行

1 gcc –Wall Interrupt.c –o Interrupt -lwirngPi

2

3 sudo ./Interrupt

注:(1)wiringPiISR(button,INT_EDGE_FALLING,&myInterrupt);设置中断下降沿触发,myInterrupt 为中断处理函数。

三、python

01 #!/usr/bin/python

02 # -*- coding:utf-8 -*-

03 import RPi.GPIO as GPIO

04 import time

05

06 KEY = 26

07

08 GPIO.setmode(GPIO.BCM)

09 GPIO.setup(KEY,GPIO.IN,GPIO.PUD_UP)

10 while True:

11 time.sleep(0.05)

12 if GPIO.input(KEY) == 0:

13 print("KEY PRESS")

14 while GPIO.input(KEY) == 0:

15 time.sleep(0.01)

执行程序,按下按键会看到窗口显示”KEY PRESS”,按Ctrl+C 结束程序。

1 sudo python key.py 注:(1)GPIO.setup(KEY,GPIO.IN,GPIO.PUD_UP) 设置管脚为上拉输入模式

(2)GPIO.input(KEY ) 读取管脚值

通过中断模式编程

01 #!/usr/bin/python

02 # -*- coding:utf-8 -*-

03 import RPi.GPIO as GPIO

04 import time

05

06 KEY = 26

07

08 def MyInterrupt(KEY):

09 print("KEY PRESS")

10

11 GPIO.setmode(GPIO.BCM)

12 GPIO.setup(KEY,GPIO.IN,GPIO.PUD_UP)

13 GPIO.add_event_detect(KEY,GPIO.FALLING,MyInterrupt,200) 14

15 while True:

16 time.sleep(1) 注:(1)def MyInterrupt(KEY): 定义中断处理函数

(2) GPIO.add_event_detect(KEY,GPIO.FALLING,MyInterrupt,200) 增加事件检测,下降沿触发,忽略由于开关抖动引起的小于200ms 的边缘操作。

关于树莓派事件中断编程请参考:https://www.360docs.net/doc/8c18267880.html,/post/480073/focus/1797650173/

相关文档
最新文档