基于51单片机的ds1302程序.docx

上传人:b****4 文档编号:3724066 上传时间:2023-05-06 格式:DOCX 页数:17 大小:16.95KB
下载 相关 举报
基于51单片机的ds1302程序.docx_第1页
第1页 / 共17页
基于51单片机的ds1302程序.docx_第2页
第2页 / 共17页
基于51单片机的ds1302程序.docx_第3页
第3页 / 共17页
基于51单片机的ds1302程序.docx_第4页
第4页 / 共17页
基于51单片机的ds1302程序.docx_第5页
第5页 / 共17页
基于51单片机的ds1302程序.docx_第6页
第6页 / 共17页
基于51单片机的ds1302程序.docx_第7页
第7页 / 共17页
基于51单片机的ds1302程序.docx_第8页
第8页 / 共17页
基于51单片机的ds1302程序.docx_第9页
第9页 / 共17页
基于51单片机的ds1302程序.docx_第10页
第10页 / 共17页
基于51单片机的ds1302程序.docx_第11页
第11页 / 共17页
基于51单片机的ds1302程序.docx_第12页
第12页 / 共17页
基于51单片机的ds1302程序.docx_第13页
第13页 / 共17页
基于51单片机的ds1302程序.docx_第14页
第14页 / 共17页
基于51单片机的ds1302程序.docx_第15页
第15页 / 共17页
基于51单片机的ds1302程序.docx_第16页
第16页 / 共17页
基于51单片机的ds1302程序.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

基于51单片机的ds1302程序.docx

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

基于51单片机的ds1302程序.docx

基于51单片机的ds1302程序

#include

#include

#defineucharunsignedchar

#defineuintunsignedint

sbitSCK=P3^6;

sbitSDA=P3^4;

sbitRST=P3^5;//DS1302复位

sbitset=P3^0;//定义按键端口

sbits1=P3^1;

sbits2=P3^2;

sbits3=P3^3;

/************液晶口定义*******/

sbitRS=P2^4;//RS为数据/指令选择1/0

sbitRW=P2^5;//读写

sbitE=P2^6;//E为使能信号

bitflag;//是否读取时间的标志位

ucharsetn=0;//复位键、增加键、减少键按下次数

ucharcodetime0[]="DATE:

--";

ucharcodetime1[]="TIME:

:

:

";

ucharl_tmpdate[7]={0,55,16,22,8,6,9};//秒分时日月周年//可随时更改

ucharcodewrite_add[7]={0x80,0x82,0x84,0x86,

0x88,0x8a,0x8c};//秒分时日月周年写的寄存器地址

ucharcoderead_add[7]={0x81,0x83,0x85,0x87,

0x89,0x8b,0x8d};//秒分时日月周年读的寄存器地址

ucharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d

0x7d,0x07,0x7f,0x6f,0x40};//共阴数码管0-9加'-'熄灭表

voiddelay(uintz)

{

while(--z);

}

voidwrite_com(ucharcom)//液晶写命令函数

{

RS=0;//写命令使能

RW=0;//写

delay(5000);

/****在高脉冲下命令输入*****/

E=0;

P0=com;

delay(500);

E=1;

delay(500);

E=0;

}

voidwrite_data(uchardat)//液晶写数据函数

{

RS=1;//写数据使能

RW=0;

delay(5000);

/****在高脉冲下数据输入*****/

E=0;

P0=dat;

delay(500);

E=1;

delay(500);

E=0;

}

voidinit()//液晶初始化

{

ucharnum;

E=0;

write_com(0x38);//显示模式设置

delay(500);

write_com(0x38);

delay(500);

write_com(0x38);

write_com(0x08);//显示关闭

delay(500);

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

write_com(0x06);//光标移动设置整屏不移动

write_com(0x0c);//屏幕开、光标不闪(0)不显示(0)

delay(500);

write_com(0x80);

for(num=0;num<16;num++)

{

write_data(time0[num]);

delay(5);

}

write_com(0x80+0x40);

for(num=0;num<13;num++)

{

write_data(time1[num]);

delay(5);

}

}

voidWrite_Ds1302_Byte(uchartemp)//写一个字节

{

uchari;

for(i=0;i<8;i++)//循环8次写入数据

{

SCK=0;

SDA=temp&0x01;//每次传输低字节

temp>>=1;//右移一位

SCK=1;

}

}

/****************************************************************************/

voidWrite_Ds1302(ucharadd,uchardat)//向寄存器写数据

{

RST=0;

_nop_();

SCK=0;

_nop_();

RST=1;//启动

_nop_();

Write_Ds1302_Byte(add);//发送地址

Write_Ds1302_Byte(dat);//发送数据

RST=0;//恢复

}

ucharRead_Ds1302_Byte()//读一个字节

{

uchari,temp=0x00;

for(i=0;i<8;i++)//循环8次读取数据

{

if(SDA)

temp|=0x80;//每次传输低字节

SCK=0;

temp>>=1;//右移一位

SCK=1;

}

return(temp);

}

ucharRead_Ds1302(ucharaddress)//从寄存器读数据

{

uchardate;

RST=0;

_nop_();

SCK=0;

_nop_();

RST=1;

_nop_();

Write_Ds1302_Byte(address);

date=Read_Ds1302_Byte();

RST=0;

_nop_();//以下为DS1302复位的稳定时间

RST=0;

SCK=0;

_nop_();

SCK=1;

_nop_();

SDA=0;

_nop_();

SDA=1;

_nop_();

return(date);

}

/*********************************************************/

voidRead_RTC(void)//读取日历

{

uchari,*p;

p=read_add;//读的地址传递

for(i=0;i<7;i++)//分7次读取秒分时日月周年

{

l_tmpdate[i]=Read_Ds1302(*p);

p++;

}

}

/******************************************************/

voidSet_RTC(void)//设定日历

{

uchari,*p,tmp;

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

{//BCD处理,将十六进制转换成十进制

tmp=l_tmpdate[i]/10;

l_tmpdate[i]=l_tmpdate[i]%10;

l_tmpdate[i]=l_tmpdate[i]+tmp*16;

}

Write_Ds1302(0x8E,0X00);//关闭写保护

p=write_add;//传要写的寄存器地址

for(i=0;i<7;i++)//7次写入秒分时日月周年

{

Write_Ds1302(*p,l_tmpdate[i]);

p++;

}

Write_Ds1302(0x8E,0x80);//打开写保护

}

voidwrite_lcd()

{

staticuchari,temp[2];

switch(i)

{

case0:

write_com(0x80+0x40+0x0b);break;//秒

case1:

write_com(0x80+0x40+8);break;//分

case2:

write_com(0x80+0x40+5);break;//时

case3:

write_com(0x80+0x0b);break;//日

case4:

write_com(0x80+8);break;//月

case5:

write_com(0x80+0x0e);break;//星期

case6:

write_com(0x80+5);break;//年

}

temp[0]=l_tmpdate[i]/16;//数据的转换

temp[1]=l_tmpdate[i]&0x0f;

write_data(0x30+temp[0]);

write_data(0x30+temp[1]);

i++;

if(i==7)

i=0;

}

ucharchange(ucharx)//十进制转换成十六进制

{

uchartep,y;

y=x;

tep=y/16;

y=y%16;

y=y+tep*10;

returny;

}

voidkeyscan()

{

uchartem[2],tp,Flag;//存储要更改的时间数据

if(set==0)

{

flag=1;//时间停止

delay(500);//去键盘按下抖动

if(set==0)

{

delay(500);

while(!

set);

delay(500);//去键盘抬起时抖动

while(!

set);

delay(500);

setn++;

if(setn==1)

{

write_com(0x80+0x40+12);//将光标移动到秒个位

write_com(0x0f);//显示光标并且闪烁

}

if(setn==2)

{

write_com(0x80+0x40+9);//将光标移动到分个位

write_com(0x0f);//显示光标并且闪烁

}

if(setn==3)

{

write_com(0x80+0x40+6);//将光标移动到时个位

write_com(0x0f);//显示光标并且闪烁

}

if(setn==4)

{

write_com(0x80+15);

write_com(0x0f);

}

if(setn==5)

{

write_com(0x80+12);

write_com(0x0f);

}

if(setn==6)

{

write_com(0x80+9);

write_com(0x0f);

}

if(setn==7)

{

write_com(0x80+6);

write_com(0x0f);

}

if(setn==8)

{

setn=0;

Write_Ds1302(0x8E,0X00);//关闭写保护

Write_Ds1302(write_add[0],l_tmpdate[0]);

Write_Ds1302(0x8E,0x80);//打开写保护

flag=0;//时钟正常运行

write_com(0x0c);//光标停止闪烁

}

}

}

if(setn!

=0)//更改时间

{

if(s1==0)//增加时间

{

delay(5000);

if(s1==0)

{

while(!

s1);

delay(5000);//去键盘抬起时抖动

while(!

s1);

if(setn==1)

{

l_tmpdate[0]=change(l_tmpdate[0]);

l_tmpdate[0]++;

if(l_tmpdate[0]>=60)

l_tmpdate[0]=0;

{

tp=l_tmpdate[0]/10;

l_tmpdate[0]=l_tmpdate[0]%10;

l_tmpdate[0]=l_tmpdate[0]+tp*16;

Write_Ds1302(0x8E,0X00);//关闭写保护

Write_Ds1302(write_add[0],l_tmpdate[0]);

Write_Ds1302(0x8E,0x80);//打开写保护

}

write_com(0x80+0x40+11);

tem[0]=l_tmpdate[0]/16;//数据的转换

tem[1]=l_tmpdate[0]&0x0f;

write_data(0x30+tem[0]);

write_data(0x30+tem[1]);

write_com(0x80+0x40+12);

Flag=1;

}

if(setn==2)

{l_tmpdate[1]=change(l_tmpdate[1]);

l_tmpdate[1]++;

if(l_tmpdate[1]==60)

l_tmpdate[1]=0;

{

tp=l_tmpdate[1]/10;

l_tmpdate[1]=l_tmpdate[1]%10;

l_tmpdate[1]=l_tmpdate[1]+tp*16;

Write_Ds1302(0x8E,0X00);//关闭写保护

Write_Ds1302(write_add[1],l_tmpdate[1]);

Write_Ds1302(0x8E,0x80);//打开写保护

}

write_com(0x80+0x40+8);

tem[0]=l_tmpdate[1]/16;//数据的转换

tem[1]=l_tmpdate[1]&0x0f;

write_data(0x30+tem[0]);

write_data(0x30+tem[1]);

write_com(0x80+0x40+9);

}

if(setn==3)

{l_tmpdate[2]=change(l_tmpdate[2]);

l_tmpdate[2]++;

if(l_tmpdate[2]>=24)

l_tmpdate[2]=0;

{

tp=l_tmpdate[2]/10;

l_tmpdate[2]=l_tmpdate[2]%10;

l_tmpdate[2]=l_tmpdate[2]+tp*16;

Write_Ds1302(0x8E,0X00);//关闭写保护

Write_Ds1302(write_add[2],l_tmpdate[2]);

Write_Ds1302(0x8E,0x80);//打开写保护

}

write_com(0x80+0x40+5);

tem[0]=l_tmpdate[2]/16;//数据的转换

tem[1]=l_tmpdate[2]&0x0f;

write_data(0x30+tem[0]);

write_data(0x30+tem[1]);

write_com(0x80+0x40+6);

}

if(setn==4)

{

l_tmpdate[5]=change(l_tmpdate[5]);

l_tmpdate[5]++;

if(l_tmpdate[5]==8)

l_tmpdate[5]=1;

{

tp=l_tmpdate[5]/10;

l_tmpdate[5]=l_tmpdate[5]%10;

l_tmpdate[5]=l_tmpdate[5]+tp*16;

Write_Ds1302(0x8E,0X00);//关闭写保护

Write_Ds1302(write_add[5],l_tmpdate[5]);

Write_Ds1302(0x8E,0x80);//打开写保护

}

write_com(0x80+14);

tem[0]=l_tmpdate[5]/16;//数据的转换

tem[1]=l_tmpdate[5]&0x0f;

write_data(0x30+tem[0]);

write_data(0x30+tem[1]);

write_com(0x80+15);

}

if(setn==5)

{

l_tmpdate[3]=change(l_tmpdate[3]);

l_tmpdate[3]++;

if(l_tmpdate[3]==32)

l_tmpdate[3]=0;

{

tp=l_tmpdate[3]/10;

l_tmpdate[3]=l_tmpdate[3]%10;

l_tmpdate[3]=l_tmpdate[3]+tp*16;

Write_Ds1302(0x8E,0X00);//关闭写保护

Write_Ds1302(write_add[3],l_tmpdate[3]);

Write_Ds1302(0x8E,0x80);//打开写保护

}

write_com(0x80+11);

tem[0]=l_tmpdate[3]/16;//数据的转换

tem[1]=l_tmpdate[3]&0x0f;

write_data(0x30+tem[0]);

write_data(0x30+tem[1]);

write_com(0x80+12);

}

if(setn==6)

{

l_tmpdate[4]=change(l_tmpdate[4]);

l_tmpdate[4]++;

if(l_tmpdate[4]==13)

l_tmpdate[4]=0;

write_com(0x80+8);

{

tp=l_tmpdate[4]/10;

l_tmpdate[4]=l_tmpdate[4]%10;

l_tmpdate[4]=l_tmpdate[4]+tp*16;

Write_Ds1302(0x8E,0X00);//关闭写保护

Write_Ds1302(write_add[4],l_tmpdate[4]);

Write_Ds1302(0x8E,0x80);//打开写保护

}

tem[0]=l_tmpdate[4]/16;//数据的转换

tem[1]=l_tmpdate[4]&0x0f;

write_data(0x30+tem[0]);

write_data(0x30+tem[1]);

write_com(0x80+9);

}

if(setn==7)

{

l_tmpdate[6]=change(l_tmpdate[6]);

l_tmpdate[6]++;

if(l_tmpdate[6]==100)

l_tmpdate[6]=0;

write_com(0x80+5);

{

tp=l_tmpdate[6]/10;

l_tmpdate[6]=l_tmpdate[6]%10;

l_tmpdate[6]=l_tmpdate[6]+tp*16;

Write_Ds1302(0x8E,0X00);//关闭写保护

Write_Ds1302(write_add[6],l_tmpdate[6]);

Write_Ds1302(0x8E,0x80);//打开写保护

}

tem[0]=l_tmpdate[6]/16;//数据的转换

tem[1]=l_tmpdate[6]&0x0f;

write_data(0x30+tem[0]);

write_data(0x30+tem[1]);

write_com(0x80+6);

}

}

}

if(s2==0)//减少时间

{

delay(5000);

if(s2==0)

{

while(!

s2);

delay(5000);//去键盘抬起时抖动

while(!

s2);

if(setn==1)

{

l_tmpdate[0]=change(l_tmpdate[0]);

if(l_tmpdate[0]!

=0)

l_tmpdate[0]--;

if(l_tmpdate[0]==0)

l_tmpdate[0]=59;

{

tp=l_tmpdate[0]/10;

l_tmpdate[0]=l_tmpdate[0]%10;

l_tmpdate[0]=l_tmpdate[0]+tp*16;

Write_Ds1302(0x8E,0X00);//关闭写保护

Write_Ds1302(write_add[0],l_tmpdate[0]);

Write_Ds1302(0x8E,0x80);//打开写保护

}

write_com(0x80+0x40+11);

tem[0]=l_tmpdate[0]/16;//数据的转换

tem[1]=l_tmpdate[0]&0x0f;

write_data(0x30+tem[0]);

write_data(0x30+tem[1]);

write_com(0x80+0x40+12);

}

if(setn==2)

{

l_tmpdate[1]=change(l_tmpdate[1]);

if(l_tmpdate[1]!

=0)

l_tmpdate[1]--;

if(l_tmpdate[1]==0)

l_tmpdate[1]=59;

{

tp=l_tmpdate[1]/10;

l_tmpdate[1]=l_tmpdate[1]%10;

l_tmpdate[1]=l_tmpdate[1]+tp*16;

Write_Ds1302(0x8E,0X00);//关闭写保护

Write_Ds1302(write_add[1],l_tmpdate[1]);

Write_Ds1302(0x8E,0x80);//打开写保护

}

write_com(0x80+0x40+8);

tem[0]=l_tmpdate[1]/16;//数据的转换

tem[1]=l_tmpdate[1]&0x0f;

write_data(0x30+tem[0]);

write_data(0x30+tem[1]);

write_com(0x80+0x40+9);

}

if(setn==3)

{l_tmpdate[2]=change(l_tmpdate[2]);

if(l_tmpdate[2]!

=0)

l_tmpdate[2]--;

if(l_tmpdate[2]==0)

l_tmpdate[2]=23;

{

tp=l_tmpdate[2]/10;

l_tmpdate[2]=l_tmpdate[2]%10;

l_tmpdate[2]=l_tmpdate[2]+tp*16;

Write_Ds1302(0x8E,0X00);//关闭写保护

Write_Ds1302(write_add[2],l_tmpdate[2]);

Write_Ds1302(0x8E,0x80);//打开写保护

}

write_com(0x80+0x40+5);

tem[0]=l_tmpdate[2]/16;//数据的转换

tem[1]=l_tmpdate[2]&0x0f;

write_data(0x30+tem[0]);

write_data(0x30+tem[1]);

write_com(0x80+0x40+6);

}

if(setn==4)

{

l_tmpdate[5]=change(l_tmpdate[5]);

if(l_tmpdate[5]!

=0)

l_tmpdate[5]--;

if(l_tmpdate[5]==0)

l_tmpdate[5]=7;

{

tp=l_tmpdate[5]/10;

l_tmpdate[5]=l_tmpdate[5]%10;

l_tmpdate[5]=l_tmpdate[5]+tp*16;

Write_Ds1302(0x8E,0X00);//关闭写保护

Write_Ds1302(write_add[5],l_tmpdate[5]);

Write_Ds1302(0x8E,0x80);//打开写保护

}

write_com(0x80+14);

tem[0]=l_tmpdate[5]/16;//数据的转换

tem[1]=l

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

当前位置:首页 > PPT模板 > 商务科技

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

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