Results 1 to 4 of 4

Thread: I2C control question

  1. #1
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question I2C control question

    Hi everyone,

    I am currently writing a program on Qt to communicate RTC (DS1307) for my Gumstix Overo project. (My final goal is to let Gumstix talk to PIC)

    Here is my code:
    Qt Code:
    1. // for i2c test
    2. #include <sys/types.h>
    3. #include <sys/stat.h>
    4. #include <sys/ioctl.h>
    5. #include <fcntl.h>
    6. #include <linux/i2c-dev.h>
    7.  
    8. void MainScr::on_pushButton_11_clicked()
    9. {
    10. int i2c;
    11. int ioc;
    12. unsigned char buf[2];
    13.  
    14. if ((i2c = open("/dev/i2c-3", O_RDWR)) < 0)
    15. ui->label_i2c_rd->setText("- " + QString::number(i2c));
    16. else
    17. ui->label_i2c_rd->setText("+ " + QString::number(i2c));
    18.  
    19. // tell the driver we want the device with address 0x68 on the I2C bus
    20. if ((ioc = ioctl(i2c, I2C_SLAVE, 0x68)) < 0)
    21. ui->label_i2c_rd_2->setText("- " + QString::number(ioc));
    22. else
    23. ui->label_i2c_rd_2->setText("+" + QString::number(ioc));
    24.  
    25. // write 5 to register 0x03
    26. buf[0] = 0x03; // register address
    27. buf[1] = 0x05; // register value
    28. write(i2c, buf, 2);
    29.  
    30. // read 1 bytes from register 0x03
    31. read(i2c, buf, 1);
    32. ui->label_i2c_rd_3->setText("Today is: " + QString::number(buf[0]));
    33. //i2c = close();
    34. }
    To copy to clipboard, switch view to plain text mode 

    After run the program, i got i2c > 0. this means i2c-3 opened.
    However, I got ioc = 0. is this value correct?

    and also, the value in register 0x03 is always 3, not 5.

    Can anyone point out where i got wrong in my program please?
    Thanks in advance.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: I2C control question

    After run the program, i got i2c > 0. this means i2c-3 opened.
    If you say so.
    However, I got ioc = 0. is this value correct?
    It indicates success according to my man pages.

    and also, the value in register 0x03 is always 3, not 5.
    With your read, how does the device know to return register 3? Does it return the last register written? Do you need to send another device/register address?

  3. #3
    Join Date
    Apr 2012
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: I2C control question

    Hi

    I would be interested to learn how to setup the Qt environment on the Gumstix Overo.
    As for the I2C issue, google returns lots of pages about I2C communication ...

    Brahim

  4. #4
    Join Date
    Dec 2011
    Posts
    27
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: I2C control question

    Just a point here...

    You put data into a buffer, then call write() without checking its return value, and then call read() with the same buffer without checking its return value. Then, you happen to print out the first byte which just happens to have the exact same value as the first byte you wrote into the buffer. Why not check the return value of read() to see how many bytes were actually read from the file?

Similar Threads

  1. QT - Navigational Control.
    By Girija in forum Qt Programming
    Replies: 6
    Last Post: 23rd September 2010, 14:36
  2. Replies: 0
    Last Post: 16th December 2009, 09:45
  3. How to rotate the control?
    By tszzp in forum Qt Programming
    Replies: 9
    Last Post: 25th November 2009, 09:05
  4. Control name
    By sonuani in forum Newbie
    Replies: 7
    Last Post: 22nd February 2008, 10:28
  5. UIC eol control
    By Max Yaffe in forum Qt Tools
    Replies: 0
    Last Post: 13th July 2007, 20:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.