Skip to main content
P89V51RD2 Interfacing with DS1307 RTC

          The DS1307 RTC IC Interfacing with P89V51RD2 Micro Controller use of the KEIL IDE, and proteus simulator. The source code and thhe screen short of simulator model are attached with this.

             This interfacing is done by the I2C Communication between the Micro Controller and DS1307 RTC.

Source Code:

#include <reg51.h>
#include <stdio.h>

sbit SCL = 0x90; // SCL for I2C
sbit SDA = 0x91; // SDA for I2C

volatile unsigned char DS1307_WA=0xD0;
volatile unsigned char DS1307_RA=0xD1;
unsigned char Rdata;

unsigned char sec1,min1,hour1,date1,month1,year1,d,l,m,j,i;
unsigned char sec[20],min[2],hour[2],ch1;
unsigned char RTCDateTime[15];
unsigned char Buf[20];

void Serial_Init()
{
    TMOD = 0x20;
    SCON = 0x50;
    TH1  = 0xFD;
    TR1  = 1;
    TI   = 1;
    RI   = 0;
     
}


void delay(unsigned int i)
{
    unsigned int j,k;
    for(j=0;j<=i;j++)
    {
        for(k=0;k<0x0f;k++);
    }
}


void Delay() // Delay(1)= delay 20 ms
{
   
    unsigned char i;
    for(i=0;i<=0x0f;i++);

}
void SCL_Rise()
{
    SCL=1;
    Delay();   
    Delay();
    Delay();
    return;
}
void SCL_Fall()
{
    SCL=0;
    Delay();
    Delay();
    Delay();
    return;
}
void I2C_Start()
{
    SDA=1;
    SCL_Rise();
    SDA=0; // "Start mode" SDA change state to "0" while SCL keeping state "1"
    Delay();
    SCL_Fall();
    return;
}
void I2C_Stop()
{
    SDA=0;
    SCL_Rise();
    SDA=1; // "Stop mode" SDA change state to "1" while SCL keeping state "1"
    Delay();
    return;

void Write_I2C(unsigned char Data)
{
    bit outbit;
    unsigned char i;
    for (i=1;i<=8;i++)
    {
        outbit=Data & 0x80;
        Data=Data<<1;
        SDA=outbit;
        SCL_Rise();
        SCL_Fall();
    }
    SDA=1;
    SCL_Rise();
    outbit=SDA;
    SCL_Fall();
    return;
}
void Read_I2C()
{
    bit Ibit;
    unsigned char i;
    Rdata=0;
    SDA=1;
    for (i=1;i<=8;i++)
    {
        Rdata=Rdata<<1;
        SCL_Rise();
        Ibit=SDA;
        Rdata=Rdata|Ibit;
        SCL_Fall();
    }
    SDA=1; // Sending Ack
    SCL_Rise();
    SCL_Fall();
    return;
}

unsigned char Read_DS1307(unsigned char Addr)
{
    I2C_Start();
    Write_I2C(DS1307_WA);
    Write_I2C(Addr);
    I2C_Start();
    Write_I2C(DS1307_RA);
    Read_I2C();
    I2C_Stop();
    return(Rdata);
}

void Write_DS1307(unsigned char Addr, unsigned char Data)
{
    I2C_Start();
    Write_I2C(DS1307_WA);
    Write_I2C(Addr);
    Write_I2C(Data);
    I2C_Stop();
    return;
}
void Check_DS1307()
{
    sec1 = Read_DS1307(0x00);
    min1 = Read_DS1307(0x01);
    hour1 = Read_DS1307(0x02);
    date1 = Read_DS1307(0x04);
    month1 = Read_DS1307(0x05);
    year1 = Read_DS1307(0x06);

    /*RTCDateTime[0] = (date1 >> 4)+0x30;
    RTCDateTime[1] = (date1&0x0f)+0x30;
    RTCDateTime[2] = (month1 >> 4)+0x30;
    RTCDateTime[3] = (month1&0x0f)+0x30;
    RTCDateTime[4] = (hour1 >> 4)+0x30;
    RTCDateTime[5] = (hour1&0x0f)+0x30;
    RTCDateTime[6] = (min1 >> 4)+0x30;
    RTCDateTime[7] = (min1&0x0f)+0x30;
    RTCDateTime[8] = (sec1 >> 4)+0x30;
    RTCDateTime[9] = (sec1&0x0f)+0x30;*/

   
}
void Set_DS1307()
{
       Write_DS1307(0x00,sec1);
    Write_DS1307(0x01,min1);
    Write_DS1307(0x02,hour1);
    Write_DS1307(0x04,date1);
    Write_DS1307(0x05,month1);
    Write_DS1307(0x06,year1);
    return;
}
void Set_DateTime()
{
          //for(i=0;i<=11;i++)
       
        if((ch1 >= '0')&&(ch1 <= '9')) 
        {
            Buf[i] = ch1;
            //i++;

        }
        /*else
        {
            Buf[i] = '\0';
        }*/
       
        i++;
        if(i > 11)
        {
            i=0;
            //break;
        }
        //}       
        //printf("%2bx\n",sec[1]);
        /*sec[0] = Buf[0];
        sec[1] = Buf[1];
        min[0] = Buf[2];
        min[1] = Buf[3];
        hour[0] = Buf[4];
        hour[1] = Buf[5];*/
        sec1 = ((Buf[0]-'0')<<4)+(Buf[1]-'0');
        min1 = ((Buf[2]-'0')<<4)+(Buf[3]-'0');
        hour1 = ((Buf[4]-'0')<<4)+(Buf[5]-'0');
        date1 = ((Buf[6]-'0')<<4)+(Buf[7]-'0');
        month1 = ((Buf[8]-'0')<<4)+(Buf[9]-'0');
        year1 = ((Buf[10]-'0')<<4)+(Buf[11]-'0');
        //}

        if((date1<=0x31)&&(month1<=0x12)&&(year1<=0x99)&&(hour1<=0x24)&&(min1<=0x60)&&(sec<=0x60))
        {
            Set_DS1307();   
        }
        /*else
        {
            /*for(i=0;i<=12;i++)
            {
                Buf[i] = '\0';

            } 
            Set_DS1307();
        }*/
   
}
void main(void)
{
    Serial_Init();
    printf("Hello World \n");
    while(1)
    {
       
        //delay(20);
        delay(15);
        //putchar(c);
        ch1 = getchar();
        Set_DateTime();
        //Set_DS1307();
        //Write_DS1307(0x01,min1);
        //Write_DS1307(0x02,hour1);
        //Check_DS1307();
        //sec1 = Read_DS1307(0x00);
        //printf("Date = %2bx\n",sec[0]-0x30);
        //Set_DateTime();
        //for(i=0;i<=2;i)
        if(ch1 == '\n') 
        {
            printf("\nTime: %2bx:%2bx:%2bx\n",hour1,min1,sec1);
            printf("\nDate: %2bx:%2bx:%2bx\n",date1,month1,year1);   
        }

    }
   
}                   




 

Comments

  1. I can not find p89v51rd2 in proteus??? from where I can find the library for p89v51rd2??? or if there is any controller similar to it???

    ReplyDelete
  2. can u post the way to make real time 7 segment clock with ds12c887 & 8051 microcontroller

    ReplyDelete

Post a Comment

Popular posts from this blog

P89V51RD2 Interfacing with the UART Serial Communication

P89V51RD2 Interfacing with the UART Serial Communication Description                     Interfacing the UART Serial Communication with the P89v51rd2 Micro controller, with the baud rate of 9600bps. The Source code and Simulator Model of this Communication is listed Below. Source : #include <reg51.h> unsigned char var; unsigned char var1; void Serial_Init() {     TMOD = 0x20;     SCON = 0x50;     TH1  = 0xFD;     TR1   = 1;       } void Transmit_Char(unsigned char x) {     SBUF =  x;     while(!TI)     {     ;        }     TI = 0; } void Transmit_String(unsigned char *s) {     unsigned char i;     while((s[i]) != 0)     {         Transmit_Char(s[i]);         i++;     } } char Rcv() {         while(!RI)     {;}     RI = 0;     return SBUF; } void delay(unsigned int i) {     unsigned int j,k;     for(j=0;j<=i;j++)     {         for(k=0;k<256;k++);     } } void main(void) {     unsigned char r;     Serial_Init();     while(1)     {         Transmit_String("He

Interfacing UART with 8051

The interfacing of serial communication (UART) with 8051 micro controller, with the baud rate of 9600bps.  Code: #include <reg51.h> #include <stdio.h> unsigned char c; void Serial_Init() {     TMOD = 0x20;     SCON = 0x50;     TH1  = 0xFD;     TR1  = 1;     TI   = 1;     RI   = 0;       } void delay(unsigned int i) {     unsigned int j,k;     for(j=0;j<=i;j++)     {         for(k=0;k<256;k++);     } } void main(void) {     Serial_Init();     printf("Hello World \n");     while(1)     {                 delay(5);         c = getchar();         delay(5);         //putchar(c);         delay(5);            }                    } Design Proteus: