?? zzp0.c
字號:
#include "reg51.h"
#define unchar unsigned char
#define PSEG 0x30 /*P.的代碼*/
#define N 60
unchar code seg[]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09,0x30,0xff,0x71,0x13};/*0-9和暗的代碼*/
sbit DISPCON=P3^2;/*顯示控制*/
sbit S1=P1^3; /*設(shè)置鍵*/
sbit S2=P1^2; /*數(shù)字+1鍵*/
sbit L1=P1^7; /*L1,L2為秒指示*/
sbit L2=P1^6;
sbit S3=P1^5; /*鬧鈴開設(shè)置鍵*/
sbit S4=P1^4; /*鬧鈴關(guān)設(shè)置鍵*/
sbit light=P3^7; /*鬧鈴控制*/
unchar buf[4]; /*顯示緩沖區(qū)*/
unchar hourh;
unchar hourl;
unchar minuh;
unchar minul;
unchar second;
unchar onhh;
unchar onhl;
unchar onmh;
unchar onml;
unchar offhh;
unchar offhl;
unchar offmh;
unchar offml;
unchar count; /*溢出時鐘計數(shù)器*/
unchar bdata setflag; /*設(shè)置標志字節(jié)*/
unchar bdata uflag; /*設(shè)置通用字節(jié)*/
unchar onset; /*鬧鈴是否設(shè)置標志*/
sbit hh=setflag^0; /*設(shè)置當前設(shè)置的時間標志位*/
sbit hl=setflag^1;
sbit mh=setflag^2;
sbit ml=setflag^3;
sbit fresh=uflag^0; /*顯示刷新標志位*/
sbit ss=uflag^1; /*為閃爍標志*/
sbit sec=uflag^2; /*秒指示標志*/
sbit onf=uflag^3; /*鬧鈴標志*/
sbit tfix=uflag^4; /*修正指示標志*/
void system(void); /*系統(tǒng)初始化函數(shù)*/
void t0_init(void); /*T0初始化函數(shù)*/
void uart_init(void); /*串行口傳送初始化函數(shù)*/
void display(void); /*顯示函數(shù)*/
void delay20ms(void);
void settime(void); /*時間設(shè)置函數(shù)*/
void ontime(void); /*鬧鈴開設(shè)置函數(shù)*/
void offtime(void); /*鬧鈴關(guān)設(shè)置函數(shù)*/
void adjust(void); /*鬧鈴時間到判斷函數(shù)*/
void t1_init(void); /*T1初始化函數(shù)*/
/****************************************************/
/* 以下為主函數(shù) */
/****************************************************/
void main(void)
{
system();
t0_init();
t1_init();
uart_init();
if(!S3)
{
buf[0]=0x0b;
buf[1]=0x0b;
buf[2]=0x0d;
buf[3]=0x0;
display();
ontime();
onset=1;
tfix=0;
count=0;
}
if(!S4)
{
buf[0]=0x0b;
buf[1]=0x0c;
buf[2]=0x0c;
buf[3]=0x0;
display();
offtime();
}
buf[0]=0x0b;
buf[1]=0x0b;
buf[2]=0x0b;
buf[3]=0x0a;
display();
settime();
TR0=1;
ET0=1;
display();
while(1)
{
if(fresh)
{
buf[0]=minul;
buf[1]=minuh;
buf[2]=hourl;
buf[3]=hourh;
display();
fresh=0;
}
else
{
if(sec)
{
sec=!sec;
L1=!L1;
L2=!L2;
if(onf)
{
light=1;
}
else
light=0;
delay20ms();
}
}
}
}
/****************************************************/
/* 以下為顯示函數(shù) */
/****************************************************/
void display(void)
{
unchar out;
DISPCON=1;
out=seg[buf[0]];
SBUF=out;
while(!TI);
TI=0;
out=seg[buf[1]];
out&=0xfe;
SBUF=out;
while(!TI);
TI=0;
out=seg[buf[2]];
SBUF=out;
while(!TI);
TI=0;
out=seg[buf[3]];
SBUF=out;
while(!TI);
TI=0;
DISPCON=0;
}
/****************************************************/
/* 以下為時間走時函數(shù) */
/****************************************************/
void timer0(void) interrupt 1 using 1
{
unchar x;
fresh=0;
sec=0;
TH0=0x3c;
TL0=0xb0;
count++;
if(tfix)
x=N;
else
x=10;
if(count!=x)
return;
else
{//⑤
tfix=0;
sec=1;
count=0;
second++;
if(second!=60)
return;
else
{//④
fresh=1;
second=0;
minul++;
if(minul!=10)
{
adjust();
return;
}
else
{//③
minul=0;
minuh++;
if(minuh!=6)
{
adjust();
return;
}
else
{//②
minuh=0;
hourl++;
if(hourh==0||hourh==1)
{
if(hourl!=10)
{
adjust();
return;
}
else
{
hourl=0;
hourh++;
return;
}
}
else
{//①
if(hourl!=5)
{
adjust();
return;
}
else
{
hourl=0;
hourh=0;
tfix=1;
return;
}
}//①
}//②
}//③
}//④
}//⑤
}
/****************************************************/
/* 以下為鬧鈴判斷函數(shù) */
/****************************************************/
void adjust(void)
{
if(!onset)
{
onf=0;
return;
}
else
{
if(hourh==onhh&&hourl==onhl&&minuh==onmh&&minul==onml)
onf=1;
else
onf=0;
return;
}
}
/****************************************************/
/* ;以下為T0初始化函數(shù) */
/****************************************************/
void t0_init(void)
{
TMOD=0x11;
TH0=0x3c;
TL0=0xb0;
}
/****************************************************/
/* 以下為串行口傳送初始化函數(shù) */
/****************************************************/
void uart_init(void)
{
SCON=0;
}
/****************************************************/
/* 以下為系統(tǒng)初始化函數(shù) */
/****************************************************/
void system(void)
{
IE=0;
buf[0]=0;
buf[1]=0;
buf[2]=0;
buf[3]=0;
hourh=0;
hourl=0;
minuh=0;
minul=0;
second=0;
count=0;
setflag=0;
uflag=0;
EA=1;
}
/****************************************************/
/* 以下為時間設(shè)置函數(shù) */
/****************************************************/
void settime(void)
{
unchar i;
while(S1) {};
for(i=0;i<4;i++)
delay20ms();
while(S1) {};
setflag=1;
ss=0;
TR1=1;
ET1=1;
while(1)
{
buf[0]=minul;
buf[1]=minuh;
buf[2]=hourl;
buf[3]=hourh;
if(ss)
{
switch(setflag)
{
case 0x01:buf[0]=0x0b;
break;
case 0x02:buf[1]=0x0b;
break;
case 0x04:buf[2]=0x0b;
break;
case 0x08:buf[3]=0x0b;
break;
}
}
display();
sett_0: if(S1) goto sett_1;
for(i=0;i<4;i++)
delay20ms();
if(S1) goto sett_1;
if(ml)
{
TR1=0;
ET1=0;
buf[0]=minul;
buf[1]=minuh;
buf[2]=hourl;
buf[3]=hourh;
return;
}
continue;
sett_1: if(S2) goto sett_0;
for(i=0;i<4;i++)
delay20ms();
if(S2) goto sett_0;
if(!hh) goto sett_2;
{
hourh++;
if(hourh==3)
hourh=0;
}
continue;
sett_2:if(!hl)goto sett_3;
{
hourl++;
if(hourh==2) goto sett_20;
if(hourl==10) goto sett_21;
continue ;
sett_21:hourl=0;
continue;
sett_20:if(hourl==5) goto sett_22;
continue;
sett_22:hourl=0;
continue;
}
sett_3:if(!mh) goto sett_4;
{
minuh++;
if(minuh==6) goto sett_30;
continue;
sett_30:minuh=0;
continue;
}
sett_4:
minul++;
if(minul==10)goto sett_40;
continue;
sett_40:minul=0;
continue;
}
}
void ontime(void)
{
unchar i;
while(S1);
for(i=0;i<4;i++)
delay20ms();
while(S1);
setflag=1;
ss=0;
TR1=1;
ET1=1;
while(1)
{
buf[0]=onml;
buf[1]=onmh;
buf[2]=onhl;
buf[3]=onhh;
if(ss)
{
switch(setflag)
{
case 0x01:buf[0]=0x0b;
break;
case 0x02:buf[1]=0x0b;
break;
case 0x04:buf[2]=0x0b;
break;
case 0x08:buf[3]=0x0b;
break;
}
}
display();
sett_0: if(S1) goto sett_1;
for(i=0;i<4;i++)
delay20ms();
if(S1) goto sett_1;
if(ml)
{
TR1=0;
ET1=0;
buf[0]=onml;
buf[1]=onmh;
buf[2]=onhl;
buf[3]=onhh;
return;
}
setflag<<=1;
continue;
sett_1: if(S2) goto sett_0;
for(i=0;i<4;i++)
delay20ms();
if(S2) goto sett_0;
if(!hh) goto sett_2;
{
onhh++;
if(onhh!=3)
continue;
onhh=0;
continue;
}
sett_2:if(!hl)goto sett_3;
{
onhl++;
if(onhh==2) goto sett_20;
if(onhl==10) goto sett_21;
continue ;
sett_21:onhl=0;
continue;
sett_20:if(onhl==5) goto sett_22;
continue;
sett_22:onhh=0;
continue;
}
sett_3:if(!mh) goto sett_4;
{
onmh++;
if(onmh==6) goto sett_30;
continue;
sett_30:onmh=0;
continue;
}
sett_4:
onml++;
if(onml==10)goto sett_40;
continue;
sett_40:onml=0;
continue;
}
}
void offtime(void)
{
unchar i;
while(S1);
for(i=0;i<4;i++)
delay20ms();
while(S1);
setflag=1;
ss=0;
TR1=1;
ET1=1;
while(1)
{
buf[0]=offml;
buf[1]=offmh;
buf[2]=offhl;
buf[3]=offhh;
if(ss)
{
switch(setflag)
{
case 0x01:buf[0]=0x0b;
break;
case 0x02:buf[1]=0x0b;
break;
case 0x04:buf[2]=0x0b;
break;
case 0x08:buf[3]=0x0b;
break;
}
}
display();
sett_0: if(S1) goto sett_1;
for(i=0;i<4;i++)
delay20ms();
if(S1) goto sett_1;
if(ml)
{
TR1=0;
ET1=0;
buf[0]=offml;
buf[1]=offmh;
buf[2]=offhl;
buf[3]=offhh;
return;
}
setflag<<=1;
continue;
sett_1: if(S2) goto sett_0;
for(i=0;i<4;i++)
delay20ms();
if(S2) goto sett_0;
if(!hh) goto sett_2;
{
offhh++;
if(offhh!=3)
continue;
offhh=0;
continue;
}
sett_2:if(!hl)goto sett_3;
{
offhl++;
if(offhh==2) goto sett_20;
if(offhl==10) goto sett_21;
continue ;
sett_21:offhl=0;
continue;
sett_20:if(offhl==5) goto sett_22;
continue;
sett_22:offhh=0;
continue;
}
sett_3:if(!mh) goto sett_4;
{
offmh++;
if(offmh==6) goto sett_30;
continue;
sett_30:offmh=0;
continue;
}
sett_4:
offml++;
if(offml==10)goto sett_40;
continue;
sett_40:offml=0;
continue;
}
}
void delay20ms(void)
{
unchar i,j;
for(i=0;i<20;i++)
for(j=0;j<255;j++);
}
/****************************************************/
/* 秒指示閃爍函數(shù) */
/****************************************************/
void timer1(void)interrupt 3 using 1
{
TH1=0x3c;
TL1=0xb0;
count++;
if(count!=5)
return;
ss=!ss;
count=0;
}
/****************************************************/
/* 以下為T1初始化函數(shù) */
/****************************************************/
void t1_init(void)
{
TMOD=0x11;
TH1=0x3c;
TL1=0xb0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -