1、#include#include#define uchar unsigned char#define uint unsigned int/1为功能键,2为增加时间键,3为减少时间键#include/#include/#include#define uchar unsigned charsbit sclk=P15;sbit io=P16;sbit rst=P17;uchar time=0x00,0x42,0x08,0x04,0x09,0x06,0x10;uchar timetmp7;uchar MAX=59,59,23,31,12,7,99;uchar MINX=0,0,0,1,1,1,1;ui
2、nt readdata7;uchar sec,min,hour,day,month,week,year;uchar keynum,ss;void write(uchar date) /写一个字节uchar i;for(i=0;i1;uchar read() /读一个字节uchar shuju,i;for(i=0;i1;sclk=0;if(io)shuju=shuju|0x80;sclk=1;return shuju;void writebyte(uchar add,date) /写时间的一个单位(分/秒/时/年/月/日/周)rst=0;sclk=0;rst=1;write(0x8e);writ
3、e(0x00);rst=0;sclk=0;rst=1;write(add);write(date);rst=0;sclk=0;rst=1;write(0x8e);write(0x80);rst=0;sclk=1;void writetime() /初始化时间uchar i,j=0x80;for(i=0;i7;i+)writebyte(j,timei);j+=2;uchar readbyte(add) /读一个单位时间(分/秒/时/年/月/日/周)uchar date;rst=0;sclk=0;rst=1;write(add);date=read();rst=0;sclk=1;return da
4、te; void readtime() /读取时间uchar i,j=0x81;for(i=0;i7;i+)readdatai=readbyte(j);j+=2;void process() /数制转换 uchar i;for(i=0;i4)*10+(readdatai&0x0f);void display() /显示函数display_1602N(0,1,2);display_1602N(1,1,0);display_1602N(2,1,year/10);display_1602N(3,1,year%10);display_1602S(4,1,-); display_1602N(5,1,mo
5、nth/10);display_1602N(6,1,month%10);display_1602S(7,1,-); display_1602N(8,1,day/10);display_1602N(9,1,day%10);display_1602S(11,1,WEEK);display_1602N(15,1,week);display_1602S(1,2,TIME);display_1602S(5,2,:);display_1602N(6,2,hour/10);display_1602N(7,2,hour%10);display_1602S(8,2,:);display_1602N(9,2,mi
6、n/10);display_1602N(10,2,min%10);display_1602S(11,2,:);display_1602N(12,2,sec/10);display_1602N(13,2,sec%10); void timeout() /提取时间sec=readdata0;min=readdata1;hour=readdata2;day=readdata3;month=readdata4;week=readdata5;year=readdata6;void shanshuo2(uchar add,time) /1602第二行闪烁函数write_com(0x80+0x40+add)
7、;write_date(0x10);write_com(0x80+0x40+add-1);write_date(0x10);delay(100);display_1602N(add,2,time%10);display_1602N(add-1,2,time/10);delay(100);void shanshuo1(uchar add,time) /1602第一行闪烁函数write_com(0x80+add); /当前位什么也不显write_date(0x10);write_com(0x80+add-1);write_date(0x10);delay(100); /延时display_1602
8、N(add,1,time%10); /显示时间display_1602N(add-1,1,time/10);delay(100); /延时void zj(uchar num) /增减函数switch(keynum)case 1:ss+;break; /如果是1,则进入下一位的调整case 2: /如果是2,则当前闪烁的位加1timetmpnum+;if(timetmpnumMAXnum) /如何超过了闪烁位的最大值,返回最小值timetmpnum=MINXnum;break;case 3: /如果是3,则当前闪烁的位减1timetmpnum-;if(timetmpnumMINXnum) /如何
9、超过了闪烁位的最小值,返回最大值timetmpnum=MAXnum;break;default:break;void jpaction() /调时操作while(ss7) while(ss=0) /调秒key=0;timetmp0=readbyte(0x81)/16*10+readbyte(0x81)%16; /从1302中取出时间shanshuo2(13,timetmp0); /显示时间并闪烁keynum=jiance(); /按键检测zj(0); /对时间进行操作writebyte(0x80,timetmp0/10*16+timetmp0%10); /把调整过的时间写到1302中 whil
10、e(ss=1) /调分key=0;timetmp1=readbyte(0x83)/16*10+readbyte(0x83)%16; /同上shanshuo2(10,timetmp1);keynum=jiance();zj(1);writebyte(0x82,timetmp1/10*16+timetmp1%10);while(ss=2) /调时key=0;timetmp2=readbyte(0x85)/16*10+readbyte(0x85)%16; /同上shanshuo2(7,timetmp2);keynum=jiance();zj(2);writebyte(0x84,timetmp2/10
11、*16+timetmp2%10);while(ss=3) /调日key=0;timetmp3=readbyte(0x87)/16*10+readbyte(0x87)%16; /同上shanshuo1(9,timetmp3);keynum=jiance();zj(3);writebyte(0x86,timetmp3/10*16+timetmp3%10); while(ss=4) /调月key=0;timetmp4=readbyte(0x89)/16*10+readbyte(0x89)%16;shanshuo1(6,timetmp4);keynum=jiance();zj(4);writebyte
12、(0x88,timetmp4/10*16+timetmp4%10); while(ss=5) /调周key=0;timetmp5=readbyte(0x8b)/16*10+readbyte(0x8b)%16; /同上shanshuo1(15,timetmp5);keynum=jiance();zj(5);writebyte(0x8a,timetmp5/10*16+timetmp5%10); while(ss=6) /调年key=0;timetmp6=readbyte(0x8d)/16*10+readbyte(0x8d)%16; /同上shanshuo1(3,timetmp6);keynum=j
13、iance();zj(6);writebyte(0x8c,timetmp6/10*16+timetmp6%10); void main()init_1602(); /初始化1602writetime(); /首次上电,往1302里面写时间while(1)readtime(); /从1302中读时间process(); /把取出的时间(BCD码)转化为十进制timeout(); /把时间从数组中提取出来display(); /在1302上显示时间keynum=jiance(); /检测按键是不是要调整时间if(keynum=1)ss=0;jpaction(); /相应调时操作writebyte(0x80,timetmp0/10*16+timetmp0%10); /重写秒,把秒写回到调之前的位置ss=0;key=0;