飞凌嵌入式
直播中

damiaa

12年用户 59经验值
擅长:电源/新能源 嵌入式技术 控制/MCU
私信 关注
[技术]

【飞凌OK113i-S开发板试用】应用程序编写控制gpio和led

【飞凌OK113i-S开发板试用】应用程序编写控制gpio和led

环境:

搭建虚拟机,创建共享文件夹,在共享文件夹中用vscode编写。搞了个sh文件出来编译。用sfpt工具下载编译的程序到设备板子。
7.png

`4{I{V_L1}9)CT28ALDI~DY.png
2.png
@TSOEXV7OR%Z0)3E}L5){P8.png

在设备的调试串口下试试gpio和心跳的led怎么控制

gpio203的控制 就是哪个在纽扣电池边上的

3.png

核心板心跳led的控制

4.png

程序编写

6.png

gpio203的控制部分的程序编写

5.png

核心板心跳部分的控制部分的程序编写

6.png

完整代码如下


#include<stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

#include <string.h>

int main(int argc,char **argv[])

{

printf("--- hello gpio 203---! \n");

//control gpio203

FILE *export_f =fopen("/sys/class/gpio/export","w");

if(export_f == NULL){

perror("Error to open GPIO export file");

return -1;

}

fprintf(export_f,"203");

fclose(export_f);

char direction_p[50];
snprintf(direction_p,sizeof(direction_p),"/sys/class/gpio/gpio203/direction");
FILE *d_f =fopen(direction_p,"w");
if(d_f == NULL){
	perror("Error to open gpio203 direction file");
	return -1;
}
fprintf(d_f,"out");
fclose(d_f);

char value_p[50];
char cat_command[100];
snprintf(value_p,sizeof(value_p),"/sys/class/gpio/gpio203/value");
snprintf(cat_command,sizeof(cat_command),"cat %s",value_p);
FILE *value_f =fopen(value_p,"w");
if((value_f ==NULL)){
	perror("Error to open gpio203 value file");
	return -1;
}
for(int i=0;i<6;i++){
	fprintf(value_f,"1");
	fflush(value_f);
	system(cat_command);
	sleep(2);
	fprintf(value_f,"0");
	fflush(value_f);
	system(cat_command);
	sleep(2);
}
fclose(value_f);

FILE *unexport_f = fopen("/sys/class/gpio/unexport","w");
if(unexport_f == NULL){
	perror("Error to open gpio unexport file");
	return -1;
}
fprintf(unexport_f,"203");

//control heartbeep
printf("--- hello gpio heartbeat leds---! \\\\n");
char brightness_p[50];
char cat_command2[100];
snprintf(brightness_p,sizeof(brightness_p),"/sys/class/leds/heartbeat/brightness");
snprintf(cat_command2,sizeof(cat_command2),"cat %s",brightness_p);
FILE *v_f =fopen(brightness_p,"w");
if((v_f ==NULL)){
	perror("Error to open heartbeat leds brightness file");
	return -1;
}
for(int i=0;i<6;i++){
fprintf(v_f,"1");
fflush(v_f);
system(cat_command2);
sleep(2);
fprintf(v_f,"0");
fflush(v_f);
system(cat_command2);
sleep(2);
}
fclose(value_f);


return 0;

}


编译测试下载到板子看结果

7.png

8

更多回帖

发帖
×
20
完善资料,
赚取积分