一个完整的使用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. Go 语言defer用法

    defer延迟调用: 1.确保调用在函数结束时发生: 2.defer列表为先进后出: 3.通常在Open/Close  Lock/Unlock中使用. defer调用顺序示例: package mai ...

  2. eclipse设置高亮显示的颜色

    设置高亮显示的颜色:Window-->preferences-->General-->Editors-->Text Editors-->Annotations--> ...

  3. Spring之rmi实例演示

    环境介绍:本文中服务端客户端使用的都是ssm框架,配置文件分为spring_servlet.xml,spring_service.xml,mybatis.xml 在spring里面使用rmi完成远程调 ...

  4. KestrelHttpServer

    source code of Kestrel of documentation https://github.com/aspnet/KestrelHttpServer https://github.c ...

  5. ios 发布相关材料记录

    1 app icon Spotlight iOS 5,6 base: 29pt, 需要 @1x, @2x, @3x,得出:29 x 29, 58 x 58, 87 x 87 iOS 7,8 base: ...

  6. jsonp 实现跨域例子

    直接上代码: js: <html> <head> <title>JSONP</title> </head> <script src = ...

  7. 如何用wamp配置多域名虚拟目录

    一.前言 自从重装了最新版wamp64后就不可用了,下面来介绍我的解决方法,两者均可尝试,差别并不大! 二.配置虚拟主机 1.首先打开apache的配置文件httpd.conf (路径\wamp64\ ...

  8. Linux嵌入式 -- 内核 - 内存管理

    1.  逻辑地址 线性地址 物理地址 段式管理: 16位CPU,20根地址总线,可寻址1M内存,但是只有16位的寄存器,64K. 逻辑地址  =  段基地址 + 段内偏移地址 物理地址 PA  = 段 ...

  9. poj 2478 Farey Sequence 欧拉函数前缀和

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K       Description The Farey Sequence Fn for ...

  10. 在openstack环境中安装rackspace private cloud --1 环境准备

    在一个openstack环境中安装rackspace private cloud, 环境准备: 在good-net网络中创建3个虚拟机vm Network Detail: good-net Netwo ...