//控制过程是计时开始时:OUT=0;计时过程有声音,OUT=0;后5秒KV=0;结束前5秒KV=1; 结束时OUT=1;并一直报警。
//OUT=0;后5秒KV=0;没整好。有不规范的地方,也请规范一下。
#include<STC15.h>
#define ulong unsigned long
char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
#define LED P1 //P1为数码管的段选
sbit W1=P3^7; //P37为数码管的位选
sbit W2=P3^1; //P31为数码管的位选
sbit W3=P3^0; //P30为数码管的位选
sbit time_shift=P3^4; //P3^4为"移位"按键
sbit time_set=P3^5; //P3^5为"修改"按键
sbit set_inc=P3^2; //P3^2为"增加"按键
sbit time_run=P3^3; //P3^3为"计时开始暂停"按键
sbit OUT=P2^4; //P5^4为MA高压开,P24仿真。
sbit KV=P2^5; //P5^5为升压输出,P25仿真。
sbit KVI=P2^6; //P2^6为KV状态检测
sbit LI=P3^6; //P3^6为MA过流输入
sbit WA=P2^7; //P3^7为无MA输入
sbit GU=P1^7; //P1^7为MA过流故障灯
sbit LIGHT=P2^3; //P2^3
sbit SPEAKER=P2^7; //P2^7喇叭
unsigned char sec,min,sec_ge,sec_shi,min_ge,min_shi,t; //定义秒,分,时,秒的个位,十位,分的个位,十位
int state; //秒表状态指示 0-计时暂停 1-正在倒计时 2-计时时间参数修改
int flag; //秒表修改数据指示 0-分钟十位 1-分钟个位 2-秒钟十位 3-秒钟个位
int blink;
//---------------------------------------------------------------------
void init();
void display_run();
void display_pause();
void delay(int);
void time_modify();
//---------------------------------------------------------------------
void main()
{
init();
state=0;
flag=0;
blink=1;
t=0;
LIGHT=1;
SPEAKER=1;
OUT=1;
KVI=1;
min=2;
sec=0;
sec_ge=0; //秒的个位和十位
sec_shi=0;
min_ge=2; //分的个位和十位
while(1)
{
if(time_set==0) //如果"调时"按键按下
{
delay(5); //延时去抖
if(time_set==0)
state=2;
}
if(state==0)
{
TR0=0;
TR1=0;
display_pause();
}
if(state==0)
{
OUT=1;
display_pause();
}
if(state==1)
{
TR0=1;
TR1=0;
display_run();
}
if(state==2)
{
TR1=1;
OUT=1;
time_modify();
}
if(state==1)
{
TR1=1;
OUT=0;
time_modify();
}
if(state==1)
{
OUT=0;
time_modify();
}
if(state==1)
{ if(KVI==0)
{time_set=0;
delay(5); //延时
OUT=1;
time_modify();
}
}
}
}
//---------------------------------------------------------------------
void init()
{
TMOD=0x11;
EA=1; //开总中断
ET0=0;
ET1=0; //定义定时器0工定时器1作与方式1
TH0=0x4d; //定时器0赋初值
TL0=0xfc;
TH1=15536/256; //定时器1赋初值
TL1=15536%256;
//中断初始化
EX0=1;
PX0=0;
IT0=1;
EX1=1;
PX1=1;
IT1=1;
}
//外中断1
void INT_1() interrupt 2 using 0
{
if( time_run==0)//表示按钮有按下
{
delay(20); //延时
if(time_run==0)
{
state=(state+1)%2;
LIGHT=1;
SPEAKER=1;
}
}
}
//外中断0
void INT_0() interrupt 0 using 1
{
if( set_inc==0)//表示按钮有按下
{
delay(20); //延时去抖
if(set_inc==0)
{
switch(flag) //分支结构,根据flag的值来判断哪一位加1
{
case 0:
{
min=(min+1)%10+min/10*10;
break;
}
case 1:
{
sec=(sec+10)%60;
break;
}
case 2:
{
sec=(sec+1)%10+sec/10*10;
break;
}
default:break;
}
}
}
}
//--------------------------------延时子函数------------------------------------
void delay(int x)
{
int i,j;
for(i=x;i>0l;i--)
for(j=255;j>0;j--)
;
}
//-------------------------------显示子函数---------------------------------------
void display_run()
{
if(TF0==1) //如果定时器溢出
{
TF0=0; //清中断标志位
t++;
if(t==20)
{
t=0;
if(min==0&&sec==0)
{
OUT=1;
}
if(sec==0)
{
sec=59;
min--;
}
else sec--;
if(state==1)
{ GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(5000); //想开机5秒后KV=0;没整好。影响读秒。
}
KV=0;
if(min==0&&sec<5)
{
KV=1;
SPEAKER=0;
delay(20);
SPEAKER=1;
}
if(min==0&&sec==0)
{
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
OUT=1;
state=3;
min=2;//回归初始化
sec=0;
sec_ge=0; //秒的个位和十位
sec_shi=0;
min_ge=2; //分的个位和十位
while(1)
{
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(2);
GU=1;
SPEAKER=1;
delay(2);
GU=0;
SPEAKER=0;
delay(2);
GU=1;
SPEAKER=1;
delay(2);
GU=0;
SPEAKER=0;
delay(2);
GU=1;
SPEAKER=1;
delay(2);
GU=0;
SPEAKER=0;
delay(2);
GU=1;
SPEAKER=1;
delay(2);
GU=0;
SPEAKER=0;
delay(2);
GU=1;
SPEAKER=1;
delay(2);
GU=0;
SPEAKER=0;
delay(2);
GU=1;
SPEAKER=1;
delay(2);
GU=0;
SPEAKER=0;
delay(2);
GU=1;
SPEAKER=1;
delay(2);
GU=0;
SPEAKER=0;
delay(2);
GU=1;
SPEAKER=1;
delay(2);
GU=0;
SPEAKER=0;
delay(2);
GU=1;
SPEAKER=1;
delay(2);
GU=0;
SPEAKER=0;
delay(2);
GU=1;
SPEAKER=1;
delay(2);
GU=0;
SPEAKER=0;
delay(2);
GU=1;
SPEAKER=1;
delay(2);
GU=0;
SPEAKER=0;
delay(2);
GU=1;
SPEAKER=1;
delay(2);
GU=0;
SPEAKER=0;
delay(2);
GU=1;
SPEAKER=1;
delay(2);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0; SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(1);
GU=1;
SPEAKER=1;
delay(1);
GU=0;
SPEAKER=0;
delay(5);
GU=1;
SPEAKER=1;
delay(5);
GU=0;
SPEAKER=0;
delay(5);
GU=1;
SPEAKER=1;
delay(5);
GU=0;
SPEAKER=0;
delay(5);
GU=1;
SPEAKER=1;
delay(5);
GU=0;
delay(500);
}
}
}
}
sec_ge=sec%10; //秒的个位和十位
sec_shi=sec/10;
min_ge=min%10; //分的个位和十位
W1=0; //循环扫描
LED=tab[sec_ge];
delay(1);
W1=1;
W2=0;
LED=tab[sec_shi];
delay(1);
W2=1;
W3=0;
LED=tab[min_ge];
delay(1);
W3=1;
}
void display_pause()
{
W1=0; //循环扫描
LED=tab[sec_ge];
delay(1);
W1=1;
W2=0;
LED=tab[sec_shi];
delay(1);
W2=1;
W3=0;
LED=tab[min_ge];
delay(1);
W3=1;
}
void time_modify()
{ //修改位的闪烁状态
if(TF1==1)
{
TF1=0;
blink=(blink+1)%2;
}
//修改具体哪个数码管指示
if(time_shift==0)
{
delay(60);
if(time_shift==0)
flag=(flag+1)%3;
}
// 修改状态下的数码管显示
switch(flag) //分支结构,根据flag的值来决定扫描状态
{
case 0:
{
sec_ge=sec%10; //秒的个位和十位
sec_shi=sec/10;
min_ge=min%10; //分的个位和十位
min_shi=min/10;
W1=0; //循环扫描
LED=tab[sec_ge];
delay(1);
W1=1;
W2=0;
LED=tab[sec_shi];
delay(1);
W2=1;
if(blink==1)
{
W3=0;
LED=tab[min_ge];
delay(1);
W3=1;
}
break;
}
case 1:
{
sec_ge=sec%10; //秒的个位和十位
sec_shi=sec/10;
min_ge=min%10; //分的个位和十位
min_shi=min/10;
W1=0; //循环扫描
LED=tab[sec_ge];
delay(1);
W1=1;
if(blink==1)
{
W2=0;
LED=tab[sec_shi];
delay(1);
W2=1;
}
W3=0;
LED=tab[min_ge];
delay(1);
W3=1;
break;
}
case 2:
{
sec_ge=sec%10; //秒的个位和十位
sec_shi=sec/10;
min_ge=min%10; //分的个位和十位
min_shi=min/10;
if(blink==1)
{
W1=0; //循环扫描
LED=tab[sec_ge];
delay(1);
W1=1;
}
W2=0;
LED=tab[sec_shi];
delay(1);
W2=1;
W3=0;
LED=tab[min_ge];
delay(1);
W3=1;
break;
break;
}
default:break;
}
}
|