在QT上的实现,主要实现了,24CXX的 读与写(参照应用例子),数据是写成功,不过读的数据在Qt上显示是字符的ASCII码。主要是想结合着linux从最底下到最上面完全弄懂IIC,因为IIC太 重要了。 贴出myiic.h头文件,需要用到mini2440里面的两个文件,24cxx.h和24cxx.c(在这里将其改为.cpp要不编译不过)
#ifndef MYIIC_H
#define MYIIC_H
#include #include "24cXX.h"
class myiic : public QWidget {
Q_OBJECT
public: myiic();
~myiic();
void iic_layout();
void myopen_iic();
public slots:
void myread_iic();
void mywrite_iic();
void myclose_iic();
public: QLineEdit *lineedit;
QTextBrowser *textbrowser2;
QPushButton *writebutton;
QPushButton *readbutton;
QPushButton *aboutbutton;
QPushButton *closebutton;
QLabel *label1;
QLabel *label2;
};
#endif
// MYIIC_H然后是myiic.h的具体函数实现,还存在的问题就是显示的是字符的ASCII码,大家可以看到我在数据类型转换那(注释部分),转来转去,显示的还是ASCII码。不知道怎么改了,于是去看底下一点的程序,关键还是要改底下的程序才可以,因此才会发去前面的感慨。
#include "myiic.h"
struct eeprom iic_e;
myiic::myiic()
{
iic_layout();
myopen_iic();
connect(readbutton,SIGNAL(clicked()),this,SLOT(myread_iic()));
connect(writebutton,SIGNAL(clicked()),this,SLOT(mywrite_iic()));
connect(closebutton,SIGNAL(clicked()),this,SLOT(myclose_iic()));
}
myiic::~myiic()
{ }
void myiic::myopen_iic()
{
int a;
a=eeprom_open("/dev/i2c/0", 0x50, EEPROM_TYPE_8BIT_ADDR, &iic_e);
}
void myiic::myread_iic()
{
int ch,i;
int size=256;
int addr;
for(i=0;i<size;i++,++addr)
{
ch = eeprom_read_byte(&iic_e, addr);
QString s;
s=QString::number(ch);
//char *a=s.toLatin1().data();
//QByteArray temp=s.toAscii();
//char *a=temp.data();
//s=QString(a); textbrowser2->insertPlainText(s);
}
}
void myiic::mywrite_iic()
{
int i;
int addr;
QString temp=lineedit->text();
char *datas=temp.toLatin1().data();
for(i=0,addr=0; i<256; i++, addr++)
{
eeprom_write_byte(&iic_e, addr, *datas); datas++;//地址加
}
}
void myiic::myclose_iic()
{
eeprom_close(&iic_e); close();
}
void myiic::iic_layout()
{
lineedit=new QLineEdit(this);
lineedit->setText("12345678abcd");
lineedit->setMaximumHeight(100);
lineedit->setMaximumWidth(220);
textbrowser2=new QTextBrowser(this);
textbrowser2->setMaximumHeight(100);
textbrowser2->setMaximumWidth(220);
writebutton=new QPushButton;
writebutton->setText("write Data into EEPROM via IIC");
readbutton=new QPushButton; readbutton->setText("Read EEPROM Data via IIC");
label1=new QLabel; label1->setText("Status OK");
aboutbutton=new QPushButton("About");
closebutton=new QPushButton("Close");
QHBoxLayout *hlayout=new QHBoxLayout;
hlayout->addWidget(label1);
//hlayout->addWidget(aboutbutton);
hlayout->addWidget(closebutton);
QVBoxLayout *vlayout=new QVBoxLayout;
vlayout->addWidget(writebutton);
vlayout->addWidget(lineedit);
vlayout->addWidget(readbutton);
vlayout->addWidget(textbrowser2);
vlayout->addLayout(hlayout);
setLayout(vlayout);
}