Raspberry Pi GPIO control--C language articles

1. Introduction to

common


quote
【Development language】——python
【Brief introduction】——Recommended in the official information of Raspberry Pi and easy to use. python GPIO is a small python library that can help users complete raspberry related IO port operations, but python GPIO library does not yet support bus interfaces such as SPI, I2C or 1-wire.
【Official website】—— https://code.google.com/p/raspberry-gpio-python/

2. wiringPi
quote
[Development language]——C language
[Brief introduction]——wiringPi is suitable for those who have a foundation in C language and have been exposed to microcontroller or embedded development before contacting Raspberry Pi. The API function of wiringPi is very similar to arduino, which also makes it popular. The author gives a lot of instructions and sample codes, which also include UART devices, I2C devices, and SPI devices, etc.
【Official Website】—— http://wiringpi.com/

3. BCM2835 C Library
quote
[Development language] - C language
[Brief introduction] BCM2835 C Library can be understood as the underlying driver implemented in C language. The driver library of BCM2835 C Library includes GPIO, SPI and UART, etc. You can learn BCM2835 C Library to become familiar with BCM2835 related register operations. If you have the opportunity to develop a linux driver on the Raspberry Pi, or develop your own python or PHP extension driver, you can find a lot of "inspiration" from the BCM2835 C Library.
【Official Website】—— http://www.airspayce.com/mikem/bcm2835/


2. Raspberry Pi GPIO numbering method

1. Function physical pins:
quote
Left to right, top to bottom: left base, right even: 1-40

2. BCM:
quote
The numbering focuses on the CPU registers, according to the GPIO register numbering of the BCM2835.

3. wiringpi:
quote
The numbering focuses on the implementation logic, and the extended GPIO ports are numbered from 0, which is convenient for programming. As shown in Figure 3 WiringPi column.



3. WiringPi GPIO

1. Description:
quote
WiringPi is a GPIO control library function applied to the Raspberry Pi platform. WiringPi complies with GUN Lv3. wiringPi is developed in C or C++ and can be packaged by other languages, such as python, ruby ​​or PHP.
wiringPi includes a set of gpio control commands, which can be used to control the GPIO pins of the Raspberry Pi. Users can use gpio commands to control or query GPIO pins through shell scripts.


2.wiringPi installation

1) Use GIT tools
quote
git clone git://git.drogon.net/wiringPi
cd wiringPi ./build The
build
script will help you compile and install wiringPi

2) Direct download
quote
Download the latest version at https://git.drogon.net/?p=wiringPi;a=summary Compile and use
tar xfz wiringPi-xx.tar.gz
cd wiringPi-xx
./build

3) raspbian is installed using apt-get
quote
sudo apt-get install wiringpi


3. Test:
quote
wiringPi includes a set of gpio commands. You can use gpio commands to control various interfaces on the Raspberry Pi. You can test whether wiringPi is installed successfully by using the following commands.
$gpio -v


$gpio readall #The above gpio graph will appear

4. Sample code:
#include <wiringPi.h>
int main(void) {
 wiringPiSetup() ;
 pinMode (0, OUTPUT) ;
 for(;;) {
  digitalWrite(0, HIGH) ; delay (500) ;
  digitalWrite(0, LOW) ; delay (500) ;
 }
}

5. Compile and run:

On Raspberry Pi:
quote
gcc -Wall -o test test.c -lwiringPi
sudo ./test

In a virtual machine:
quote
am-linux-gcc -Wall -o test test.c -lwiringPi
sudo ./test

Note:
1) The numbering method of IO is slightly different, and the wiring encoding method is adopted.
2) -lwiringPi means to dynamically load the wiringPi shared library.

4. BCM2835 C Library

1. Download:
quote
$ wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.35.tar.gz


2. Unzip:
quote
$ tar xvzf bcm2835-1.35.tar.gz


3. Enter the compressed directory:
quote
$cd bcm2835-1.35


4. Configure the compilation:
quote
./configure make


5. Perform the check:
quote
$sudo make check


6. Install the bcm2835 library:
quote
$sudo make install


7. Sample code
#include < bcm2835.h>
//P1 socket pin 11
#define PIN RPI_GPIO_P1_11
int main(int argc, char **argv) {
  if (!bcm2835_init())
   return 1;

 // output method
 bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);

 while (1) {
   bcm2835_gpio_write(PIN, HIGH);
  bcm2835_delay(100);

  bcm2835_gpio_write(PIN, LOW);
  bcm2835_delay(100);
 }
 bcm2835_close();
 return 0;
}


compile and run
quote
gcc -o blink blink.c -lbcm2835
./blink


Note:
1) The numbering method of IO is slightly different, and the BCM encoding method is adopted.
2) -lbcm2835 indicates that the bcm2835 shared library is dynamically loaded

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326220416&siteId=291194637