“树莓派系列教程:按键”的版本间的差异

来自丢石头百科
 
(未显示同一用户的1个中间版本)
第1行: 第1行:
 
+
这一章我们讲解一下按键以及事件中断。
上两章我们讲解了在树莓派上如何点亮一个LED灯,这一章我们讲解一下按键以及事件中断。
+
== bcm2835 ==
一、bcm2835
 
 
 
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
#include <bcm2835.h>
 
#include <bcm2835.h>
第37行: 第35行:
  
 
sudo ./key</syntaxhighlight>
 
sudo ./key</syntaxhighlight>
注:(1)bcm2835_gpio_fsel(KEY, BCM2835_GPIO_FSEL_INPT);设置管脚为输入模式
+
注:
      (2)bcm2835_gpio_set_pud(KEY, BCM2835_GPIO_PUD_UP);设置为上拉模式
+
 
      (3) bcm2835_gpio_lev(KEY);读取管脚状态
+
1. bcm2835_gpio_fsel(KEY, BCM2835_GPIO_FSEL_INPT);设置管脚为输入模式
    二、wiringPi
+
 
 +
2. bcm2835_gpio_set_pud(KEY, BCM2835_GPIO_PUD_UP);设置为上拉模式
 +
 
 +
3. bcm2835_gpio_lev(KEY);读取管脚状态
 +
 
 +
== wiringPi ==
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
#include <stdio.h>
 
#include <stdio.h>
第64行: 第67行:
 
     }   
 
     }   
 
}</syntaxhighlight>
 
}</syntaxhighlight>
     编译并执行,按下按键会看到窗口显示”KEY PRESS”,按Ctrl+C结束程序。
+
 
 +
编译并执行,按下按键会看到窗口显示”KEY PRESS”,按Ctrl+C结束程序。
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
gcc –Wall key.c –o key –wiringPi
 
gcc –Wall key.c –o key –wiringPi
第117行: 第121行:
  
 
sudo ./Interrupt</syntaxhighlight>
 
sudo ./Interrupt</syntaxhighlight>
注:(1)wiringPiISR(button,INT_EDGE_FALLING,&amp;myInterrupt);设置中断下降沿触发,myInterrupt为中断处理函数。
+
注:
      三、python
+
 
 +
wiringPiISR(button,INT_EDGE_FALLING,&amp;myInterrupt);设置中断下降沿触发,myInterrupt为中断处理函数。
 +
 
 +
== python ==
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
#!/usr/bin/python
 
#!/usr/bin/python
第158行: 第165行:
 
while True:
 
while True:
 
     time.sleep(1)</syntaxhighlight>
 
     time.sleep(1)</syntaxhighlight>
注:(1)def MyInterrupt(KEY): 定义中断处理函数
+
注:
 +
 
 +
1. def MyInterrupt(KEY): 定义中断处理函数
 +
 
 +
2. GPIO.add_event_detect(KEY,GPIO.FALLING,MyInterrupt,200) 增加事件检测,下降沿触发,忽略由于开关抖动引起的小于200ms的边缘操作。
  
 (2) GPIO.add_event_detect(KEY,GPIO.FALLING,MyInterrupt,200) 增加事件检测,下降沿触发,忽略由于开关抖动引起的小于200ms的边缘操作。
 
 
关于树莓派事件中断编程请参考:
 
关于树莓派事件中断编程请参考:
 
http://www.guokr.com/post/480073/focus/1797650173/
 
http://www.guokr.com/post/480073/focus/1797650173/
 +
 +
== 系列教程 ==
 +
{{Raspberry Pi Study}}

2020年11月20日 (五) 15:39的最新版本

这一章我们讲解一下按键以及事件中断。

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,&amp;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

注:

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/

系列教程

树莓派系列教程