Thursday, 13 September 2012

how to make a digital clock with 8051 and without RTC




code:

/******** simple clock with hour,minute & second **********
********************24 hour mode***************************
code from: www.pandyaelectronics.blogspot.com**************/



#include<reg51.h>
sfr lcd_data_pin=0xA0; // data port P2
sbit rs=P0^0; // Register select pin
sbit rw=P0^1; // Read write pin
sbit en=P0^2; // Enable pin


void delay(unsigned int msec) //delay function
{
int i,j;
for(i=0;i<msec;i++)
for(j=0;j<1275;j++);
}



void onesecond(void) //genrates accurate delay of one second
{
int i;
for(i=0;i<20;i++)
{
TL0=0xB0; //calculation made for 12Mhz crystal
TH0=0X3C;
TR0=1;
while(TF0!=1);
TR0=0;
TF0=0;
}
}



void lcd_command(unsigned char comm) // function to send command to LCD
{
lcd_data_pin=comm;
en=1;
rs=0;
rw=0;
delay(1);
en=0;
}


void lcd_data(unsigned char disp) // function to send data on LCD
{
lcd_data_pin=disp;
en=1;
rs=1;
rw=0;
delay(1);
en=0;
}



lcd_dataa(unsigned char *disp) // function to send string to LCD
{
int x;
for(x=0;disp[x]!=0;x++)
{
lcd_data(disp[x]);
}
}



void lcd_ini() //Function to inisialize the LCD
{
lcd_command(0x38);
delay(5);
lcd_command(0x0C);
delay(5);
lcd_command(0x01);
delay(5);
lcd_command(0x80);
delay(5);
}

/*
24 hour format:nm:lk:ji
i=1st digit of second
j=2nd digit of second
k=1st digit of minute
l=2nd digit of minute
m=1st digit of hour
n=2nd digit of hour
*/


void clock(void) //handels clocking operation
{
/*
enter your current time in below varriables
example:if your time is 19:59:00
then give :unsigned int n=1,m=9,l=5,k=9,j=0,i=0;
*/

unsigned int n=0,m=0,l=0,k=0,j=0,i=0;
unsigned char a[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};

while(1)
{
for(i=0;i<10;i++) //handles 1st digit of second
{
lcd_command(0x87);
lcd_data(a[i]);
onesecond();

if(i==9) //handles 2nd digit of second
{
j=j+1;

if(j==6) //handles 1st digit of minute
{
j=0;
k=k+1;

if(k==10) //handles 2nd digit of minute
{
k=0;
l=l+1;

if(l==6) //handles 1st digit of hour
{
l=0;
m=m+1;

if(n==2 && m==4)
{
n=0;
m=0;
lcd_command(0x80);
lcd_data(a[n]);
}

if(m==10) //handles 2nd digit of hour
{
m=0;
n=n+1;
lcd_command(0x80);
lcd_data(a[n]);
}

lcd_command(0x81);
lcd_data(a[m]);
}


lcd_command(0x83);
lcd_data(a[l]);
}


lcd_command(0x84);
lcd_data(a[k]);
}


lcd_command(0x86);
lcd_data(a[j]);
}


}
}
}


void main()
{
lcd_ini();
TMOD=0x01; //timer0,mode1
lcd_dataa("00:00:00"); //enter your current time here
clock();
}
circuit diagram:

PandyaElectronics' Prototype:



check out video:


                                   
pls comment if any problem.

2 comments:

  1. nice project,
    Can u post a real time 7 segment clock with 8051 & DS12C887 rtc,

    ReplyDelete
  2. thank you jito,i m currently busy with my final year project.but i will post when i will be free.

    ReplyDelete