GPIO(General purpose Input/Output):
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include <unistd.h>
char export_path[100] = "/sys/class/gpio/export";
char direction_path[100] = "/sys/class/gpio/";
char value_path[100] = "/sys/class/gpio/";
int main(int argc,char **argv)
{
int fd;
int value;
fd = open(export_path,O_WRONLY);
perror("open");
value = atoi(argv[1]);
write(fd,argv[1],strlen(argv[1])+1);
perror("write");
close(fd);
if(value>=0 && value <=31)
{
sprintf(direction_path+16,"%s%d%s","PA",value,"/direction");
sprintf(value_path+16,"%s%d%s","PA",value,"/value");
}
else if(value>=32 && value <=63)
{
sprintf(direction_path+16,"%s%d%s","PB",value-32,"/direction");
sprintf(value_path+16,"%s%d%s","PB",value-32,"/value");
}
else if(value>=64 && value <=95)
{
sprintf(direction_path+16,"%s%d%s","PC",value-64,"/direction");
sprintf(value_path+16,"%s%d%s","PC",value-64,"/value");
}
else
{
sprintf(direction_path+16,"%s%d%s","PD",value-96,"/direction");
sprintf(value_path+16,"%s%d%s","PD",value-96,"/value");
}
printf("%s\n",direction_path);
printf("%s\n",value_path);
fd = open(direction_path,O_WRONLY);
perror("open");
write(fd,"out",4);
perror("write");
close(fd);
fd = open(value_path,O_WRONLY);
perror("open");
while(1)
{
write(fd,"1",2);
perror("write");
sleep(5);
write(fd,"0",2);
perror("write");
sleep(5);
}
close(fd);
}Last updated