本帖最后由 yzbx 于 2013-7-31 16:33 编辑
C语言参考:C语言控制CB上的绿灯
gpio配置: CubieBoard通过GPIO控制步进电机-python
或者:CubieBoard通过GPIO控制步进电机-python
当然,要感谢CB的工程师们,他们已经将 gpio集成到Android 之中,我们只要修改script.bin配置就可以了。楼主lanybass的script.bin配置,大家可以和CB扩展口手册对比。(lanybass和超级版主windland应该是同一个人!照片一样,工作相关)
给出经修改的源码cb0.c:- //源码来自:http://cn.cubieboard.org/forum.php?mod=viewthread&tid=345 再次感谢楼主马甲一号的分享!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/ioctl.h>
- #include <sys/mman.h>
- #include <sys/time.h>
- #include <fcntl.h>
- /**
- *功能 Cubieboard Linux 下用C语言控制 GPIO ,实现对板载绿灯的控制
- * 此代码只要稍微更改一下PIN口的名称也可以控制其他的设备,如继电器,步进电机。。。
- * 更多知道请用关键字 cubieboard,gpio,script.bin进行搜索
- *博客 http://hi.baidu.com/smsspy/
- */
- int main()
- {
- int fd;
- int myfd[12];
- char PinPath[]={"/sys/devices/virtual/misc/sun4i-gpio/pin/"};
- char PinName[]={"pg"};
- int PinNumber=0;
- char fullpath[100];
- char fullpath0[100];
- char addname[3]= {'\0','\0','\0'};
- int i,j;
- strcpy(fullpath,PinPath);
- //printf("fullpath=%s\n",fullpath);
- strcat(fullpath,PinName);
- //printf("fullpath=%s\n",fullpath);
- //printf("addname=%s\n",addname);
- strcpy(fullpath0,fullpath);
- //调试用代码
- /*
- printf("eg PinPath+PinName+PinNumber: %s+%s+%d\n",PinPath,PinName,PinNumber);
- printf("fullpath0=%s\n",fullpath0);
- printf("fullpath=%s\n",fullpath);
- for(i=0;i<12;i++)
- {
- PinNumber=i;
- if(PinNumber<=9)
- {
- addname[0]='0'+PinNumber;
- }
- else
- {
- addname[0]='0'+PinNumber/10;
- addname[1]='0'+PinNumber%10;
- }
- strcpy(fullpath0,fullpath);
- strcat(fullpath0,addname);
- printf("fullpath0=%s\n",fullpath0);
- }
- */
- //如果不存在sun4i-gpio,那是驱动没安装好,请自行GOOGLE一下。
- fd = open("/sys/devices/virtual/misc/sun4i-gpio/pin/pg0",O_RDWR|O_NOCTTY);
- if(fd < 0)
- {
- printf("cannot open the file!\n");
- return 0;
- }
- //60个周期,一个周期一个“呼吸”动作
- for(i=0; i<60; i++)
- {
- //发送一个1,高电平,灯亮
- write(fd,"1", 1);
- sleep(1);
- printf("change the V\n");
- //发送一个0,低电平,灯灭
- write(fd,"0", 1);
- sleep(1);
- }
- close(fd);
- //open file pg0 to pg11;
- for(i=0;i<12;i++)
- {
- PinNumber=i;
- addname[0]='0'+PinNumber;
- strcpy(fullpath0,fullpath);
- strcat(fullpath0,addname);
- printf("fullpath0=%s\n",fullpath0);
- myfd[i]=open(fullpath0,O_RDWR|O_NOCTTY);
- }
- //write file pg0 to pg11
- for(i=0;i<60;i++)
- {
- for(j=0;j<12;j++)
- {
- write(myfd[j],"1",1);
- }
- sleep(1);
- printf("change the voltage!\n");
- //发送一个0,低电平,灯灭
- for(j=0;j<12;j++)
- {
- write(myfd[j],"0",1);
- }
- sleep(1);
- }
- return 0;
- }
复制代码 在ubuntu的终端下使用如下命令:- 1)//静态交叉编译,普通编译会导致出错
- yzbx@:桌面$ arm-linux-gcc cb0.c -static -o cb0
- 2)//插上数据线,接OTG口.
- yzbx@:桌面$ lsusb
- Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
- Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
- Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
- Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
- Bus 001 Device 003: ID 04b3:3107 IBM Corp. ThinkPad 800dpi Optical Travel Mouse
- Bus 001 Device 004: ID 04f2:b257 Chicony Electronics Co., Ltd
- Bus 002 Device 003: ID 18d1:0003 Google Inc.
- 3)//查看连接设备,adb的可以不用,只要将程序拷到CB上即可。不过推荐用adb,因为更方便。
- yzbx@:桌面$ adb devices
- List of devices attached
- 20080411 device
- 4)//传送文件到/data 目录之中,yzbx文件夹是我自己建立的工作目录,程序不是在任何位置都可以运行的,在/sdcard//之中似乎就不能运行。
- yzbx@:桌面$ adb push cb0 /data/yzbx
- * daemon not running. starting it now on port 5037 *
- * daemon started successfully *
- 3352 KB/s (587550 bytes in 0.171s)
- //进入android shell环境之中
- yzbx@:桌面$ adb shell
- root@android:/ # cd /data/yzbx
- root@android:/data/yzbx # ls
- cb0
- cbjava.class
- cblight
- cbpg0
- test
- root@android:/data/yzbx # ./cb0
- change the V
- change the V
- change the V
- ...
复制代码 1)的参考 交叉编译后的C语言程序怎么运行
3)的参考 adb安装
再用万能表去测量,就可以发现示数随时间变化。
|