首先是GPIO的编号,右下角编号40的 GPIO接口对应的是GPIO44 , 应该使用44这个编号,而不是40.
用以下的命令进行操作和可看。
具体的介绍 export in out, 这些的意思,不讲了。 树莓派的文档里面很多。
cd /sys/class/gpio
echo 44 > export
cd gpio44
echo out > direction
echo 1 > value
cat value
echo 0 > value
cat value
echo in > direction
cat value
分享一个main.c文件, 作用是模拟树莓派里面的gpio程序。用来进行读写gpio引脚
gcc main.c -o gpio
gpio read 44
gpio mode 44 in / out
gpio write 44 0
main.c文件内容:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
//#define GPIO44 492
//#define GPIO22 470
#define MAX_BUF 128 //Define array size
#define StarF_Gpio_Dir "/sys/class/gpio" //GPIO control paths
//
// GND
// GPIO36
// GPIO61
// GPIO44
//
int StarF_gpio_export(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
// /sys/class/gpio/export
fd = open( "/sys/class/gpio/export", O_WRONLY);
if (fd < 0) {
perror("gpio/export");
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
int StarF_gpio_unexport(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
// /sys/class/gpio/unexport
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if (fd < 0) {
perror("gpio/export");
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
//0 input, 1 output
int StarF_gpio_set_dir(unsigned int gpio, unsigned int out_flag)
{
int fd, len;
char buf[MAX_BUF];
// /sys/class/gpio/gpioN/direction
len = snprintf(buf, sizeof(buf), StarF_Gpio_Dir "/gpio%d/direction", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
perror(buf);
return fd;
}
if (out_flag) //'1' set to output
write(fd, "out", 4);
else //'0' set input
write(fd, "in", 3);
close(fd);
return 0;
}
int StarF_gpio_set_value(unsigned int gpio, unsigned int value)
{
int fd, len;
char buf[MAX_BUF];
// /sys/class/gpio/gpioN/value
len = snprintf(buf, sizeof(buf), StarF_Gpio_Dir "/gpio%d/value", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
perror(buf);
return fd;
}
if (value) //'1' output is high level
write(fd, "1", 2);
else //'0' output is Low level
write(fd, "0", 2);
close(fd);
return 0;
}
int StarF_gpio_get_value(unsigned int gpio, unsigned int *value)
{
int fd, len;
char buf[MAX_BUF];
char ch;
// /sys/class/gpio/gpioN/value
len = snprintf(buf, sizeof(buf), StarF_Gpio_Dir "/gpio%d/value", gpio);
fd = open(buf, O_RDONLY);
if (fd < 0) {
perror("gpio/get-value");
return fd;
}
read(fd, &ch, 1); //Read the external input level
if (ch != '0') { //'1' Input is high level
*value = 1;
} else { //'0' Input is Low level
*value = 0;
}
close(fd);
return 0;
}
void doMode (int argc, char *argv [])
{
int pin ;
char *mode ;
if (argc != 4)
{
fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
exit (1) ;
}
pin = atoi (argv [2]) ;
mode = argv [3] ;
int sys = pin;
int ret = StarF_gpio_export(sys);
if(ret<0)
return ret;
if (strcasecmp (mode, "in")== 0)
StarF_gpio_set_dir(sys, 0);
else if (strcasecmp (mode, "input") == 0)
StarF_gpio_set_dir(sys, 0);
else if (strcasecmp (mode, "out") == 0)
StarF_gpio_set_dir(sys, 1);
else if (strcasecmp (mode, "output") == 0)
StarF_gpio_set_dir(sys, 1);
else
{
fprintf (stderr, "%s: Invalid mode: %s. Should be in/out\n", argv [1], mode) ;
exit (1) ;
}
exit(0);
}
void doRead(unsigned int pin)
{
unsigned int sys = pin;
int ret = StarF_gpio_export(sys);
if(ret<0)
return ret;
//StarF_gpio_set_dir(sys, 0);
unsigned int value=0;
StarF_gpio_get_value(sys, &value);
printf("%d\n", value);
exit(0);
return 0;
}
void doWrite(unsigned int pin, unsigned int value)
{
unsigned int sys = pin;
int ret = StarF_gpio_export(sys);
if(ret<0)
return ret;
StarF_gpio_set_dir(sys, 1);//output
ret = StarF_gpio_set_value(sys, value);
return 0;
}
void help()
{
printf("\t******** gpio HELP ************\n");
printf("gpio read 1 \n");
printf("gpio write 1 0 \n");
printf("gpio mode 1 in/out \n");
}
int main(int argc, char **argv)
{
if(argc < 3)
{
help();
return -1;
}
if (strcasecmp (argv [1], "mode") == 0)
{ if(argc <= 3)
{
help();
return -1;
}
doMode(argc, argv);
}
else if (strcasecmp (argv [1], "read") == 0)
doRead(atoi(argv[2])) ;
else if (strcasecmp (argv [1], "write") == 0)
{
if(argc <= 3)
{
help();
return -1;
}
doWrite(atoi(argv [2]), atoi(argv [3])) ;
}
//help();
return -1;
}