I have made a digital clock using 8051. The time is found out by using the timers present in the 8051. For this project we need both the software Proteus and an IDE Keil. We would be writing the code for 8051 in embedded C.
Lets get started!!
The schematic diagram is given below. You need to make this in Proteus software.
on the left side you can see the components used.
After finishing this les get to coding.
I would encourage you to find your own logic rather going for mine for gaining more experience.
Whenever you look for understanding a code, it would be helpful if you start from the main.
#include<reg51.h>
sbit rs=P1^0;
sbit rw=P1^1;
sbit en=P1^2;
unsigned int msec=0;
unsigned int sec=0;
unsigned int min=0;
unsigned int day=1;
int c;
unsigned char ch1[3]="MON";
void changedisp();
void lcdcmd(unsigned char);
void intdisplay(unsigned int);
void lcddata(unsigned char);
void valuedisplay(unsigned char);
int hour=0;
int a=5;
int i1=0;
unsigned char ch[2];
unsigned int r;
void delay()
{unsigned int i;
for(i=0;i<12000;++i);
}
void timerinterrupt (void) interrupt 1 //will call this function when timer counts and //reaches FFFF automatically.
{
TR0=0; //stop the timer.
msec++; // increase the variable.
if(msec>=1000) // as 1000 miliseconds is a second.
{
sec++; // increase seconds.
c++;
if(c==27)
{ sec=sec+2;
c=0;} // these are just to adjust time and overcome the delay we get in this //program
msec=0; //start counting again from 0.
}
if(sec>=60) // 60 seconds give 1 minute.
{min++; // increase minute.
sec=0; // start counting from 0 to 60.
lcdcmd(0x01); //clear the screen( to solve a problem found during testing).
}
if(min==60) // 60 minutes gives 1 hour.
{hour++; // increase hour;
min=0; // start counting from 0 to 60.
}
if(hour==24) //24 hours gives a day.
{day=day+1; // increase day each time hour becomes 24.
hour=0; // again start counting from 0 to 24.
}
TF0=0; // The interrupt is called when there is overflow and TF0 becomes 1 after // doing all the action we need we make them 0.
TH0=0xFC;
TL0=0x66; // these are the values to start counting from until it reaches FFFF. this //gives a timing value of 1 millisecond.
TR0=1; // start timer.
}
void timer1() // function to initialize timer
{
TMOD=0x01; // make timer0 to mode 1- 16 bit mode
TH0=0XFC;
TL0=0X66; // these are the intial values given to timer to start counting till it //reaches FFFF
EA=1; //enable global interrupt
ET0=1; // enable timer 0 interrupt.
TR0=1; // start timer 0.
}
void lcdcmd(unsigned char value) // function to send commands through //datalines.
{P2=value;
rs=0; // here rs should be made 0.
rw=0;
en=1;
delay();
en=0;
}
void lcddata(unsigned char value) // function to display.
{P2=value;
rs=1; // here rs should be made 1
rw=0;
en=1;
delay();
en=0;
}
void intdisplay(unsigned int value) // get a value as argument
{
i1=0;
if(value==0) // if hour is 0 do this.
{lcddata('0');}
while(value>0)
{
r=value%10;
ch[i1]=(char)(r+48);
value=value/10;
i1++;
} // this will store the integer hour value as charecter in th ch array.
i1--;
while(i1>=0)
{lcddata(ch[i1]); // print the charecter to screen.
i1--;
}
} // this will print the value as charecter in the lcd.
void main()
{
P2=0x00; // initialize port 2 as output
P3=0x0f; // make P3 as input
lcdcmd(0x38); //initialize lcd
delay();
lcdcmd(0x01); //clear the screen
delay();
lcdcmd(0x10); //move cursor to left
delay();
lcdcmd(0x0c); //stop the blinking and remove the cursor from the lcd
delay();
lcdcmd(0x80); //gives the location of the cursor in the lcd.
delay();
timer1(); // call this function
while(1)
{
lcdcmd(0x80); // give position of the cursor.
intdisplay(hour); // call this function
lcdcmd(0x82); // give position of the cursor.
lcddata(':');
lcdcmd(0x83); // give position of the cursor.
intdisplay(min);
lcdcmd(0x85); // give position of the cursor.
lcddata(':');
lcdcmd(0x86);
intdisplay(sec);
//lcdcmd(0x01);
lcdcmd(0xc0);
lcddata(ch1[0]);
lcddata(ch1[1]);
lcddata(ch1[2]); // These are instructions to change position of cursor and print //values.
switch(day) // based on the value of day ch is given values like MON
{ case 2: ch1[0]='T';ch1[1]='U';ch[2]='E';break;
case 3:ch1[0]='W';ch1[1]='E';ch[2]='D';break;
case 4: ch1[0]='T';ch1[1]='H';ch[2]='U';break;
case 5: ch1[0]='F';ch1[1]='R';ch[2]='I';break;
case 6:ch1[0]='S';ch1[1]='A';ch[2]='T';break;
case 7:ch1[0]='S';ch1[1]='U';ch[2]='N';day=1;break;}
}
}
This is the whole code have fun doing this!!
Comments