一个完整的使用GPIO捕捉中断的程序:

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<poll.h>

#define MSG(args...) printf(args) 

//函数声明
static int gpio_export(int pin);
static int gpio_unexport(int pin);
static int gpio_direction(int pin, int dir);
static int gpio_write(int pin, int value);
static int gpio_read(int pin);
static int gpio_edge(int pin, int edge);

static int gpio_export(int pin)
{
    ];
    int len;
    int fd;  

    fd = open("/sys/class/gpio/export", O_WRONLY);
    )
    {
        MSG("Failed to open export for writing!\n");
        );
    }  

    len = snprintf(buffer, sizeof(buffer), "%d", pin);
    printf("%s,%d,%d\n",buffer,sizeof(buffer),len);
    )
    {
        MSG("Failed to export gpio!");
        ;
    }  

    close(fd);
    ;
}
static int gpio_unexport(int pin)
{
    ];
    int len;
    int fd;  

    fd = open("/sys/class/gpio/unexport", O_WRONLY);
    )
    {
        MSG("Failed to open unexport for writing!\n");
        ;
    }  

    len = snprintf(buffer, sizeof(buffer), "%d", pin);
    )
    {
        MSG("Failed to unexport gpio!");
        ;
    }  

    close(fd);
    ;
}
//dir: 0-->IN, 1-->OUT
static int gpio_direction(int pin, int dir)
{
    static const char dir_str[] = "in\0out";
    ];
    int fd;  

    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", pin);
    fd = open(path, O_WRONLY);
    )
    {
        MSG("Failed to open gpio direction for writing!\n");
        ;
    }  

     ?  : ], dir ==  ?  : ) < )
    {
        MSG("Failed to set direction!\n");
        ;
    }  

    close(fd);
    ;
}
//value: 0-->LOW, 1-->HIGH
static int gpio_write(int pin, int value)
{
    ";
    ];
    int fd;  

    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
    fd = open(path, O_WRONLY);
    )
    {
        MSG("Failed to open gpio value for writing!\n");
        ;
    }  

     ?  : ], ) < )
    {
        MSG("Failed to write value!\n");
        ;
    }  

    close(fd);
    ;
}
static int gpio_read(int pin)
{
    ];
    ];
    int fd;  

    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
    fd = open(path, O_RDONLY);
    )
    {
        MSG("Failed to open gpio value for reading!\n");
        ;
    }  

    ) < )
    {
        MSG("Failed to read value!\n");
        ;
    }  

    close(fd);
    return (atoi(value_str));
}
// none表示引脚为输入,不是中断引脚
// rising表示引脚为中断输入,上升沿触发
// falling表示引脚为中断输入,下降沿触发
// both表示引脚为中断输入,边沿触发
// 0-->none, 1-->rising, 2-->falling, 3-->both
static int gpio_edge(int pin, int edge)
{
const char dir_str[] = "none\0rising\0falling\0both";
char ptr;
];
    int fd;
switch(edge)
{
    :
        ptr = ;
        break;
    :
        ptr = ;
        break;
    :
        ptr = ;
        break;
    :
        ptr = ;
        break;
    default:
        ptr = ;
} 

    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/edge", pin);
    fd = open(path, O_WRONLY);
    )
    {
        MSG("Failed to open gpio edge for writing!\n");
        ;
    }  

    )
    {
        MSG("Failed to set edge!\n");
        ;
    }  

    close(fd);
    ;
}
//GPIO1_17
int main()
{
    int gpio_fd, ret;
    ];
    ];
    unsigned ;

    gpio_unexport();
    gpio_unexport();

    //p8_12 init
    gpio_export();
    gpio_direction(, );//output out
    gpio_write(, );

    //p8_11 init
    gpio_export();
    gpio_direction(, );//input in
    gpio_edge(,);
    gpio_fd = open("/sys/class/gpio/gpio45/value",O_RDONLY);
    )
    {
        MSG("Failed to open value!\n");
        ;
    }
    fds[].fd = gpio_fd;
    fds[].events  = POLLPRI;

    )
    {
        ret = read(gpio_fd,buff,);
         )
        MSG("read\n");

        ret = poll(fds,,);
         )
        MSG("poll\n");
        ].revents & POLLPRI)
        {
            ret = lseek(gpio_fd,,SEEK_SET);
             )
            MSG("lseek\n");

            //gpio_write(44, cnt++%2);
            printf("**********************************\n");
        }
        usleep();
    }
    ;
}

GPIO编程2:使用GPIO监听中断完整程序的更多相关文章

  1. Python网络编程(epoll内核监听,多任务多进程)

    OJBK    接着昨天的说 select模块内的epoll函数还没说  说完epoll和本地套接字套接字基本就没了 今天主要是多进程   理论性东西比较多  主要是理解         epoll ...

  2. Android 编程下短信监听在小米手机中失效的解决办法

    相信很多人写的短信监听应用在小米手机上是拦截不到短信的,这是因为小米对短信的处置权优先分给了系统.我们可以在短信的[设置]→[高级设置]→[系统短信优先]中发现短信的优先处理权默认是分给系统的,只要关 ...

  3. 使用GPIO监听中断

    #include<stdlib.h> #include<stdio.h> #include<string.h> #include<unistd.h> # ...

  4. (原创)用Receiver和SystemService监听网络状态,注册Receiver的两种方式

    android中网络编程不可避免地要监听网络状态,wifi或者3G以及以太网,并根据当前状态做出相应决策. 在MyReceiver类中的onReceive方法中获得系统服务 ConnectivityM ...

  5. Java并发编程的艺术(六)——中断、安全停止线程

    什么是中断 Java的一种机制,用于一个线程去暂停另一个线程的运行.就是一个正在运行的线程被其他线程给打断,停止运行挂起了. 我觉得,在Java中,这种中断机制只是一种方便程序员编写进程间的通信罢了. ...

  6. Python自动化编程-树莓派GPIO编程(二)

    树莓派我们编程一般都直接用高效的python,针对于GPIO编程,python也是有这一方面的库的,这里最有名也是最常用的就是RPI.GPIO了.这个库是专门为树莓派GPIO编程所设计的,利用它你可以 ...

  7. 4.JAVA之GUI编程事件监听机制

    事件监听机制的特点: 1.事件源 2.事件 3.监听器 4.事件处理 事件源:就是awt包或者swing包中的那些图形用户界面组件.(如:按钮) 事件:每一个事件源都有自己特点有的对应事件和共性事件. ...

  8. Python黑客编程基础3网络数据监听和过滤

    网络数据监听和过滤 课程的实验环境如下: •      操作系统:kali Linux 2.0 •      编程工具:Wing IDE •      Python版本:2.7.9 •      涉及 ...

  9. 树莓派的GPIO编程

    作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁转载. 树莓派除了提供常见的网口和USB接口 ,还提供了一组GPIO(General Purpose Input/ ...

随机推荐

  1. memcpy与memmove

    函数原型: void* memcpy(void *dst,void const *src,size_t count) void* memmove(void *dst,void const *src,s ...

  2. linux下java unrecognized class file version错误的解决

    root@Mr javaPC]# java HelloWorldException in thread “main” java.lang.ClassFormatError: HelloWorld (u ...

  3. json数据的拼接与解析

    json数据格式 [{ "firstName": "Brett", "lastName":"McLaughlin", & ...

  4. YII2笔记之二

    module id / module id /.../ controller id / action idmodule id / directory / controller id / action ...

  5. Android当下最流行的开源框架总结

    Android中能够简化开发流程的一些框架 本文介绍的是一些博主在开发过程中经常用到的Android开源框架,所谓开源框架我的理解就是别人封装好的代码,可以直接拿过来使用,并且源码也全部公开的代码库. ...

  6. Microsoft SQL Server for Linux安装和配置

    虽说mssql for linux早已经出来了,但原本没有打算这么早就去尝试的,无奈之下还是得先尝试用了,这里分几篇介绍我在用mssql for linux时遇到的问题,不得不说作为先吃螃蟹的人总是要 ...

  7. .net mvc超过了最大请求长度

    项目中遇到"超过了最大请求长度"如下图所示,是因为IIS默认请求长度4M,当请求长度大于这个值的时候报错. 修改maxRequestLength:在web.config中我们修改& ...

  8. c++中函数参数传递(值传递、指针传递,引用传递)进一步认识

    概念    首先从概念上来说一下这几种函数传参方式及区别:    1.值传递:形参是实参的拷贝,改变函数形参的值并不会影响外部实参的值,这是最常用的一种传参方法,也是最简单的一种传参方法,只需要传递参 ...

  9. CentOS7 常用命名整理

    前言: 总是为了一点命令去查搜索引擎,今天简单整理了一下常用的一些命名: 一.网络配置相关 0.修改 IP&DNS 信息 # vi /etc/sysconfig/network-scripts ...

  10. Eclipse_插件_05_自动下载jar包源码插件

    一.Java Source Attacher 1.下载 官网:http://marketplace.eclipse.org/content/java-source-attacher#.U5RmTePp ...