1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<unistd.h>
  5. #include<fcntl.h>
  6. #include<poll.h>
  7. #define MSG(args...) printf(args)
  8. //函数声明
  9. static int gpio_export(int pin);
  10. static int gpio_unexport(int pin);
  11. static int gpio_direction(int pin, int dir);
  12. static int gpio_write(int pin, int value);
  13. static int gpio_read(int pin);
  14. static int gpio_edge(int pin, int edge);
  15. static int gpio_export(int pin)
  16. {
  17. char buffer[64];
  18. int len;
  19. int fd;
  20. fd = open("/sys/class/gpio/export", O_WRONLY);
  21. if (fd < 0)
  22. {
  23. MSG("Failed to open export for writing!\n");
  24. return(-1);
  25. }
  26. len = snprintf(buffer, sizeof(buffer), "%d", pin);
  27. printf("%s,%d,%d\n",buffer,sizeof(buffer),len);
  28. if (write(fd, buffer, len) < 0)
  29. {
  30. MSG("Failed to export gpio!");
  31. return -1;
  32. }
  33. close(fd);
  34. return 0;
  35. }
  36. static int gpio_unexport(int pin)
  37. {
  38. char buffer[64];
  39. int len;
  40. int fd;
  41. fd = open("/sys/class/gpio/unexport", O_WRONLY);
  42. if (fd < 0)
  43. {
  44. MSG("Failed to open unexport for writing!\n");
  45. return -1;
  46. }
  47. len = snprintf(buffer, sizeof(buffer), "%d", pin);
  48. if (write(fd, buffer, len) < 0)
  49. {
  50. MSG("Failed to unexport gpio!");
  51. return -1;
  52. }
  53. close(fd);
  54. return 0;
  55. }
  56. //dir: 0-->IN, 1-->OUT
  57. static int gpio_direction(int pin, int dir)
  58. {
  59. static const char dir_str[] = "in\0out";
  60. char path[64];
  61. int fd;
  62. snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", pin);
  63. fd = open(path, O_WRONLY);
  64. if (fd < 0)
  65. {
  66. MSG("Failed to open gpio direction for writing!\n");
  67. return -1;
  68. }
  69. if (write(fd, &dir_str[dir == 0 ? 0 : 3], dir == 0 ? 2 : 3) < 0)
  70. {
  71. MSG("Failed to set direction!\n");
  72. return -1;
  73. }
  74. close(fd);
  75. return 0;
  76. }
  77. //value: 0-->LOW, 1-->HIGH
  78. static int gpio_write(int pin, int value)
  79. {
  80. static const char values_str[] = "01";
  81. char path[64];
  82. int fd;
  83. snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
  84. fd = open(path, O_WRONLY);
  85. if (fd < 0)
  86. {
  87. MSG("Failed to open gpio value for writing!\n");
  88. return -1;
  89. }
  90. if (write(fd, &values_str[value == 0 ? 0 : 1], 1) < 0)
  91. {
  92. MSG("Failed to write value!\n");
  93. return -1;
  94. }
  95. close(fd);
  96. return 0;
  97. }
  98. static int gpio_read(int pin)
  99. {
  100. char path[64];
  101. char value_str[3];
  102. int fd;
  103. snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
  104. fd = open(path, O_RDONLY);
  105. if (fd < 0)
  106. {
  107. MSG("Failed to open gpio value for reading!\n");
  108. return -1;
  109. }
  110. if (read(fd, value_str, 3) < 0)
  111. {
  112. MSG("Failed to read value!\n");
  113. return -1;
  114. }
  115. close(fd);
  116. return (atoi(value_str));
  117. }
  118. // none表示引脚为输入,不是中断引脚
  119. // rising表示引脚为中断输入,上升沿触发
  120. // falling表示引脚为中断输入,下降沿触发
  121. // both表示引脚为中断输入,边沿触发
  122. // 0-->none, 1-->rising, 2-->falling, 3-->both
  123. static int gpio_edge(int pin, int edge)
  124. {
  125. const char dir_str[] = "none\0rising\0falling\0both";
  126. char ptr;
  127. char path[64];
  128. int fd;
  129. switch(edge)
  130. {
  131. case 0:
  132. ptr = 0;
  133. break;
  134. case 1:
  135. ptr = 5;
  136. break;
  137. case 2:
  138. ptr = 12;
  139. break;
  140. case 3:
  141. ptr = 20;
  142. break;
  143. default:
  144. ptr = 0;
  145. }
  146. snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/edge", pin);
  147. fd = open(path, O_WRONLY);
  148. if (fd < 0)
  149. {
  150. MSG("Failed to open gpio edge for writing!\n");
  151. return -1;
  152. }
  153. if (write(fd, &dir_str[ptr], strlen(&dir_str[ptr])) < 0)
  154. {
  155. MSG("Failed to set edge!\n");
  156. return -1;
  157. }
  158. close(fd);
  159. return 0;
  160. }
  161. //GPIO1_17
  162. int main()
  163. {
  164. int gpio_fd, ret;
  165. struct pollfd fds[1];
  166. char buff[10];
  167. unsigned char cnt = 0;
  168. gpio_unexport(44);
  169. gpio_unexport(45);
  170. //p8_12 init
  171. gpio_export(44);
  172. gpio_direction(44, 1);//output out
  173. gpio_write(44, 1);
  174. //p8_11 init
  175. gpio_export(45);
  176. gpio_direction(45, 0);//input in
  177. gpio_edge(45,2);
  178. gpio_fd = open("/sys/class/gpio/gpio45/value",O_RDONLY);
  179. if(gpio_fd < 0)
  180. {
  181. MSG("Failed to open value!\n");
  182. return -1;
  183. }
  184. fds[0].fd = gpio_fd;
  185. fds[0].events = POLLPRI;
  186. while(1)
  187. {
  188. ret = read(gpio_fd,buff,10);
  189. if( ret == -1 )
  190. MSG("read\n");
  191. ret = poll(fds,1,0);
  192. if( ret == -1 )
  193. MSG("poll\n");
  194. if( fds[0].revents & POLLPRI)
  195. {
  196. ret = lseek(gpio_fd,0,SEEK_SET);
  197. if( ret == -1 )
  198. MSG("lseek\n");
  199. //gpio_write(44, cnt++%2);
  200. printf("**********************************\n");
  201. }
  202. usleep(5);
  203. }
  204. return 0;
  205. }

使用GPIO监听中断的更多相关文章

  1. GPIO编程2:使用GPIO监听中断完整程序

    一个完整的使用GPIO捕捉中断的程序: #include<stdlib.h> #include<stdio.h> #include<string.h> #inclu ...

  2. Dcloud HTML5 监听蓝牙设备 调用 原生安卓实现

    最近一直搞Dcloud ,这是HTML5版本的开发,打包时候,可以打包成 apk 和ipa 分别运行在安卓和ios 机器上面, 但是这里面的资料很少,遇到问题,之后只能自己钻研总结, 现在有这么一个需 ...

  3. epoll的内部实现 & 百万级别句柄监听 & lt和et模式非常好的解释

    epoll是Linux高效网络的基础,比如event poll(例如nodejs),是使用libev,而libev的底层就是epoll(只不过不同的平台可能用epoll,可能用kqueue). epo ...

  4. ORACLE 监听

    今天来学习一下监听的相关内容,昨晚被老大问了两个关于监听很简单的问题,但是却吞吞吐吐回答,而且有一个问题还答错了,刚刚查了下资料,才发现"驴头对了马嘴",哭笑不得. 一.监听(li ...

  5. 横向滑动的listview和其中用到的触摸监听事件详解

    一.首先把横向的listview的代码放上来 HorizontalListView: package com.common.cklibrary.utils.myview; import java.ut ...

  6. 从零开始学 Web 之 HTML5(三)网络监听,全屏,文件读取,地理定位接口,应用程序缓存

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  7. js监听指定元素的css动画属性

    MDN 监听css动画,开始,迭代次数,结束,中断 回调函数返回 animationEvent属性 <!DOCTYPE html> <html> <head> &l ...

  8. 迷你MVVM框架 avalonjs 学习教程15、属性监听与模块通信

    avalon的ViewModel对象从其内部EventManager里继承了三个方法,$watch.$unwatch.$fire三个方法,它们就是我们本节的主题. 词如其名,非常直白,一看就知道做什么 ...

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

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

随机推荐

  1. What's App has the Qt?

    收集了我看到的使用Qt开发的应用程序或者含有Qt库的应用程序 CNTV CNTV, 一个中央电视台的视频直播软件, 从下面卸载后的残余目录树,可以看到,存在部分库使用的就是Qt的.下面的目录树,已经删 ...

  2. android studio升级方法

    android studio 更新问题: 如果被墙则采用以下步骤: 一:看版本 help-->about    AI***************** 二:查看android studio最新版 ...

  3. code1167 树网的核

    floyd+枚举 看点: 1.floyd同时用数组p记录转移节点k,这样知道线段的端点u v就可以得到整条线段 2.任意一点c到线段a b的距离=(d[a][c]+d[c][b]-d[a][b])/2 ...

  4. 企业招聘:UX设计师需要满足他们哪些期望?

    以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具.   为了确定2017年最有价值的用户体验技能和特质,我特地参考了150多份工作要求.最后,得出了以下 ...

  5. Android 4.0关于开机启动服务

    针对使用App应用管理强制停止的App,重启系统后不能收到开机启动, 需要运行一次后,在下次再启动时,才可以正确收到.

  6. nginx 集群介绍

    nginx 集群介绍 完成一次请求的步骤 1)用户发起请求 2)服务器接受请求 3)服务器处理请求(压力最大) 4)服务器响应请求 缺点:单点故障 单台服务器资源有限 单台服务器处理耗时长 ·1)部署 ...

  7. 6、Docker Image

    6.1 什么是image 文件和meta data的集合(root filesystem) 分层的,并且每一层都可以添加.改变.删除文件,成为一个新的image 不同的image可以共享相同的laye ...

  8. solr特点一:高亮(highlighting)

    高亮的配置 参数详细说明: hl.fl: 用空格或逗号隔开的字段列表.要启用某个字段的highlight功能,就得保证该字段在schema中是stored.如果该参数未被给出,那么就会高亮默认字段 s ...

  9. .net程序员书单

    C# 基础 <CLR via C#> <c# 高级编程> 框架学习 <WPF编程宝典 > (英文名:<Pro WPF 4.5 in C#. Windows P ...

  10. bat windows10系统垃圾清理---

    @echo off color 0a title windows10系统垃圾清理--- echo ★☆ ★☆ ★☆ ★☆ ★☆★☆★☆ ★☆ ★☆ ★☆ ★☆★ echo ★☆ ★☆ ★☆ ★☆ ★☆ ...