树莓派系列教程:按键

来自丢石头百科
Admin讨论 | 贡献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/

系列教程

树莓派系列教程