基于ds1302的51单片机简易实时时钟1602显示源程序.docx
- 文档编号:26863494
- 上传时间:2023-06-23
- 格式:DOCX
- 页数:10
- 大小:79.96KB
基于ds1302的51单片机简易实时时钟1602显示源程序.docx
《基于ds1302的51单片机简易实时时钟1602显示源程序.docx》由会员分享,可在线阅读,更多相关《基于ds1302的51单片机简易实时时钟1602显示源程序.docx(10页珍藏版)》请在冰豆网上搜索。
基于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++; } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基于 ds1302 51 单片机 简易 实时 时钟 1602 显示 源程序
![提示](https://static.bdocx.com/images/bang_tan.gif)