#include<reg51.h>
uchar code tab[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}
void view() /*七段数码管显示程序 七个管轮流液悉显示*/
{P3=0x01
unsigned int i=0
while(i<6)
{n=vnum
P1=tab[n]
P3<<=1
i++
}
}
void mDelay(unsigned int Delay) /*延时程序*/
{ unsigned int i
for(Delay>虚埋州0Delay--)
{ for(i=0i<124i++)
{}
}
}
void jumpnum() /*用于钟表时分秒的进位*/
{if(jnum(5)>9) /*秒进位
{jnum[4]=jnum[4]+1
jnum[5]=0
if(jnum[4]>6) /*秒到分进位
{jnum[3]=jnum[3]+1
jum[4]=0
if(jnum[3]>9) /*分进位
{jnum[2]=jnum[2]+1
jnum[3]=0
if(jnum[2]>6) /*分到时进位
{jnum[1]=jnum[1]+1
jnum[2]=0
if(jnum[1]>9) /*时的进位
{jnum[1]=0
jnum[0]=jnum[0]+1
}
else if(jnum[1]>4&&jnum[0]>=2)
{jnum[1]=0
jnum[0]=0
}
}
}
}
}
void key() /*键盘控制程序
{if((P2&0xFF)!=0xFF)/*防抖动
{mDelay(3000)
if((P2&0xFF)!=0xFF)
{unsign int c
c=1
while(c<4)
{ if((P2&0xFF)!=0xFF)
{mDelay(3000)
vnum[]=num[]
void view()
if((P2&0xFF)!=0xFF)
{switch(P2): /*判断不同的按键
{case 0xFE: c++/*c用于判断是闹钟调时c=3还是时钟调时c=2
case oxFD :if(c=2) /*分位增加
{num[3]++}
else if(c=3)
{lnum[3]++
}
case oxFC :if(c=2) /*时位增加
{num[1]++}
else if(c=3)
{lnum[1]++
}
}
if(c=2) /*增加后判断是否需要进位
{jnum[]=num[]
jumpnum()
vnum[]=num[]
void view[]}
if(c=3)
{jnum[]=lnum[]
jumpnum()
vnum[]=num[]
void view[]}}}}}}}
void clock() /*闹钟程序
{for(cl=0num[cl]==lnum(cl)cl=cl+1) /*当前时间与设定时间相同
{}
unsigned int kp=0
if(cl==5&&kp==0) /*闹钟铃声
{for(i=1i<100i++)
{P1_7=~P1_7
for(j=1i<=100j++)}
void mDelay(3000)
for(i=1i<100i++)
{P1_7=~P1_7
for(j=1i<=100j++)}
vnum[]=num[]
void view()
if((P2&0xFF)!=0xFF)/*判断是否有按键,有则停止闹钟
{mDelayd(3000)
void view()
if((P2&0xFF)!=0xFF)
{kp=1}
}}}
viod intclock() /*整点报时程序
{unsigned int ic,i
if(num[2]==5&&num[3]==9&&num[4]==5&&num[5]==0) /*判断差十秒整点差蔽
{if(ic=num[5]+2(ic==(num[5]+2))&&num[5]<59ic=num[5]) /*开始发出500hz声因
{for(i=0i<100i++)
{P1_7=~P1_7
for(i=0i<=100i++)
}
for(i=1i<150i++) /*1000hz声音
{P1_7=~P1_7
for(i=0i<=50i++)
}}}}
void count() inturrupt 1 using 2 /*计时程序
{cn++
if(cn==20)
{num[5]++
cn=0
}
}
void main()
{ P2=0x00
unsigned int num[5],jnum[5],lnum[5]
ET0=1
EA=1
TR0=1
EX0=1
EX1=1
IT0=1
IT1=1
TMOD=0x01
TH0=0x3C
TL0=0xB0
unsigned int cn=0
P2=0x00
while(1)
{void key()
void view()
void jumpnumber()
void clock()
void intclock()
}
}
用C语言实现的闹钟程序#include
#include
#include
#include
#include
void
main()
{
int
gd=DETECT,gm
int
x=320,y=240,r=200,i,h,m,s,thetamin,thetasec
struct
time
t
char
n[12][3]={"3","2","1","12","11","10","9","8","7","6","5","4"}
initgraph(&gd,&gm,"g:\\物亩tc\\bgi")//图形驱动器路径,根据自己的系统更换。
circle(x,y,210)
setcolor(4)
settextstyle(4,0,5)
for(i=0i<12i++)
{
if(i!=3)
outtextxy(x+(r-14)*cos(M_PI/6*i)-10,y-(r-14)*sin(M_PI/6*i)-26,n[i])
else
outtextxy(x+(r-14)*cos(M_PI/6*i)-20,y-(r-14)*sin(M_PI/6*i)-26,n[i])
}
gettime(&t)
printf("The
current
time
is:
%2d:%02d:%02d.%02d\n",t.ti_hour,
t.ti_min,
t.ti_sec,
t.ti_hund)
while(!kbhit())
{
setcolor(5)
setfillstyle(1,5)
circle(x,y,10)
floodfill(x,y,5)
gettime(&t)
if(t.ti_min!=m)
{
setcolor(0)
line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180
)))
circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180))
,10)
line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h
-((m/2)*(M_PI/180))))
circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-(
(m/2)*(M_PI/180))),10)
}
if(t.ti_hour>12)
t.ti_hour=t.ti_hour-12
if(t.ti_hour<核腔4)
h=abs(t.ti_hour-3)
else&n
bsp
h=15-t.ti_hour
m=t.ti_min
if(t.ti_min<=15)
thetamin=(15-t.ti_min)*6
else
thetamin=450-t.ti_min*6
if(t.ti_sec<=15)
thetasec=(15-t.ti_sec)*6
else
thetasec=450-t.ti_sec*6
setcolor(4)
line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h
-((m/2)*(M_PI/罩氏森180))))
circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-(
(m/2)*(M_PI/180))),10)
line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180
)))
circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180))
,10)
setcolor(15)
line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180
)))
delay(1000)
setcolor(0)
line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180
)))
}
}
该程序已在Turbo
C++3.0中通过编译.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)