基于ds1302的51单片机简易实时时钟1602显示源程序.docx

上传人:b****1 文档编号:14461197 上传时间:2023-06-23 格式:DOCX 页数:10 大小:79.96KB
下载 相关 举报
基于ds1302的51单片机简易实时时钟1602显示源程序.docx_第1页
第1页 / 共10页
基于ds1302的51单片机简易实时时钟1602显示源程序.docx_第2页
第2页 / 共10页
基于ds1302的51单片机简易实时时钟1602显示源程序.docx_第3页
第3页 / 共10页
基于ds1302的51单片机简易实时时钟1602显示源程序.docx_第4页
第4页 / 共10页
基于ds1302的51单片机简易实时时钟1602显示源程序.docx_第5页
第5页 / 共10页
基于ds1302的51单片机简易实时时钟1602显示源程序.docx_第6页
第6页 / 共10页
基于ds1302的51单片机简易实时时钟1602显示源程序.docx_第7页
第7页 / 共10页
基于ds1302的51单片机简易实时时钟1602显示源程序.docx_第8页
第8页 / 共10页
基于ds1302的51单片机简易实时时钟1602显示源程序.docx_第9页
第9页 / 共10页
基于ds1302的51单片机简易实时时钟1602显示源程序.docx_第10页
第10页 / 共10页
亲,该文档总共10页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

基于ds1302的51单片机简易实时时钟1602显示源程序.docx

《基于ds1302的51单片机简易实时时钟1602显示源程序.docx》由会员分享,可在线阅读,更多相关《基于ds1302的51单片机简易实时时钟1602显示源程序.docx(10页珍藏版)》请在冰点文库上搜索。

基于ds1302的51单片机简易实时时钟1602显示源程序.docx

基于ds1302的51单片机简易实时时钟1602显示源程序

==================主程序=================

#include

#include"ds1302.h"

#include"LCD1602.h"

voidchange();

uchartimes[9];

uchardate[9];

main()

{LCD_init();//LCD初始化

init_1302(time_1302);

gotoxy(1,1);

LCD_display("Time:

");

gotoxy(1,2);

LCD_display("Date:

");

times[8]='\0';//

date[8]='\0';

while

(1)

{get_1302(time_1302);

change();

gotoxy(7,1);

LCD_display(times);

gotoxy(7,2);

LCD_display(date);

}

}

/*===========================

转换子程序

===========================*/

voidchange()

{

//时间的转换

times[0]=time_1302[2]/10+'0';

times[1]=time_1302[2]%10+'0';

times[2]=':

';

times[3]=time_1302[1]/10+'0';

times[4]=time_1302[1]%10+'0';

times[5]=':

';

times[6]=time_1302[0]/10+'0';

times[7]=time_1302[0]%10+'0';

//日期的转换

date[0]=time_1302[6]/10+'0';

date[1]=time_1302[6]%10+'0';

date[2]='-';

date[3]=time_1302[4]/10+'0';

date[4]=time_1302[4]%10+'0';

date[5]='-';

date[6]=time_1302[3]/10+'0';

date[7]=time_1302[3]%10+'0';

}

 

========================================

==============DS1302驱动程序=============

#defineucharunsignedchar

uchartime_1302[7]={0x00,0x00,0x00,0x03,0x07,0x03,0x08};//设置时间初始值数组

//秒分时号月份星期年

 

sbitT_IO=P1^0;//与硬件相关的连线clk为DS1302的时钟信号线

sbitT_CLK=P1^1;//DAT为DS1302的I/O数据线

sbitT_RST=P1^2;//RST为DS1302的RST信号线

ucharbdatadatbyte;

sbitdatbyte0=datbyte^0;

sbitdatbyte7=datbyte^7;

voidget_1302(uchartime[]);

voidwrite_ds1302(uchardat);

ucharr_1302(ucharadd);

voidw_1302(ucharadd,uchardat);

ucharread_ds1302(void);

voidinit_1302(uchar*time);

/*==========================================

DS1302初始化

===========================================*/

voidinit_1302(uchar*time)

{uchari,add;

//uchartime_bcd[7];

add=0x80;//0写入,1读出

w_1302(0x8e,0x00);

for(i=0;i<7;i++)

{w_1302(add,*time);

add+=2;

time++;

}

w_1302(0x8e,0x80);

}

//===========================

//读取当前时间

//===========================

voidget_1302(uchartime[])

{uchari;

ucharadd=0x81;

w_1302(0x8e,0x00);

for(i=0;i<7;i++)

{

time[i]=r_1302(add);

add+=2;

}

w_1302(0x8e,0x80);

}

/*=================================

DS1302写入一个字节(上升沿有效)

=================================*/

voidwrite_ds1302(uchardat)

{uchari;

datbyte=dat;

for(i=0;i<8;i++)

{T_IO=datbyte0;

T_CLK=1;

T_CLK=0;

datbyte=datbyte>>1;

}

}

/*=======================================

DS1302读取一个字节(下降沿有效)

=======================================*/

ucharread_ds1302(void)

{uchari;

for(i=0;i<8;i++)

{datbyte=datbyte>>1;

datbyte7=T_IO;

T_CLK=1;

T_CLK=0;

}

return(datbyte);

}

/*=========================================

指定位置读取数据

=========================================*/

ucharr_1302(ucharadd)

{uchartemp,dat1,dat2;

T_RST=0;

T_CLK=0;

T_RST=1;

write_ds1302(add);

temp=read_ds1302();

T_CLK=1;

T_RST=0;

dat1=temp/16;

dat2=temp%16;

temp=dat1*10+dat2;

return(temp);

}

/*==========================================

指定位置写入数据

==========================================*/

voidw_1302(ucharadd,uchardat)

{

T_RST=0;

T_CLK=0;

T_RST=1;

write_ds1302(add);

write_ds1302(dat/10<<4|dat%10);

T_CLK=1;

T_RST=0;

}

===========================================

=============LCD1602驱动程序================

#defineLCD_DBP0//定义LCD的数据端口

sbitLCD_RS=P2^0;

sbitLCD_RW=P2^1;

sbitLCD_E=P2^2;

#defineucharunsignedchar

#defineuintunsignedint

//===========延时子函数========================

voiddelay_lcd(uintx)

{uinti,j;

for(i=x;i>0;i--)

for(j=0;j<2;j++);

}

//==================写指令函数=================

voidLCD_write_command(ucharcommand)

{LCD_DB=command;

LCD_RS=0;//指令

LCD_RW=0;//写入

LCD_E=1;

LCD_E=0;

delay_lcd

(1);//等待执行完毕。

}

//===================写数据函数=================

voidLCD_write_data(uchardat)

{LCD_DB=dat;

LCD_RS=1;//数据寄存器

LCD_RW=0;//写入数据

LCD_E=1;

LCD_E=0;

delay_lcd

(1);//等待程序执行完毕。

}

/*============显示一个字符子函数===============

voidLCD_disp_char(ucharx,uchary,uchardat)

{ucharaddress;

if(y==1)

address=0x80+x;

else

address=0xc0+x;

LCD_write_command(address);

LCD_write_data(dat);

}

===================================*/

//==========延时1====================

voiddelay1(uintx)

{uinti,j;

for(j=0;j

for(i=0;i<100;i++);

}

//===========初始化函数=============

voidLCD_init(void)

{LCD_write_command(0x38);//设置8位格式,2行,5*7

LCD_write_command(0x0c);//整体显示,关光标,不闪烁

LCD_write_command(0x06);//设定输入方式,增量不移位

LCD_write_command(0x01);//清屏显示

delay_lcd(200);

}

//==========光标定位=======================

voidgotoxy(ucharx,uchary)

{ucharaddress;

if(y==1)

address=0x80+x;

else

address=0xc0+x;

LCD_write_command(address);

}

//=========显示一个字符串==================

voidLCD_display(uchar*dat)

{while(*dat!

='\0')

{LCD_write_data(*dat);

dat++;

}

}

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

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2