/*
Program effect: clock, including adjustable time, alarm rings when time is up, buzzer, this program has been debugged
You can
download the code of this program
from
http://www.51hei.com/ziliao/file/naozhong.rar
.
The speaker is connected to p2.0. The port can be changed by yourself.
*/
#include
miao=0;//When reached, clear
fen++; //add 1
if(fen>=59) //The following meanings are the same as above
{
fen=0;
shi++;
if(shi>23)
shi=0;
}
}
}
}
void key_scan() //Keyboard scanning subfunction
{
uchar i; //Define local variablesif
(key1==0)
{
while(!key1) //Prevent display loss
{
if(cnt==0||cnt==1||cnt==2||cnt==3||cnt==4||cnt==8)
{
display();
}
if(cnt==5||cnt==6||cnt==7)
{
display_1();
}
}
cnt++; //Record the number of times key1 is pressedif
(cnt==1) //First press, stop countingTR0
=0;
if(cnt==2) //Second press
{
miao1=miao; //Save the value of seconds
miao=99; //Display 99, indicating that the value of seconds can be adjusted
for(i=0;i<100;i++)
display(); //Display 99
miao=miao1; //Restore the value of seconds from the previous moment
}
if(cnt==3) //The following meanings are the same as above
{
fen1=fen;
fen=99;
for(i=0;i<100;i++)
display();
fen=fen1;
}
if(cnt==4)
{
shi1=shi;
shi=99;
for(i=0;i<100;i++)
display();
shi=shi1;
}
if(cnt==5)
{
miao1=miao2;
miao2=88;
for(i=0;i<100;i++)
display_1();
miao2=miao1;
}
if(cnt==6)
{
fen1=fen2;
fen2=88;
for(i=0;i<100;i++)
display_1();
fen2=fen1;
}
if(cnt==7)
{
shi1=shi2;
shi2=88;
for(i=0;i<100;i++)
display_1();
shi2=shi1;
}
if(cnt==8) //Eighth press
{
TR0=1; //Start counting
cnt=0; //Reset the number of presses
}
}
if(key2==0) //Judge whether key2 is pressed
{
while(!key2) //Prevent display from dropping
{
if(cnt==0||cnt==1||cnt==2||cnt==3||cnt==4||cnt==8)
{
display();
}
if(cnt==5||cnt==6||cnt==7)
{
display_1();
}
}
jia();//Call the sub-function of adding 1
}
if(key3==0) //Judge whether key3 is pressed
{
while(!key3) //Prevent display from dropping
{
if(cnt==0||cnt==1||cnt==2||cnt==3||cnt==4||cnt==8)
{
display();
}
if(cnt==5||cnt==6||cnt==7)
{
display_1();
}
}
jian(); //Call the subtraction subfunction
}
}[page]
void jia() //addition subfunction
{
if(cnt==2) //Judge whether the number of times key1 is pressed is 2
{
miao++; //If yes, add 1 to the second
if(miao>59) //Judge whether the second is greater than 59, if yes, clear the second
miao=0;
}
if(cnt==3) //The following meaning is the same as above
{
fen++;
if(fen>59)
fen=0;
}
if(cnt==4)
{
shi++;
if(shi>23)
shi=0;
}
if(cnt==5)
{
miao2++;
if(miao2>59)
miao2=0;
}
if(cnt==6)
{
fen2++;
if(fen2>59)
fen2=0;
}
if(cnt==7)
{
shi2++;
if(shi2>23)
shi2=0;
}
}
void jian() //Subtract 1 subfunction
{
if(cnt==2) //Judge whether the number of times key1 is pressed is 2, if so, subtract 1 from the second
{
miao--;
if(miao==255) //Judge whether the second is reduced to 255, if so, clear the second
miao=59;
}
if(cnt==3)
{
fen--;
if(fen==255)
fen=59;
}
if(cnt==4)
{
shi--;
if(shi==255)
shi=23;
}
if(cnt==5)
{
miao2--;
if(miao2==255)
miao2=59;
}
if(cnt==6)
{
fen2--;
if(fen2==255)
fen2=59;
}
if(cnt==7)
{
shi2--;
if(shi2==255)
shi2=23;
}
}
void clock() //Alarm sub-function
{
if(miao2==miao) //Display whether the values of seconds are equalif
(fen2==fen) //Yes, then judge whether the minutes are equalif
(shi2==shi) //Yes, then judge whether they are equal
{
flag1=0; //Yes, then clear the flag1while
(!(flag1==100)) //Judge whether flag1 reaches 100
{
fmq=0; //No, then continue to drive the buzzer to sound, the time is about: 5s
shijian(); //Call the time sub-function
display(); //Call the display sub-function
}
fmq=1;//Turn off the buzzer
}
}
void main()
{
init();//Call the initialization sub-functionwhile
(1)
{
key_scan(); //Call the keyboard scan sub-function
shijian(); //Time sub-function
clock(); //Alarm sub-function
//Display sub-function
if(cnt==0||cnt==1||cnt==2||cnt==3||cnt==4||cnt==8)
{
display();
}
if(cnt==5||cnt==6||cnt==7)
{
display_1();
}
}
}
void time0() interrupt 1 //Timer 0
{
TH0=(65536-50000)/256; //Initial value 50ms
TL0=(65536-50000)%256;
flag++; //Flag
flag1++;
}
Previous article:Notes on using Keil C51 MCU development environment
Next article:DS18B20 reads temperature and displays it - assembly program
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- 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
- A senior project used a digital tube, a three-pin inductor, and a three-pin capacitor.
- The role of parallel resistance on the capacitor of the integration circuit
- Practical sharing: The harm of Miller effect to MOSFET switching process
- Last day! Free review of Beetle ESP32-C3, hurry up and get it
- SVPWM principle, implementation, simulation analysis
- Why is the waveform of LM324 distorted?
- Answer the questions to win prizes | TDK special reports are waiting for you
- Today, People's Daily pushes information from the Ministry of Industry and Information Technology
- RK3399 development board Android image burning Windows system image burning
- TouchGFX Design Whack-a-Mole