GPIO编程2:使用GPIO监听中断完整程序
一个完整的使用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监听中断完整程序的更多相关文章
- Python网络编程(epoll内核监听,多任务多进程)
OJBK 接着昨天的说 select模块内的epoll函数还没说 说完epoll和本地套接字套接字基本就没了 今天主要是多进程 理论性东西比较多 主要是理解 epoll ...
- Android 编程下短信监听在小米手机中失效的解决办法
相信很多人写的短信监听应用在小米手机上是拦截不到短信的,这是因为小米对短信的处置权优先分给了系统.我们可以在短信的[设置]→[高级设置]→[系统短信优先]中发现短信的优先处理权默认是分给系统的,只要关 ...
- 使用GPIO监听中断
#include<stdlib.h> #include<stdio.h> #include<string.h> #include<unistd.h> # ...
- (原创)用Receiver和SystemService监听网络状态,注册Receiver的两种方式
android中网络编程不可避免地要监听网络状态,wifi或者3G以及以太网,并根据当前状态做出相应决策. 在MyReceiver类中的onReceive方法中获得系统服务 ConnectivityM ...
- Java并发编程的艺术(六)——中断、安全停止线程
什么是中断 Java的一种机制,用于一个线程去暂停另一个线程的运行.就是一个正在运行的线程被其他线程给打断,停止运行挂起了. 我觉得,在Java中,这种中断机制只是一种方便程序员编写进程间的通信罢了. ...
- Python自动化编程-树莓派GPIO编程(二)
树莓派我们编程一般都直接用高效的python,针对于GPIO编程,python也是有这一方面的库的,这里最有名也是最常用的就是RPI.GPIO了.这个库是专门为树莓派GPIO编程所设计的,利用它你可以 ...
- 4.JAVA之GUI编程事件监听机制
事件监听机制的特点: 1.事件源 2.事件 3.监听器 4.事件处理 事件源:就是awt包或者swing包中的那些图形用户界面组件.(如:按钮) 事件:每一个事件源都有自己特点有的对应事件和共性事件. ...
- Python黑客编程基础3网络数据监听和过滤
网络数据监听和过滤 课程的实验环境如下: • 操作系统:kali Linux 2.0 • 编程工具:Wing IDE • Python版本:2.7.9 • 涉及 ...
- 树莓派的GPIO编程
作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁转载. 树莓派除了提供常见的网口和USB接口 ,还提供了一组GPIO(General Purpose Input/ ...
随机推荐
- complexHeatmap包画分类热图
用途:一般我们画热图是以连续变量作为填充因子,complexHeatmap的oncopoint函数可以以类别变量作为填充因子作热图. 用法:oncoPrint(mat, get_type = func ...
- 关闭Selinux 命令
在nginx 配置文件中,新增location中的内容,完成后,web上403报错 方法:关闭Selinux即可. Follow below steps: 虚拟机服务器环境补充: # vim/etc/ ...
- Android 下的usb框架及功能点【转】
本文转载自:https://blog.csdn.net/tianruxishui/article/details/37902959 有关USB android框架的链接 http://blog.sin ...
- poj 1330 【最近公共祖先问题+fa[]数组+ 节点层次搜索标记】
题目地址:http://poj.org/problem?id=1330 Sample Input 2 16 1 14 8 5 10 16 5 9 4 6 8 4 4 10 1 13 6 15 10 1 ...
- 算法总结之 数组的partition调整 三个值的升序
给定一个数组arr, 其中只可能有 0,1,2三个值,请实现arr排序 另一种问法: 有一个数组,只有红 蓝 黄 球,请事先红球全放在数组的左边,蓝球放中间,黄球放右边 另一种问法: 有一个数组,再给 ...
- mysql删除重复记录
Solution 1: Add Unique Index on your table: ALTER IGNORE TABLE `TableA` ADD UNIQUE INDEX (`member_id ...
- MySql增加用户、授权、修改密码等语句
1. mysql 增加新用户: insert into mysql.user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject) value ...
- jquery02-jQuery效果=隐藏和显示+切换+淡入淡出+滑动+动画+回调+链
隐藏和显示 $(selector).hide(speed,callback); $(selector).show(speed,callback); 可选的 speed 参数规定隐藏/显示的速度, ...
- QT 利用QSplitter 分割区域, 并添加QScrollArea 滚动区域,滚动条
1. QSplitter 分割区域, 可以分割区域中可以随意添加自己的布局 2. #include "dialog.h" #include <QApplication> ...
- array_merge函数的注意事项
array_merge — 合并一个或多个数组 array_merge() 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面.返回作为结果的数组 如果输入的数组中有相同的字符串键名 ...