树莓派3B-linux控制GPIO(不用树莓派的库)

这篇博客详细记录了如何在Linux环境下,不使用树莓派官方库,直接通过映射物理地址到虚拟地址来控制树莓派3B的GPIO。作者首先下载BCM2835手册,了解到GPIO寄存器地址和设置方法,然后依据手册编写了控制GPIO输出电平的程序,以GPIO Pin3为例进行实验。程序需要GCC编译并以管理员权限运行。
摘要由CSDN通过智能技术生成

这篇博客记录了我在用户程序中将物理地址映射到虚拟地址,然后使用虚拟地址控制树莓派3B的GPIO的过程。以下是整个过程的记录:

1、下载数据手册

和控制单片机IO口相似,如果用户想控制树莓派的GPIO,就得先知道GPIO相关寄存器的地址和设置的方法。树莓派的网站上提供了外设说明手册(Peripheral specification),这个手册对芯片上的外设怎么使用进行了描述。不过,Pi 3 的处理器是BCM2837,官网只提供了BCM2835(Pi 1 处理器)的外设说明手册。由于两个芯片外设上区别不大,我直接下载了BCM2835的手册来参考。下载手册的网址:https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/README.md

2、查阅GPIO相关寄存器地址和设置方法

翻到第5页,可以看到下图这个关于ARM地址映射的描述。

中间的部分为ARM的物理地址分配方式。IO外设(IO Peripherals)的物理地址分配在0x20000000(这是BCM2835的)。由于芯片不同,BCM2837的IO设备地址已经改为 0x3F000000,这一点,在官网提供的文档中也有说到

pdf,epub,mobi Three in one zip file. Jump right into the pro-level guts of the Raspberry Pi with complete schematics and detailed hardware explanations as your guide. You'll tinker with runlevels, reporting voltages and temperatures, and work on a variety of project examples that you can tune for your own project ideas.. This book is fully updated for the latest Pi boards with three chapters dedicated to GPIO to help you master key aspects of the Raspberry Pi. You'll work with Linux driver information and explore the different Raspberry Pi models, including the Pi Zero, Pi Zero W, Pi 2, Pi3 B and Pi3 B+. You'll also review a variety of project examples that you can tune for your own project ideas. Other topics covered include the 1-Wire driver interface, how to configure a serial Linux console, and cross-compile code, including the Linux kernel. You'll find yourself turning to Advanced Raspberry Pi over and over again for both inspiration and reference. Whether you're an electronics professional, an entrepreneurial maker, or just looking for more detailed information on the Raspberry Pi, this is exactly the book for you.What You'll LearnMaster I2C and SPI communications from Raspbian Linux in CProgram USB peripherals, such as a 5-inch LCD panel with touch control and the Pi cameraStudy GPIO hardware, the sysfs driver interface and direct access from C programsUse and program the UART serial device. Who This Book Is ForAdvanced Raspberry Pi users who have experience doing basic projects and want to take their projects further.
树莓派4B上,可以使用WiringPi来进行GPIO控制。要检测8个GPIO的输入状态,可以按照以下步骤进行: 1. 安装WiringPi: ``` sudo apt-get install wiringpi ``` 2. 编写C程序,使用wiringPiSetup()函数初始化WiringPi,并使用wiringPiSetupGpio()函数将GPIO引脚模式设置为BCM模式。然后使用wiringPiISR()函数设置中断处理函数来检测GPIO输入状态,例如: ``` #include <wiringPi.h> #include <stdio.h> #define GPIO_1 18 #define GPIO_2 23 #define GPIO_3 24 #define GPIO_4 25 #define GPIO_5 12 #define GPIO_6 16 #define GPIO_7 20 #define GPIO_8 21 void gpioInterrupt1(void) { if (digitalRead(GPIO_1) == HIGH) { printf("GPIO 1 is HIGH\n"); } else { printf("GPIO 1 is LOW\n"); } } void gpioInterrupt2(void) { if (digitalRead(GPIO_2) == HIGH) { printf("GPIO 2 is HIGH\n"); } else { printf("GPIO 2 is LOW\n"); } } void gpioInterrupt3(void) { if (digitalRead(GPIO_3) == HIGH) { printf("GPIO 3 is HIGH\n"); } else { printf("GPIO 3 is LOW\n"); } } void gpioInterrupt4(void) { if (digitalRead(GPIO_4) == HIGH) { printf("GPIO 4 is HIGH\n"); } else { printf("GPIO 4 is LOW\n"); } } void gpioInterrupt5(void) { if (digitalRead(GPIO_5) == HIGH) { printf("GPIO 5 is HIGH\n"); } else { printf("GPIO 5 is LOW\n"); } } void gpioInterrupt6(void) { if (digitalRead(GPIO_6) == HIGH) { printf("GPIO 6 is HIGH\n"); } else { printf("GPIO 6 is LOW\n"); } } void gpioInterrupt7(void) { if (digitalRead(GPIO_7) == HIGH) { printf("GPIO 7 is HIGH\n"); } else { printf("GPIO 7 is LOW\n"); } } void gpioInterrupt8(void) { if (digitalRead(GPIO_8) == HIGH) { printf("GPIO 8 is HIGH\n"); } else { printf("GPIO 8 is LOW\n"); } } int main(void) { wiringPiSetup(); wiringPiSetupGpio(); pinMode(GPIO_1, INPUT); pinMode(GPIO_2, INPUT); pinMode(GPIO_3, INPUT); pinMode(GPIO_4, INPUT); pinMode(GPIO_5, INPUT); pinMode(GPIO_6, INPUT); pinMode(GPIO_7, INPUT); pinMode(GPIO_8, INPUT); wiringPiISR(GPIO_1, INT_EDGE_BOTH, &gpioInterrupt1); wiringPiISR(GPIO_2, INT_EDGE_BOTH, &gpioInterrupt2); wiringPiISR(GPIO_3, INT_EDGE_BOTH, &gpioInterrupt3); wiringPiISR(GPIO_4, INT_EDGE_BOTH, &gpioInterrupt4); wiringPiISR(GPIO_5, INT_EDGE_BOTH, &gpioInterrupt5); wiringPiISR(GPIO_6, INT_EDGE_BOTH, &gpioInterrupt6); wiringPiISR(GPIO_7, INT_EDGE_BOTH, &gpioInterrupt7); wiringPiISR(GPIO_8, INT_EDGE_BOTH, &gpioInterrupt8); while (1) { delay(1000); } return 0; } ``` 这里定义了8个GPIO引脚的编号,然后分别编写了8个中断处理函数,使用digitalRead()函数来读取GPIO输入状态,如果为HIGH则输出“GPIO x is HIGH”,否则输出“GPIO x is LOW”。然后在主函数中使用wiringPiISR()函数将中断处理函数绑定到相应的GPIO引脚上,并使用delay()函数来阻塞程序运行,等待中断事件的发生。 3. 编译程序并运行: ``` gcc -o gpio_test gpio_test.c -lwiringPi sudo ./gpio_test ``` 这样就可以检测8个GPIO的输入状态了。需要注意的是,这里使用了中断处理的方式来检测GPIO输入状态,因此在程序运行期间,如果有GPIO输入状态发生变化,就会触发相应的中断处理函数。如果不想使用中断处理方式,可以使用digitalRead()函数来轮询读取GPIO输入状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值