Raspberry Pi hardware --- GPIO chapter 4

  Raspberry Pi get our hands on more than two months, in fact, installed ROS expect from the beginning, a few days ago to complete the ROS source code compiler installation, tuning time for linux also spent some time. Now finally remembered, raspberries come in there GPIO, I have not used up. He went ahead and start. The following operations are the Raspberry Pi has been installed the official system has also been upgraded to the latest version. A total of 40 GPIO, the actual pin Pictured:

  

  In the computer, usually high and low voltage to represent a binary 1 and 0. Raspberry Pi as well. GPIO data denoted by the same manner. The PIN can in each GPIO input or output state. When the output state, the system may be 0 or 1 passed the PIN. If it is 1, then the corresponding physical PIN externally output a high voltage of 3.3V, and otherwise outputs a low voltage of 0V. Appropriate, PIN input state can be detected voltages physical PIN. If the voltage is high, then the system will return to the PIN 1, and 0 otherwise. With the above mechanism is simple, GPIO achieve interaction and physical circuitry. FIG via the pins, is easy to see and use other boards are the same, the direction of the first pin set, then set the value of the pin. Be configured as inputs or outputs by the corresponding register value read and write IO, IO state acquisition, by obtaining numerical values ​​IO, IO completion of the change of state.

Create a python code:

 1 import RPi.GPIO as GPIO
 2 import time
 3 GPIO.setmode(GPIO.BCM)
 4 GPIO.setup(21,GPIO.OUT)
 5 
 6 while True :
 7     GPIO.output(21,GPIO.HIGH)
 8     time.sleep(0.05)
 9     GPIO.output(21,GPIO.LOW)
10     time.sleep(0.05)

Create a C code:

 

Use get bash: bash with commands to control GPIO21. In Linux, the external device is often expressed as a file. Character to write or read a file, device or equivalent from the output device to input characters. Raspberries come in GPIO port is also true, which represents the file is located in / sys / class / gpio / down. First, activate GPIO21:

echo 21 > /sys/class/gpio/export 

Meaning the top command is the character "21" is written to / sys / class / gpio / export . It can be seen after the command, / sys / class / gpio / below adds a directory on behalf of GPIO21, the directory name is gpio21. Next, we put the GPIO21 output state:

echo out > /sys/class/gpio/gpio21/direction

File / sys / class / gpio / gpio21 / direction for the directional control GPIO21. We write the character representing the output of the "out" to the inside.

echo 1 > /sys/class/gpio/gpio21/value

Finally, write to GPIO21 1, so that at a high voltage PIN: see, LED lights up. If you want to turn off the LED lights only need to write GPIO21 0:

echo 0 > /sys/class/gpio/gpio21/value

After use GPIO21, you can delete the port:

echo 21 > /sys/class/gpio/unexport

/ Sys / class / gpio / gpio21 disappear.

2 Python GPIO achieve

    [Setup]
        [1] to install python-dev, enter the command.
sudo apt-get install python-dev
        [2] mounted RPi.GPIO, sequentially enter the command. Special note, due RPi.GPIO still in the process of continuous improvement, refer to the preface of the link to download the latest installation code.
# Download 
$ wget http://raspberry-gpio-python.googlecode.com/files/RPi.GPIO-0.5.3a.tar.gz
# decompress 
$ tar xvzf RPi.GPIO-0.5.3a.tar.gz 
# enter directory after extracting 
$ cd RPi.GPIO-0.5.3a 
# start installing 
$ sudo python setup.py install

    [] Sample code
    creates a new file named led.py, the document contents are as follows:
# - * - Coding: UTF-. 8 - * -
Import RPi.GPIO the GPIO AS
Import Time
# BOARD numbering, based Foot No.
GPIO.setmode (GPIO.BOARD)
# output mode
GPIO.setup (. 11, GPIO.OUT)

the while True:
GPIO.output (. 11, GPIO.HIGH)
the time.sleep (. 1)
GPIO.output (. 11, GPIO.LOW)
time.sleep (1)
    [run]
    if you are writing code on windows, use FTP software to download raspberry Pi using SSH client login raspberry pie, cd command to enter the directory where the file, and then enter the command
    sudo python led. py
    Okay, LED and other expansion board start flashing. Finally, use Ctrl + C end Python programs.
    [BRIEF DESCRIPTION]
    [1] GPIO.setmode (GPIO.BOARD), using the numbering socket pins.
    [2] As a result of socket pins numbering, where the foot 11 corresponds to the register pins 11 BCM2835 numbering.

4 WiringPi achieve

    [Setup]
    see [Raspberry Pi --wiringPi profile study notes, and pin mounting DESCRIPTION
    [] sample code
    called a new blink.c file contents are as follows:
#include <wiringPi.h>
int main (void)
{
wiringPiSetup ();
the pinMode (0, the OUTPUT);
for (;;)
{
digitalWrite (0, HIGH); Delay (500);
digitalWrite (0, the LOW); Delay (500);
}
}
    [run ]    
    If you write the code on windows, use FTP software to download raspberry Pi using SSH client login raspberry pie, cd command to enter the directory where the file, and then enter the command gcc
    gcc -Wall -o blink blink.c -lwiringPi
    then enter the following command to execute the program
    sudo ./blink
    running smoothly, use ctrl + c can finally end the program.

    [BRIEF DESCRIPTION]
    [1] uncertain final state, the program stops since the time is unknown, all the LED may be lit might be off.
    [2] and python program compared to, GPIO port number that seems to have changed, but in fact for the same IO, IO numbering just a slightly different way.
    [3] -lwiringPi represents the dynamic load wiringPi shared libraries, if not familiar with the instructions and gcc makefile, please refer to the blog series - Linux [study notes - for example, said Bowen makefile index]

5 BCM2835 C Library

    [Installation]
    special note, because the BCM2835 C Library is still in the process of continuous improvement, refer to the preface of the link to download the latest installation code.
# Download
$ wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.35.tar.gz 
# decompress
$ tar xvzf bcm2835-1.35.tar.gz 
# after entering the compression directory
$ cd bcm2835-1.35
# configuration
./configure
# installation package from the source code to generate
the make
# perform checks
the sudo the make check
# mounted bcm2835 libraries
sudo make install

    [] Sample code
    creates a new program named blink.c, the specific contents of the program are as follows:
#include <bcm2835.h>

// Pl socket pin 11
#define the PIN RPI_GPIO_P1_11

int main (int argc, char ** the argv)
{
IF (! bcm2835_init ())
return. 1;

// output
bcm2835_gpio_fsel (the PIN, BCM2835_GPIO_FSEL_OUTP);

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

bcm2835_gpio_write (the PIN, the LOW);
bcm2835_delay (100);
}
bcm2835_close ();
return 0;
}
    [run]       
    gcc command if you are writing code on windows, use FTP software to download raspberry Pi using SSH client login raspberry pie, cd command to enter the directory where the file, and then enter
    gcc -o blink blink.c -lbcm2835
    then executes the program, enter the command
    sudo ./blink
    Running smoothly, use ctrl + c can finally end the program.
    Similar to the case [1] and wiringPi, if the shutdown procedure may light LED lamp may extinguish.
    [2] Although the GPIO port changed again, but still point to the LED lights.
    [3] represents -lbcm2835 bcm2835 dynamically loaded shared libraries,
Reference: https: //blog.csdn.net/xukai871105/article/details/12684617

References: https: //www.cnblogs.com/vamei/p/6751992.html

Guess you like

Origin www.cnblogs.com/guochaoxxl/p/11728108.html