| BBB板第七课:GPIO编程控制         在一上课我们通过IO函数做了简单的GPIO端口输出高低电平输出,实现对一个LED指示灯的点亮和关闭控制,本节将通过完整的C++程序,实现第四课Shell脚本的全部功能,实现两个LED指示灯的交替闪亮。 直接通过进入功能程序     1、实现echo 44 > export 打开端口功能     上一课简单测试中,通过手工在BBB板终端模式下打开gpio44端口可通过以下程序实现: #include <stdio.h> #define GPIO_DIR "/sys/class/gpio/"       /*GPIO主目录*/ int main() {     FILE *stream=NULL;     stream=fopen(GPIO_DIR"gpio44/direction","r+");     if (stream==NULL)                      /*如果打开文件失败则打开相应端口*/     {          stream=fopen(GPIO_DIR"export","w");   /*export打开只能选择w模式*/          fwrite("44",sizeof(int),2,stream);         fclose(stream);         stream=fopen(GPIO_DIR"gpio44/direction","r+");  /*重新打开文件*/     } } 可以先编译和执行这一段代码,然后在gpio目录下查看gpio44 是不是已经打开了。 这里面关于“export”文件为什么只能用w模式,可以 ls  –all列表下文件,就可以发现左侧的运行模式为-w-----,只有一个w可选,我原来没注意,用了r或r+等模式,老是没能打开gpio44端口,找了很久原因才发现是这个问题。 前车之鉴,希望大家不要走那么多弯路。 
     2、实现echo 44 > unexport 关闭端口功能     和打开端口操作一样,只是一个文件名不同而已,因为一个好的程序打开完相应端口,在结束的时候总是得关闭掉,也算是有始有终吧。直接上代码,其他就不多说,自己试   ...     stream=fopen(GPIO_DIR"unexport","w");   /*unexport打开只能选择w模式*/     fwrite("44",sizeof(int),2,stream);     fclose(stream); ... 
 3、实现P8.12和P8.11两引脚隔一秒闪亮完整程序 
 #include <stdio.h> #include <unistd.h> #define GPIO_DIR "/sys/class/gpio/" 
 main() {     FILE *stream=NULL;     FILE *stream1=NULL;     int i=0; 
     stream=fopen(GPIO_DIR"gpio44/direction","r+");     if (stream==NULL)               /*打开P8.12端口*/     {         stream=fopen(GPIO_DIR"export","w");         fwrite("44",sizeof(int),2,stream);         fclose(stream);         stream=fopen(GPIO_DIR"gpio44/direction","r+");     }     fwrite("out",sizeof(char),3,stream); /* P8.12端口为输出*/     fclose(stream); 
     stream1=fopen(GPIO_DIR"gpio45/direction","r+");     if (stream1==NULL)              /*打开P8.11端口*/     {         stream1=fopen(GPIO_DIR"export","w");          fwrite("45",sizeof(int),2,stream1);         fclose(stream1);         stream1=fopen(GPIO_DIR"gpio45/direction","r+");     }     fwrite("out",sizeof(char),3,stream1);   /* P8.11端口为输出*/     fclose(stream1); 
     for (i=0;i<10;i++)     /*10次闪亮*/     {         stream=fopen(GPIO_DIR"gpio44/value","r+");   /* P8.12端口输出1*/         fwrite("1",sizeof(char),1,stream);         fclose(stream);         stream1=fopen(GPIO_DIR"gpio45/value","r+");  /* P8.11端口输出0*/         fwrite("0",sizeof(char),1,stream1);         fclose(stream1);         sleep(1);         stream=fopen(GPIO_DIR"gpio44/value","r+");   /* P8.12端口输出0*/         fwrite("0",sizeof(char),1,stream);         fclose(stream);         stream1=fopen(GPIO_DIR"gpio45/value","r+");  /* P8.11端口输出1*/         fwrite("1",sizeof(char),1,stream1);         fclose(stream1);         sleep(1);     }     stream=fopen(GPIO_DIR"gpio44/value","r+");  /* P8.12端口输出0*/     fwrite("0",sizeof(char),1,stream);     fclose(stream);     stream1=fopen(GPIO_DIR"gpio45/value","r+");  /* P8.11端口输出0*/     fwrite("0",sizeof(char),1,stream1);     fclose(stream1); 
     stream=fopen(GPIO_DIR"unexport","w");     /*关闭端口*/     fwrite("44",sizeof(int),2,stream);     fclose(stream);     stream1=fopen(GPIO_DIR"unexport","w");     fwrite("45",sizeof(int),2,stream1);     fclose(stream1); } 
 写完这个程序,再想想第四课shell脚本,感觉shell 脚本几条命令就搞定了,C++这些 高级语言的好处根本感觉不到一样。    可能细心的人会发现程序中sizeof(int)和sizeof(char),为什么不统一用,其实我也是随意用的,因为之前也提过,在32位系统的BBB板用这两个做为常数,暂时没发现有什么区别,都是计算为4个字节。以后真发现不同用处再说吧,这些函数我也是不熟悉用法,能把BBB板搞起来就行了。 
 4、在C程序中嵌入shell命令或shell脚本简化实现功能 有了编程比较,的确shell命令在一些场合非常实用和简洁,我也查了相关函数,通过system()函数就可以把shell命令嵌入到C语言中执行,这里只是做个示范,当了解和学习下。比如上面C程序中最后几行关闭相应GPIO端口的代码:     stream=fopen(GPIO_DIR"unexport","w");     fwrite("44",sizeof(int),2,stream);     fclose(stream);     stream1=fopen(GPIO_DIR"unexport","w");     fwrite("45",sizeof(int),2,stream1);     fclose(stream1); 
 就可通过以下两行代码实现: 
     system("echo 44 > /sys/class/gpio/unexport"); system("echo 45 > /sys/class/gpio/unexport"); 
 由于要使用system()函数,所以得增加头文件: #include <stdlib.h> 
 自己测试下,保证成功! 5、传递文件数据至字符数组 打开GPIO端口后,我们如何读取和使用value 和 direction 的值,我试了好几个读写函数,才最终实现传递到变量中,以备使用。如果使用fgetc()函数,只能读取一个字符,像value这个文件只有一个0或1的就可以,后来才确认使用fscanf()比较完整,不过传递的变量必须定义字符串数组,否则读direction就只能读出第一个字符。 fscanf(文件指针,读取格式,变量),其中文件指针就是我们打开的文件,读取格式我这里取 %s 指输入数据为以空格字符为终止的字符串,变量就是定义的字符串数组名。 
 测试程序led4.cpp如下:(默认已经打开gpio44端口) #include <stdio.h> #define GPIO_DIR "/sys/class/gpio/" int main() {     FILE *stream=NULL;     FILE *stream1=NULL;     int i=0; char a[10],b[10]; 
     stream=fopen(GPIO_DIR"gpio44/value","r+");       fscanf(stream,"%s",a);        /*读出数据传递到 a */     printf("value:%s \n",a);      /*输出到屏幕 */ 
     stream1=fopen(GPIO_DIR"gpio44/direction","r+");     fscanf(stream1,"%s",b);        /*读出数据传递到 b */ printf("direction:%s \n",b);  /*输出到屏幕 */ 
     fclose(stream);     fclose(stream1);     return 0; } 
 编译运行结果如下: /home/binc #g++  -o led4  led4.cpp /home/binc #./led4 Value:0 Direction:in 
 |