本文來源電子發燒友社區,作者:劉建華, 帖子地址: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文章
134瀏覽量
16135
發布評論請先 登錄
相關推薦
評論