完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
` #include #include #define uint unsigned int #define uchar unsigned char ///////时钟芯片端口定义//////////////////////// ***it sck=P1^0; ***it io=P1^1; ***it rst=P1^2; //////液晶控制端口定义////////////////// ***it rs=P2^7; ***it en=P2^6; //////按键端口定义////////////////////////////// ***it key1=P1^3; ***it key2=P1^4; ***it key3=P1^5; ***it key4=P1^6; /////////自动或者按照作息时间切换//////////////////////// ***it qh=P1^7;// 自动或者按照作息时间切换 /////自动控制时信号输入///////////////////////////////////////////////// ***it gg=P3^5;//光敏电阻信号输入 ***it yy=P3^6;//下雨传感器信号输入 uchar table[]={"0123456789"};//液晶显示字符 uchar writetime[7]; uchar writeadd[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80}; //写入数据的地址 uchar readadd[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81};//时钟芯片读出数据的地址 //////步进电机接口////////////// uchar fan[4]={0xf8,0xf4,0xf2,0xf1};//反转 uchar zheng[4]={0xf1,0xf2,0xf4,0xf8};//正转 uchar nian,yue,ri,shi,fen,miao;//变量定义 uchar shi1,fen1,shi2,fen2;//时分1开启和2关闭比较变量 bit chuang_flag;//窗户开关标志位,0为窗户开,1为窗户关 void delay(uint ms) //延时函数 { uchar y; for(ms;ms>0;ms--) for(y=110;y>0;y--); } void ds1302_write_byte(uchar date) //时钟芯片写字节 { uchar i; for(i=0;i<8;i++) { sck=0; io=date&0x01; date=date>>1; sck=1; } } void ds1302_write(uchar add,uchar date)//时钟芯片相应地址写入数据 { rst=0;_nop_(); sck=0;_nop_(); rst=1;_nop_(); ds1302_write_byte(add); ds1302_write_byte(date); rst=0;_nop_(); sck=0;_nop_(); io=1;sck=1; } uchar ds1302_read(uchar add) //时钟芯片相应地址读出数据 { uchar i,date; rst=0;_nop_(); sck=0;_nop_(); rst=1;_nop_(); ds1302_write_byte(add); for(i=0;i<8;i++) { date=date>>1; sck=0; if(io) date=date|0x80; sck=1; } rst=0;_nop_(); sck=0;_nop_(); io=1;sck=1; return date; } void yj1602_write_com(uchar com)//液晶写入地址 { en=0;delay(1); en=1;delay(1); rs=0;delay(1); P0=com; en=0;delay(1); } void yj1602_write_date(uchar date)//液晶写入数据 { en=0;delay(1); en=1;delay(1); rs=1;delay(1); P0=date; en=0;delay(1); } void yj1602_init()//液晶初始化 { yj1602_write_com(0x38); yj1602_write_com(0x0c); yj1602_write_com(0x06); yj1602_write_com(0x01); } void write_1602(uchar add,uchar date)//液晶显示数字字符 { uchar shi,ge; shi=date/16; ge=date%16; yj1602_write_com(0x80+add); yj1602_write_date(0x30+shi); //0x30码制转换 yj1602_write_date(0x30+ge); } void display() //显示函数 显示年月日时分秒 { yj1602_write_com(0x80); yj1602_write_date('D'); yj1602_write_date('a'); yj1602_write_date('t'); yj1602_write_date('a'); yj1602_write_date(':'); writetime[0]=ds1302_read(readadd[0]); write_1602(5,writetime[0]); yj1602_write_com(0x80+7); yj1602_write_date('-'); writetime[2]=ds1302_read(readadd[2]); write_1602(8,writetime[2]); yj1602_write_com(0x80+10); yj1602_write_date('-'); writetime[3]=ds1302_read(readadd[3]); write_1602(11,writetime[3]); yj1602_write_com(0x80+14); if(chuang_flag==0)yj1602_write_date('K'); if(chuang_flag==1)yj1602_write_date('G'); yj1602_write_com(0x80+0x40); yj1602_write_date('T'); yj1602_write_date('i'); yj1602_write_date('m'); yj1602_write_date('e'); yj1602_write_date(':'); writetime[4]=ds1302_read(readadd[4]); write_1602(0x40+5,writetime[4]); yj1602_write_com(0x80+0x40+7); yj1602_write_date(':'); writetime[5]=ds1302_read(readadd[5]); write_1602(0x40+8,writetime[5]); yj1602_write_com(0x80+0x40+10); yj1602_write_date(':'); writetime[6]=ds1302_read(readadd[6]); write_1602(0x40+11,writetime[6]); yj1602_write_com(0x80+0x40+11); } void display1() //显示调整开启和关闭时间函数 { yj1602_write_com(0x80); yj1602_write_date('K'); yj1602_write_date('a'); yj1602_write_date('i'); yj1602_write_date(':'); yj1602_write_com(0x87); yj1602_write_date(table[shi1/10]); yj1602_write_date(table[shi1%10]); yj1602_write_date('-'); yj1602_write_date(table[fen1/10]); yj1602_write_date(table[fen1%10]); yj1602_write_com(0x80+0x40); yj1602_write_date('G'); yj1602_write_date('a'); yj1602_write_date('n'); yj1602_write_date(':'); yj1602_write_com(0x87+0x40); yj1602_write_date(table[shi2/10]); yj1602_write_date(table[shi2%10]); yj1602_write_date(':'); yj1602_write_date(table[fen2/10]); yj1602_write_date(table[fen2%10]); } void keyscan() //按键扫描 { uchar date_flag; uchar set_flag; uchar j,k; if(key1==0) { yj1602_init(); delay(10); if(key1==0) { display(); ds1302_write(0x8e,0x00); date_flag=1; yj1602_write_com(0x80+5); yj1602_write_date('*');//依次调整年月日时分,秒不可调整 yj1602_write_date('*'); nian=ds1302_read(readadd[0]); j=nian/16; k=nian%16; nian=j*10+k; } while(!key1); } ////////////////////////调整年/////////////////////////////////////// while(date_flag==1) { display(); if(key1==0) { delay(10); if(key1==0) { date_flag=2; yj1602_write_com(0x80+8); yj1602_write_date('*');//依次调整年月日时分,秒不可调整 yj1602_write_date('*'); yue=ds1302_read(readadd[2]); j=yue/16; k=yue%16; yue=j*10+k; } while(!key1); } if(key2==0) { delay(10); if(key2==0) { nian++; yj1602_write_com(0x80+5); yj1602_write_date('*'); yj1602_write_date('*'); if(nian==100)//年 nian=0; j=nian/10; k=nian%10; writetime[0]=j*16+k; ds1302_write(writeadd[0],writetime[0]); } while(!key2); } if(key3==0) { delay(10); if(key3==0) { if(nian==0) nian=100; yj1602_write_com(0x80+5); yj1602_write_date('*'); yj1602_write_date('*'); nian--; j=nian/10; k=nian%10; writetime[0]=j*16+k; ds1302_write(writeadd[0],writetime[0]); } while(!key3); } } ///////////// 调整月/////////////////////////////////////////////// while(date_flag==2) { display(); if(key1==0) { delay(10); if(key1==0) { date_flag=3; yj1602_write_com(0x80+11); yj1602_write_date('*');//依次调整年月日时分,秒不可调整 yj1602_write_date('*'); ri=ds1302_read(readadd[3]); j=ri/16; k=ri%16; ri=j*10+k; } while(!key1); } if(key2==0) { delay(10); if(key2==0) { yj1602_write_com(0x80+8); yj1602_write_date('*'); yj1602_write_date('*'); yue++; if(yue>=13) yue=1; j=yue/10; k=yue%10; writetime[2]=j*16+k; ds1302_write(writeadd[2],writetime[2]); } while(!key2); } if(key3==0) { delay(10); if(key3==0) { if(yue<=1) yue=13; yj1602_write_com(0x80+8); yj1602_write_date('*'); yj1602_write_date('*'); yue--; j=yue/10; k=yue%10; writetime[2]=j*16+k; ds1302_write(writeadd[2],writetime[2]); } while(!key3); } } ///////////// 调整日/////////////////////////////////////////////// while(date_flag==3) { display(); if(key1==0) { delay(10); if(key1==0) { date_flag=4; yj1602_write_com(0x80+0x40+5); yj1602_write_date('*');//依次调整年月日时分,秒不可调整 yj1602_write_date('*'); shi=ds1302_read(readadd[4]); j=shi/16; k=shi%16; shi=j*10+k; } while(!key1); } if(key2==0) { delay(10); if(key2==0) { yj1602_write_com(0x80+11); yj1602_write_date('*'); yj1602_write_date('*'); ri++; if((yue==1)||(yue==3)||(yue==5)||(yue==7)||(yue==8)||(yue==10)||(yue==12)) { if(ri==32) ri=1; } if(yue==2) { if(nian%4==0) { if(ri==30) ri=1; } else { if(ri==29) ri=1; } } if((yue==4)||(yue==6)||(yue==9)||(yue==11)) { if(ri==31) ri=1; } j=ri/10; k=ri%10; writetime[3]=j*16+k; ds1302_write(writeadd[3],writetime[3]); } while(!key2); } if(key3==0) { delay(10); if(key3==0) { if((yue==1)||(yue==3)||(yue==5)||(yue==7)||(yue==8)||(yue==10)||(yue==12)) { if(ri==1) ri=32; } if(yue==2) { if(nian%4==0) { if(ri==1) ri=30; } else { if(ri==1) ri=29; } } if((yue==4)||(yue==6)||(yue==9)||(yue==11)) { if(ri==1) ri=31; } yj1602_write_com(0x80+11); yj1602_write_date('*'); yj1602_write_date('*'); ri--; j=ri/10; k=ri%10; writetime[3]=j*16+k; ds1302_write(writeadd[3],writetime[3]); } while(!key3); } } ///////////// 调整时/////////////////////////////////////////////// while(date_flag==4) { display(); if(key1==0) { delay(10); if(key1==0) { date_flag=5; yj1602_write_com(0x80+0x40+8); yj1602_write_date('*');//依次调整年月日时分,秒不可调整 yj1602_write_date('*'); fen=ds1302_read(readadd[5]); j=fen/16; k=fen%16; fen=j*10+k; } while(!key1); } if(key2==0) { delay(10); if(key2==0) { yj1602_write_com(0x80+0x40+5); yj1602_write_date('*'); yj1602_write_date('*'); shi++; if(shi==24)shi=0; j=shi/10; k=shi%10; writetime[4]=j*16+k; ds1302_write(writeadd[4],writetime[4]); } while(!key2); } if(key3==0) { delay(10); if(key3==0) { if(shi==0)shi=24; yj1602_write_com(0x80+0x40+5); yj1602_write_date('*'); yj1602_write_date('*'); shi--; j=shi/10; k=shi%10; writetime[4]=j*16+k; ds1302_write(writeadd[4],writetime[4]); } while(!key3); } } ///////////// 调整分/////////////////////////////////////////////// while(date_flag==5) { display(); if(key1==0) { delay(10); if(key1==0) { date_flag=0; yj1602_write_com(0x80+5); yj1602_write_date('*'); yj1602_write_date('*'); yj1602_write_com(0x80+8); yj1602_write_date('*'); yj1602_write_date('*'); yj1602_write_com(0x80+11); yj1602_write_date('*'); yj1602_write_date('*'); yj1602_write_com(0x80+0x40+5); yj1602_write_date('*'); yj1602_write_date('*'); yj1602_write_com(0x80+0x40+8); yj1602_write_date('*'); yj1602_write_date('*'); yj1602_write_com(0x80+0x40+11); yj1602_write_date('*'); yj1602_write_date('*'); writetime[6]=0; ds1302_write(writeadd[6],writetime[6]); ds1302_write(0x8e,0x80); } while(!key1); } if(key2==0) { delay(10); if(key2==0) { yj1602_write_com(0x80+0x40+8); yj1602_write_date('*'); yj1602_write_date('*'); fen++; if(fen==60)fen=0; j=fen/10; k=fen%10; writetime[5]=j*16+k; ds1302_write(writeadd[5],writetime[5]); } while(!key2); } if(key3==0) { delay(10); if(key3==0) { if(fen==0)fen=60; yj1602_write_com(0x80+0x40+8); yj1602_write_date('*'); yj1602_write_date('*'); fen--; j=fen/10; k=fen%10; writetime[5]=j*16+k; ds1302_write(writeadd[5],writetime[5]); } while(!key3); } } if(key4==0) { delay(10); if(key4==0) { yj1602_init(); set_flag=1; yj1602_write_com(0x87); yj1602_write_date('*'); yj1602_write_date('*'); } while(!key4); } while(set_flag==1) { display1(); if(key2==0) { delay(10); if(key2==0) { yj1602_write_com(0x87); yj1602_write_date('*'); yj1602_write_date('*'); shi1++; if(shi1==24)shi1=0; } while(!key2); } if(key3==0) { delay(10); if(key3==0) { yj1602_write_com(0x87); yj1602_write_date('*'); yj1602_write_date('*'); if(shi1==0)shi1=24; shi1--; } while(!key3); } if(key4==0) { delay(10); if(key4==0) { yj1602_write_com(0x8a); yj1602_write_date('*'); yj1602_write_date('*'); set_flag=2; } while(!key4); } } while(set_flag==2) { display1(); if(key2==0) { delay(10); if(key2==0) { yj1602_write_com(0x8a); yj1602_write_date('*'); yj1602_write_date('*'); fen1++; if(fen1==60)fen1=0; } while(!key2); } if(key3==0) { delay(10); if(key3==0) { yj1602_write_com(0x8a); yj1602_write_date('*'); yj1602_write_date('*'); if(fen1==0)fen1=60; fen1--; } while(!key3); } if(key4==0) { delay(10); if(key4==0) { yj1602_write_com(0x87+0x40); yj1602_write_date('*'); yj1602_write_date('*'); set_flag=3; } while(!key4); } } while(set_flag==3) { display1(); if(key2==0) { delay(10); if(key2==0) { yj1602_write_com(0x87+0x40); yj1602_write_date('*'); yj1602_write_date('*'); shi2++; if(shi2==24)shi2=0; } while(!key2); } if(key3==0) { delay(10); if(key3==0) { yj1602_write_com(0x87+0x40); yj1602_write_date('*'); yj1602_write_date('*'); if(shi2==0)shi2=24; shi2--; } while(!key3); } if(key4==0) { delay(10); if(key4==0) { yj1602_write_com(0x8a+0x40); yj1602_write_date('*'); yj1602_write_date('*'); set_flag=4; } while(!key4); } } while(set_flag==4) { display1(); if(key2==0) { delay(10); if(key2==0) { yj1602_write_com(0x8a+0x40); yj1602_write_date('*'); yj1602_write_date('*'); fen2++; if(fen2==60)fen2=0; } while(!key2); } if(key3==0) { delay(10); if(key3==0) { yj1602_write_com(0x8a+0x40); yj1602_write_date('*'); yj1602_write_date('*'); if(fen2==0)fen2=60; fen2--; } while(!key3); } if(key4==0) { delay(10); if(key4==0) { set_flag=0; yj1602_write_com(0x87); yj1602_write_date('*'); yj1602_write_date('*'); yj1602_write_com(0x8a); yj1602_write_date('*'); yj1602_write_date('*'); yj1602_write_com(0x87+0x40); yj1602_write_date('*'); yj1602_write_date('*'); yj1602_write_com(0x8a+0x40); yj1602_write_date('*'); yj1602_write_date('*'); } while(!key4); } } } void deal_with() { uchar ss,ff,a,b; uchar i,j; if(qh==1)//不接短路帽为自动定时控制 { display(); keyscan(); a=writetime[4]/16; b=writetime[4]%16; ss=a*10+b; a=writetime[5]/16; b=writetime[5]%16; ff=a*10+b; if((shi1!=0)&&(fen1!=0)&&(shi2!=0)&&(fen2!=0)) { if((shi1==ss)&&(fen1==ff)&&(chuang_flag==1))//需要打开窗户的时间和窗是关闭的 { //yy晴天1,下雨0//gg白天0,白天开窗 for(j=0;j<128;j++)//步进电机转90° { for(i=0;i<4;i++) { P2=zheng; display(); } } chuang_flag=0;//窗户当前状态开启 } if((shi2==ss)&&(fen2==ff)&&(chuang_flag==0))//需要关闭窗户的时间和窗是打开的 { for(j=0;j<128;j++)//步进电机转90° { for(i=0;i<4;i++) { P2=fan; display(); } } chuang_flag=1;//1为窗户当前状态关 } } } if(qh==0)//接上短路帽为触发信号控制(光敏电阻和雨滴传感器) { display(); if((yy==0)&&(chuang_flag==0))//yy=0下雨关窗,0为窗户开的情况(白天)关闭窗户 { for(j=0;j<128;j++)//步进电机转90° { for(i=0;i<4;i++) { P2=fan; display(); } } chuang_flag=1;//1为窗户当前状态关 } if((yy==1)&&(gg==1)&&(chuang_flag==0))//0为窗户开,到了晚上需要关窗 { //yy晴天1,下雨0 //gg晚上1,晚上关窗 for(j=0;j<128;j++)//步进电机转90° { for(i=0;i<4;i++) { P2=fan; display(); } } chuang_flag=1;//1为窗户当前状态关 } if((yy==1)&&(gg==0)&&(chuang_flag==1))//窗户关,到白天需要开窗 { //yy晴天1,下雨0//gg白天0,白天开窗 for(j=0;j<128;j++)//步进电机转90° { for(i=0;i<4;i++) { P2=zheng; display(); } } chuang_flag=0;//窗户当前状态开启 } } } void main() //主函数 { yj1602_init(); while(1) { display();//显示函数 keyscan();//按键扫描函数 deal_with();//窗开关处理 } } ` |
|
相关推荐
6个回答
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-LCD显示图片编程示例之介绍mmap
238 浏览 0 评论
《DNESP32S3使用指南-IDF版_V1.6》第二章 常用的C语言知识点
629 浏览 0 评论
【RA-Eco-RA2E1-48PIN-V1.0开发板试用】(第三篇)ADC采集+PWM输出
552 浏览 0 评论
《DNK210使用指南 -CanMV版 V1.0》第四十五章 人脸识别实验
552 浏览 0 评论
1074 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11763 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 23:32 , Processed in 0.571362 second(s), Total 51, Slave 43 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号