您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 酒店餐饮 > 基于51单片机的实时时间显示程序
#includereg52.h#includeintrins.h#defineLCD1602_4PINS#defineucharunsignedchar#defineuintunsignedint#defineLCD1602_DATAPINSP0voidLcd1602_Delay1ms(uintc);//误差0us/*LCD1602写入8位命令子函数*/voidLcdWriteCom(ucharcom);/*LCD1602写入8位数据子函数*/voidLcdWriteData(uchardat);/*LCD1602初始化子程序*/voidLcdInit();voidLcdDisplay();voidDs1302Write(ucharaddr,uchardat);ucharDs1302Read(ucharaddr);voidDs1302Init();voidDs1302Readtime();voidkeydown();voidadress_up();voidadress_down();voiddate_up();voiddate_down();voidtime_up();voidtime_down();//---加入全局变量--//externuchartime[7];//加入全局变量sbitLCD1602_E=P2^7;sbitLCD1602_RW=P2^5;sbitLCD1602_RS=P2^6;sbitDSIO=P3^4;sbitRST=P3^5;sbitSCLK=P3^6;sbitkey0=P2^0;sbitkey1=P2^1;sbitkey2=P2^2;sbitkey3=P2^3;sbitkey4=P2^4;ucharcodeREAD_RTC_ADDR[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};ucharcodeWRITE_RTC_ADDR[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};uchartime[7]={0,0x30,0x20,0x18,0x12,0x04,0x14},num,num1;unsignedintadress;/********************************************************************************函数名:main*函数功能:主函数*输入:无*输出:无*******************************************************************************/voidLcd1602_Delay1ms(uintc)//误差0us{uchara,b;for(;c0;c--){for(b=199;b0;b--){for(a=1;a0;a--);}}}voidDs1302Write(ucharaddr,uchardat){ucharn;RST=0;_nop_();SCLK=0;//先将SCLK置低电平。_nop_();RST=1;//然后将RST(CE)置高电平。_nop_();for(n=0;n8;n++)//开始传送八位地址命令{DSIO=addr&0x01;//数据从低位开始传送addr=1;SCLK=1;//数据在上升沿时,DS1302读取数据_nop_();SCLK=0;_nop_();}for(n=0;n8;n++)//写入8位数据{DSIO=dat&0x01;dat=1;SCLK=1;//数据在上升沿时,DS1302读取数据_nop_();SCLK=0;_nop_();}RST=0;//传送数据结束_nop_();}/********************************************************************************函数名:Ds1302Read*函数功能:读取一个地址的数据*输入:addr*输出:dat*******************************************************************************/ucharDs1302Read(ucharaddr){ucharn,dat,dat1;RST=0;_nop_();SCLK=0;//先将SCLK置低电平。_nop_();RST=1;//然后将RST(CE)置高电平。_nop_();for(n=0;n8;n++)//开始传送八位地址命令{DSIO=addr&0x01;//数据从低位开始传送addr=1;SCLK=1;//数据在上升沿时,DS1302读取数据_nop_();SCLK=0;//DS1302下降沿时,放置数据_nop_();}_nop_();for(n=0;n8;n++)//读取8位数据{dat1=DSIO;//从最低位开始接收dat=(dat1)|(dat17);SCLK=1;_nop_();SCLK=0;//DS1302下降沿时,放置数据_nop_();}RST=0;_nop_();//以下为DS1302复位的稳定时间,必须的。SCLK=1;_nop_();DSIO=0;_nop_();DSIO=1;_nop_();returndat;}/********************************************************************************函数名:Ds1302Init*函数功能:初始化DS1302.*输入:无*输出:无*******************************************************************************/voidDs1302Init(){ucharn;Ds1302Write(0x8E,0X00);//禁止写保护,就是关闭写保护功能for(n=0;n7;n++)//写入7个字节的时钟信号:分秒时日月周年{Ds1302Write(WRITE_RTC_ADDR[n],time[n]);}Ds1302Write(0x8E,0x80);//打开写保护功能}/********************************************************************************函数名:Ds1302Readtime*函数功能:读取时钟信息*输入:无*输出:无*******************************************************************************/voidDs1302Readtime(){ucharn;for(n=0;n7;n++)//读取7个字节的时钟信号:分秒时日月周年{time[n]=Ds1302Read(READ_RTC_ADDR[n]);}}voidLcdWriteCom(ucharcom)//写入命令{LCD1602_E=0;//使能LCD1602_RS=0;//选择发送命令LCD1602_RW=0;//选择写入LCD1602_DATAPINS=com;//放入命令Lcd1602_Delay1ms(1);//等待数据稳定LCD1602_E=1;//写入时序Lcd1602_Delay1ms(5);//保持时间LCD1602_E=0;}voidLcdWriteData(uchardat)//写入数据{LCD1602_E=0;//使能清零LCD1602_RS=1;//选择输入数据LCD1602_RW=0;//选择写入LCD1602_DATAPINS=dat;//写入数据Lcd1602_Delay1ms(1);LCD1602_E=1;//写入时序Lcd1602_Delay1ms(5);//保持时间LCD1602_E=0;}voidLcdInit()//LCD初始化子程序{LcdWriteCom(0x38);//开显示LcdWriteCom(0x0c);//开显示不显示光标LcdWriteCom(0x06);//写一个指针加1LcdWriteCom(0x01);//清屏LcdWriteCom(0x80);//设置数据指针起点num1=0;}voidmain(){LcdInit();Ds1302Init();while(1){keydown();if(num1==0){Ds1302Readtime();LcdDisplay();}}}/********************************************************************************函数名:LcdDisplay()*函数功能:显示函数*输入:无*输出:无*******************************************************************************/voidLcdDisplay(){LcdWriteCom(0x80+0X40);LcdWriteData('0'+time[2]/16);//时LcdWriteData('0'+(time[2]&0x0f));LcdWriteData(':');LcdWriteData('0'+time[1]/16);//分LcdWriteData('0'+(time[1]&0x0f));LcdWriteData(':');LcdWriteData('0'+time[0]/16);//秒LcdWriteData('0'+(time[0]&0x0f));LcdWriteCom(0x80);LcdWriteData('2');LcdWriteData('0');LcdWriteData('0'+time[6]/16);//年LcdWriteData('0'+(time[6]&0x0f));LcdWriteData('-');LcdWriteData('0'+time[4]/16);//月LcdWriteData('0'+(time[4]&0x0f));LcdWriteData('-');LcdWriteData('0'+time[3]/16);//日LcdWriteData('0'+(time[3]&0x0f));LcdWriteCom(0x8D);switch(time[5]&0x0f){case(1):LcdWriteData('M');LcdWriteData('o');LcdWriteData('n');break;case(2):LcdWriteData('T');LcdWriteData('u');LcdWriteData('e');break;case(3):LcdWriteData('W');LcdWriteData('e');LcdWriteData('d');break;case(4):LcdWriteData('T');LcdWriteData('h');LcdWriteData('u');break;case(5):LcdWriteData('F');LcdWriteData('r');LcdWriteData('i');break;case(6):LcdWriteData('S');LcdWriteData('a');LcdWriteData('t');break;case(7):LcdWriteData('S');LcdWriteData('u');LcdWriteData('n');break;}LcdWriteCom(0x0c);}voidkeydown(){if(key0==0){Lcd1602_Delay1ms(10);if(key0==0){adress=0x80+0x48;if(num1==0){Ds1302Write(0x8e,0x00);Ds1302Write(0x80,0x80);LcdWriteCo
本文标题:基于51单片机的实时时间显示程序
链接地址:https://www.777doc.com/doc-4020336 .html