树莓派 -- 按键 (key)使用BCM2835 gpio library
BCM2835 GPIO library介紹
This is a C library for Raspberry Pi (RPi). It provides access to GPIO
and other IO functions on the Broadcom BCM 2835 chip, as used in the
RaspberryPi, allowing access to the GPIO pins on the 26 pin IDE plug
on the RPi board so you can control and interface with various
external devices.
其鏈接如下
http://www.airspayce.com/mikem/bcm2835/
在之前的文章中也有介紹
https://blog.csdn.net/feiwatson/article/details/80781683
https://blog.csdn.net/feiwatson/article/details/80779159
利用其來實現簡單按鍵應用。代碼如下
/* key.c* you can build this like: * gcc -Wall key.c -o key -lbcm2835* sudo ./key */ #include <bcm2835.h> #include <stdio.h>char KEY = 20; unsigned char i; 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; }交叉編譯makefile如下,(也可以選擇直接在樹莓派板上編譯)
CC = /home/xxx/Raspberry/tools-master/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc-4.9.3 CFLAGS = -I./ -L./lib CFLAGS += -Wall key:key.c$(CC) $(CFLAGS) key.c -o key -lbcm2835 clean:rm key event如果直接在板上編譯,
gcc -Wall key.c -o key -lbcm2835運行, 按下按鍵后,輸出“KEYPRESS”
pi@raspberrypi:~/learning/bcm2835/key_test $ ./key Key Test Program!!!! KEY PRESS KEY PRESS KEY PRESS KEY PRESS KEY PRESS硬件接線:
轉載于:https://www.cnblogs.com/feiwatson/p/9478210.html
總結
以上是生活随笔為你收集整理的树莓派 -- 按键 (key)使用BCM2835 gpio library的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Django 框架 数据库操作
- 下一篇: 浅析data:image/png;bas