C51 program for adjustable time alarm clock

Publisher:ShimmeringStarLatest update time:2012-10-31 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/*
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 //Header file #include #define uchar unsigned char //Macro definition #define uint unsigned int sbit key1=P3^5; //Bit declaration sbit key2=P3^6; sbit key3=P3^7; sbit fmq=P2^0; uchar code table[]={0x3f,0x06,0x5b, //Number displayed by digital tube 0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77, 0xbf,0x86,0xdb,//values ​​with decimal points 0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef}; void jia(); //function declaration void jian(); uchar table_1[6]; //define an array containing 6 values ​​uchar table_2[6]; uchar shi=23,fen=59,miao=50; //display initial value uchar shi1,fen1,miao1,shi2,fen2,miao2,shi3,fen3,miao3;//define global variables uchar flag,flag1,cnt,count;//define global variables void delay(uchar i) //delay function for dynamic scanning of digital tubes { uchar x,y; for(x=i;x>0;x--) for(y=110;y>0;y--); } void init() //initialization function { TMOD=0X01; //Working mode 1 TH0=(65536-50000)/256; //Timing time: 50ms TL0=(65536-50000)%256; ET0=1; //Open timer EA=1; //Open total interrupt TR0=1; //Start timer } void display() //Display sub-function, used to display time value { uchar i,j; table_1[0]=miao%10; //Separate the seconds and tens digitstable_1[1]=miao/10; table_1[2]=fen%10+11; //Separate the minutes and tens digitstable_1[3]=fen/10; table_1[4]=shi%10+11; //Separate the hours and tens digitstable_1[5]=shi/10; j=0x7f; //Scan from seconds to hoursfor (i=0;i<6;i++) { P2=j; P0=table[table_1[i]];//display value delay(10); j=_cror_(j,1);//circular right shift } } void display_1() //display sub-function, used to display timing time { uchar i,j; table_2[0]=miao2%10; //the following meanings are the same as abovetable_2[1]=miao2/10; table_2[2]=fen2%10+11; table_2[3]=fen2/10; table_2[4]=shi2%10+11; table_2[5]=shi2/10; j=0x7f; for(i=0;i<6;i++) { P2=j; P0=table[table_2[i]]; delay(10); j=_cror_(j,1); } } void shijian() //time sub-function { if(flag>=20) //judge whether it is one second { flag=0; //When it reaches 60s, the flag is cleared miao++; //Increase the seconds by 1 if(miao>=60) //Judge whether the seconds have reached 60s {
















































































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++;
}

Reference address:C51 program for adjustable time alarm clock

Previous article:Notes on using Keil C51 MCU development environment
Next article:DS18B20 reads temperature and displays it - assembly program

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号