#include <at89x51.h>
#include "ds1302_51.h"
#include "led.h"
#include "onekey51.h"
#define K_SET P1_0
#define K_ADD P1_1
#define K_SUB P1_2
#define K_EXIT P1_3
uchar /*dis_buf[]={0,0,0,0,0,0},*/ tmp_buf [6],pos=0,i_set,count_blk,count_500ms=0,c_rdkey=0;
bit set_flag=0,blink=1,point_flag=1,f_rdkey=0;
void
timerInit()
{
TMOD&=~T0_M1_;
TMOD|=T0_M0_;
TH0=0xf7;
TL0=0x00;
ET0=1;
EA=1;
TR0=1;
}
void ISR_T0() interrupt TF0_VECTOR
{
TH0=0xf7;
TL0=0x00;
ds1302_read_time();
if(++count_500ms==200)
{
count_500ms=0;
point_flag^=1;
}
if(set_flag)
{
if(++count_blk==100)
{
count_blk=0;
blink^=1;
}
}
P2=0x00;
if(!set_flag)
P0=led[dis_buf[pos]];
else
{
if(pos!=i_set||blink)
P0=led[tmp_buf[pos]];
else
P0=0xff;
}
if(!set_flag && point_flag && pos==2)
{
P0&=0x7f;
}
if(pos==4 || (set_flag && pos==2))
P0&=0x7f;
P2=1<<pos;
if(++pos==6)
pos=0;
if(++c_rdkey==4)
{
c_rdkey=0;
f_rdkey=1;
}
}
void TimeAdd()
{
uchar tmp;
swich(i_set)
{
case 0:
case 2:
tmp=10;
break;
case 1:
case 3:
tmp=6;
break;
case 4:
if(tmp_buf[5]<2)
tmp=10;
else
tmp=4;
break;
case 5:
if(tmp_buf[4]<4)
tmp=3;
else
tmp=2;
break;
}
if(++tmp_buf[i_set]==tmp)
tmp_buf[i_set]=0;
}
void TimeSub()
{
uchar tmp;
swich(i_set)
{
case 0:
case 2:
tmp=9;
break;
case 1:
case 3:
tmp=5;
break;
case 4:
if(tmp_buf[5]<2)
tmp=9;
else
tmp=3;
break;
case 5:
if(tmp_buf[4]<4)
tmp=2;
else
tmp=1;
break;
}
if(tmp_buf[i_set]==0)
tmp_buf[i_set]=tmp;
else
tmp_buf[i_set]--;
}
void SaveSet()
{
uchar h,m,s;
s=(tmp_buf[1]<<4) | tmp_buf[0];
m=(tmp_buf[3]<<4) | tmp_buf[2];
h=(tmp_buf[5]<<4) | tmp_buf[4];
ds1302_set_time(h,m,s);
}
void main()
{
uchar temp,i;
TimerInit();
DS1302Init();
while(1)
{
if(f_rdkey)
{
f_rdkey=0;
if(!set_flag && Read1Key(0,K_SET))
{
i_set=5;
count_blk=0;
for(i=0;i<6;i++)
tmp_buf[i]=dis_buf[i];
set_flag=1;
}
if(set_flag)
{
if(Read1Key(0,K_SET))
{
if(i_set--==0)
i_set=5;
}
if(Read1Key(1,K_ADD))
{
TimeAdd();
}
if(Read1Key(2,K_SUB))
TimeSub();
}
if(temp==Read1Key(3,K_EXIT))
{
if(temp==2)
SaveSet();
set_flag=0;
}
}
}
}
}
一周热门 更多>