查看: 2516|回复: 0

看看在BBB的下的C程序

[复制链接]
  • TA的每日心情
    开心
    2014-2-20 10:23
  • 签到天数: 45 天

    连续签到: 1 天

    [LV.5]常住居民I

    发表于 2017-7-12 09:58:18 | 显示全部楼层 |阅读模式
    分享到:
    今天我要用C语言完成GPIO控制和串口发送数据的例子。
    编译的命令如下:

    TIM截图20170712095226.png

    上面的gpio.c内容如下:

    TIM截图20170712095256.png

    源码:
    1. <font size="3">#include <stdlib.h>  
    2.     #include <stdio.h>  
    3.     #include <string.h>  
    4.     #include <fcntl.h> //define O_WRONLY and O_RDONLY  
    5.     #define SYSFS_GPIO_DIR "/sys/class/gpio"  
    6.     #define MAX_BUF 64  

    7.     #define SLOTS "/sys/devices/bone_capemgr.9/slots"
    8.     #define UART1 "/dev/ttyO1"
    9.       
    10.     void main()  
    11.     {  
    12.         int fd, fdu, len;  
    13.         char buf[MAX_BUF];  
    14.         char ch;  
    15.         int i;  
    16.       
    17.         //export gpio66
    18.         fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);  
    19.         len = snprintf(buf,sizeof(buf),"66");  
    20.         write(fd,buf,len);  
    21.         close(fd);  
    22.       
    23.         //set direction  
    24.         snprintf(buf,sizeof(buf),SYSFS_GPIO_DIR"/gpio44/direction");  
    25.         fd = open(buf, O_WRONLY);  
    26.         write(fd, "in", 3);  
    27.         close(fd);  
    28.       
    29.         //open uart1
    30.         fdu = open(SLOTS,O_WRONLY);
    31.         len = snprintf(buf,sizeof(buf),"BB-UART1");
    32.         write(fdu,buf,len);
    33.         close(fdu);
    34.         fdu = open(UART1,O_WRONLY);
    35.         len = snprintf(buf,sizeof(buf),"iysheng");
    36.         write(fdu,buf,len);
    37.         close(fdu);

    38.         //read and print value 10 times  
    39.         snprintf(buf,sizeof(buf),SYSFS_GPIO_DIR"/gpio66/value");  
    40.         for(i=0;i<10;i++)  
    41.         {  
    42.         fd = open(buf, O_RDONLY);  
    43.             read(fd,&ch,1);  
    44.             printf("%c\n",ch);  
    45.             usleep(1000000);  
    46.        close(fd);  
    47.         }  
    48.     }</font>
    复制代码
    看看效果图,串口1正常输出

    TIM截图20170712095307.png

    终端里面正常显示,GPIO66的引脚高低电平。

    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-4-25 15:01 , Processed in 0.105286 second(s), 16 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.