您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 酒店餐饮 > 基于51单片机与DS1302时钟芯片和LCD1602液晶显示的数字时钟
PROTEUS仿真图:KEILC程序:#includeREGX52.H#includeLCD1602.h#includeDS1302.hvoidDelay1ms(unsignedintcount){unsignedinti,j;for(i=0;icount;i++)for(j=0;j120;j++);}main(){SYSTEMTIMECurrentTime;LCD_Initial();Initial_DS1302();GotoXY(0,0);Print(tianmadianzi);GotoXY(0,1);Print(Time:);XTAL218XTAL119ALE30EA31PSEN29RST9P0.0/AD039P0.1/AD138P0.2/AD237P0.3/AD336P0.4/AD435P0.5/AD534P0.6/AD633P0.7/AD732P1.0/T21P1.1/T2EX2P1.23P1.34P1.45P1.56P1.67P1.78P3.0/RXD10P3.1/TXD11P3.2/INT012P3.3/INT113P3.4/T014P3.7/RD17P3.6/WR16P3.5/T115P2.7/A1528P2.0/A821P2.1/A922P2.2/A1023P2.3/A1124P2.4/A1225P2.5/A1326P2.6/A1427U1AT89C52X112MHzC122pFC222pFGNDC320uFR11k+5VGND+5VGND+5VGND+5VRV110k234567891RP110K+5VD714D613D512D411D310D29D18D07E6RW5RS4VSS1VDD2VEE3LCD1LM016LRST5SCLK7I/O6X12X23VCC18VCC21U2DS1302X232768+5V1.5VGND1.5Vwhile(1){DS1302_GetTime(&CurrentTime);TimeToStr(&CurrentTime);GotoXY(6,1);Print(CurrentTime.TimeString);Delay1ms(400);}}#ifndefLCD_CHAR_1602_2005_4_9#defineLCD_CHAR_1602_2005_4_9#includeintrins.h//PortDefinitions**********************************************************sbitLcdRs=P2^0;sbitLcdRw=P2^1;sbitLcdEn=P2^2;sfrDBPort=0x80;//P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.数据端口//内部等待函数**************************************************************************unsignedcharLCD_Wait(void){LcdRs=0;LcdRw=1;_nop_();LcdEn=1;_nop_();//while(DBPort&0x80);//在用Proteus仿真时,注意用屏蔽此语句,在调用GotoXY()时,会进入死循环,//可能在写该控制字时,该模块没有返回写入完备命令,即DBPort&0x80==0x80//实际硬件时打开此语句LcdEn=0;returnDBPort;}//向LCD写入命令或数据************************************************************#defineLCD_COMMAND0//Command#defineLCD_DATA1//Data#defineLCD_CLEAR_SCREEN0x01//清屏#defineLCD_HOMING0x02//光标返回原点voidLCD_Write(bitstyle,unsignedcharinput){LcdEn=0;LcdRs=style;LcdRw=0;_nop_();DBPort=input;_nop_();//注意顺序LcdEn=1;_nop_();//注意顺序LcdEn=0;_nop_();LCD_Wait();}//设置显示模式************************************************************#defineLCD_SHOW0x04//显示开#defineLCD_HIDE0x00//显示关#defineLCD_CURSOR0x02//显示光标#defineLCD_NO_CURSOR0x00//无光标#defineLCD_FLASH0x01//光标闪动#defineLCD_NO_FLASH0x00//光标不闪动voidLCD_SetDisplay(unsignedcharDisplayMode){LCD_Write(LCD_COMMAND,0x08|DisplayMode);}//设置输入模式************************************************************#defineLCD_AC_UP0x02#defineLCD_AC_DOWN0x00//default#defineLCD_MOVE0x01//画面可平移#defineLCD_NO_MOVE0x00//defaultvoidLCD_SetInput(unsignedcharInputMode){LCD_Write(LCD_COMMAND,0x04|InputMode);}//移动光标或屏幕************************************************************/*#defineLCD_CURSOR0x02#defineLCD_SCREEN0x08#defineLCD_LEFT0x00#defineLCD_RIGHT0x04voidLCD_Move(unsignedcharobject,unsignedchardirection){if(object==LCD_CURSOR)LCD_Write(LCD_COMMAND,0x10|direction);if(object==LCD_SCREEN)LCD_Write(LCD_COMMAND,0x18|direction);}*///初始化LCD************************************************************voidLCD_Initial(){LcdEn=0;LCD_Write(LCD_COMMAND,0x38);//8位数据端口,2行显示,5*7点阵LCD_Write(LCD_COMMAND,0x38);LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR);//开启显示,无光标LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN);//清屏LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE);//AC递增,画面不动}//************************************************************************voidGotoXY(unsignedcharx,unsignedchary){if(y==0)LCD_Write(LCD_COMMAND,0x80|x);if(y==1)LCD_Write(LCD_COMMAND,0x80|(x-0x40));}voidPrint(unsignedchar*str){while(*str!='\0'){LCD_Write(LCD_DATA,*str);str++;}}/*voidLCD_LoadChar(unsignedcharuser[8],unsignedcharplace){unsignedchari;LCD_Write(LCD_COMMAND,0x40|(place*8));for(i=0;i8;i++)LCD_Write(LCD_DATA,user[i]);}*///************************************************************************#endif#ifndef_REAL_TIMER_DS1302_2003_7_21_#define_REAL_TIMER_DS1302_2003_7_21_sbitDS1302_CLK=P1^6;//实时时钟时钟线引脚sbitDS1302_IO=P1^7;//实时时钟数据线引脚sbitDS1302_RST=P1^5;//实时时钟复位线引脚sbitACC0=ACC^0;sbitACC7=ACC^7;typedefstruct__SYSTEMTIME__{unsignedcharSecond;unsignedcharMinute;unsignedcharHour;unsignedcharWeek;unsignedcharDay;unsignedcharMonth;unsignedcharYear;unsignedcharDateString[9];unsignedcharTimeString[9];}SYSTEMTIME;//定义的时间类型#defineAM(X)X#definePM(X)(X+12)//转成24小时制#defineDS1302_SECOND0x80#defineDS1302_MINUTE0x82#defineDS1302_HOUR0x84#defineDS1302_WEEK0x8A#defineDS1302_DAY0x86#defineDS1302_MONTH0x88#defineDS1302_YEAR0x8C#defineDS1302_RAM(X)(0xC0+(X)*2)//用于计算DS1302_RAM地址的宏voidDS1302InputByte(unsignedchard)//实时时钟写入一字节(内部函数){unsignedchari;ACC=d;for(i=8;i0;i--){DS1302_IO=ACC0;//相当于汇编中的RRCDS1302_CLK=1;DS1302_CLK=0;ACC=ACC1;}}unsignedcharDS1302OutputByte(void)//实时时钟读取一字节(内部函数){unsignedchari;for(i=8;i0;i--){ACC=ACC1;//相当于汇编中的RRCACC7=DS1302_IO;DS1302_CLK=1;DS1302_CLK=0;}return(ACC);}voidWrite1302(unsignedcharucAddr,unsignedcharucDa)//ucAddr:DS1302地址,ucData:要写的数据{DS1302_RST=0;DS1302_CLK=0;DS1302_RST=1;DS1302InputByte(ucAddr);//地址,命令DS1302InputByte(ucDa);//写1Byte数据DS1302_CLK=1;DS1302_RST=0;}unsignedcharRead1302(unsignedcharucAddr)//读取DS1302某地址的数据{unsignedcharucData;DS1302_RST=0;DS1302_CLK=0;DS1302_RST=1;DS1302InputByte(ucAddr|0x01);//地址,命令ucData=DS1302OutputByte();//读1Byte数据DS1302_CLK=1;DS1302_RST=0;return(ucData);}voidDS1302_SetProtect(bitflag)//是否写保护{if(flag)Write1302(0x8E,0
本文标题:基于51单片机与DS1302时钟芯片和LCD1602液晶显示的数字时钟
链接地址:https://www.777doc.com/doc-1678241 .html