EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / RTC DS1307 interfacing with 8051 in C using I2C…….

RTC DS1307 interfacing with 8051 in C using I2C…….

|

Microcontroller › 8051 › RTC DS1307 interfacing with 8051 in C using I2C…….

  • This topic has 24 replies, 21 voices, and was last updated 10 years, 5 months ago by nagar.
Viewing 10 posts - 16 through 25 (of 25 total)
← 1 2
  • Author
    Posts
  • January 25, 2013 at 2:00 pm #9047
    danesh
    Participant

    i have used the above code its works..but the clock is running very speed not according to time……….please do help me….

     

     

     

     

    Thanks in Advance

    March 3, 2013 at 8:49 am #9224
    Aniket Patel
    Participant

    Hi,

    thanks to all !

    I want to make a multiple alarm regarding to calander using ds1307

    can any budy halp me to solve it ?

    March 20, 2013 at 12:22 pm #9356
    nagalaxmi
    Participant

     

        hi,

                   write_lcd((c/16)+48)

                      write_lcd((c%16)+48);

    can any one pls explain me how is works ? am not getting logic behind it ………..

    March 22, 2013 at 5:03 am #9361
    Arun Sukumaran
    Participant

    hi nagalaxmi…..,

    try this code…. it is easy….

     

     

    unsigned char x,y;
    //hour
                    c=read_i2c(0xd0,0x02);
    x= c & 0x0F;
    x=x |’0′;
     
                            y= c & 0xF0;
    y=y>>4;
    y=y | ‘0’;
    write_lcd(y);
    write_lcd(x);
     
                            write_lcd(‘:’);
    March 23, 2013 at 4:20 am #9362
    lekh
    Participant

    hey can anybody tell me which version of Proteus one can download and from where to download some link or so

     

    June 26, 2013 at 6:21 am #10035
    Anonymous
    Guest

    RTC interfacing with 8051 using I2C

     

    this is my code…….it displays time on lcd but the time doesnt change (my rtc also not changing)…….

     

    Can someone please help me…… im stuck at this…..

     

    ******************main program**********************

     

     

    #include<reg51.h>
    #include<string.h>
    #include “lcd.h”
    #include “i2c.h”
     
     
    #define SEC   0x00
    #define MIN   0x01
    #define HOUR  0x02
    #define DATE  0x04
    #define MONTH 0x05
    #define YEAR  0x06
     
     
     
    unsigned char read(unsigned char);
    void send2lcd(unsigned char );
     
     
    void send2lcd(unsigned char value)
    {
    unsigned char buf = 0;
     
    buf = value & 0xF0; /* Filter for high byte */
    buf = (buf>>4)|(0x30); /* Convert  to ascii code */
     
    lcd_data(buf); /* Show on LCD */
     
    buf = value & 0x0F; /* Filter for low byte */
    buf = buf | 0x30;       /* Convert to ascii code */
     
    lcd_data(buf); /* Show on LCD */
    }
     
     
     
    unsigned char read(unsigned char addr)
    {
    unsigned char ret;
    start();
    write_get(0xD0);
    write_get(addr);
    start();
    write_get(0xD1);
    ret = read_get();
    stop();
    return ret;
     
    }
    void main()
    {
    unsigned char sec, min, hour,date,month,year;
     
       while(1) 
    {
      lcd_init();
      lcd_cmd(0x80);
      lcd_string(“TIME-“);
     
    /* Get Time */
    lcd_cmd(0x85);
    hour = read(HOUR);
    if(hour >= 23)
    {
    send2lcd(hour);
    }
    else
    {
    send2lcd(hour);
    }
     
     
    lcd_data(‘:’);
     
    min = read(MIN);
    send2lcd(min);
     
     
    lcd_data(‘:’);
     
    sec = read(SEC);
    if (sec <= 59)
    {
    send2lcd(sec);
    }
    else
    {
    send2lcd(0x00);
    }
     
     lcd_cmd(0xC0);
    lcd_string(“DATE-“);
     
    lcd_cmd(0xC5);
     
     
    date = read(DATE);
    send2lcd(date);
     
    lcd_data(‘/’);
     
    month = read(MONTH);
    send2lcd(month);
     
    lcd_data(‘/’);
     
    year = read(YEAR);
    send2lcd(year);
     
      }
     
    }
     
     
     
    **************i2c.h*****************
     
     
    sbit SCL = P2^0;
    sbit SDA = P2^1;
     
    void start(void);
    void stop(void);
    unsigned char read_get(void);
    void write_get(unsigned char);
    void ack();
    void noack();
    void delay(unsigned int);
     
     
    void delay(unsigned int s)
    {
    unsigned int i,j;
    for(i=0;i<=s;i++)
      for(j=0;j<=10;j++);
    }
     
     
    void start()
    {
      SDA = 1;
    SCL = 1;
    delay(10);
    SDA = 0;
    SCL = 0;
    }
     
    void write_get(unsigned char value)
    {
    unsigned char i,write;
    for(i = 0;i < 8;i++)
      {
    write = value&0x80;
    SDA = write;
    SCL = 1;
    delay(10);
    SCL = 0;
    value = value<<1;
      }
     
    SDA = 1;
    SCL = 1;
    ack();
      delay(10);
    SCL = 0;
    }
     
     
    unsigned char read_get()
    {
      unsigned char read = 0x00;
      unsigned char i;
    SDA = 1;
     
    for(i=0;i<8;i++)
    {
    SCL = 0;
    delay(20);
    SCL = 1;
    read = read| SDA;
    if(i<7)
    read = read <<1;
    }
    noack();
    return read;
     
     
    }
     
    void stop()
    {
    SDA = 0;
    SCL = 1;
    delay(10);
    SDA = 1; 
    SCL =0;
     
    }
     
    void ack()
    {
    SDA = 0; /* Clear SDA */
     
    delay(10);    
     
    SCL = 1;
    delay(10);
    SCL = 0;
     
    SDA = 1; /* Set SDA */
    }
     
     
    void noack()
    {
    SDA = 1; /* Set SDA */
     
    delay(10);
    SCL = 1;
    delay(10);
    SCL = 0;
     
    SCL = 1; /* Set SCL */
    }
     
    **********************lcd.h**************
     
    sfr lcd = 0x90;
     
    sbit rs = P3^0;
    sbit rw = P3^1;
    sbit en = P3^2;
     
    void lcd_cmd(unsigned char);
    void lcd_init();
    void lcd_data(unsigned char);
    void delay1(unsigned int);
    void lcd_string(unsigned char a[]);
     
    void lcd_init()
    {
      lcd_cmd(0x01);
    lcd_cmd(0x38);
    lcd_cmd(0x06);
    lcd_cmd(0x0E);
    }
     
    void lcd_cmd(unsigned char x)
    {
      lcd = x;
    rs = 0;
    rw = 0;
    en = 1;
    delay1(10);
    en = 0;
    }
     
    void lcd_data(unsigned char z)
    {
      lcd = z;
    rs = 1;
    rw = 0;
    en = 1;
    delay1(10);
    en = 0;
    }
     
    void delay1(unsigned int y)
    {
    unsigned int j;
    for(;y>0;y–)
    for(j=0;j<100;j++);
    }
     
    void lcd_string(unsigned char a[])
    {
    unsigned int i;
    for(i = 0;i < strlen(a);i++)
    {
    lcd_data(a);;
    }
     
    }
     
     
    June 29, 2013 at 7:55 am #10057
    Sivabalamurugan
    Participant

    i need what is i2c concept

     

    July 4, 2013 at 12:53 pm #10095
    AJISH ALFRED
    Participant

    Hi Sivabalamurugan,

    Suggest you to go through this tutorial on the following link.

    engineersgarage.com/tutorials/twi-i2c-interface

    July 5, 2013 at 5:51 am #10102
    Anonymous
    Guest

    THANK U Frnd…………………….

     

    May 28, 2015 at 4:38 am #12943
    nagar
    Participant
    /*   '_nop_ ()' ( no operation function) is used for delay purpose 
     
    'bit'   is a data tye contain only  1 or 0 (any value other then 0 treated as 1)
     
    #include<intrins.h>   is used to avoke from nop() function
     
    */
     
     
     
    #include<reg51.h>
    #include<stdio.h>
    #include<string.h>
    #include<intrins.h>
    sbit rs=P2^4; 
    sbit rw=P2^5; 
    sbit en=P2^6; 
     
    sbit SCL = P2^0;    // connect to SCL pin (Clock)
    sbit SDA = P2^1;    // connect to SDA pin (Data)
     
    unsigned char i;
    unsigned char Data;
     
    #define ACK      0
    #define NO_ACK   1
    void DelayMs(unsigned int count)
    { 
    // mSec Delay 11.0592 Mhz
       unsigned int i;                
       while(count)
        {
           i = 115;
             while(i>0) i–;
           count–;
       }
    }
     
    void cmd(unsigned char x)
    {
    P1=x;
    rs=0;
    rw=0;
    en=1;
    DelayMs(20);
    en=0;
    }  
     
    void dat(unsigned char x)
    {
    P1=x;
    rs=1;
    rw=0;
    en=1;
    DelayMs(20);
    en=0;
    }
     
    void lcd_init()
    {
    cmd(0x01);
    DelayMs(50);
    cmd(0x80);
    DelayMs(50);
    cmd(0x0e);
    DelayMs(50);
    cmd(0x38);
    }
    void string(unsigned char *p)
    {
    while(*p)
    {
    dat(*p);
    p++;
    }
    }
    void start();
    void stop();
    void WriteI2C(unsigned char);
    unsigned char ReadI2C(bit);   // bit is a data type
     
    unsigned int s=0,s1=0,s2=0,h=0,h1=0,h2=0,m=0,m1=0,m2=0;
    unsigned char z[50];
    void main()
    {
    lcd_init();
    while(1)
    {
     
      //   seconds    //
      start();
      WriteI2C(0xD0);   // address of RTC
      WriteI2C(0x00);   // address for seconds
      start();
      WriteI2C(0xD1);   // address of RTC to read value
      s=ReadI2C(ACK);  // after read data send acknowledgement
        stop();
     
    //  minutes  //
    start();
      WriteI2C(0xD0);   // address of RTC
      WriteI2C(0x01);   // address for minutess
      start();
      WriteI2C(0xD1);   
    m=ReadI2C(ACK);
    stop();
     
    //      hour     //
    start();
      WriteI2C(0xD0);   // address of RTC
      WriteI2C(0x02);   // address for hourss
      start();
      WriteI2C(0xD1);   
    h=ReadI2C(ACK);
       stop();
     
    s1=s&0x0f;
    s2=(s&0xf0)>>4;
     m1=m&0x0f;
     m2=(m&0xf0)>>4;
    h1=h&0x0f;
    h2=(h&0xf0)>>4;
    cmd(0x80);
    dat(h2+48);
    dat(h1+48);
    dat(':');
    dat(m2+48);
    dat(m1+48);
    dat(':');
    dat(s2+48);
    dat(s1+48);
    cmd(0xc0);
    // sprintf(z,"%d%d:%d%d:%d%d",h2,h1,m2,m1,s2,s1);
    // string(z);
    }
     
    }
    //


    // start I2C
    //


    void Start() // SDA pin is high to low _ when clock is high
    {
       SDA = 1;
        SCL = 1;
        _nop_();_nop_();  // for delay
        SDA = 0;
        _nop_();_nop_();  // for delay
        SCL = 0;
        _nop_();_nop_();  // for delay
    }
     
    //


    // stop I2C
    //


    void Stop()   // SDA pin is low to high _/ when clock is high
    {
        SDA = 0;   
        _nop_();_nop_();  // for delay
        SCL = 1;
        _nop_();_nop_();  // for delay
        SDA = 1;
    }
     
    //


    // Write I2C
    //


    void WriteI2C(unsigned char Data)   // data bit is change when clock is change 
    {  
     
        for (i=0;i<8;i++)
        {
           SDA = (Data & 0x80) ? 1:0;
           SCL=1;
    SCL=0;
             Data<<=1;
    }
     
     SCL = 1;    // on this extra clock the ACK is receive by the master
        _nop_();_nop_();
        SCL = 0;
     _nop_();_nop_();  // for delay*
    }
     
    //


    // Read I2C  when slave sends the complete data it latches the SDA pin high as no ack for wait of another restart bit
    //


     
    unsigned char ReadI2C(bit ACK_Bit)
    {
      
        for (i=0;i<8;i++)
        {
             SCL   = 1;      
             Data<<= 1;
             Data = (Data | SDA);    
             SCL   = 0;
             _nop_();
        }
      
       if (ACK_Bit == 1)
        SDA = 0; // Send ACK     
        else    
        SDA = 1; // Send NO ACK               
      
     SCL = 1;    // on this extra clock the ACK is transmitted by the master
        _nop_();_nop_();
        SCL = 0;
     _nop_();_nop_();  // for delay*
     
        return Data;
    }
     
  • Author
    Posts
Viewing 10 posts - 16 through 25 (of 25 total)
← 1 2
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Measuring controller current output with a meter November 18, 2025
  • need help in photodetection TIA circuit November 18, 2025
  • Anyone In The US Ordered From AliExpress Recently? November 17, 2025
  • Can a small solar panel safely trickle-charge old NiMH AA batteries? November 17, 2025
  • Have a ultrasonic washer but not knowing what detergent for cleaning soot November 17, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise