QT控制led


http://blog.csdn.net/guet_kite/article/details/76460522

  1.   

你好!这里是风筝的博客,

欢迎和我一起多多交流。

Windows下QT Creator版本为5.3.2

Linux下QT Creator版本为5.6

首先,在Windows下编译好应用程序:

控制LED,当然要有LED小灯的驱动了,在这里: 嵌入式Linux驱动学习笔记(一)——第一个LED驱动程序

驱动程序不变,把应用程序合并到QT程序里就好了。

Windows下打开QT Creator,

在源文件下添加led.c文件:

  1. <span style=“font-size:18px;”>#include <sys/types.h>  
  2. #include <sys/stat.h>  
  3. #include <fcntl.h>  
  4. #include <stdio.h>  
  5. #include “led.h”  
  6.   
  7. void led_on(void)  
  8. {  
  9.     int fd;  
  10.     int val = 1;  
  11.     fd = open(”/dev/led”, O_RDWR);  
  12.     if (fd < 0)  
  13.     {  
  14.         printf(”can’t open!\n”);  
  15.     }  
  16.     else  
  17.         write(fd, &val, 4);  
  18. }  
  19.   
  20. void led_off(void)  
  21. {  
  22.     int fd;  
  23.     int val = 0;  
  24.     fd = open(”/dev/led”, O_RDWR);  
  25.     if (fd < 0)  
  26.     {  
  27.         printf(”can’t open!\n”);  
  28.     }  
  29.     else  
  30.         write(fd, &val, 4);  
  31. }  
  32. </span>  
#include <sys/types.h>

include <sys/stat.h>

include <fcntl.h>

include <stdio.h>

include "led.h"

void led_on(void)
{
int fd;
int val = 1;
fd = open("/dev/led", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
else
write(fd, &val, 4);
}

void led_off(void)
{
int fd;
int val = 0;
fd = open("/dev/led", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
else
write(fd, &val, 4);
}

加了led.c文件,当然要在头文件里加led.h文件啦:

  1. <span style=“font-size:18px;”>#ifndef LED_H  
  2. #define LED_H  
  3.   
  4.   
  5. void led_on(void);  
  6. void led_off(void);  
  7.   
  8. #endif // LED_H  
  9. </span>  
#ifndef LED_H




define LED_H

void led_on(void);
void led_off(void);

endif // LED_H


接着,在mainwindow.h文件里,添加槽函数的定义,修改为如下:

  1. <span style=“font-size:18px;”>#ifndef MAINWINDOW_H  
  2. #define MAINWINDOW_H  
  3.   
  4. #include <QMainWindow>  
  5.   
  6. namespace Ui {  
  7. class MainWindow;  
  8. }  
  9.   
  10. class MainWindow : public QMainWindow  
  11. {  
  12.     Q_OBJECT  
  13.   
  14. public:  
  15.     explicit MainWindow(QWidget *parent = 0);  
  16.     ~MainWindow();  
  17.   
  18. private:  
  19.     Ui::MainWindow *ui;  
  20.   
  21. public slots:  
  22.     void open_led();  
  23.     void close_led();  
  24. };  
  25.   
  26. #endif // MAINWINDOW_H  
  27. </span>  
#ifndef MAINWINDOW_H




define MAINWINDOW_H

include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;

public slots:
void open_led();
void close_led();
};

endif // MAINWINDOW_H


最后,在mainwindow.cpp文件里,修改内容为:

  1. <span style=“font-size:18px;”>#include “mainwindow.h”  
  2. #include “ui_mainwindow.h”  
  3.   
  4. #include <qpushbutton.h>  
  5. #include “led.c”  
  6. MainWindow::MainWindow(QWidget *parent) :  
  7.     QMainWindow(parent),  
  8.     ui(new Ui::MainWindow)  
  9. {  
  10.     ui->setupUi(this);  
  11.     resize(480,272);  
  12.     QPushButton *LED_OFF = new QPushButton(“LED_OFF”,this);  
  13.     QPushButton *LED_ON = new QPushButton(“LED ON”,this);  
  14.     LED_ON->setGeometry(75, 50, 75, 40);  
  15.     LED_OFF->setGeometry(300, 50, 75, 40);  
  16.   
  17.     QObject::connect(LED_ON,SIGNAL(clicked()),this,SLOT(open_led()));  
  18.     QObject::connect(LED_OFF,SIGNAL(clicked()),this,SLOT(close_led()));  
  19. }  
  20.   
  21. MainWindow::~MainWindow()  
  22. {  
  23.     delete ui;  
  24. }  
  25.   
  26. void MainWindow::open_led()  
  27. {  
  28.     led_on();  
  29. }  
  30.   
  31. void MainWindow::close_led()  
  32. {  
  33.     led_off();  
  34. }  
  35. </span>  
#include "mainwindow.h"




include "ui_mainwindow.h"

include <qpushbutton.h>

include "led.c"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
resize(480,272);
QPushButton *LED_OFF = new QPushButton("LED_OFF",this);
QPushButton *LED_ON = new QPushButton("LED ON",this);
LED_ON->setGeometry(75, 50, 75, 40);
LED_OFF->setGeometry(300, 50, 75, 40);

QObject::connect(LED_ON,SIGNAL(clicked()),this,SLOT(open_led()));
QObject::connect(LED_OFF,SIGNAL(clicked()),this,SLOT(close_led()));

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::open_led()
{
led_on();
}

void MainWindow::close_led()
{
led_off();
}

这样在Windows下一编译,无错误,就可以了。

然后他这些源文件(8个文件把)移到Linux下:

比如我是移动到/work/qt/led目录,我的QT安装目录是/work/qt_install

现在就可以在Linux下编译程序了。

cd /work/qt/led

/work/qt_install/bin/qmake

上面这条指令就会生成Makefile.

make编译出可执行文件

之后把可执行文件放进根文件系统即可。


开发板运行结果如下:


点击一下左边按钮,开发板小灯即亮。

点击一下右边按钮,开发板小灯即灭。









  • 6
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值