您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 酒店餐饮 > 基于51单片机-DS1302-LCD1602数字时钟显示
#includereg51.h#includeintrins.h#includestring.h#defineuintunsignedint#defineucharunsignedcharucharcodetable[]=ILIKEMCU!;sbitRS=P2^0;sbitRW=P2^1;sbitEN=P2^2;sbitIO=P1^0;sbitsclk=P1^1;sbitrst=P1^2;uchar*week[]={SUN,****,MON,TUS,WEN,THU,FRI,SAT};ucharlcd_buffer1[]={DATE00-00-00};ucharlcd_buffer2[]={TIME00:00:00};uchardatetime[7];voiddelay(ucharms){uchari;while(ms--)for(i=0;i120;i++);}//忙检测ucharbusy(){uchartest;RW=1;RS=0;EN=1;test=P0;EN=0;returntest;}//写LCD命令voidwritecmd(ucharcmd){while((busy()&0x80)==0x80);RS=0;RW=0;P0=cmd;EN=1;delay(5);EN=0;}//发送数据voidwritedata(uchardat){while((busy()&0x80)==0x80);RW=0;RS=1;P0=dat;EN=1;delay(5);EN=0;}voidinit(){writecmd(0x38);delay(5);writecmd(0x01);delay(5);writecmd(0x06);delay(5);writecmd(0x0c);delay(5);}voidwrite_a_byte_tods1302(ucharx){uchari;for(i=0;i8;i++){IO=x&0x01;sclk=1;sclk=0;x=x1;}}ucharget_a_byte_fromds1302(){uchari,b=0x00;for(i=0;i8;i++){b|=_crol_((uchar)IO,i);sclk=1;sclk=0;}returnb/16*10+b%16;}ucharread_data(ucharaddr){uchardat;rst=0;sclk=0;rst=1;write_a_byte_tods1302(addr);dat=get_a_byte_fromds1302();sclk=1;sclk=0;returndat;}voidgettime(){uchari,addr=0x81;for(i=0;i8;i++){datetime[i]=read_data(addr);addr=addr+2;}}voidset_pos(ucharp){writecmd(p|0x80);}voiddispaly_string(ucharp,uchar*s){uchari;set_pos(p);for(i=0;i16;i++){writedata(s[i]);delay(5);}}voidzhuanhuan(uchard,uchar*a){a[0]=d/10+'0';a[1]=d%10+'0';}voidmain(){init();while(1){gettime();zhuanhuan(datetime[6],lcd_buffer1+5);zhuanhuan(datetime[4],lcd_buffer1+8);zhuanhuan(datetime[3],lcd_buffer1+11);strcpy(lcd_buffer1+13,week[datetime[5]]);zhuanhuan(datetime[2],lcd_buffer2+5);zhuanhuan(datetime[1],lcd_buffer2+8);zhuanhuan(datetime[0],lcd_buffer2+11);dispaly_string(0x00,lcd_buffer1);dispaly_string(0x40,lcd_buffer2);}}
本文标题:基于51单片机-DS1302-LCD1602数字时钟显示
链接地址:https://www.777doc.com/doc-6338835 .html