立即注册 找回密码

微雪课堂

搜索
微雪课堂 树莓派 树莓派入门教程 查看内容

树莓派系列教程9:按键

2015-8-27 15:03| 发布者: MyMX1213| 查看: 43255| 评论: 5|原作者: MyMX1213

摘要: 本章将讲解树莓派的按键以及中断。

上两章我们讲解了在树莓派上如何点亮一个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

注:wiringPiISR(button,INT_EDGE_FALLING,&myInter>rupt);设置中断下降沿触发,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 python3 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/


605

顶一下

刚表态过的朋友 (605 人)

相关阅读

发表评论

最新评论

引用 hldgaoshuo 2017-9-23 17:39
文章中有些地方用gcc编译时加载静态库忘记在前面加上l,引用wiringPi.h时忘记将P大写,大家注意。
引用 hldgaoshuo 2017-9-23 17:35
if(wiringPiISR(button,INT_EDGE_RISING,&myInterrupt) < 0)
是不是因该修改为
if(wiringPiISR(button,INT_EDGE_FALLING,&myInterrupt) < 0)
开始上拉,因该是下降沿触发吧
引用 MyMX1213 2017-1-4 09:26
: Interrupt.c这个是不是有问题?执行以后按键没有反应,烦请检查一下,谢谢。 ...
这个没有设置管脚为上拉状态,所以按键没反应,已修改
引用 游客 2017-1-3 13:28
Interrupt.c这个是不是有问题?执行以后按键没有反应,烦请检查一下,谢谢。 ...
引用 游客 2017-1-3 00:32
wiringPi的keypress错了,代码写的key=29,实际应该是key=28,重新读了一下GPIO才搞明白为啥和bcm那个代码 ...

查看全部评论(5)

基础入门
OpenCV
littleGL

微雪官网|产品资料|手机版|小黑屋|微雪课堂. ( 粤ICP备05067009号 )

GMT+8, 2024-3-19 15:39 , Processed in 0.023132 second(s), 21 queries .

返回顶部