Single chip microcomputer design LCD digital clock (perpetual calendar)

Publisher:山宝宝Latest update time:2011-08-25 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The program and circuit of the single chip microcomputer design LCD digital clock (perpetual calendar) are as follows:

#include
#include //Library function header file, the code references the _nop_() function
//Define control signal port
sbit RS=P2^4; //P2.4
sbit RW=P2^5; //P2.5
sbit E=P2^6; //P2.6
sbit set=P3^4; //Set key
sbit enter=P3^5; //Confirm key
sbit add1=P3^6; //Add 1 key
sbit sub1=P3^7; //Subtract 1 key
bit k=0,f=0; //k is 0 for running status, k is 1 for setting status; f is 0 for the first line display, f is 1 for the second line display
char sec,min,hour,week,day,month,year,n,m;
unsigned char count,key;
unsigned char lcdd[]="0123456789";
/*Declare calling function*/
void dispd(); //Date display function
void dispt(); //Time display function
unsigned char keys(); //Key scan function
void lcd_w_cmd(unsigned char com); //Write command word function
void lcd_w_dat(unsigned char dat); //Write data function
unsigned char lcd_r_start(); //Read status function
void int1(); //LCD initialization function
void delay(unsigned char t); //Controllable delay function
void delay1(); //Software implementation of delay function, 5 machine cycles
/*Main function*/
void main()
{
TMOD=0x01; //Set to timer mode 1
TH0=0x3c; //Crystal oscillator 6MHz, timing time 100ms
TL0=0xb0;
IE=0x82; //Open global interrupt and timer interrupt
TR0=1; //Start timer
sec=0; //Seconds
min=0; //Minutes
hour=0; //Hours
week=0; //Day of week
day=1; //Month
month=1; //Year of month
year=0; //year
count=0; //clear interrupt times
n=-3; //set key press times, first line date
m=-3; //set key press times, second line time
P0=0xff; //send all 1s to P0 port
int1(); //initialize LCD
delay(255);

while(1)
{
key=keys(); //read keys
switch(key)
{
case 0xe0: //press set key
{
TR0=0;
k=1;
if(f==0)
{
n=n+3;
if(n==9)
{
n=0;
m=0;
f=1;
}

}
else
{
m=m+3;
if(m==12)
{
m=0;
n=0;
f=0;
}

}
if(f==0)
{
lcd_w_cmd(0x0d);
lcd_w_cmd(0x86+n);
}
else
{
lcd_w_cmd(0x0d);
lcd_w_cmd(0xc4+m);
}
} break;

case 0xd0: //Press the confirm key
{
k=0;
TR0=1;
n=-3;
m=-3;
f=0;
} break;
case 0xb0: //Press the plus 1 key
{
if(k==1)
{
if(f==0)
{
if(n==0){year++;if(year==100) year=0;}
else if(n==3) {month++;if(month==13) month=1;}
else {day++;if(day==32) day=1;}
dispd(); //Call the first line display function
lcd_w_cmd(0x0d); //Cursor flashes
lcd_w_cmd(0x86+n);//Return the set value display address
}
else
{
if(m==0){hour++;if(hour==24) hour=0;}
else if(m==3) {min++;if(min==60) min=0;}
else if(m==6){sec++;if(sec==60) sec=0;}
else {week++;if(week==7) week=0;}
dispt(); //Call the second line display function
lcd_w_cmd(0x0d); //Cursor flashes
lcd_w_cmd(0xc4+m);//Return the set value display address
}
}
} break;
case 0x70: //Press the minus 1 key
{
if(k==1)
{
if(f==0)
{
if(n==0){year--;if(year
/*****Timer interrupt function*****/
void timer0() interrupt 1
{
TH0=0x3c;
TL0=0xb0;
count++;
if(count==10)
{
count=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
hour=0;
week++;
day++;
if(week==7) week=0;
if(day==29&&month==2&&year%4!=0) {day=1;month++;}
else if(day==30&&month==2&&year%4==0) {day=1;month++;}
else if(day==31&&(month==4||month==6||month==9||month==11)) {day=1;month++;}
else if(day==32&&(month== 1||month==3||month==5||month==7||month==8||month==10||month==12)) {day=1;month++;}
if(month==13)
{
month=1;
year++;
if(year==100) year=0;
}
}
}
}
}
}
/*Key scanning function*/
unsigned char keys()
{
unsigned char cod,del;
P3=0xf0;
cod=P3&0xf0; //Read the key value of P3 port
if(cod!=0xf0) //First detect whether there is a key pressed
{
delay(100); //Debounce
if(cod!=0xf0)
{
cod=P3&0xf0; //Read the key value
do //Wait for the key to be released
{
P3=0xf0;
del=P3&0xf0;
}while(del!=0xf0);
return(cod);//Return the key value
}
}
return(0xf0); //Return the value
}
/*First line display date function*/
void dispd()
{
lcd_w_cmd(0x0c); //Set cursor not to display, not to blink
delay(20);
lcd_w_cmd(0x83); //First line starts displaying address 0x80+0x03
delay(20);
lcd_w_dat('2');
delay(2);
lcd_w_dat('0');
delay(2);
lcd_w_dat(lcdd[year/10]);
delay(2);
lcd_w_dat(lcdd[year%10]);
delay(2);
lcd_w_dat('-');
delay(2);

lcd_w_dat(lcdd[month/10]);
delay(2);
lcd_w_dat(lcdd[month%10]);

delay(2);
lcd_w_dat('-');
delay(2);

lcd_w_dat(lcdd[day/10]);
delay(2);
lcd_w_dat(lcdd[day%10]);
delay(2);

}
/*The second line displays time and week function*/
void dispt()
{
lcd_w_cmd(0x0c); //Set cursor not to display and not to blink
delay(20);
lcd_w_cmd(0xc3); //The second line starts at the address 0x80+0x43
delay(20);
lcd_w_dat(lcdd[hour/10]);
delay(2);
lcd_w_dat(lcdd[hour%10]);
delay(2);
lcd_w_dat(':'); delay
(2);

lcd_w_dat(lcdd[min/10]);
delay(2);
lcd_w_dat(lcdd[min%10]);
delay(2);
lcd_w_dat(':'); delay
(2);

lcd_w_dat(lcdd[sec/10]);
delay(2);
lcd_w_dat(lcdd[sec%10]); delay
(2);
lcd_w_dat(' ');
delay(2);
lcd_w_dat('W');
delay(2);
lcd_w_dat(lcdd[week] );
delay(2);
}

Reference address:Single chip microcomputer design LCD digital clock (perpetual calendar)

Previous article:Design of low-cost and high-precision A/D and D/A conversion based on single-chip microcomputer
Next article:Analysis of music played by microcontroller

Latest Industrial Control Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号