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;
        digitalWrite(2,HIGH);
        delay(1);
        for(int u=0;u<8;u++)
        {
        read<<=1;
        pinMode(2,INPUT);
        digitalWrite(3,HIGH);
        delay(1);
        kk=digitalRead(2);
        int fd= 1;
        int x= wiringPiI2CRead(fd);
      
     if((kk)==1)
        {
        read = (read | 1);
        }
        else
        {
        read=(read | 0);
        }
        digitalWrite(3,LOW);
        delay(1);
        }
        pinMode(2,OUTPUT);
         delay(1);
        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:

     ReadI2C_function()
      { int fd;
        wiringPiSetup();
        ui->setupUi(this);
        fd = wiringPiI2CSetup(0X68);  //0X68 is the address of     DS1307
         
      wiringPiI2CWriteReg8 ( fd, 0xD0, 0xD0) ;  //address of register is 0XD0
     delay(500);
     wiringPiI2CWriteReg8 ( fd, 0X00, 0X00) ;  //address of register is 0X00
      delay(500);
      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

    Hi,

    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