+ Post New Thread
Results 1 to 11 of 11
  1. #1
    Full Member level 6
    Points: 2,756, Level: 12

    Join Date
    Jan 2013
    Location
    India
    Posts
    380
    Helped
    3 / 3
    Points
    2,756
    Level
    12

    Unable to read data from RTC DS1307 using Atmega8 and mikroc.

    Hello all,

    i am designing a home potted plant watering system. For this i am using RTC DS1307 , ATMEGA8 and mikroc. I am trying to read the data from RTC. Though i am able to write the data to RTC but i believe it's could't read it back means while data is displayed on an LCD neither date nor time is getting updated . Please guide me on this. Code is as follows.

    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    
    // Software I2C connections
    sbit Soft_I2C_Scl_Output    at PORTC5_bit;
    sbit Soft_I2C_Sda_Output    at PORTC4_bit;
    sbit Soft_I2C_Scl_Input     at PINC5_bit;
    sbit Soft_I2C_Sda_Input     at PINC4_bit;
    sbit Soft_I2C_Scl_Direction at DDC5_bit;
    sbit Soft_I2C_Sda_Direction at DDC4_bit;
    // End Software I2C connections
     
    // LCD module connections
    sbit LCD_RS at PORTD4_bit;
    sbit LCD_EN at PORTD3_bit;
    sbit LCD_D4 at PORTD5_bit;
    sbit LCD_D5 at PORTD6_bit;
    sbit LCD_D6 at PORTD7_bit;
    sbit LCD_D7 at PORTB0_bit;
     
    sbit LCD_RS_Direction at DDD4_bit;
    sbit LCD_EN_Direction at DDD3_bit;
    sbit LCD_D4_Direction at DDD5_bit;
    sbit LCD_D5_Direction at DDD6_bit;
    sbit LCD_D6_Direction at DDD7_bit;
    sbit LCD_D7_Direction at DDB0_bit;
    // End LCD module connections
     
    int seconds=00,minutes=19,hours=12,day=04,date=18,month=07,year=9;
    unsigned char x1,y1;
    char welcome=1,time=1;
     
    void welcome_msg(){
                    /*Lcd_Out(1,1, "Welcome");
                    Delay_ms(100);
                    Lcd_Out(1,9, "To");
                    Delay_ms(100);
                    Lcd_Out(1,12, "SMART");
                    Delay_ms(100);
                    Lcd_Out(2,1, "Home");
                    Delay_ms(100);
                    Lcd_Out(2,6, "Irrigation");
                    Delay_ms(2000);*/
                    Lcd_Cmd(_LCD_CLEAR);
                    LCD_Out(1,1,"Date:  :  :");      //' Prepare and output static text on LCD
                    Delay_ms(10);
                    LCD_Out(2,1,"Time:  :  :");
                    Delay_ms(10);
     
                    LCD_Out(1,12,"201");
                    //Lcd_Cmd(_LCD_CLEAR);
    }
     
    void RTC_Write()
    {        seconds = Dec2Bcd (seconds);
             hours   = Dec2Bcd (hours);
             minutes = Dec2Bcd (minutes);
             day     = Dec2Bcd (day);
             date    = Dec2Bcd (date);
             month   = Dec2Bcd (month);
             year    = Dec2Bcd (year);
            
             Soft_I2C_Start();
             Soft_I2C_Write(0xD1);
             Soft_I2C_Write(0x00);
             Soft_I2C_Stop();
     
             Soft_I2C_Start();
             Soft_I2C_Write(0xD0);
             Soft_I2C_Write(0x00);
     
             Soft_I2C_Write(0x00);    // Sec
             Soft_I2C_Write(seconds);    // Sec
             Soft_I2C_Write(minutes);  // min
             Soft_I2C_Write(hours);   //Hour
     
             Soft_I2C_Write(0x01);   //week
             //Soft_I2C_Write(0x03);   //week
             Soft_I2C_Write(day);   //day
             //Soft_I2C_Write(date);  //date
             Soft_I2C_Write(Month); //month
             Soft_I2C_Write(Year);  //year
             Soft_I2C_Write(0x90);
             Soft_I2C_Stop();
            Delay_ms(5);
     
    }
     
    void RTC_Read(){
            Soft_I2C_Start();
            Soft_I2C_Write(0xD0);
            Soft_I2C_Write(0);
            Soft_I2C_Start();
            Soft_I2C_Write(0xD1);
            seconds=Soft_I2C_Read(1);
            seconds = Bcd2Dec(seconds);
            minutes=Soft_I2C_Read(1);
            minutes = Bcd2Dec(minutes);
            hours=Soft_I2C_Read(1);
            hours = Bcd2Dec(hours);
            day=Soft_I2C_Read(1);
            day = Bcd2Dec(day);
            date=Soft_I2C_Read(1);
            date = Bcd2Dec(date);
            month=Soft_I2C_Read(1);
            month = Bcd2Dec(month);
            year=Soft_I2C_Read(1);
            year = Bcd2Dec(year);
            Soft_I2C_Read(0);
            Soft_I2C_Stop();         //' Issue stop signal}
            Delay_ms(20);
    }
     
    void Display_Time(){
       Lcd_Chr(1, 6, (day / 10)   + 48);    //' Print tens digit of day variable
       Lcd_Chr(1, 7, (day % 10)   + 48);  //' Print oness digit of day variable
       Lcd_Chr(1, 9, (month / 10) + 48);
       Lcd_Chr(1,10, (month % 10) + 48);
       Lcd_Chr(1,15,  year        + 48);     //'56 Print year vaiable + 8 (start from year 2008)*/
       Lcd_Chr(2, 6, (hours / 10)   + 48);
       Lcd_Chr(2, 7, (hours % 10)   + 48);
       Lcd_Chr(2, 9, (minutes / 10) + 48);
       Lcd_Chr(2,10, (minutes % 10) + 48);
       Lcd_Chr(2,12, (seconds / 10) + 48);
       Lcd_Chr(2,13, (seconds % 10) + 48);
    }
    void main() {
     //RTC_Init();
     Soft_I2C_Init();
     Delay_ms(5);
     Lcd_Init();
     Delay_ms(10);
     Lcd_Cmd(_LCD_CLEAR);
     Delay_ms(10);
                    while(1){
                    if(welcome==1){
                    welcome_msg();
                    RTC_Write();
                    welcome=0;
                    }
     
                    RTC_Read();
                    //Transform_Time();      //' Format date and time
                    Display_Time();        //' Prepare and display on LCD
     
                    }
    }

    I am using Software I2C library in mikroc.

    •   AltAdvertisement

        
       

  2. #2
    Advanced Member level 2
    Points: 3,279, Level: 13
    baileychic's Avatar
    Join Date
    Aug 2017
    Posts
    651
    Helped
    50 / 50
    Points
    3,279
    Level
    13

    Re: Unable to read data from RTC DS1307 using Atmega8 and mikroc.

    I don't see the code of TransformTime() function. TransformTime() is needed to convert the RTC data to readable form. libstock.com has the mikroE example code for the DS1307.



  3. #3
    Super Moderator
    Points: 29,849, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,914
    Helped
    1128 / 1128
    Points
    29,849
    Level
    42
    Blog Entries
    9

    Re: Unable to read data from RTC DS1307 using Atmega8 and mikroc.

    neither date nor time is getting updated
    I can't see in your code the 7th bit of Address 00h (CH) being set anywhere, which should be done at least once after DS1307 got powered. It is responsible for effectively starting the RTC peripheral, review datasheet.

    Click image for larger version. 

Name:	StartBit.png 
Views:	5 
Size:	13.1 KB 
ID:	154467

    [EDIT]

    As paulfujo warned on post #10, this bit should be cleared !!!
    Last edited by andre_teprom; 21st July 2019 at 11:54. Reason: fixed wrong information
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )


    1 members found this post helpful.

  4. #4
    Full Member level 6
    Points: 2,756, Level: 12

    Join Date
    Jan 2013
    Location
    India
    Posts
    380
    Helped
    3 / 3
    Points
    2,756
    Level
    12

    Re: Unable to read data from RTC DS1307 using Atmega8 and mikroc.

    Thanks for the reply baileychic . But job of transformation i.e to convert data from BCD to Decimal has been done in RTC_Read function itself

    - - - Updated - - -

    Thanx Andre sir. However i have written "Soft_I2C_Write(0x00);" in RTC_Write() function. Is it wrong? Is it not working as it should be.



  5. #5
    Super Moderator
    Points: 29,849, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,914
    Helped
    1128 / 1128
    Points
    29,849
    Level
    42
    Blog Entries
    9

    Re: Unable to read data from RTC DS1307 using Atmega8 and mikroc.

    In the line 125 there is this function, disabled:

    Code:
    //RTC_Init();
    Which certainly should be some routine for checking and ensuring that the clock would be always running, by resetting bit 7 of the address 0x00, if were not. Another option could be something like that (note that regardless previous value of 7th bit, it forces to '0' asserting oscillator enabled without clearing 'seconds' register):

    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    void RTC_Init(){
        int seconds=0;
     
        Soft_I2C_Start();
        Soft_I2C_Write(0xD0);      // DS1307 address
        Soft_I2C_Write(0x00);      // REG 0 -> seconds
        Soft_I2C_Start();
        Soft_I2C_Write(0xD1);      // RD from RTC
        seconds = Bcd2Dec(Soft_I2C_Read(0)); // Read current "seconds" in DS1307
        Soft_I2C_Stop();
     
        seconds = (seconds & 0x7F);  // ensure bit CH is disabled -> enable Oscillator
     
        Delay_ms(50);
     
        Soft_I2C_Start();
        Soft_I2C_Write(0xD0);      // WR to RTC
        Soft_I2C_Write(0x00);      // REG 0 -> seconds
        Soft_I2C_Write(Dec2Bcd(seconds));     // Start oscillator with current "seconds value
        Soft_I2C_Start();
        Soft_I2C_Write(0xD0);      // WR to RTC
        Soft_I2C_Write(0x07);      // Control Register
        Soft_I2C_Write(0x80);      // Disable squarewave output pin
        Soft_I2C_Stop();
    }
    Last edited by andre_teprom; 21st July 2019 at 11:51. Reason: fixed wrong information
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



    •   AltAdvertisement

        
       

  6. #6
    Full Member level 6
    Points: 2,756, Level: 12

    Join Date
    Jan 2013
    Location
    India
    Posts
    380
    Helped
    3 / 3
    Points
    2,756
    Level
    12

    Re: Unable to read data from RTC DS1307 using Atmega8 and mikroc.

    Thank you for the suggestion sir, however i had used "Soft_I2C_Init();". I thought it will do the job for me though. This is the output on LCD
    Date: 20:07:2019
    Time: 12:38:00
    Last edited by djc; 20th July 2019 at 08:16.



  7. #7
    Full Member level 6
    Points: 2,756, Level: 12

    Join Date
    Jan 2013
    Location
    India
    Posts
    380
    Helped
    3 / 3
    Points
    2,756
    Level
    12

    Re: Unable to read data from RTC DS1307 using Atmega8 and mikroc.

    Thanks for all the help Andre sir, issue was something different. Some loose connection was there from RTC to micro controller. Little re soldering is done and now i am able to read the values back from RTC and values are getting updated too.



  8. #8
    Super Moderator
    Points: 29,849, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,914
    Helped
    1128 / 1128
    Points
    29,849
    Level
    42
    Blog Entries
    9

    Re: Unable to read data from RTC DS1307 using Atmega8 and mikroc.

    however i had used "Soft_I2C_Init();". I thought it will do the job for me though.
    Just to let you know: Soft_I2C_Init() has a different purpose, it is intended to enable the I2C bus from microcontroller`s (master) side, whereas RTC_init() would enable the DS1307 (slave) throught the just activated I2C bus; surely there are some forgotten steps you have done previously, aside of the code above to effectively enable the DS1307 device; by default its oscillator is disabled at power up.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )


    1 members found this post helpful.

    •   AltAdvertisement

        
       

  9. #9
    Full Member level 6
    Points: 2,756, Level: 12

    Join Date
    Jan 2013
    Location
    India
    Posts
    380
    Helped
    3 / 3
    Points
    2,756
    Level
    12

    Re: Unable to read data from RTC DS1307 using Atmega8 and mikroc.

    That's the great piece of information sir. Thank you for nice guidance.



  10. #10
    Advanced Member level 4
    Points: 8,249, Level: 21
    Achievements:
    7 years registered
    paulfjujo's Avatar
    Join Date
    Jun 2008
    Location
    France 01120
    Posts
    1,326
    Helped
    270 / 270
    Points
    8,249
    Level
    21

    Re: Unable to read data from RTC DS1307 using Atmega8 and mikroc.

    hello,

    don't forget , put ZERo to enable oscillator, not ONE !
    a good way to test if RTC is initialised is to connect a led + R on SQWE RTC Ouput
    the led will blink every seconde (1Hz)
    and correct data must written inside the RTC to get after , correct information reading.

    Code:
            write_ds1307(0,0x00); //Reset second to 0 sec. and Enable Oscillator
            write_ds1307(1,Dec2Bcd(minute)); //write min
            write_ds1307(2,Dec2Bcd(heure)); //write hour
            write_ds1307(3,Dec2Bcd(jS)); //write day of week
            write_ds1307(4,Dec2Bcd(jour)); // write date
            write_ds1307(5,Dec2Bcd(mois)); // write month
            write_ds1307(6,Dec2Bcd(Annee)); // write year 2014
    
          write_ds1307(7,0x10); // Sortie SQW   1Hz
           //  write_ds1307(7,0x11); // Sortie SQW   4096 Hz
          //  write_ds1307(7,0x12); // Sortie SQW   8192 Hz
           // write_ds1307(7,0x13); // Sortie SQW   32768 Hz



  11. #11
    Super Moderator
    Points: 29,849, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,914
    Helped
    1128 / 1128
    Points
    29,849
    Level
    42
    Blog Entries
    9

    Re: Unable to read data from RTC DS1307 using Atmega8 and mikroc.

    Quote Originally Posted by paulfjujo View Post
    don't forget , put ZERo to enable oscillator, not ONE !
    Thanks for the warning, I will correct the code suggested above.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



--[[ ]]--