前面的程序是在终端下,用命令行,完成的,今天用C语言完成GPIO控制和串口发送数据的例子。
编译的命令如下:
上面的gpio.c内容如下:
源码:
- #include
- #include
- #include
- #include //define O_WRONLY and O_RDONLY
- #define SYSFS_GPIO_DIR "/sys/class/gpio"
- #define MAX_BUF 64
- #define SLOTS "/sys/devices/bone_capemgr.9/slots"
- #define UART1 "/dev/ttyO1"
-
- void main()
- {
- int fd, fdu, len;
- char buf[MAX_BUF];
- char ch;
- int i;
-
- //export gpio66
- fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
- len = snprintf(buf,sizeof(buf),"66");
- write(fd,buf,len);
- close(fd);
-
- //set direction
- snprintf(buf,sizeof(buf),SYSFS_GPIO_DIR"/gpio44/direction");
- fd = open(buf, O_WRONLY);
- write(fd, "in", 3);
- close(fd);
-
- //open uart1
- fdu = open(SLOTS,O_WRONLY);
- len = snprintf(buf,sizeof(buf),"BB-UART1");
- write(fdu,buf,len);
- close(fdu);
- fdu = open(UART1,O_WRONLY);
- len = snprintf(buf,sizeof(buf),"iysheng");
- write(fdu,buf,len);
- close(fdu);
- //read and print value 10 times
- snprintf(buf,sizeof(buf),SYSFS_GPIO_DIR"/gpio66/value");
- for(i=0;i<10;i++)
- {
- fd = open(buf, O_RDONLY);
- read(fd,&ch,1);
- printf("%cn",ch);
- usleep(1000000);
- close(fd);
- }
- }
复制代码
看看效果图,串口1正常输出
|