完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
以下是我的程序:
#include #include /*********************************端口定义**********************************/ ***it sclk =P3^6; ***it io=P3^4; ***it rst=P3^5; ***it rs=P2^6; ***it en=P2^7; ***it rw=P2^5; ***it key=P1^0; ***it K1=P1^1; ***it K2=P1^2; #define uchar unsigned char #define uint unsigned int uchar S1num,flag,second,minute,hour,week,day,month,year;//秒、分、时、星期、日、月、年 bit keyflag=0; void delay(uint z) //延时子程序 { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void delayus() { _nop_(); _nop_(); _nop_(); _nop_(); } void write_1602dat(uchar dat)//定义一个带参数的写数据子程序 { rs=1; //1602的rs为0时,接收命令,为1时接收数据 P0=dat;//把void write_shu(uchar shu)中的COM中的数据给P0口 delay(5); en=1; delay(5); en=0; delay(5); } void write_1602com(uchar com)//定义一个带参数的写命令子程序 { rs=0; //1602的rs为0时,接收命令,为1时接收数据 P0=com;//把void write_com(uchar com)中的COM中的数据给P0口 delay(5); en=1; delay(5); en=0; delay(5); } void Write1602(uchar add,uchar dat) { write_1602com(add); write_1602dat(dat); } void init1602()//定义一个初始化子程序 { en=0; rw=0; write_1602com(0x38);//调用 write_com子程序并把0x38赋给P0口,显示模式打开 write_1602com(0x0e);//调用 write_com子程序并把“开显示,显示光标,光标闪烁”指令码赋给P0口 write_1602com(0x06);//调用 write_com子程序并把“地址指针加1,整屏不移动”指令码赋给P0口 //write_com(0x80+0x10);//数据指针初始化,让指针指向可显示的最右端 write_1602com(0x80);//数据指针初始化,让指针指向最左端,显示从第一行开始 write_1602com(0x01);//调用 write_com子程序并把"清零指"令码赋给P0口 Write1602(0x80,'2'); Write1602(0x81,'0'); Write1602(0x80+4,'-'); Write1602(0x80+7,'-'); Write1602(0x80+0x40+5,':'); Write1602(0x80+0x40+8,':'); } /********************写一个字节**************/ void write_1302byte(dat) { uchar i; sclk=0; delayus(); for(i=8;i>0;i--) { io=dat&0x01;//只要是从低位取数,向高位一定要和0X01与 delayus(); sclk=1;//为写数据制造上升沿。 delayus(); sclk=0;//为下一次上升沿写下一个字节做准备 dat>>=1;//将数据向左移一位,准备写入下一个数据 } } /********************读一个字节***************/ uchar read_1302byte() { uchar i,dat; delayus(); for(i=8;i>0;i--) { dat>>=1; if(io==1) { dat|=0x80;//将1取出,写在dat的最高位。 } sclk=1;//把sclk拉高,为读一个字节的下降沿做准备 delayus();//稍等制造一个高电平 sclk=0;//为读一个字节制造下降沿 delayus(); } return dat; } /*************写入一个时间位*****************/ void write_1302(uchar add,uchar dat) { rst=0; delayus(); sclk=0; delayus(); rst=1; write_1302byte(add); delayus(); write_1302byte(dat); delayus(); rst=0; } /****************读1302的地址******************/ uchar read_1302add(uchar add) { uchar timedat; rst=0; _nop_(); sclk=0; _nop_(); rst=1; write_1302byte(add);//写入要读的地址 timedat=read_1302byte();//将上面地址中的数据赋给timedat sclk=1; _nop_(); rst=0; return timedat; } /****************初始化1302*********************/ void init_1302() { flag=read_1302add(0x81);//读秒寄存器的最高位,读出时钟状态 if(flag&0x80)//判断时钟是否关闭,若内部关闭,则初始,若没关闭,不初始化,还在走 { write_1302(0x8e,0x00);//去除写保护 write_1302(0x80,((55/10)<<4|(55%10)));//写秒寄存器,并写入初值55 write_1302(0x82,((59/10)<<4|(59%10)));//写分寄存器,并写入初值59 write_1302(0x84,((22/10)<<4|(22%10)));//写小时寄存器,并写入初值23 write_1302(0x86,((24/10)<<4|(24%10)));//写日寄存器,并写入初值18 write_1302(0x88,((2/10)<<4|(2%10)));//写月寄存器,并写入初值2 write_1302(0x8a,((5/10)<<4|(5%10)));//写周寄存器,并写入初值5 write_1302(0x8c,((12/10)<<4|(12%10)));//写年寄存器,并写入初值12,不能写2012年 write_1302(0x90,0xa5);//写充电方式 write_1302(0x8e,0x80);//加上写保护 } } /*****************读出秒的十进制数***************************/ uchar readsecond() { uchar dat; dat=read_1302add(0x81); second=((dat&0x70)>>4)*10+(dat&0x0f); return second; } /*****************读出分的十进制数***************************/ uchar readminute() { uchar dat; dat=read_1302add(0x83); minute=((dat&0x70)>>4)*10+(dat&0x0f); return minute; } /*****************读出小时的十进制数***************************/ uchar readhour() { uchar dat; dat=read_1302add(0x85); hour=((dat&0x70)>>4)*10+(dat&0x0f); return hour; } /*****************读出天的十进制数***************************/ uchar readday() { uchar dat; dat=read_1302add(0x87); day=((dat&0x70)>>4)*10+(dat&0x0f); return day; } /*****************读出月的十进制数***************************/ uchar readmonth() { uchar dat; dat=read_1302add(0x89); month=((dat&0x70)>>4)*10+(dat&0x0f); return month; } /*****************读出周的十进制数***************************/ uchar readweek() { uchar dat; dat=read_1302add(0x8b); week=((dat&0x70)>>4)*10+(dat&0x0f); return week; } /*****************读出年的十进制数***************************/ uchar readyear() { uchar dat; dat=read_1302add(0x8d); year=((dat&0xf0)>>4)*10+(dat&0x0f); return year; } /************************读出所有时间**********************/ readtime() { readsecond(); readminute(); readhour(); readday(); readmonth(); readweek(); readyear(); } /*********************向1602写入时间****************************/ void write_second() { uchar shi,ge; shi=second/10; ge=second%10; Write1602(0x80+0x40+9,0x30+shi); Write1602(0x80+0x40+10,0x30+ge); } void write_minute() { uchar shi,ge; shi=minute/10; ge=minute%10; Write1602(0x80+0x40+6,0x30+shi); Write1602(0x80+0x40+7,0x30+ge); } void write_hour() { uchar shi,ge; shi=hour/10; ge=hour%10; Write1602(0x80+0x40+3,0x30+shi); Write1602(0x80+0x40+4,0x30+ge); } void write_day() { uchar shi,ge; shi=day/10; ge=day%10; Write1602(0x80+8,0x30+shi); Write1602(0x80+9,0x30+ge); } void write_month() { uchar shi,ge; shi=month/10; ge=month%10; Write1602(0x80+5,0x30+shi); Write1602(0x80+6,0x30+ge); } void write_year() { uchar shi,ge; shi=year/10; ge=year%10; Write1602(0x80+2,0x30+shi); Write1602(0x80+3,0x30+ge); } void write_week() { Write1602(0x80+12,0x30+week); //uchar week; switch(week) { case 1: Write1602(0x80+12,'M'); Write1602(0x80+13,'O'); Write1602(0x80+14,'N'); break; case 2:Write1602(0x80+12,'T'); Write1602(0x80+13,'U'); Write1602(0x80+14,'E'); break; case 3:Write1602(0x80+12,'W'); Write1602(0x80+13,'E'); Write1602(0x80+14,'N'); break; case 4:Write1602(0x80+12,'T'); Write1602(0x80+13,'H'); Write1602(0x80+14,'U'); break; case 5:Write1602(0x80+12,'F'); Write1602(0x80+13,'R'); Write1602(0x80+14,'I'); break; case 6:Write1602(0x80+12,'S'); Write1602(0x80+13,'A'); Write1602(0x80+14,'T'); break; case 7:Write1602(0x80+12,'S'); Write1602(0x80+13,'U'); Write1602(0x80+14,'N'); break; } } /*********************键盘扫描****************************/ void keyscan() { if(key==0) { delay(5); if(key==0);//防止误动作 { S1num++; while(!key); switch(S1num) { case 1: keyflag=1; write_1302(0x8e,0x00);//去除写保护 write_1302(0x80,0x80);//时钟停下 write_1602com(0x80+0x40+10); write_1602com(0x0f); break; case 2: write_1602com(0x80+0x40+7); break; case 3: write_1602com(0x80+0x40+4); break; case 4: write_1602com(0x80+9); break; case 5: write_1602com(0x80+6); break; case 6: write_1602com(0x80+3); break; case 7: write_1602com(0x80+12); break; case 8: S1num=0; keyflag=0; write_1602com(0x0c);//不显示光标 write_1302(0x80,0x00);//禁止写保护 write_1302(0x80,(second/10)<<4|second%10);//将调节后的秒写入DS1302 write_1302(0x82,(minute/10)<<4|minute%10);//将调节后的分写入DS1302 write_1302(0x84,(hour/10)<<4|hour%10);//将调节后的时写入DS1302 write_1302(0x8a,(week/10)<<4|week%10);//将调节后的星期写入DS1302 write_1302(0x86,(day/10)<<4|day%10);//将调节后的日写入DS1302 write_1302(0x88,(month/10)<<4|month%10);//将调节后的月写入DS1302 write_1302(0x8c,(year/10)<<4|year%10);// //write_1302(0x80,0x00);//时钟继续走这一句不能加在这,否则每次调 完时后秒会归O write_1302(0x8e,0x80);//写保护关 //write_1602com(0x0c); break; } } } if(S1num!=0) { if(K1==0) { delay(5); while(!K1); switch(S1num) { case 1: second++; if(second==60)second=0; //write_1302(0x8e,0x00);//去除写保护 write_second(); write_1602com(0x80+0x40+10); break; case 2: minute++; if(minute==60)minute=0; write_minute(); write_1602com(0x80+0x40+7); break; case 3: hour++; if(hour==24)hour=0; write_hour(); write_1602com(0x80+0x40+4); break; case 4: day++; write_day(); write_1602com(0x80+9); break; case 5: month++; write_month(); write_1602com(0x80+6); break; case 6: year++; write_year(); write_1602com(0x80+3); break; case 7: week++; write_week(); write_1602com(0x80+12); break; // default:break; } } if(K2==0) { delay(5); while(!K2); switch(S1num) { case 1: second--; if(second<0)second=59; write_second(); write_1602com(0x80+0x40+10); break; case 2: minute--; if(minute<0)minute=59; write_minute(); write_1602com(0x80+0x40+7); break; case 3: hour--; if(hour<0)hour=23; write_hour(); write_1602com(0x80+0x40+4); break; case 4: day--; write_day(); write_1602com(0x80+9); break; case 5: month--; write_month(); write_1602com(0x80+6); break; case 6: year--; write_year(); write_1602com(0x80+3); break; case 7: week--; write_week(); write_1602com(0x80+12); break; // default:break; } } } } void main() { delay(100); init1602(); init_1302(); while(1) { keyscan(); if(keyflag==0) { readtime(); write_second(); write_minute(); write_hour(); write_day(); write_month(); write_year(); write_week(); } } }
|
|
相关推荐
1个回答
|
|
首先看看你的程序是怎么运行的,确定if(keyflag==0)这里的是执行一次,还是多次呢
最佳答案
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
7 浏览 0 评论
26 浏览 1 评论
基于瑞萨FPB-RA4E2智能床头灯项目——1编译环境搭建与点亮驱动ws2812全彩LED
13 浏览 0 评论
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-LCD显示图片编程示例之介绍mmap
644 浏览 0 评论
《DNESP32S3使用指南-IDF版_V1.6》第二章 常用的C语言知识点
1000 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11770 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-24 06:22 , Processed in 0.467746 second(s), Total 75, Slave 57 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号