This is a real time clock by using 89c51 micro-controller, clock's data format is hour:min:sec, and it is displayed on 16x2 lcd, the code has been tested and compilled on Keil uVision compiler, circuit diagram for the project is shown below.
Lcd is connected with port 2 and some pins of port 3 of 89c51 uController.
C Code :
#include
#define MAXCOUNT 20
#define HOUR 3
#define MIN 6
#define SEC 9
sbit RS=P3^3; //RS=0; command register RS=1; Data register
sbit RW=P3^4;
sbit E=P3^5;
sbit BF=P2^7;
bit time_keeping;
bit setmode;
bit packetok;
bit secflag;
bit minflag;
bit hourflag;
unsigned char data count;
unsigned char data hour;
unsigned char data hourT;
unsigned char data hourU;
unsigned char data min;
unsigned char data minT;
unsigned char data minU;
unsigned char data sec;
unsigned char data secT;
unsigned char data secU;
unsigned char data curfield;
unsigned char data curstate;
unsigned char data Debounce;
unsigned char data decrease;
//////////////////////////// DELAY/////////////////////////////
void delay(unsigned char z) //For delay (Starts)
{
unsigned char x;
unsigned int y;
for(x=0;x for(y=0;y<=1000;y++);
}
}
///////////////////////////////// LCD CURSOR POSITION//////////////
void setcurpos(unsigned char pos)
{
pos = pos + 0x80;
E=1; //Start LCD command
RS=0; //Its a command.
RW=0;
P2=pos;
E=0; //Finish LCD command
}
////////////////////////////// LCD PULSE//////////////////////////
void LCD_Pulse_E(unsigned char t)
{
unsigned char i;
E = 1; for(i=0; i E = 0; for(i=0; i }
//////////////////////////// READY/////////////////////////////
bit ready(void) //For checking that the LCD is ready or not?
{
BF=1;//Busy flag is one
RS=0;// Command register is one
RW=1;//Read the data from LCD
while(BF==1)
{
E=0;
E=1;
}
return 1;
}
///////////////////////////////// LCD SHOW///////////////////////
void LCD_Show(unsigned char CHARACTER)
{
if(ready())
{
P2=CHARACTER;
RS=1; delay(1);//Data register
RW=0; delay(1);//Write on LCD
LCD_Pulse_E(255);
delay(1);
}
}
/////////////////////////// LCD RUN/////////////////////////
void LCD_Run_Command(unsigned char COMMAND)
{
if(ready())
{
P2=COMMAND;
RS=0; delay(1);//command register
RW=0; delay(1);//read from LCD
LCD_Pulse_E(255);//call to LCD_PULSE_E() function
}
}
/////////////////////////////////// CMD ARRAY///////////////////
bit Cmd_Array(unsigned char cmd)
{
if(cmd)
{
return 1;
}
else
return 0;
}
////////////////////////////////// TIMER 0 ISR/////////////////
void timer0ISR(void) interrupt 1 using 1
{
TR0=0;
TH0 = 0x4B;
TL0 = 0xFF;
TR0 = 1;
count++;
if(count == MAXCOUNT)
{
count = 0;
sec++;
time_keeping = 1;
secflag = 1;
if(sec == 60)
{
sec = 0;
min++;
minflag=1;
if(min==60)
{
min=0;
hour++;
hourflag=1;
if(hour==24)
{
hour=0;
}
}
}
}
}
///////////////////////////////////// THE MAIN ///////////////////////////////
void main(void)
{
setmode=0;
count=0; //initialization
sec=0;
min=0;
hour=0;
curstate = 0;
setcurpos(5);
LCD_Show(':');
setcurpos(8);
LCD_Show(':');
SCON=0x50;
TH1=0xFD;
ES = 1;
TMOD = 0x21;
TH0=0x4B;
TL0=0xFF;
ET0=1;
EA=1;
TR0=1;
TR1=1;
LCD_Run_Command(0x0C); // Display=ON, Curson=OFF, Cursor Blonking=ON
while(1)
{
if(time_keeping)
{
setcurpos(SEC);
LCD_Show(secT+0x30);
LCD_Show(secU+0x30);
setcurpos(HOUR);
LCD_Show(hourT+0x30);
LCD_Show(hourU+0x30);
setcurpos(MIN);
LCD_Show(minT+0x30);
LCD_Show(minU+0x30);
if(hourflag)
{
hourflag=0;
hourT=hour/10;
hourU=hour%10;
}
if(minflag)
{
minflag=0;
minT=min/10;
minU=min%10;
}
if(secflag)
{
secflag=0;
secT=sec/10;
secU=sec%10;
}
}
}
}
Lcd is connected with port 2 and some pins of port 3 of 89c51 uController.
C Code :
#include
#define MAXCOUNT 20
#define HOUR 3
#define MIN 6
#define SEC 9
sbit RS=P3^3; //RS=0; command register RS=1; Data register
sbit RW=P3^4;
sbit E=P3^5;
sbit BF=P2^7;
bit time_keeping;
bit setmode;
bit packetok;
bit secflag;
bit minflag;
bit hourflag;
unsigned char data count;
unsigned char data hour;
unsigned char data hourT;
unsigned char data hourU;
unsigned char data min;
unsigned char data minT;
unsigned char data minU;
unsigned char data sec;
unsigned char data secT;
unsigned char data secU;
unsigned char data curfield;
unsigned char data curstate;
unsigned char data Debounce;
unsigned char data decrease;
//////////////////////////// DELAY/////////////////////////////
void delay(unsigned char z) //For delay (Starts)
{
unsigned char x;
unsigned int y;
for(x=0;x
}
}
///////////////////////////////// LCD CURSOR POSITION//////////////
void setcurpos(unsigned char pos)
{
pos = pos + 0x80;
E=1; //Start LCD command
RS=0; //Its a command.
RW=0;
P2=pos;
E=0; //Finish LCD command
}
////////////////////////////// LCD PULSE//////////////////////////
void LCD_Pulse_E(unsigned char t)
{
unsigned char i;
E = 1; for(i=0; i
//////////////////////////// READY/////////////////////////////
bit ready(void) //For checking that the LCD is ready or not?
{
BF=1;//Busy flag is one
RS=0;// Command register is one
RW=1;//Read the data from LCD
while(BF==1)
{
E=0;
E=1;
}
return 1;
}
///////////////////////////////// LCD SHOW///////////////////////
void LCD_Show(unsigned char CHARACTER)
{
if(ready())
{
P2=CHARACTER;
RS=1; delay(1);//Data register
RW=0; delay(1);//Write on LCD
LCD_Pulse_E(255);
delay(1);
}
}
/////////////////////////// LCD RUN/////////////////////////
void LCD_Run_Command(unsigned char COMMAND)
{
if(ready())
{
P2=COMMAND;
RS=0; delay(1);//command register
RW=0; delay(1);//read from LCD
LCD_Pulse_E(255);//call to LCD_PULSE_E() function
}
}
/////////////////////////////////// CMD ARRAY///////////////////
bit Cmd_Array(unsigned char cmd)
{
if(cmd)
{
return 1;
}
else
return 0;
}
////////////////////////////////// TIMER 0 ISR/////////////////
void timer0ISR(void) interrupt 1 using 1
{
TR0=0;
TH0 = 0x4B;
TL0 = 0xFF;
TR0 = 1;
count++;
if(count == MAXCOUNT)
{
count = 0;
sec++;
time_keeping = 1;
secflag = 1;
if(sec == 60)
{
sec = 0;
min++;
minflag=1;
if(min==60)
{
min=0;
hour++;
hourflag=1;
if(hour==24)
{
hour=0;
}
}
}
}
}
///////////////////////////////////// THE MAIN ///////////////////////////////
void main(void)
{
setmode=0;
count=0; //initialization
sec=0;
min=0;
hour=0;
curstate = 0;
setcurpos(5);
LCD_Show(':');
setcurpos(8);
LCD_Show(':');
SCON=0x50;
TH1=0xFD;
ES = 1;
TMOD = 0x21;
TH0=0x4B;
TL0=0xFF;
ET0=1;
EA=1;
TR0=1;
TR1=1;
LCD_Run_Command(0x0C); // Display=ON, Curson=OFF, Cursor Blonking=ON
while(1)
{
if(time_keeping)
{
setcurpos(SEC);
LCD_Show(secT+0x30);
LCD_Show(secU+0x30);
setcurpos(HOUR);
LCD_Show(hourT+0x30);
LCD_Show(hourU+0x30);
setcurpos(MIN);
LCD_Show(minT+0x30);
LCD_Show(minU+0x30);
if(hourflag)
{
hourflag=0;
hourT=hour/10;
hourU=hour%10;
}
if(minflag)
{
minflag=0;
minT=min/10;
minU=min%10;
}
if(secflag)
{
secflag=0;
secT=sec/10;
secU=sec%10;
}
}
}
}
No comments:
Post a Comment