mình mới sắm con mini2440 về tìm hiểu , đã tạo được ứng dụng điều khiển 4 con led bên trong kit và biên dịch lại nhân để thiết lập GPIO support nhưng mình lại không biết cách viết chưong trình điều khiển GPIO, xin mọi người chỉ giáo giúp( mò mãi vẫn không được), nếu có 1 ví dụ về tạo 1 ứng dụng đọc nút nhấn ở mạch ngoài thông qua GPIO rồi hiển thị trên màn hình( đọc input) đồng thời nhấn nút trên màn hình thì 1 led ở mạch bên ngoài sáng thông qua GPIO ( xuất output) thì càng tốt. Xin cám ơn mọi người
Thông báo
Collapse
No announcement yet.
cần giúp về GPIO của mini2440
Collapse
X
-
mọi người cho hỏi ở kit mini2440 thì mình khai báo và đọc trạng thái 1 chân gpio thì làm như thế nào, mình làm hoài mà ko được
ví dụ như chân số 9 là gpio160 đang ở mức 1 thì muốn để vào lệnh if( gpio160==1) để thực lệnh nào đó
nếu được thì cho mình xin code mẫu càng tốt, thank nhiều
Comment
-
của bạn đây, cái này mình viết từ hồi làm đồ án:
file gpio.h
Code:/**************************************************************************** ** ** Copyright (C) 2010 FPT University FUEMES group. ** All rights reserved. ** Contact: FPT University fpt.edu.vn | ** ** This file is part of the demonstration applications of the FUEMES mini2440 client ** ** $FUEMES_BEGIN_LICENSE:LGPL$ ** Commercial Usage ** Licensees holding valid Qt Commercial licenses may use this file in ** accordance with the Qt Commercial License Agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and Nokia. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: GNU Lesser General Public License v2.1 - GNU Project - Free Software Foundation. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: The GNU General Public License v3.0 - GNU Project - Free Software Foundation (FSF). ** ** If you have questions regarding the use of this file, please contact ** FUEMES group at fuemes@gmail.com. ** $FUEMES_END_LICENSE$ ** ****************************************************************************/ #ifndef GPIO_H #define GPIO_H #include <qstring.h> #include <qobject.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> //Header pin number definitions #define PIN9 160 #define PIN10 161 #define PIN11 162 #define PIN12 163 #define PIN13 164 #define PIN14 165 #define PIN15 166 #define PIN16 192 #define PIN17 193 #define PIN18 195 #define PIN19 197 #define PIN20 198 #define PIN21 199 #define PIN22 201 #define PIN23 202 #define PIN24 203 #define PIN25 139 #define PIN26 140 #define PIN27 141 #define PIN28 194 #define PIN29 142 #define PIN30 143 #define PIN31 32 #define PIN32 33 #define PIN33 233 #define PIN34 234 class GPIO : public QObject { Q_OBJECT public: GPIO(int pin); //pin is the pin nuber on con4 of the FriendlyARM board ~GPIO(); enum Direction {In,Out,Err}; int openPin(); int closePin(); int setDirection(Direction direction); //set direction in/out. returns 0 if ok and -1 on error int getDirection(); //returns direction int setState(bool state); void setValue(bool value); bool getState(); public: private: Direction _direction; int _state; int _pinNumber; QString _directionString; QString _valueString; QString _strPin; }; #endif // SERIALPORT_H
Code:#include "mini2440Gpio.h" //-------------------------------------------------------------------------------------------------------- GPIO::GPIO(int pin) { _pinNumber = pin; _valueString = QString("/sys/class/gpio/gpio%1/value").arg(pin); _directionString = QString("/sys/class/gpio/gpio%1/direction").arg(pin); _strPin = QString("%1").arg(pin); } //-------------------------------------------------------------------------------------------------------- GPIO::~GPIO() { FILE * fp; //This will create the folder /sys/class/gpio/gpio37 if ((fp = fopen("/sys/class/gpio/unexport", "ab")) == NULL) return; rewind(fp); //Set pointer to begining of the file fwrite(_strPin.toLatin1(), sizeof(char),_strPin.length() , fp); fclose(fp); } //-------------------------------------------------------------------------------------------------------- int GPIO::openPin() { FILE * fp; //This will create the folder /sys/class/gpio/gpio37 if ((fp = fopen("/sys/class/gpio/export", "ab")) == NULL) return -1; rewind(fp);//Set pointer to begining of the file fwrite(_strPin.toLatin1(), sizeof(char),_strPin.length() , fp); fclose(fp); return 0; } //-------------------------------------------------------------------------------------------------------- int GPIO::closePin() { FILE * fp; //This will create the folder /sys/class/gpio/gpio37 if ((fp = fopen("/sys/class/gpio/unexport", "ab")) == NULL) return -1; rewind(fp);//Set pointer to begining of the file fwrite(_strPin.toLatin1(), sizeof(char),_strPin.length() , fp); fclose(fp); return 0; } //-------------------------------------------------------------------------------------------------------- int GPIO::setDirection(Direction direction) { //set direction in/out. returns 0 if ok and -1 on error FILE * fp; if ((fp = fopen(_directionString.toLatin1(), "rb+")) == NULL) return -1; rewind(fp);//Set pointer to begining of the file if(direction == In) fwrite("in", sizeof(char),2, fp); if(direction == Out) fwrite("out", sizeof(char),3, fp); fclose(fp); return 0; } //-------------------------------------------------------------------------------------------------------- /* int GPIO::getDirection() {//returns direction }*/ //-------------------------------------------------------------------------------------------------------- int GPIO::setState(bool state) {//state is 0 or 1. No effect if other value. returns the new state or -1 on error FILE * fp; if ((fp = fopen(_directionString.toLatin1(), "rb+")) == NULL) return -1; rewind(fp);//Set pointer to begining of the file if(state) fwrite("high", sizeof(char),4, fp); else fwrite("low", sizeof(char),3, fp); fclose(fp); return 0; } void GPIO::setValue(bool value) { char set_value[4]; FILE * fp; if ((fp = fopen("/sys/class/gpio/gpio33/value", "rb+")) == NULL) { printf("Cannot open value file.\n"); return; } //Set pointer to begining of the file rewind(fp); if (value) { //Write our value of "1" to the file strcpy(set_value,"1"); fwrite(&set_value, sizeof(char), 1, fp); fclose(fp); printf("...value set to 1...\n"); } else { //Set pointer to begining of the file rewind(fp); //Write our value of "0" to the file strcpy(set_value,"0"); fwrite(&set_value, sizeof(char), 1, fp); fclose(fp); printf("...value set to 0...\n"); } return; } //-------------------------------------------------------------------------------------------------------- bool GPIO::getState() { //returns 1 or 0 - the pin state. Returns -1 on error FILE * fp; char value; if ((fp = fopen(_valueString.toLatin1(), "rb+")) == NULL) return false; rewind(fp);//Set pointer to begining of the file fread(&value, sizeof(char),1, fp); fclose(fp); if(value=='1') return true; if(value=='0') return false; return false; } //--------------------------------------------------------------------------------------------------------
khởi tạo:
gpio = new GPIO(PIN20);
gpio ->openPin();
gpio ->setDirection(GPIO::Out);
hủy:
gpio ->closePin();
delete gpio ;
truy cập:
gpio ->setState(true);
Comment
Bài viết mới nhất
Collapse
-
Trả lời cho Tiếng Anh cho người Việtbởi hankhungdtMình thỉnh thoảng cũng làm việc với người nước ngoài nói tiếng Anh thì toàn ghép nhặt từ, cộng với quơ tay quơ chân để diễn tả, hix. Nỗi khổ là không biết cách để mô tả sự việc. Tôi muốn tìm các bài giảng tiếng anh nào phù hợp...
-
Channel: Tâm tình dân kỹ thuật
hôm nay, 08:43 -
-
Trả lời cho Giúp em về mạch MPPT và Solar Panel với!?bởi ti500Chúc mừng bác đã chỉnh sửa thành công, nhưng theo em thì video hơi rối nếu bác muốn gửi cho bên supplier xem.
Nếu là em thì em chỉ cần nối dây để hiển thị liên tục điện áp ở ngõ ra là 220Vdc trước khi gắn LED, kế đến là cắm...-
Channel: Điện tử công suất
hôm nay, 07:39 -
-
Trả lời cho Giúp em về mạch MPPT và Solar Panel với!?bởi tuyennhanTrước khi có đủ thực lực thì chỉ làm vì mình thôi đừng nghĩ đến chuyện khác cái mạch toàn dãi tiện sử dụng nhưng tỷ lệ hư hỏng phải bảo hành cũng cao hơn ,lợi bất cập hại .
-
Channel: Điện tử công suất
hôm nay, 07:22 -
-
Trả lời cho Giúp em về mạch MPPT và Solar Panel với!?bởi dinhthuong80Vâng, em biết chứ bác, thực tế thì có rất nhiều điều tế nhị rất khó áp dụng được những gì tốt đẹp nhất mà chúng ta tìm ra (và nhiều khi mình nghĩ là tốt nhưng lại không tốt cho người khác). Tuy nhiên, ĐT đã và đang chọn sự sẻ...
-
Channel: Điện tử công suất
Hôm qua, 13:01 -
-
Trả lời cho Giúp em về mạch MPPT và Solar Panel với!?bởi dinhthuong801. ĐT đã thêm giải thích video test vấn đề gì, bạn xem lại phần cuối bài #69 giúp nhé!
2. Vì không muốn gỡ cái jump của họ ra bởi thêm thao tác chỉnh sửa, đồng thời để thấy rõ rằng mạch đang hoạt động, dù có thay đổi giới...-
Channel: Điện tử công suất
Hôm qua, 12:44 -
-
Trả lời cho Giúp em về mạch MPPT và Solar Panel với!?bởi tuyennhanBác Vị đã nhắc khéo rất mong dinhthuong nhận ra việc trên còn rất nhiều vấn đề đàng sau nhất là trong kinh doanh , sản xuất .
-
Channel: Điện tử công suất
Hôm qua, 07:53 -
-
Trả lời cho Giúp em về mạch MPPT và Solar Panel với!?bởi vi van phamKhông liên quan gì đến solar MPPT của Dinhthuong, vì bây giờ tôi xin phép nói chuyện riêng với nhac sỹ và các bạn thích chế cháo:
1- Bà xã tôi làm mạch nhốt muỗi vào chai nước uống, không thấy hết muỗi mà thấy ngày càng nhiều muỗi,...-
Channel: Điện tử công suất
11-02-2025, 21:28 -
-
Trả lời cho Giúp em về mạch MPPT và Solar Panel với!?bởi ti500Xem video hình như bác vẫn dùng jumper để chuyển đổi điện áp? Chưa hiểu bác mod lại thế nào. Mạch này xài pin nên chắc cần hiệu suất cao, nếu autovolt thì theo bác tổn hao thêm bao nhiêu là chấp nhận được?
-
Channel: Điện tử công suất
11-02-2025, 19:23 -
-
Trả lời cho Giúp em về mạch MPPT và Solar Panel với!?bởi dinhthuong80Vâng bác, giờ đèn bắt và giệt muỗi quá trời, rồi máy đuổi muỗi Doctor Ho,... mà chả cái nào ra hồn!
Led cực tím 365nm thì thua ạ, vì máy test màu nguồn ánh sáng công ty cháu chỉ đo được từ 380nm - 780nm thôi....-
Channel: Điện tử công suất
11-02-2025, 12:50 -
-
Trả lời cho Giúp em về mạch MPPT và Solar Panel với!?bởi vi van phamUống rượu 1 mình ko có chuyện gì để nói, chợt nghỉ ra đang sửa cái đèn bắt muỗi. Nhờ đinhthuong giúp đỡ cho biết cái led nào là 365nm.
Ngày xưa sửa cho sếp cái đèn, đổ ra rất nhiều muỗi trong đó. Ngày nay mua cái đèn chẳng...-
Channel: Điện tử công suất
11-02-2025, 03:20 -
Comment