Rcomputer.eu

Ovladanie GPIO z bash

Connect an LED using a resistor between GPIO11 and GND.
Creating a File access to GPIO using console commands:
If you write to the ./export file in the /sys/class/gpio/ subdirectory, the system creates a file with a GPIO structure according to the input. In this case we want to create an access to write directly to GPIO11 in order to handle an LED.
Create a GPIO file access:
echo 11 > /sys/class/gpio/export 
Configure the Pin Direction (In/Out):
echo out > /sys/class/gpio/gpio11/direction
Write a value to turn on the LED using the GPIO11:
echo 1 > /sys/class/gpio/gpio11/value
Now your led should be ON!!!
Write a value to clear the LED using the GPIO11
echo 0 > /sys/class/gpio/gpio11/value
 
Now your led should be OFF!!!
Delete the created GPIO (11)
echo 11 > /sys/class/gpio/unexport
You are able to access to GPIO using python or any programming language. We wrote a python script to show how. Download
Open a new terminal and execute the script
wget https://sites.google.com/site/semilleroadt/raspberry-pi-tutorials/gpio/ADT_blink.py
python ADT_blink
The script prompts the questions to determine which pin and how many times you want it to blink:
Type the number of the GPIO you want to blink : 11
Type the number of times you want it to blink e.g: 10
Now your GPIO 11 is blinking like a christmas tree.
Open the python script with a text editor and study all the lines (try to understand, is a simple script using file access)
Let’s start with the Second method (Pointers):
If you want to read/write to GPIOs using pointers the better way is installing the bcm2635 library and understanding how it works, you can read the datasheet of bcm2835 starting with the section 1.2.2
Installing bcm2835 library
wget http://www.open.com.au/mikem/bcm2835/bcm2835-1.15.tar.gz
tar zxvf bcm2835-1.15.tar.gz
./configure
make
make check
make install
Now you can easily use this library to access the GPIOs, the library uses pointers to write/read to the GPIOs or to changes the values in registers of the HW, modifying the function of each peripheral (PWM modules, UART, etc)
Download the code example written in C
Open the Terminal and use gcc to compile the code.
wget https://sites.google.com/site/semilleroadt/raspberry-pi-tutorials/gpio/main.c
gcc -o main -l rt main.c -l bcm2835   

 

Execute the compiled code
./main
Now your LED connected in the GPIO11 is Blinking!!! Like the example with the python script.
Open the C code with a text editor and study all the lines.
Now the Question is: What is the best way to access GPIOs on linux?

Napísať komentár