How to use I2C functions to interface DS1307 with qt ?

  • Hello
    Problem Statement : To read data (calendar/time data) from DS 1307 in Qt
    My approach: I am using wiring-pi serial functions (digitalwrite, digital read, pinmode) to interface DS1307 by suitably turning on / off the pins of my hardware board and reading the data

    Problem Encountered so far: While using serial functions I m not able to read correct data from DS1307 everytime. Sometimes I read FF instead of correct data.

    Question - Is there any other way of reading correct data from DS1307 (using I2C functions or others)?

    Relevant Code of read function-

    unsigned char I2C_READ(void)
        unsigned char read=0,kk=0;
        for(int u=0;u<8;u++)
        int fd= 1;
        int x= wiringPiI2CRead(fd);
        read = (read | 1);
        read=(read | 0);
        return read;

  • @SHUBHAM-SINGH-RAO said in How to use I2C functions to interface DS1307 with qt ?:

    reading correct data from DS1307 (using I2C functions

    is there any reason you're not using the built-in I2C library from wiringPi?

  • @Pablo-J.-Rogina
    Sorry for late response
    After your reply, I have been trying to implement I2C functions but I m unable to get read any data from DS 1307.
    Here my code goes:

      { int fd;
        fd = wiringPiI2CSetup(0X68);  //0X68 is the address of     DS1307
      wiringPiI2CWriteReg8 ( fd, 0xD0, 0xD0) ;  //address of register is 0XD0
     wiringPiI2CWriteReg8 ( fd, 0X00, 0X00) ;  //address of register is 0X00
      int returnValue= wiringPiI2CReadReg8 (fd, 0X00) ;


    Response : I m getting -1 as return value which imply I m unable to read the register

  • Lifetime Qt Champion


    Did you check that the user on your device has proper access privilege for the I2C device ?

  • @SHUBHAM-SINGH-RAO have you tried reading the device with another program (not using Qt) I found this example just in case.

    Once you confirm that you can access the device (and that wiring is Ok) you can then jump into the task of integrating the wiringPi functions with the Qt framework.

  • I'm aware that this is not actually direct answer for your question, but isn't it better to have this RTC support on system a have proper time in whole system rather than control it only in your application?
    Check out this link

  • Qt Champions 2018

    @tomasz3dk This should be the correct and accepted answer :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.