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);
}
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
- Popular Resources
- Popular amplifiers
- Molex leverages SAP solutions to drive smart supply chain collaboration
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- CGD and Qorvo to jointly revolutionize motor control solutions
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Nidec Intelligent Motion is the first to launch an electric clutch ECU for two-wheeled vehicles
- Bosch and Tsinghua University renew cooperation agreement on artificial intelligence research to jointly promote the development of artificial intelligence in the industrial field
- GigaDevice unveils new MCU products, deeply unlocking industrial application scenarios with diversified products and solutions
- Advantech: Investing in Edge AI Innovation to Drive an Intelligent Future
- CGD and QORVO will revolutionize motor control solutions
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- DSP decimal conversion
- [Jihai APM32E103VET6S MINI Development Board Review] Part 5: Software Triggering ADC
- Here comes a smart home device controller that supports synchronous wireless communication. Let’s discuss it.
- Designing low-power AC-DC power supplies with ultra-high power density
- Raspberry Pi Bluetooth Basics - Use of bluezero library
- "Recommended Chinese Chip" + JieFa AC781x ARM Chip
- 5 important attributes of EMC analysis
- Bandwidth Management System (BWM) for TI C64x+ DSPs
- C++Primer Plus (Sixth Edition) Chinese Edition
- Xunwei RK3399 development board supports installing Docker in Ubuntu system