您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 酒店餐饮 > DS1302实时时钟LCD1602显示
/*******************说明:**************************将实时时钟数据通过LCD1602显示--------------------------------------------------基于战神单片机工作室51/AVR最小系统板及DS1302实时时钟模块编写**************************************************/#includereg52.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedintuchardis_time_buf[16]={0};//LCD1602引脚定义//采用8位并行方式,DB0~DB7连接至LCDDATA0~LCDDATA7sbitRS=P2^0;sbitRW=P2^1;sbitCS=P2^2;#defineLCDDATAP0//DS1302引脚定义sbitRST=P1^3;sbitIO=P1^2;sbitSCK=P1^1;//DS1302地址定义#defineds1302_sec_add0x80//秒数据地址#defineds1302_min_add0x82//分数据地址#defineds1302_hr_add0x84//时数据地址#defineds1302_date_add0x86//日数据地址#defineds1302_month_add0x88//月数据地址#defineds1302_day_add0x8a//星期数据地址#defineds1302_year_add0x8c//年数据地址#defineds1302_control_add0x8e//控制数据地址#defineds1302_charger_add0x90#defineds1302_clkburst_add0xbe//初始时间定义uchartime_buf[8]={0x20,0x10,0x06,0x01,0x23,0x59,0x55,0x02};//初始时间2010年6月1号23点59分55秒星期二//功能:延时1毫秒//入口参数:x//出口参数:无//说明:当晶振为12M时,j112;当晶振为11.0592M时,j122voidDelay_xms(uintx){uinti,j;for(i=0;ix;i++)for(j=0;j112;j++);}//功能:12us延时//STC89C52为1T单片机,即1个时钟/机器周期,速度为AT89C52的12倍voidDelay_xus(uintt){for(;t0;t--){_nop_();}}//控制LCD写时序voidLCD_en_write(void){CS=1;Delay_xus(20);CS=0;Delay_xus(20);}//写指令函数voidWrite_Instruction(ucharcommand){RS=0;RW=0;CS=1;LCDDATA=command;LCD_en_write();//写入指令数据}//写数据函数voidWrite_Data(ucharWdata){RS=1;RW=0;CS=1;LCDDATA=Wdata;LCD_en_write();//写入数据}//字符显示初始地址设置voidLCD_SET_XY(ucharX,ucharY){ucharaddress;if(Y==0)address=0x80+X;//Y=0,表示在第一行显示,地址基数为0x80elseaddress=0xc0+X;//Y非0时,表时在第二行显示,地址基数为0xC0Write_Instruction(address);//写指令,设置显示初始地址}//在第X行Y列开始显示Wdata所对应的单个字符voidLCD_write_char(ucharX,ucharY,ucharWdata){LCD_SET_XY(X,Y);//写地址Write_Data(Wdata);//写入当前字符并显示}//清屏函数voidLCD_clear(void){Write_Instruction(0x01);Delay_xms(5);}//显示屏初始化函数voidLCD_init(void){Write_Instruction(0x38);//8bitinterface,2line,5*7dotsDelay_xms(5);Write_Instruction(0x38);Delay_xms(5);Write_Instruction(0x38);Write_Instruction(0x08);//关显示,不显光标,光标不闪烁Write_Instruction(0x01);//清屏Delay_xms(5);Write_Instruction(0x04);//写一字符,整屏显示不移动//Write_Instruction(0x05);//写一字符,整屏右移//Write_Instruction(0x06);//写一字符,整屏显示不移动//Write_Instruction(0x07);//写一字符,整屏左移Delay_xms(5);//Write_Instruction(0x0B);//关闭显示(不显示字符,只有背光亮)Write_Instruction(0x0C);//开显示,光标、闪烁都关闭//Write_Instruction(0x0D);//开显示,不显示光标,但光标闪烁//Write_Instruction(0x0E);//开显示,显示光标,但光标不闪烁//Write_Instruction(0x0F);//开显示,光标、闪烁均显示}//DS1302初始化函数voidds1302_init(void){RST=0;//RST脚置低SCK=0;//SCK脚置低}//向DS1302写入一字节数据voidds1302_write_byte(ucharaddr,uchard){uchari;RST=1;//启动DS1302总线//写入目标地址:addraddr=addr&0xFE;//最低位置零,寄存器0位为0时写,为1时读for(i=0;i8;i++){if(addr&0x01){IO=1;}else{IO=0;}SCK=1;//产生时钟SCK=0;addr=addr1;}//写入数据:dfor(i=0;i8;i++){if(d&0x01){IO=1;}else{IO=0;}SCK=1;//产生时钟SCK=0;d=d1;}RST=0;//停止DS1302总线}//从DS1302读出一字节数据uchards1302_read_byte(ucharaddr){uchari,temp;RST=1;//启动DS1302总线//写入目标地址:addraddr=addr|0x01;//最低位置高,寄存器0位为0时写,为1时读for(i=0;i8;i++){if(addr&0x01){IO=1;}else{IO=0;}SCK=1;SCK=0;addr=addr1;}//输出数据:tempfor(i=0;i8;i++){temp=temp1;if(IO){temp|=0x80;}else{temp&=0x7F;}SCK=1;SCK=0;}RST=0;//停止DS1302总线returntemp;}//向DS302写入时钟数据voidds1302_write_time(void){ds1302_write_byte(ds1302_control_add,0x00);//关闭写保护ds1302_write_byte(ds1302_sec_add,0x80);//暂停时钟//ds1302_write_byte(ds1302_charger_add,0xa9);//涓流充电ds1302_write_byte(ds1302_year_add,time_buf[1]);//年ds1302_write_byte(ds1302_month_add,time_buf[2]);//月ds1302_write_byte(ds1302_date_add,time_buf[3]);//日ds1302_write_byte(ds1302_hr_add,time_buf[4]);//时ds1302_write_byte(ds1302_min_add,time_buf[5]);//分ds1302_write_byte(ds1302_sec_add,time_buf[6]);//秒ds1302_write_byte(ds1302_day_add,time_buf[7]);//周ds1302_write_byte(ds1302_control_add,0x80);//打开写保护}//从DS302读出时钟数据voidds1302_read_time(void){time_buf[1]=ds1302_read_byte(ds1302_year_add);//年time_buf[2]=ds1302_read_byte(ds1302_month_add);//月time_buf[3]=ds1302_read_byte(ds1302_date_add);//日time_buf[4]=ds1302_read_byte(ds1302_hr_add);//时time_buf[5]=ds1302_read_byte(ds1302_min_add);//分time_buf[6]=(ds1302_read_byte(ds1302_sec_add))&0x7f;//秒,屏蔽秒的第7位,避免超出59time_buf[7]=ds1302_read_byte(ds1302_day_add);//周}voidDisplay(void){LCD_write_char(3,0,dis_time_buf[0]+'0');LCD_write_char(4,0,dis_time_buf[1]+'0');LCD_write_char(5,0,dis_time_buf[2]+'0');LCD_write_char(6,0,dis_time_buf[3]+'0');LCD_write_char(7,0,'/');LCD_write_char(8,0,dis_time_buf[4]+'0');LCD_write_char(9,0,dis_time_buf[5]+'0');LCD_write_char(10,0,'/');LCD_write_char(11,0,dis_time_buf[6]+'0');LCD_write_char(12,0,dis_time_buf[7]+'0');LCD_write_char(15,0,dis_time_buf[14]+'0');//第2行显示LCD_write_char(3,1,dis_time_buf[8]+'0');LCD_write_char(4,1,dis_time_buf[9]+'0');LCD_write_char(5,1,':');LCD_write_char(6,1,dis_time_buf[10]+'0');LCD_write_char(7,1,dis_time_buf[11]+'0');LCD_write_char(8,1,':');LCD_write_char(9,1,dis_time_buf[12]+'0');LCD_write_char(10,1,dis_time_buf[13]+'0');}//定时器中断函数voidTimer2()interrupt5//定时器2是5号中断{staticuchart;TF2=0;t++;if(t==4)//间隔200ms(50ms*4)读取一次时间{t=0;ds1302_read_time();//读取时间dis_time_buf[0]=(time_buf[0]4);//年dis_time_buf[1]=(time_buf[0]&0x0f);dis_time_buf[2]=(time_b
本文标题:DS1302实时时钟LCD1602显示
链接地址:https://www.777doc.com/doc-5286597 .html