明远智睿技术论坛
标题:
写QT程序控制LED灯的亮灭
[打印本页]
作者:
scx2006114
时间:
2019-10-18 14:10
标题:
写QT程序控制LED灯的亮灭
核心板:My-IMX6-CB200-6q-2g
评估板:
MY-IMX6-EK200
系统内核:
Linux-3.14.52
u-boot版本:
u-boot-2015.04-svn54
请问如何通过写C语言或者Qt程序控制LED的亮灭,有没有类似的教程?不是通过测试手册上面的指令控制LED的亮灭。
作者:
软件01
时间:
2019-10-19 16:24
可以参考一下下面的gpio输出和输入下降沿中断
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>
int gpio_open(int gpio)
{
FILE *fp;
if ((fp = fopen("/sys/class/gpio/export", "w")) == NULL)
{
printf("Cannot open export file.\n");
return -1;
}
fprintf(fp, "%d", gpio);
fclose(fp);
return 0;
}
int gpio_direction(int gpio)
{
FILE *fp;
char s[50]="";
sprintf(s,"/sys/class/gpio/gpio%d/direction",gpio);
if ((fp = fopen(s, "rb+")) == NULL)
{
printf("Cannot open %s.\n",s);
return -1;
}
fprintf(fp, "out");
fclose(fp);
return 0;
}
int gpio_high(int gpio)
{
FILE *fp;
char buffer[10];
char s1[50]="";
sprintf(s1,"/sys/class/gpio/gpio%d/value",gpio);
if ((fp = fopen(s1, "rb+")) == NULL)
{
printf("Cannot open %s.\n",s1);
return -1;
}
strcpy(buffer,"1");
fwrite(buffer, sizeof(char), sizeof(buffer) - 1, fp);
fclose(fp);
return 0;
}
int gpio_low(int gpio)
{
FILE *fp;
char buffer[10];
char s1[50]="";
sprintf(s1,"/sys/class/gpio/gpio%d/value",gpio);
if ((fp = fopen(s1, "rb+")) == NULL)
{
printf("Cannot open %s.\n",s1);
return -1;
}
strcpy(buffer,"0");
fwrite(buffer, sizeof(char), sizeof(buffer) - 1, fp);
fclose(fp);
return 0;
}
int main(int argc, char *argv[])
{
int fd, ret;
char value;
struct pollfd pfd;
gpio_open(435);
gpio_direction(435);
gpio_low(435);
system("echo \"444\" > /sys/class/gpio/export");
system("echo \"in\" > /sys/class/gpio/gpio444/direction");
system("echo \"falling\" > /sys/class/gpio/gpio444/edge");
// system("echo \"both\" > /sys/class/gpio/gpio443/edge");
fd = open("/sys/class/gpio/gpio444/value", O_RDWR);
if (fd < 0) {
printf("open file fail\n");
return -1;
}
pfd.fd = fd;
pfd.events = POLLPRI | POLLERR;
ret = lseek(fd, 0, SEEK_SET);
if (ret == -1) {
printf("lseek error\n");
return -1;
}
//读取1字节
ret = read(fd, &value, 1);
while (1) {
/* 监听个数1, 等待时间无限 */
ret = poll(&pfd, 1, -1);
if (ret == -1) {
printf("poll error\n");
return -1;
}
/* 监听的时间会保存在revents成员中 */
if (pfd.revents & POLLPRI) {
//文件指针只想文件的开头
ret = lseek(fd, 0, SEEK_SET);
if (ret == -1) {
printf("lseek error\n");
return -1;
}
//读取,结果为字符'0'或者‘1’
read(fd, &value, 1);
printf("value:%c\n", value);
/* if (value == '0')
gpio_high(434);
if (value == '1')
gpio_low(434); */
}
}
close(fd);
return 0;
}
欢迎光临 明远智睿技术论坛 (http://bbs.myzr.com.cn/)
Powered by Discuz! X3.2