Orange-PI-GPIO控制详细教程.pdf

所需积分/C币:0 2016-12-23 09:58:03 131KB PDF
收藏 收藏
举报

Orange-PI-GPIO控制详细教程.pdf
close fd /*设置端口方向/sys/c1ass/gpio/gpio48井 echo out> direction*/ fd =open( SYSFS GPIO RST DIR, 0 WRONLY )i if(fd==-1) printf( "ErR: Radio hard reset pin direction open error. \n "); return(EXIT FAILURE); write fd, SYSFS GPIO RST DIR VAL, sizeof(SYSFS GPIo RST DIR VAL)); /*输出复位信号:拉高18ns*/ fd =open( SYSFS GPIO RST VAL, 0 RDWR )i if(fd==-1) printf("ERR: Radio hard reset pin value open error. \n"); return (EXIT FAIlURe While( 1 write( fd, SYSFS- GPIO_ RST_ VAL H, sizeof(SYSFS_ GPIO RST VAL_H)); usleep( 1000000 ) write( fd, SYSFS GPIO RST_ VAL L, sizeof(sYSFS GPIO RST VAL L)); usleep( 1000000 ) close( fd printf(INFo: Radio hard reset pin value open error. \n"); return(e 、中断检测 #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 )i static int gpio unexport( int pin static int gpio direction( int pin, int dir static int gpio write( int pin, int value ) tatic int gpio read( int pin static int gpio export( int pin charbuffer[64]j int len int fd fd open("/sys/class/gpio/export",0_ WRONLY if( fd< 0) MSG("Failed to open export for writing! \n"3 turn(-1) len snprintf( buffer, sizeof(buffer),%", pin if write( fd, buffer, len)<0) MSG("Failed to expo rt gpio! " return(-1); (fd ) return(0)i static int gpio unexport( int pin charbuffer[64]: int 1 int fd fd =open("/ sys/class/gpio/unexport",0 WRONLY if( fd< 0 MSG( "Failed to open unexport for writing!\n"; return (-1); len snprintf( buffer, sizeof (buffer),%d", pin if write( fd, buffer, len)<0) MSG( Failed to unexport gpio! urn(-1 eturn (0 *di:8-->IN,1-->0UT*/ static int gpio direction( int pin, int dir static const char dir strll in\eout j char path [64] int snprintf( path, sizeof(path),"/sys/class/gpiogpio%d/direction", pin fd open( path,0 WRONLY ) fc MSG( Failed to open gpio direction for writing! \n"); if write( fd, &dir str[dir ==0?0:3], dir ==0?2:3)<0) MS led to set return (-1) } close( fd )i return(o) *Vaue:8-->0,1--HIGH* static int gpio write( int pin, int value static const char values str[]=01"i th[64] int fd snprintf( path, sizeof(path),"/sys/ class/gpio/gpio%/ value", pin fd open( path, 0_ WRONLY ) if( fd< 0 MSG( "Failed to open gpio value for writing! \n"), return(-1); if write( fd, &values str[value MSG(Failed to write value!\n") eturn (-1); close( fd return(o) static int gpio read( int pin charpath[64]: charvalue str[3]; int fd snprintf( path, sizeof(path),"/sys/class/gpio/gpio%d/value", pin ) fd= open( path, o RDONLY if( fd<a) MSG( "Failed to open gpio value for reading! \ n"); return(-1) if read( fd, value str, 3)<0) MSG("Failed to read value!\n"; Jrn(-1); close( fd ) return(atoi( value str )) none表示引脚为输入,不是中断引脚 * rIsing表示引脚为中断输入,上升沿触发 fa11ing表示引脚为中断输入,下降沿触发 *both表示引脚为中断输入,边沿触发 k 0-->none, 1-->rising, 2-->falling, 3-->both static int dge( int pin, int edge const char dir[="none\Orisinglofalling\oboth cha t fd switch edge case 1 p break case 2 tr=12 case 3 ptr 20 default } snprintf( path, sizeof(path),"/sys/class/gpio/gpio%d/edge", pin )i fd open( path,O WRONLY )i if( fd<0) MSG(Failed to open gpio edge for wri ting return(-1); if write( fd, &dir str[ptr], strlen( &dir str[ptr]))<0) MSG("Failed to set edge! \n"; urn(-1); close( fd return(0); /*GP工0117* int main() nt gp f d. ret truct pollfd fds[1li char buff[10] chan cnt =0: *LED引脚初始化* gpio export( 115 ) io direction 115, 1 i gpl (115,8); /*按键引脚初始化* gpio direction(49,0); ge(49,1) gpio fd= open ("/sys/class/gpio/gpio49/value",0_RDONLY ) if( gpio fd MSG(" Failed to open value!\n"; eturn (-1); } 6[0.fd gpio fd fds[e].events POL LPR工 ret read( gpio fd, buff, 10 )i if ret 1) MSG( ) e o11(fds,1,8); if ( ret ("po11\n"); if( fds[o]. revents POLLPRI ret =lseek( gpio fd, b, SEEK_ SET ) if( ret ==-1) MSG("lseek\n"); ret =read( gpio fd, buff, 10 ) if( ret MSG(read\n") gpio write( 115, cnt++ %2; usleep(199890); return(0)

...展开详情
试读 9P Orange-PI-GPIO控制详细教程.pdf
立即下载 低至0.43元/次 身份认证VIP会员低至7折
    一个资源只可评论一次,评论内容不能少于5个字
    qq81574695 资料是不错,可12分啊,太多了,希望给返点分吧。
    2019-12-07
    回复
    cs9dn003 片子!从博客copy,别人騙1分,他騙8份
    2019-03-28
    回复
    shagua1985 挺好的一个资料,解释的比较清楚。
    2018-07-11
    回复
    • 分享王者

      成功上传51个资源即可获取
    关注 私信 TA的资源
    上传资源赚积分,得勋章
    最新推荐
    Orange-PI-GPIO控制详细教程.pdf 0积分/C币 立即下载
    1/9
    Orange-PI-GPIO控制详细教程.pdf第1页
    Orange-PI-GPIO控制详细教程.pdf第2页
    Orange-PI-GPIO控制详细教程.pdf第3页

    试读已结束,剩余6页未读...

    0积分/C币 立即下载 >