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.