收藏 分享(赏)

基于51单片机与DS1302时钟芯片和LCD1602液晶显示的数字时钟.doc

上传人:精品资料 文档编号:10616540 上传时间:2019-12-07 格式:DOC 页数:9 大小:42.90KB
下载 相关 举报
基于51单片机与DS1302时钟芯片和LCD1602液晶显示的数字时钟.doc_第1页
第1页 / 共9页
基于51单片机与DS1302时钟芯片和LCD1602液晶显示的数字时钟.doc_第2页
第2页 / 共9页
基于51单片机与DS1302时钟芯片和LCD1602液晶显示的数字时钟.doc_第3页
第3页 / 共9页
基于51单片机与DS1302时钟芯片和LCD1602液晶显示的数字时钟.doc_第4页
第4页 / 共9页
基于51单片机与DS1302时钟芯片和LCD1602液晶显示的数字时钟.doc_第5页
第5页 / 共9页
点击查看更多>>
资源描述

1、PROTEUS 仿真图:XTAL218XTAL119ALE30EA31PSEN29RST9P0./AD039P0.1/A138P0.2/AD237P0.3/A336P0.4/AD435P0.5/A534P0.6/AD63P0.7/A732P1.0/T21P1./T2EX2P1.23P1.34P1.45P1.56P1.67P1.78P3.0/RXD10P3.1/TX1P3.2/INT012P3./IT113P3.4/T014P3.7/RD17P3.6/W16P3.5/T115P2.7/A1528P2.0/A821P2.1/A92P2./A1023P2.3/A124P2.4/A1225P2.5/A

2、1326P2.6/A1427U1AT89C52X112MHzC12pFC22pFGNDC320uFR11k+5VGND +5VGND+5VGND+5V RV110k234567891RP110K+5VD714613D51241D31029D1807E6RW5S4VS1VD2VE3LCD1LM016LRST5SCLK7I/O6X12X23VC18V21U2DS1302X232768 +5V1.5VGND 1.5VKEIL C 程序:#include #include “LCD1602.h“#include “DS1302.h“void Delay1ms(unsigned int count)un

3、signed int i,j;for(i=0;i/Port Definitions*sbit LcdRs = P20;sbit LcdRw = P21;sbit LcdEn = P22;sfr DBPort = 0x80; /P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.数据端口/内部等待函数*unsigned char LCD_Wait(void)LcdRs=0;LcdRw=1;_nop_();LcdEn=1; _nop_();/while(DBPort/在用 Proteus 仿真时,注意用屏蔽此语句,在调用 GotoXY()时,会进入死循环,/可能在写该控制字时,该模块没

4、有返回写入完备命令,即DBPortreturn DBPort;/向 LCD 写入命令或数据*#define LCD_COMMAND 0 / Command#define LCD_DATA 1 / Data#define LCD_CLEAR_SCREEN 0x01 / 清屏#define LCD_HOMING 0x02 / 光标返回原点void LCD_Write(bit style, unsigned char input)LcdEn=0;LcdRs=style;LcdRw=0; _nop_();DBPort=input; _nop_();/注意顺序LcdEn=1; _nop_();/注意顺序

5、LcdEn=0; _nop_();LCD_Wait();/设置显示模式*#define LCD_SHOW 0x04 /显示开#define LCD_HIDE 0x00 /显示关 #define LCD_CURSOR 0x02 /显示光标#define LCD_NO_CURSOR 0x00 /无光标 #define LCD_FLASH 0x01 /光标闪动#define LCD_NO_FLASH 0x00 /光标不闪动void LCD_SetDisplay(unsigned char DisplayMode)LCD_Write(LCD_COMMAND, 0x08|DisplayMode);/设置

6、输入模式*#define LCD_AC_UP 0x02#define LCD_AC_DOWN 0x00 / default#define LCD_MOVE 0x01 / 画面可平移#define LCD_NO_MOVE 0x00 /defaultvoid LCD_SetInput(unsigned char InputMode)LCD_Write(LCD_COMMAND, 0x04|InputMode);/移动光标或屏幕*/*#define LCD_CURSOR 0x02 #define LCD_SCREEN 0x08#define LCD_LEFT 0x00#define LCD_RIGHT

7、 0x04void LCD_Move(unsigned char object, unsigned char direction)if(object=LCD_CURSOR)LCD_Write(LCD_COMMAND,0x10|direction);if(object=LCD_SCREEN)LCD_Write(LCD_COMMAND,0x18|direction);*/初始化 LCD*void LCD_Initial()LcdEn=0;LCD_Write(LCD_COMMAND,0x38); /8 位数据端口,2 行显示,5*7 点阵LCD_Write(LCD_COMMAND,0x38);LCD

8、_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); /开启显示, 无光标LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); /清屏LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE); /AC 递增, 画面不动/*void GotoXY(unsigned char x, unsigned char y)if(y=0)LCD_Write(LCD_COMMAND,0x80|x);if(y=1)LCD_Write(LCD_COMMAND,0x80|(x-0x40);void Print(unsigned char *str)whi

9、le(*str!=0)LCD_Write(LCD_DATA,*str);str+;/*void LCD_LoadChar(unsigned char user8, unsigned char place)unsigned char i;LCD_Write(LCD_COMMAND,0x40|(place*8);for(i=0; i0; i-)DS1302_IO = ACC0; /相当于汇编中的 RRCDS1302_CLK = 1;DS1302_CLK = 0;ACC = ACC 1; unsigned char DS1302OutputByte(void) /实时时钟读取一字节( 内部函数) u

10、nsigned char i;for(i=8; i0; i-)ACC = ACC 1; /相当于汇编中的 RRC ACC7 = DS1302_IO;DS1302_CLK = 1;DS1302_CLK = 0; return(ACC); void Write1302(unsigned char ucAddr, unsigned char ucDa) /ucAddr: DS1302 地址, ucData: 要写的数据DS1302_RST = 0;DS1302_CLK = 0;DS1302_RST = 1;DS1302InputByte(ucAddr); / 地址,命令 DS1302InputByt

11、e(ucDa); / 写 1Byte 数据DS1302_CLK = 1;DS1302_RST = 0; unsigned char Read1302(unsigned char ucAddr) /读取 DS1302 某地址的数据unsigned char ucData;DS1302_RST = 0;DS1302_CLK = 0;DS1302_RST = 1;DS1302InputByte(ucAddr|0x01); / 地址,命令 ucData = DS1302OutputByte(); / 读 1Byte 数据DS1302_CLK = 1;DS1302_RST = 0;return(ucDa

12、ta);void DS1302_SetProtect(bit flag) /是否写保护if(flag)Write1302(0x8E,0x10);elseWrite1302(0x8E,0x00);void DS1302_SetTime(unsigned char Address, unsigned char Value) / 设置时间函数DS1302_SetProtect(0);Write1302(Address, (Value/10)Second = (ReadValueReadValue = Read1302(DS1302_MINUTE);Time-Minute = (ReadValueRe

13、adValue = Read1302(DS1302_HOUR);Time-Hour = (ReadValueReadValue = Read1302(DS1302_DAY);Time-Day = (ReadValueReadValue = Read1302(DS1302_WEEK);Time-Week = (ReadValueReadValue = Read1302(DS1302_MONTH);Time-Month = (ReadValueReadValue = Read1302(DS1302_YEAR);Time-Year = (ReadValuevoid DateToStr(SYSTEMT

14、IME *Time)Time-DateString0 = Time-Year/10 + 0;Time-DateString1 = Time-Year%10 + 0;Time-DateString2 = -;Time-DateString3 = Time-Month/10 + 0;Time-DateString4 = Time-Month%10 + 0;Time-DateString5 = -;Time-DateString6 = Time-Day/10 + 0;Time-DateString7 = Time-Day%10 + 0;Time-DateString8 = 0;void TimeTo

15、Str(SYSTEMTIME *Time)Time-TimeString0 = Time-Hour/10 + 0;Time-TimeString1 = Time-Hour%10 + 0;Time-TimeString2 = :;Time-TimeString3 = Time-Minute/10 + 0;Time-TimeString4 = Time-Minute%10 + 0;Time-TimeString5 = :;Time-TimeString6 = Time-Second/10 + 0;Time-TimeString7 = Time-Second%10 + 0;Time-DateStri

16、ng8 = 0;void Initial_DS1302(void)unsigned char Second=Read1302(DS1302_SECOND);if(Second/*void BurstWrite1302(unsigned char *pWClock) /往 DS1302 写入时钟数据(多字节方式)unsigned char i;Write1302(0x8e,0x00); / 控制命令,WP=0,写操作?DS1302_RST = 0;DS1302_CLK = 0;DS1302_RST = 1;DS1302InputByte(0xbe); / 0xbe:时钟多字节写命令for (i

17、= 8; i0; i-) /8Byte = 7Byte 时钟数据 + 1Byte 控制DS1302InputByte(*pWClock); / 写 1Byte 数据pWClock+;DS1302_CLK = 1;DS1302_RST = 0; void BurstRead1302(unsigned char *pRClock) /读取 DS1302 时钟数据(时钟多字节方式)unsigned char i;DS1302_RST = 0;DS1302_CLK = 0;DS1302_RST = 1;DS1302InputByte(0xbf); / 0xbf:时钟多字节读命令 for (i=8; i0; i-) *pRClock = DS1302OutputByte(); / 读 1Byte 数据 pRClock+;DS1302_CLK = 1;DS1302_RST = 0;void DS1302_TimeStop(bit flag) / 是否将时钟停止unsigned char Data;Data=Read1302(DS1302_SECOND);DS1302_SetProtect(0);if(flag)Write1302(DS1302_SECOND, Data|0x80);elseWrite1302(DS1302_SECOND, Data*/#endif

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报