Today I wrote a gpio as an example of input port on the basis of LED light. In the experiment, I use a key as IO input device, then use Bb_black to collect the current information.
First Step: Hardware introduction
1, Key introduction
Touch the structure of the keys as shown, the keys of the feet, 3,4 feet are connected together, when the button is the 1,3 (or 2,4) pin short connection, loosen the open between two feet.
2. Hardware principle
Note:
(1) In order to prevent misoperation, 3.3V will burn the IO port The author has connected a current limiting resistor (10k) between the key and the 3.3V;
(2) The author of this experiment used the 14-pin of the P8 row seat, which is the gpio_26 pin.
3. Hardware connection
I use the button is removed from the scrap board, welded two lines, two lines of the other end of the direct welding pin, welded parts with heat shrinkable tube to hold, in doing a variety of development when used up very simple and practical.
Step Two: Software code
1, first look at the first part of the code
1#include <stdio.h>2 #defineGpio_dir "/sys/class/gpio/"3 4 intMain ()5 { 6FILE *stream=NULL;7 Chara[Ten]; 8Stream=fopen (Gpio_dir"Export","W");9 TenFwrite" -",sizeof(int),2, stream); One fclose (stream); A - -Stream=fopen (Gpio_dir"gpio26/direction","r+"); theFwrite"inch",sizeof(Char),2, stream); - fclose (stream); - while(1) - { +Stream=fopen (Gpio_dir"Gpio26/value","r+"); -FSCANF (Stream,"%s", a); +printf"%s\n", a); A fclose (stream); atSleep1); - } - - return 0; -}
2. Experimental results:
Beaglebone_black_ Study notes-(8) Key for GPIO input