本文来源电子发烧友社区,作者:刘建华, 帖子地址:https://bbs.elecfans.com/jishu_2305990_1_1.html
【目的】熟悉LED灯的驱动
从手册上查到驱动LED的说明:
应该是向文件/sys/class/gpio/export写入IO的编号,然后往sys/class/gpioXX/value里面写1或者0来控制LED的亮与灭。
1、在虚拟机work目录下面新建LED文件夹,下面新建led.c文件:
#include
#include
#include
#include
#include
int main(int argc ,char *argv[])
{
FILE *fp=NULL;
int i=0;
fp = fopen("/sys/class/gpio/export","w");
fprintf(fp,"%d",271);
fclose(fp);
fp = fopen("/sys/class/gpio/export","w");
fprintf(fp,"%d",250);
fclose(fp);
fp = fopen("/sys/class/gpio/gpio271/direction","w");
fprintf(fp,"out");
fclose(fp);
fp = fopen("/sys/class/gpio/gpio250/direction","w");
fprintf(fp,"out");
fclose(fp);
fp = fopen("/sys/class/gpio/gpio43/direction","w");
fprintf(fp,"out");
fclose(fp);
for(i=0;i<100;i++)
{
fp = fopen("/sys/class/gpio/gpio250/value","w");
fprintf(fp,"%d",1);
sleep(1);
fclose(fp);
fp = fopen("/sys/class/gpio/gpio250/value","w");
fprintf(fp,"%d",0);
sleep(1);
fclose(fp);
fp = fopen("/sys/class/gpio/gpio271/value","w");
fprintf(fp,"%d",1);
sleep(1);
fclose(fp);
fp = fopen("/sys/class/gpio/gpio271/value","w");
fprintf(fp,"%d",0);
sleep(1);
fclose(fp);
}
fp = fopen("/sys/class/gpio/gpio271/unexport","w");
fprintf(fp, "%d",271);
fclose(fp);
return 0;
}
2、编写makefile
TARGET =LED
DEST_DIR ?=_install
#CC ?=arm-linux-gcc
CC =arm-linux-gnueabihf-gcc-5.3.1
build:
@(CC) -o (TARGET) led.c
hold:
[url=home.php?mod=space&uid=70594]@echo[/url] "LED hold[skip]"
install:
[url=home.php?mod=space&uid=2293869]@MKDIR[/url] -p $(DEST_DIR)
@cp (TARGET) (DEST_DIR)
uninstall:
@rm -fr (DEST_DIR)/(TARGET)
distclean: uninstall
@rm -fr $(TARGET)
@rm -fr _install
编译好后上传开发板,运行,LED5、LED6交替闪烁。
全部0条评论
快来发表一下你的评论吧 !