/***************
文件名:main.c
***************/
#include"main.h"
#include"delay.h"
#include"ds1302.h"
void settime();
void scankey();
void readtime(void);
void disp(uchar show,uchar dat);
void show_time(void);
sbit ADD=P3^3;
sbit SUB=P3^4;
uchar hour,min,sec;
uchar year,month,date,day;
uchar set_flag=0;
uchar have_set=0;
uchar save_time=0;
uchar code a[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};//共阳极-cc
void int0() interrupt 0
{
if(have_set==0)
{
set_flag++;
if(set_flag>7)
set_flag=1;}
else
{save_time=1;}
}
void scankey()
{
if(ADD==0)
{
delay_ms(10);
if(ADD==0)
{ have_set=1;
switch(set_flag)
{
case 1:
year++;break;
case 2:
month++;break;
case 3:
date++;break;
case 4:
hour++;break;
case 5:
min++;break;
case 6:
sec++;break;
case 7:
day++;break;
default:break;
}
}
}
else if(SUB==0)
{
delay_ms(10);
if(SUB==0)
{ have_set=1;
switch(set_flag)
{
case 1:
year--;break;
case 2:
month--;break;
case 3:
date--;break;
case 4:
hour--;break;
case 5:
min--;break;
case 6:
sec--;break;
case 7:
day--;break;
default:break;
}
}
}
if(year>99)
{year=0;}
if(month>12)
{month=1;}
if(date>31)
{date=1;}
if(hour>24)
hour=1;
if(min>60)
min=0;
if(sec>60)
sec=0;
if(day>7)
day=1;
}
void settime(void)
{
switch(set_flag)
{
case 1:
write_ds1302(0x8c,year);break;
case 2:
write_ds1302(0x88,month);break;
case 3:
write_ds1302(0x86,date);break;
case 4:
write_ds1302(0x80,hour);break;
case 5:
write_ds1302(0x82,min);break;
case 6:
write_ds1302(0x84,sec);break;
case 7:
write_ds1302(0x8a,day);break;
default:
break;
}
}
void readtime(void)
{
// uchar tmp;
// tmp=read_ds1302(addr);
// return(tmp);
hour=read_ds1302(0x85);
min=read_ds1302(0x83);
sec=read_ds1302(0x81);
year=read_ds1302(0x8d);
month=read_ds1302(0x89);
date=read_ds1302(0x87);
day=read_ds1302(0x8b);
}
void disp(uchar show,uchar dat)
{
P0=show;
P2=~a[dat];
delay_ms(1);
}
void show_time(void)
{
uchar tmp;
// hour=readtime(0x85);
tmp=hour/16;
disp(0,tmp);
tmp=hour%16;
disp(1,tmp);
// min=readtime(0x83);
tmp=min/16;
disp(2,tmp);
tmp=min%16;
disp(3,tmp);
// sec=readtime(0x81);
tmp=sec/16;
disp(4,tmp);
tmp=sec%16;
disp(5,tmp);
disp(6,2);
disp(7,0);
// year=readtime(0x8d);
tmp=year/16;
disp(8,tmp);
tmp=year%16;
disp(9,tmp);
// month=readtime(0x89);
tmp=month/16;
disp(10,tmp);
tmp=month%16;
disp(11,tmp);
// date=readtime(0x87);
tmp=date/16;
disp(12,tmp);
tmp=date%16;
disp(13,tmp);
// day=readtime(0x8b);
if(day==1)
{tmp=7;}
else
{tmp=day-1;}
disp(14,tmp);
}
void main(void)
{
set_flag=0;
P3=0xff;
EX0=1;
IT0=1;
EA=1;
while(1)
{
if(set_flag==0)
{
readtime();
}
else
{
scankey();
}
show_time();
if(save_time!=0)
{
settime();
set_flag=0;
have_set=0;
save_time=0;
}
}
}
上面程序中有关ds1302的读写函数没贴出来。
问题:我进入调时,按一下ADD和SUB键时,不是加1(减一),而是加(减)很多,比较乱,这是为什么?
此帖出自
小平头技术问答
一周热门 更多>