dolphinlabs
...
Doplhin labs
News

RTC Interfacing


·         Problem Statement: Interfacing of RTC with 89C51 microcontroller.

Connections: :     P2.0=RS,P2.1=RW,P2.2=EN , PORT0 =LCD data pins

                                    P2.4=SCL, P2.5=SDA, P1.2=Push button1 (Set), P1.3=Push     

                                    button2 (move cursor), P1.4= Push button3 (Increment),

                                    P3.7= Push button4 (Decrement), P3.6=Push button5 (Enter)

 

 

Hardware Connections for 8051 Board:-

1.      Give supply to 8051 board with the help of adapter

2.       Connect +5V pin of 8051 microcontroller to +5V supply and GND pin of 8051 microcontroller to GND pin of supply.

3.      Connect +5V pin of RS232 interface to +5V supply and GND pin of RS232 interface to GND pin of supply.

4.      Connect R2OUT pin of RS232 interface to RXD pin of 8051 and T2IN pin of RS232 interface to TXD pin of 8051.

5.      Connect +5V pin of LCD interface to +5V supply pin and GND pin of LCD interface to GND pin of supply.

6.      Connect RS pin of LCD to P2.0 pin of microcontroller.

7.      Connect RW pin of LCD to P2.1 pin of microcontroller.

8.      Connect EN pin of LCD to P2.2 pin of microcontroller.

9.      Connect data pins of LCD D0-D7 to P0.0-P0.7 pins of microcontroller by using bus.

10.  Connect SCL pin to P2.4 pin of microcontroller.

11.  Connect SDA pin to P2.5 pin of microcontroller.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Circuit Diagram:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Program Code:

#include<reg51.h>

sbit set=P1^2;                                     //P1.2=Push button1

sbit mov=P1^3;                                  //P1.3=Push button2

sbit inc=P1^4;                                     //P1.4=Push button3

sbit dec =P3^7;                                   //P3.7=Push button4

sbit ent=P3^6;                                    //P3.6=Push button5

void all_disp();

void time_set();

#define First_Line 0x80

#define                Second_Line 0xc0

#define Curser_On 0x0f

#define Curser_Off 0x0c

#define Clear_display 0x01

 

#define data_port P0

sbit rs=P2^0;                                       //RS=P2.0

sbit rw=P2^1;                                 //R/W=P2.1

sbit en=P2^2;                                      //EN=P2.2

sbit sda=P2^5;                                    //SDA=P2.5

sbit scl=P2^4;                                     //SCL=P2.4

/************************Function Prototype***************************/

void lcd8_init();

void lcd8_command(unsigned char);

void lcd8_write(unsigned char,unsigned char);

void lcd8_display(unsigned char,const unsigned char*,unsigned int);

void lcd8_decimal2(unsigned char,unsigned char);

void lcd8_decimal3(unsigned char,unsigned char);

void lcd8_decimal4(unsigned char,unsigned int);

void delay(unsigned int);

void del();

 

#define DS1307_ID 0xD0

  #define SEC 0x00

  #define MIN 0x01

  #define HOUR 0x02

  #define DATE 0x04

  #define MONTH 0x05

  #define YEAR 0x06

 

  DS1307_get(unsigned char);

  void DS1307_settime(unsigned char,unsigned char,unsigned char);

   void DS1307_setdate(unsigned char ,unsigned char ,unsigned char);

               send2lcd(unsigned char);

              void rtc_write(unsigned char,unsigned char);

              rtc_read(unsigned char);

              void rtc_rd_wr_sub();

              void rtc_init();

              void rtc_start();

              void rtc_tx();

              void rtc_rx();

              void rtc_stop();

              void rtc_ack();

              unsigned int rtc_add_wr,rtc_add_rd;

              unsigned char d_rtc,datain_rtc,in_rtc,temp_rtc,dat_rtc,flag_rtc;

 

              unsigned char i,j,a[10],cur=0;

              unsigned char sec,min,hour,date,month,year,sec1,hour1,min1,sec2,hour2,min2,hh,mm,ss,dd,mn,yy;

              unsigned char count[6]={0x86,0x89,0x8c,0xc6,0xc9,0xcc};

 

              unsigned char dec_hex(unsigned char tt)

              {

              if(tt>59)

              tt+=36;

              else if(tt>49)

              tt+=30;

              else if(tt>39)

              tt+=24;

              else if(tt>29)

              tt+=18;

              else if(tt>19)

              tt+=12;

              else if(tt>9)

              tt+=6;

              return(tt);

              }

 

              void all_disp()                                                                      //display function

              {

                              sec=DS1307_get(SEC);

                              min=DS1307_get(MIN);

                              hour=DS1307_get(HOUR);

                              date=DS1307_get(DATE);

                              month=DS1307_get(MONTH);

                              year=DS1307_get(YEAR);

    

                              sec=send2lcd(sec);                           //value of sec send on LCD

                               min=send2lcd(min);                           //min send on LCD

                               hour=send2lcd(hour);                        //value of hour send on LCD

                              date=send2lcd(date);

                              month=send2lcd(month);

                              year=send2lcd(year);

 

                              lcd8_decimal2(0x86,date);

                              lcd8_write(0x88,'-');

                              lcd8_decimal2(0x89,month);

                              lcd8_write(0x8b,'-');

                              lcd8_decimal2(0x8c,year);

 

                              lcd8_decimal2(0xc6,hour);

                              lcd8_write(0xc8,'.');

                              lcd8_decimal2(0xc9,min);

                              lcd8_write(0xcb,'.');

                              lcd8_decimal2(0xcc,sec);

}

void main()

{           

lcd8_init();                                                          //Initialize LCD

lcd8_display(0x80,"RTC TESTING:",16);

delay(65000);                                                                    //call delay

delay(65000);

lcd8_command(0x01);                                                    //Clear LCD

lcd8_display(First_Line,"Date:",5);

lcd8_display(Second_Line,"Time:",5);                        

rtc_init();                                                                             //Initialize RTC

 

while(1)

{

all_disp();                                                                            //call all_disp function

if(set==0)                                                                             //Check condition

time_set();                                                                           //Call time set function

}

}

 

void time_set()                                                                   //Function defination

{

lcd8_display(0x80,"Time Set",16);

lcd8_display(0xc0,"Mode",16);

delay(65000);                                                                    //Call delay

lcd8_command(0x01);                                                    //Clear LCD

 lcd8_display(First_Line,"Date:",5);

lcd8_display(Second_Line,"Time:",5);

all_disp();                                                                    //call all_disp function

cur=0;                                                                                 

while(ent)

{

lcd8_command(0x0f);                                                     //cursor on, display blinking

lcd8_command(count[cur]);                                          

if(mov==0)                                                                         //check condition

{

while(!mov);

cur++;                                                                                  //Increment cursor

if(cur==6)                                                                            //Check whether the cur=6

cur=0;

}

else if(count[cur]==0x86)                                          //check condition

{

if(inc==0)                                                                            //check whether the inc=0

{

while(!inc);

date++;                                                                                                //Increment date

if(date>=32)

date=1;

lcd8_decimal2(count[cur],date);                                    //call  lcd8_decimal2 function

}

else if(dec==0)                                                                    //check whether the inc=0

{

while(!dec);

date--;                                                                                  //decrement date

if(date==0xff)                                                                   

date=31;

lcd8_decimal2(count[cur],date);

 }

 }

 

 else if(count[cur]==0x89)                                               //Check cursor position

  {

if(inc==0)                                                                            //Check condition

{

while(!inc);

month++;                                                                            //Increment month

if(month>=13)

month=1;

lcd8_decimal2(count[cur],month);                                                //Call function

}

else if(dec==0)                                                                    //Check condition

{

while(!dec);

month--;                                                                              //decrement month

if(month==0xff)

date=12;

lcd8_decimal2(count[cur],month);                                                //call function

 }          

 }

 

 else if(count[cur]==0x8c)                                                //Check cursor position

  {

if(inc==0)                                                                            //Check condition

{

while(!inc);

year++;                                                                                                //Increment year

if(year>99)

year=0;

lcd8_decimal2(count[cur],year);

}

else if(dec==0)                                                                    //check condition

{

while(!dec);                                                                        

year--;                                                                                  //decrement year

if(year==0xff)

year=99;

lcd8_decimal2(count[cur],year);

 }          

 }

 

 else if(count[cur]==0xc6)                                                //Check cursor position

  {

if(inc==0)                                                                            //Check condition

{

while(inc==0);                                                                   

hour++;                                                                                //Increment hours

if(hour>=24)

hour=0;

lcd8_decimal2(count[cur],hour);

 

}

else if(dec==0)                                                                    //Check condition

{

while(dec==0);

hour--;                                                                                  //Decrement hours

if(hour==0)

hour=23;

lcd8_decimal2(count[cur],hour);

 }          

 }

 

   else if(count[cur]==0xc9)                                              //Check condition

  {

if(inc==0)                                                                            //Check condition

{

while(inc==0);

min++;                                                                                 //Increment min

if(min>=60)

min=0;

lcd8_decimal2(count[cur],min);

 

}

else if(dec==0)                                                                    //Check condition

{

while(dec==0);

min--;                                                                                   //Decrement min

if(min==0)

min=59;

lcd8_decimal2(count[cur],min);

 }          

 }

 

 else if(count[cur]==0xcc)

  {

if(inc==0)

{

while(inc==0);

lcd8_command(0x0c);

sec++;

if(sec>=60)

sec=0;

lcd8_decimal2(count[cur],sec);

 

}

else if(dec==0)

{

while(dec==0);

lcd8_command(0x0c);

sec--;

if(sec==0xff)

sec=59;

lcd8_decimal2(count[cur],sec);

 }          

 }

  }

 lcd8_command(0x0c);                                                   //Display on, ,cursor off

 lcd8_display(0x80,"Time is",16);

 lcd8_display(0xc0,"stored",16);

 delay(65000);                                                                   //call delay

 delay(65000);

 

 dd=dec_hex(date);

 mn=dec_hex(month);

 yy=dec_hex(year);

 hh=dec_hex(hour);

 mm=dec_hex(min);

 ss=dec_hex(sec);

 

 DS1307_settime(hh,mm,ss);

 DS1307_setdate(dd,mn,yy);

 lcd8_command(0x01);

 lcd8_display(First_Line,"Date:",5);

lcd8_display(Second_Line,"Time:",5);

delay(65000);

}

 

void lcd8_init()

{

lcd8_command(0x38);                    //2line,5x7 matrix

lcd8_command(0x06);                    //Shift cursor right

lcd8_command(0x0c);                    //display on, cursor off

lcd8_command(0x01);                    //Clear  display

}

void lcd8_command(unsigned char com)

{

data_port=com;            

en=1;                                                    //EN=1

rs=0;                                                     //RS=0

rw=0;

delay(125);

en=0;                                                    //EN=0

delay(125);

}

 

void lcd8_write(unsigned char com,unsigned char lr)

{

lcd8_command(com);

 data_port=lr;

en=1;

rs=1;                                                     //RS=1

rw=0;

delay(125);

en=0;

delay(125);

 }

 

 void lcd8_display(unsigned char com,const unsigned char *word,unsigned int n)  //display function definition

 {

 unsigned char Lcd_i;

 for(Lcd_i=0;Lcd_i<n;Lcd_i++)

 {

 lcd8_write(com+Lcd_i,word[Lcd_i]);             //call lcd8_write function

 }

 }

 void lcd8_decimal2(unsigned char com,unsigned char val)             //lcd8_decimal2 function

 {

 unsigned int Lcd_hr,Lcd_t,Lcd_o;

 Lcd_hr=val%100;

 Lcd_t=Lcd_hr/10;

 Lcd_o=Lcd_hr%10;

 

 lcd8_write(com,Lcd_t+0x30);

  lcd8_write(com+1,Lcd_o+0x30);

  }

 

  void lcd8_decimal3(unsigned char com,unsigned char val)                   //lcd8_decimal3 function

 {

 unsigned int Lcd_h,Lcd_hr,Lcd_t,Lcd_o;

 Lcd_h=val/100;                              

 Lcd_hr=val%100;

 Lcd_t=Lcd_hr/10;

 Lcd_o=Lcd_hr%10;

 

 lcd8_write(com,Lcd_h+0x30);

  lcd8_write(com+1,Lcd_t+0x30);

  lcd8_write(com+2,Lcd_o+0x30);

  }

 

    void lcd8_decimal4(unsigned char com,unsigned int val)

 {

 unsigned int Lcd_th,Lcd_thr,Lcd_h,Lcd_hr,Lcd_t,Lcd_o;

 val=val%10000;

 Lcd_th=val/1000;

 Lcd_thr=val%1000;

 Lcd_h=Lcd_thr/100;

 Lcd_hr=Lcd_thr%100;

 Lcd_t=Lcd_hr/10;

 Lcd_o=Lcd_hr%10;

 

 lcd8_write(com,Lcd_th+0x30);

  lcd8_write(com+1,Lcd_h+0x30);

  lcd8_write(com+2,Lcd_t+0x30);

  lcd8_write(com+3,Lcd_o+0x30);

  }

 

              void delay(unsigned int del)

              {

              while(del--);

              }

              void del()

              {

              delay(65000);

              delay(65000);

              }

 

              DS1307_get(unsigned char addr)

              {

              unsigned char ret;

              rtc_start();

              ret=rtc_read(addr);

              rtc_stop();

              return ret;

              }

 

              void DS1307_settime(unsigned char hh,unsigned char mm,unsigned char ss)

              {

              rtc_start();

              rtc_write(0x00,ss);

              rtc_write(0x01,mm);

                              rtc_write(0x02,hh);

                              rtc_stop();

                              }

 

                              void DS1307_setdate(unsigned char dd,unsigned char mn,unsigned char yy)

              {

              rtc_start();

              rtc_write(0x04,dd);

              rtc_write(0x05,mn);

                              rtc_write(0x06,yy);

                              rtc_stop();

                              }

 

                                               send2lcd(unsigned char value)

              {

              unsigned char buf1,buf2,buf=0;

              buf1=value&0xF0;

              buf1=(buf1>>4);

              buf2=value&0x0F;

              buf=(buf1*10)+buf2;

              return buf;

              }

 

              void rtc_init()

              {

              rtc_add_wr=0xd0;

              rtc_add_rd=0xd1;

              }

 

              void rtc_write(unsigned char zig,unsigned char zag)

              {

              dat_rtc=zig;

              temp_rtc=zag;

              rtc_rd_wr_sub();

              above:

              d_rtc=temp_rtc;

              rtc_tx();

              if(CY==1)goto above;

              CY=0;

              rtc_stop();

              }

              rtc_read(unsigned char zig)

              {

              dat_rtc=zig;

              rtc_rd_wr_sub();

              rtc_start();

              be:

              d_rtc=rtc_add_rd;

              rtc_tx();

              if(CY==1)goto be;

              rtc_rx();

              rtc_ack();

              CY=0;

              rtc_stop();

              return(datain_rtc);

              }

 

              void rtc_start()

              {

              sda=1;

              scl=1;

              sda=0;

              scl=0;

              }

 

              void rtc_stop()

              {

              sda=0;

              scl=1;

              sda=1;

              }

 

              void rtc_tx()

              {

              signed char i_rtc;

              for(i_rtc=7;i_rtc>=0;i_rtc--)

              {

              CY=(d_rtc>>i_rtc)&0x01;

              sda=CY;

              scl=1;

              scl=0;

              }

              sda=1;

              scl=1;

              CY=sda;

              scl=0;

              }

 

              void rtc_rx()

              {

              unsigned char l_rtc;

              sda=1;

              for(l_rtc=0;l_rtc<=7;l_rtc++)

              {

              scl=1;

              in_rtc=in_rtc<<1;

              in_rtc|=sda;

              scl=0;

              }

              datain_rtc=in_rtc;

              in_rtc=0;

              }

 

              void rtc_ack()

              {

              sda=1;

              scl=1;

              scl=0;

              }

              void rtc_rd_wr_sub()

              {

              rtc_start();

              here1:

              d_rtc=rtc_add_wr;

              rtc_tx();

              if(CY==1)goto here1;

              again1:

              d_rtc=dat_rtc;

              rtc_tx();

              if(CY==1)goto again1;

              }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Get SOCIAL







SAFE & SECURE SHOPPING

visa card master card maestro card
Maverick IT Industries Pvt. Ltd.