stc 单片机 做的电子表
#include<stc90.h>
#define uchar unsigned char
#define uint unsigned int
uchar d0,ss,gs,sf,gf,sm,gm;
uchar e1,e2,e3,e4,e5,e6;
uchar s1,q1;
uchar shi,fen,miao;
// 8 4 2 1 8 4 2 1
// h g f e d c b a
//效正 累加数98 跑1320秒 实际1316
//当前已走秒数 乘以 当前累加数 除以除以实际秒数 等于 累加数应得值
uchar code num[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x80};//0-9 点
uchar code wes[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0x7f};//1-8位
sbit dua=P3^0;
sbit laba=P3^1;
sbit a1=P3^2;//时间设置
sbit a2=P3^3;//闹钟设置
sbit a3=P3^4;//按键+
sbit a4=P3^5;//按键-
sbit a5=P3^7;//返回 启动闹钟
void weiduan(uchar z,uchar x); //位段 函数
void chushi(); //初始化函数
void shijian(); //时间显示函数
void shjianbata();//测试时间函数
void shezhi(); //时间设置函数
void naozhong(); //闹钟这是函数
void shu(); //时间最大值限制函数
void delay(uchar d1);//延时函数
//.....................................................................................
void main()
{ chushi(); //初始化时间
TH0=(65535-40000)/256;//定时器装初值
TL0=(65535-40000)%256;
ET0=1;//开定时器0中断
TR0=1;//启动定时器0
EA=1; //打开总中断
laba=1;
while(1)
{
shijian(); //调用时间显示函数 显示时间
if(a1==1){if(a2==1){if(a3==1){if(a4==1){ weiduan(1,10);delay(5);weiduan(3,10);delay(5);weiduan(5,10);delay(5); }}}}
if(a1==0){delay(100);if(a1==0){shezhi();}}
if(a2==0){delay(100);if(a2==0){naozhong();}}//调用闹钟设置函数
if(a5==0){delay(100);{if(a5==0){while(1){if(q1==0){q1=1;break;}else{q1=0;break;}}while(a5==0);delay(100);}}}
if(a2==0){delay(100);if(a2==0){naozhong();} }
if(q1==1){weiduan(6,0);delay(20);if(fen==e2){if(shi==e1){laba=0;}}if(fen!=e2){laba=1;} }
else{laba=1;}
}
}
//.....................................................................................
void naozhong()
{while(1)
{ ss=e1/10;
gs=e1%10;
sf=e2/10;
gf=e2%10;
sm=e3/10;
gm=e3%10;
delay(10);
weiduan(0,ss);delay(10);
weiduan(1,gs);delay(10);
weiduan(2,sf);delay(10);
weiduan(3,gf);delay(10);
weiduan(4,sm);delay(10);
weiduan(5,gm);delay(10);
if(a1==0){delay(100);if(a1==0){while(a1==0);delay(20);break;}}
if(a2==0){delay(100);while(a2==0);delay(20);s1++;}
if(s1==3){s1=0;}
if(s1==1){weiduan(0,10);delay(5);weiduan(1,10);delay(5);
if(a3==0){delay(20);if(a3==0){e1++;}while(a3==0);delay(100);}
if(a4==0){delay(20);if(a4==0){e1--;}while(a4==0);delay(100);}
}
if(s1==2){weiduan(2,10);delay(5);weiduan(3,10);delay(5);
if(a3==0){delay(20);if(a3==0){e2++;}while(a3==0);delay(100);}
if(a4==0){delay(20);if(a4==0){e2--;}while(a4==0);delay(100);}
}
if(s1==0){weiduan(4,10);delay(5);weiduan(5,10);delay(5);
if(a3==0){delay(20);if(a3==0){e3++;}while(a3==0);delay(100);}
if(a4==0){delay(20);if(a4==0){e3--;}while(a4==0);delay(100);}
}
}
}
//.....................................................................................
void shezhi()//时间设置函数
{while(1)
{
ss=shi/10;
gs=shi%10;
sf=fen/10;
gf=fen%10;
sm=miao/10;
gm=miao%10;
shijian();
if(a2==0){delay(100);if(a2==0){while(a2==0);delay(20);break;}}
if(a1==0){delay(100);while(a1==0);delay(20);s1++;}
if(s1==3){s1=0;}
if(s1==1){weiduan(0,10);delay(5);weiduan(1,10);delay(5);
if(a3==0){delay(20);if(a3==0){shi++;}while(a3==0);delay(100);}
if(a4==0){delay(20);if(a4==0){shi--;}while(a4==0);delay(100);}
}
if(s1==2){weiduan(2,10);delay(5);weiduan(3,10);delay(5);
if(a3==0){delay(20);if(a3==0){fen++;}while(a3==0);delay(100);}
if(a4==0){delay(20);if(a4==0){fen--;}while(a4==0);delay(100);}
}
if(s1==0){weiduan(4,10);delay(5);weiduan(5,10);delay(5);
if(a3==0){delay(20);if(a3==0){miao++;}while(a3==0);delay(100);}
if(a4==0){delay(20);if(a4==0){miao--;}while(a4==0);delay(100);}
}
}}
//.....................................................................................
void zhongduan() interrupt 1 //定时器中断函数体
{ TH0=(65535-40400)/256;
TL0=(65535-40400)%256;
if(e3>=60)//判断秒等于或超过60
{e2++; //分钟加1
e3=0;} //秒数清零
if(e2>=60) //判断分钟等于或超过60
{e1++; //小时加1
e2=0;} //分钟数清零
if(e1>=24) //判断小时等于或大于24
{e1=0;} //小时数 清零
d0++;//累加数
shu(); //调用时间最大值限制函数
}
//.....................................................................................
void delay(uchar d1)//延时函数
{uchar t0,t1;
for(t0=d1;t0>0;t0--)
for(t1=110;t1>0;t1--);
}
//.....................................................................................
void weiduan(uchar z,uchar x) //位段选择函数
{ P1=num[x];
dua=1;
dua=0;
P1=wes[z];
}
//.....................................................................................
void shijian()//动态扫描时间显示函数 带延时
{
chushi();//调用时间初始化函数
delay(10);
weiduan(0,ss);delay(10);
weiduan(1,gs);delay(10);
weiduan(2,sf);delay(10);
weiduan(3,gf);delay(10);
weiduan(4,sm);delay(10);
weiduan(5,gm);delay(10);
}
//.....................................................................................
void shu() //时间最大限制函数体
{ if(d0>=98) //判断定时器进入 多少次
{d0=0; //清除累加数
miao++;} //秒数自+
if(miao>=60)//判断秒等于或超过60
{fen++; //分钟加1
miao=0;} //秒数清零
if(fen>=60) //判断分钟等于或超过60
{shi++; //小时加1
fen=0;} //分钟数清零
if(shi>=24) //判断小时等于或大于24
{shi=0;} //小时数 清零
上一篇:STC单片机学习型遥控器程序