一个完整的使用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. Linux 调优

    一.系统优化 1.硬件优化 增加内存 更换速度跟高磁盘(sata->sas)可以增加固态硬盘 更换更高校率的网卡,或者双网卡绑定,两个网卡作为一个网卡使用.服务器网卡一般为千兆 2.系统层优化 ...

  2. 缩略图悬浮效果的jQuery焦点图

    在线演示 本地下载

  3. SpringBoot 入门笔记

    1. Spring 4.3中引入了: @GetMapping @PostMapping @PutMapping @DeleteMapping @PatchMapping 2. @RequestMapp ...

  4. HDU 5183 Negative and Positive (NP) (hashmap+YY)

    学到了以邻接表方式建立的hashmap 题意:给你一串数a和一个数k,都有正有负,问知否能找到一对数(i,j)(i<=j)保证a [i] - a [i+1] + a [i+2] - a [i+3 ...

  5. Excel 如何复制粘贴一整行

    在某些时候,我们需要重复性的录入一些信息,而且表头都是一样的,一直拉列宽是相当令人烦躁的事情. 所以,就想可以直接复制黏贴出一整行,包括行的各个列宽. 具体操作如图: ——>首先,一整行复制, ...

  6. jquery02-jQuery效果=隐藏和显示+切换+淡入淡出+滑动+动画+回调+链

    隐藏和显示 $(selector).hide(speed,callback);  $(selector).show(speed,callback);   可选的 speed 参数规定隐藏/显示的速度, ...

  7. pandas的Series

    pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False) 首先介绍一下基本的: d ...

  8. 0.00-050613_boot.s

    ! boot.s ! ! It then loads the system at 0x10000, using BIOS interrupts. Thereafter ! it disables al ...

  9. JavaScript作用域新总结

    作用域是什么 当我们将变量引入程序后,这些变量住在哪里,当程序需要的时候如何找到他们?这些问题都需要一个规则来存储变量,并且之后可以方便的找到这些变量,这套规则就被称为 作用域 .(管理变量的规则) ...

  10. 小米刷机教程和GAE for android

    小米上的刷机教程:http://www.miui.com/getrom.php?r=2gae for android :https://github.com/madeye/gaeproxy/wiki