您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 酒店餐饮 > 基于8051单片机和DS12C8878位数码管显示的时钟 (2)
#includereg52.h#includeabsacc.h#definesecXBYTE[0xed00]//秒寄存器地址#defineminXBYTE[0xed02]//分寄存器地址#definehourXBYTE[0xed04]//小时寄存器地址#defineDS12887aXBYTE[0xed0a]//12887A寄存器地址#defineDS12887bXBYTE[0xed0b]//12887B寄存器地址unsignedcharcodeb[8]={0x1f,0x9f,0x5f,0xdf,0x3f,0xbf,0x7f,0xff};//数码管地址unsignedcharcodec[12]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40,0x00};//数码管字符码sbitbeep=P1^4;sbitK2=P1^0;sbitK1=P1^1;sbitK3=P1^2;//sbitDS12887CS=P2^1;sbitDISP=P2^4;sbitS1=P2^2;unsignedcharSS_1,SS_2,SS_3,SS_0;//*****************单位延时2ms函数************************voiddelay1ms(unsignedchart){unsignedchari,j;for(;t0;t--)for(i=2;i0;i--)for(j=248;j0;j--);}//********//12887初始化,所有中断禁止,24h制,二进制格式********voidclock_initial(void){//DS12887CS=0;DS12887b=0x86;//禁止时钟更新hour=0x00;min=0x00;sec=0x00;DS12887a=0x20;//启动内部锂电池DS12887b=0x06;//启动时钟更新//DS12887CS=1;}//******************时间修改函数修改小时****************voidclock_adj_h(charadj){chartemp;temp=hour;if((temp+adj)==0x18){DS12887b=0x86;hour=0x00;DS12887b=0x06;}elseif((temp+adj)==-1){DS12887b=0x86;hour=0x17;DS12887b=0x06;}else{DS12887b=0x86;hour=temp+adj;DS12887b=0x06;}}//******************时间修改函数修改分钟************************voidclock_adj_m(charadj){chartemp;temp=min;if((temp+adj)==0x3c){DS12887b=0x86;min=0x00;DS12887b=0x06;}elseif((temp+adj)==-1){DS12887b=0x86;min=0x3b;DS12887b=0x06;}else{DS12887b=0x86;min=temp+adj;DS12887b=0x06;}}//******************读取时间及显示函数************************voiddisp(void){unsignedchari,h,m,s,j;unsignedchara[8];//DS12887CS=0;h=hour;m=min;s=sec;//读取时间//DS12887CS=1;a[0]=c[h/10]&SS_1;//小时十位a[1]=c[h%10]&SS_1;//小时个位a[2]=c[10]&SS_0;//显示“-”“a[3]=c[m/10]&SS_2;//分钟十位a[4]=c[m%10]&SS_2;//分钟个位a[5]=c[10]&SS_0;//显示“-”“a[6]=c[s/10]&SS_3;//秒十位a[7]=c[s%10]&SS_3;//秒个位for(j=0;j2;j++)for(i=0;i8;i++){DISP=0;P0=a[i];DISP=1;P2=b[i];delay1ms(1);S1=0;}}//*****************按键扫描及求键码*********************voidkey_scan(void){unsignedchara;charb;b=0;P1=P1|0x07;if(!(K1&&K2&&K3))//是否有按键按下{delay1ms(10);//延时消抖if(!(K1&&K2&&K3))//确认是否有键按下{if(K1==0){a++;while(K1==0)disp();if(a==4)a=0;}if(K2==0){b=1;while(K2==0)disp();}if(K3==0){b=-1;while(K3==0)disp();}}}switch(a){case0:{SS_3=0xff;b=0;}break;case1:{SS_1=SS_0;if(b!=0){clock_adj_h(b);b=0;}}break;case2:{SS_1=0xff;SS_2=SS_0;if(b!=0){clock_adj_m(b);b=0;}}break;case3:{SS_2=0xff;SS_3=SS_0;if(b!=0){DS12887b=0x86;sec=0x00;DS12887b=0x06;b=0;}}break;default:{a=0;b=0;}break;}}//***********************主函数****************************voidmain(void){unsignedchari=1;S1=0;TMOD=0x01;TR0=1;TH0=0x5f;TL0=0x00;SS_0=0xff;SS_1=0xff;SS_2=0xff;SS_3=0xff;clock_initial();//初始化while(1){key_scan();//按键扫描if(TF0){if(i==7){TF0=0;SS_0=~SS_0;//-闪烁显示i=0;}else{TF0=0;i++;}}disp();}}
本文标题:基于8051单片机和DS12C8878位数码管显示的时钟 (2)
链接地址:https://www.777doc.com/doc-2567933 .html