📄 timer.c
字号:
//------------------------------
//设置当前值:FE A0 02 MM NN (BCD码)
//要当前值: FE A1 01 00 回传值 FE A1 02 MM NN BCD码)
//------------------------------
#include<At89x52.h>
#include<initled.h>
//------------------------------------------
#define condition_1 time_min==0&&time_sec==0
#define condition_2 time_min==0&&time_sec!=0
#define condition_3 time_min!=0&&time_sec==0
#define condition_4 time_min!=0&&time_sec!=0
#define so_pclength p_pc_head-so_pc_buf
//------------------------------------------
sbit d_clk=P3^7;
sbit d_dat=P3^6;
sbit d_st=P3^5;
bit bit_timech=0;
unsigned char d[5];
unsigned char so_pc_buf[10];
unsigned char time60,time_min=99,time_sec=59,*p_pc_head=so_pc_buf;
//////////////////////
void out_disp(unsigned char *p)
{unsigned char i,j;
for(i=0;i<5;i++,p++)
{for (j=0;j<8;j++)
{if (*p&1) d_dat=1;else d_dat=0;
d_clk=1;*p>>=1;d_clk=0;
}
}
d_st=1;
for(i=0;i<10;i++);
d_st=0;
}
//------------------------------------
//d0min/ d1min% d2- d3sec/ d4sec%
//------------------------------------
void calculattime(void )
{ if(condition_1);
else if(condition_2)time_sec-=1;
else if(condition_3){time_sec=59;time_min-=1;}
else if(condition_4)time_sec-=1;
else {time_sec=88;time_min=88;}
d[0]=un_disp[time_min/10];d[1]=un_disp[time_min%10];
d[2]=l_;
d[3]=un_disp[time_sec/10];d[4]=un_disp[time_sec%10];
out_disp(d);
}
////////////////////
void int_t0(void) interrupt 1 using 1
{ TL0=0xa0;TH0=0x42;
if ((++time60)>=19) {time60=0;bit_timech=1;}
}
////////////////////
void int_pc(void) interrupt 4 using 2
{ if(RI) {RI=0;
if((SBUF==0xfe)||(so_pclength>=8))p_pc_head=so_pc_buf;
*p_pc_head++=SBUF;
}
}
//------------------
void pc_comand(void )
{unsigned char datebuf;
if((so_pclength)>=4)//while
{if(so_pclength>=(so_pc_buf[2]+3))
{switch(so_pc_buf[1]){
case 0xa0:if(so_pc_buf[3]<=0x99&&so_pc_buf[4]<=0x99)
{datebuf=so_pc_buf[3];
time_min=(datebuf>>4)*10+(so_pc_buf[3]&=0x0f);
datebuf=so_pc_buf[4];
time_sec=(datebuf>>4)*10+(so_pc_buf[4]&=0x0f);}
break;
case 0xa1:SBUF=0xfe;
while(TI==0);TI=0;SBUF=0xa1;
while(TI==0);TI=0;SBUF=0x02;
while(TI==0);TI=0;SBUF=((datebuf=time_min/10)<<4)|(time_min%10);
while(TI==0);TI=0;SBUF=((datebuf=time_sec/10)<<4)|(time_sec%10);
while(TI==0);TI=0;
break;
default:break;}
p_pc_head=so_pc_buf;
}
}
//return(0);
}
////////////////////
void watchdog(void )
{unsigned char i;
d_clk=1;
for(i=0;i<10;i++);
d_clk=0;
}
////////////////////
void main(void)
{ TMOD=0x21;
SCON=0x50;
TH1=TL1=0xE8; //1200
TH0=0x42;// ;50MS
TL0=0xA0;
TR1=TR0=ET0=ES=1;
ET1=0;
EA=1;
d_clk=d_dat=d_st=0;
while (1)
{if(bit_timech){bit_timech=0;calculattime();}
watchdog();
pc_comand();
}
}
//////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -