#include <stdio.h>
#include <reg51.h>
#include <string.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit Sys_running = P1^0
sbit beep = P2^3
sbit Contract = P1^1 //继电器状态
sbit dula = P2^6
sbit wela = P2^7
sbit menu = P3^4
sbit up = P3^5
sbit down = P3^6
sbit cancel = P3^7
sfr ISP_CONTR=0xe7
uchar Total_Hour,Total_Minute,Total_Second //定时小时、分钟、秒
uchar shiwan_c,wan_c,qian_c,bai_c,sh_c,g_c //显示各位参数
uchar count_ms,count_second,count_minute //计时计数器
uchar key_num //按键功能计数器
uchar beep_flag //定时时间到标志
uchar Sys_flag //系统大循环标志
uchar code table[] ={
0x3f,0x06,0x5b,0x4f, //0~15数码管编码
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
0x40,0x6f,0x5c,0x5e, //- g o d数码管编码
0x54,0x50,0x00 //n r "消隐"数码管编码
}
uchar code table_dot[]={
0x3f,0x06,0x5b,0x4f, //0~15数码管编码
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
0xbf,0x86,0xdb,0xcf, //0~9数码管编码,带小数点
0xe6,0xed,0xfd,0x87,
0xff,0xef
}
void delay(unsigned int i) //延时程序
{
unsigned char j
unsigned char k
for(j=ij>0j--)
for(k=125k>0k--)
}
void System_Init()
{
TMOD=0x01
//PCON=0x80 //设定SMOD为1,波特率加倍
TH0=(65536-50000)/256 //时钟初值
TL0=(65536-50000)%256 //时钟初值
EA=1 //开总中断
ET0=1 //开定时器0中断
TR0=1 //启动定时器0
}
void Sys_run_indicator()
{
Sys_flag++
if(Sys_flag==100)
{
Sys_flag=0
Sys_running=~Sys_running
delay(5)
}
}
void Data_deal()
{
if(count_ms==20) //1S处理
{
count_ms=0
if(Total_Second==0)
{
Total_Second=60
count_second=1
}
Total_Second--
}
if(count_second==1) //1MIN处理
{
count_second=0
if(Total_Minute==0)
{
Total_Minute=60
count_minute=1
}
Total_Minute--
}
if(count_minute==1) //1HOUR处理
{
count_minute=0
if(Total_Hour==0)
{
Total_Hour=60 //定时时间到处理
beep_flag=1
}
Total_Hour--
}
shiwan_c=Total_Hour/10
wan_c=Total_Hour%10
qian_c=Total_Minute/10
bai_c=Total_Minute%10
sh_c=Total_Second/10
g_c=Total_Second%10
}
void Time_reach_deal()
{
beep=1 //定时时间到报警处理
if(beep_flag==1)
{
TR0=0 //关闭定时器
Contract=0 //计时时间到,继电器断开
}
}
void display_all(uint shiwan,uint wan,uint qian,uint bai,uint sh,uint g)
{
dula=0
P0=table[shiwan] //显示十万位
dula=1
dula=0
wela=0
P0=0xfe
wela=1
wela=0
delay(1)
dula=0
P0=table[wan] //显示万位
dula=1
dula=0
wela=0
P0=0xfd
wela=1
wela=0
delay(1)
dula=0
P0=table[qian] //显示千位
dula=1
dula=0
wela=0
P0=0xfb
wela=1
wela=0
delay(1)
dula=0
P0=table[bai] //显示百位
dula=1
dula=0
wela=0
P0=0xf7
wela=1
wela=0
delay(1)
dula=0
P0=table[sh] //显示十位
dula=1
dula=0
wela=0
P0=0xef
wela=1
wela=0
delay(1)
dula=0
P0=table[g] //显示个位
dula=1
dula=0
wela=0
P0=0xdf
wela=1
wela=0
delay(1)
}
/*
void display_adjust(uint shiwan,uint wan,uint qian,uint bai,uint sh,uint g,uchar key_num)
{
uchar idisplay
if(key_num==1)
{
}
}
*/
void Key_num_deal()
{
if(menu==0)
{
delay(5)
if(menu==0)
{
key_num++
if(key_num>=4)
{
key_num=1
}
}
while(!menu)
}
if(cancel==0) //返回大循环
{
delay(5)
if(cancel==0)
{
key_num=0
}
while(!cancel)
}
if(key_num==1) //时调节
{
if(up==0)
{
delay(5)
if(up==0)
{
Total_Hour++
if(Total_Hour>99)
{
Total_Hour=0
}
}
while(!up)
}
if(down==0)
{
delay(5)
if(down==0)
{
Total_Hour--
if(Total_Hour<=0)
{
Total_Hour=99
}
}
while(!down)
}
display_all(shiwan_c,wan_c,22,22,22,22) //定时时间未到,小时闪烁显示
}
if(key_num==2) //分调节
{
if(up==0)
{
delay(5)
if(up==0)
{
Total_Minute++
if(Total_Minute>59)
{
Total_Minute=0
}
}
while(!up)
}
if(down==0)
{
delay(5)
if(down==0)
{
Total_Minute--
if(Total_Minute<=0)
{
Total_Minute=59
}
}
while(!down)
}
display_all(22,22,qian_c,bai_c,22,22) //定时时间未到,小时闪烁显示
}
if(key_num==3) //秒调节
{
if(up==0)
{
delay(5)
if(up==0)
{
Total_Second++
if(Total_Second>59)
{
Total_Second=0
}
}
while(!up)
}
if(down==0)
{
delay(5)
if(down==0)
{
Total_Second--
if(Total_Second<=0)
{
Total_Second=59
}
}
while(!down)
}
display_all(22,22,22,22,sh_c,g_c) //定时时间未到,小时闪烁显示
}
}
void main() //主程序
{
uint i,j //系统初始化临时变量
//////////////热启动烧写程序////////////////
if((PCON&0x10)==0) //如果POF位=0
{
PCON=PCON|0x10 //将POF位置1
ISP_CONTR=0x60 //软复位,从ISP监控区启动
}
else
{
PCON=PCON&0xef //将POF位清零
}
//////////////////////////////////////////////
Total_Hour=8 //定时初值初始化
Total_Minute=0
Total_Second=0
for(i=0i<30i++)
for(j=0j<30j++)
{
display_all(16,17,18,18,19,16) //初始化,显示"-good-"
}
System_Init()
while(1)
{
Sys_run_indicator()
Data_deal()
Time_reach_deal()
Key_num_deal()
if(key_num==0)
{
switch(beep_flag)
{
case 0:
display_all(shiwan_c,wan_c,qian_c,bai_c,sh_c,g_c) //定时时间未到,正常显示
break
case 1:
display_all(16,14,20,13,16,16) //定时时间到,显示"-End--"
break
default:
display_all(14,21,21,18,21,1) //否则显示"Error1"
break
}
}
}
}
/*******************************************************/
/*****3 为定时器1的中断号 1 定时器0的中断号 ***********/
/*****0 外部中断1 2 外部中断2 ****************/
/*****4 串口********************************************/
void T0_time() interrupt 1 //定时器0中断服务
{
TH0=(65536-50000)/256 //重装时钟初值
TL0=(65536-50000)%256 //重装时钟初值
count_ms++
}
/************************* The End *****************************/
PrivateSub
Form_Load()
Timer1.Enabled
=
True
Timer1.Interval
=
1000
Label1.Caption
=
Time$
'在Form_Load就要加上,否则你要等1秒后Label才会开始跑
End
Sub
Private
Sub
Timer1_Timer()
Label1.Caption
=
Time$
End
Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)