完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
本帖最后由 好食欲 于 2015-6-3 11:20 编辑
求大神指导下错在那里了 #include #include #define uint unsigned int #define uchar unsigned char #define Key P1 ***it lcd1=P3^0; //lcd灯 ***it bump=P2^2;//fengmingqi蜂 ***it relay=P2^3;//jidianqi ***it lcden=P2^7;//LCD1602液晶控制端 ***it lcdrs=P2^6; // ***it lcdrw=P2^5; ***it sda=P2^0; //AT24C02数据端口 ***it scl=P2^1; //时钟端口 uchar key_state=0; #define lcd1602 P0 uchar keyValue; uchar num=0,count; uchar code xianshi0[]="input password:"; uchar code xianshi1[]="Please come in!"; uchar code xianshi2[]="*"; uchar code xianshi3[]="error!"; uchar code xianshi4[]="new password"; uchar code xianshi5[]="Change Password"; uchar code xianshi6[]="Re-enter"; uchar code xianshi7[]="you are a thief"; uchar code xianshi8[]="welcome"; uchar mima[]={0}; uchar table[]={0}; void delay(uchar x); void key(); void writecom(uchar com); void writedate(uchar dat); void lcdinit(); void writebyte(uchar date); uchar readbyte(); void write_add(uchar addr,uchar date); uchar read_add(uchar addr); void SetPassword(); void input(); void compare(); void display(); /************delay()***************/ void delay(uchar x) { uchar i,j; for(i=x;i>0;i--) for(j=199;j>0;j--); } void key() { uint a=0; Key=0x7f; if(Key!=0x7f) { delay(1); if(Key!=0x7f) { key_state=1; switch(Key) { case(0x77):keyValue=0x31; break; case(0x7b):keyValue=0x32; break; case(0x7d):keyValue=0x33; break; case(0x7e):keyValue=0x34; break; } while(Key!=0x7f&&a<50) { delay(1); a++; } } } a=0; Key=0xbf; if(Key!=0xbf) { delay(1); if(Key!=0xbf) { key_state=1; switch(Key) { case(0xb7):keyValue=0x35; break; case(0xbb):keyValue=0x36; break; case(0xbd):keyValue=0x37; break; case(0xbe):keyValue=0x38; break; } while(Key!=0xbf&&a<50) { delay(1); a++; } } } a=0; Key=0xdf; if(Key!=0xdf) { delay(1); if(Key!=0xdf) { key_state=1; switch(Key) { case(0xd7):keyValue=0x39; break; case(0xdb):keyValue=0x30; break; case(0xdd):keyValue=0x41; break; case(0xde):keyValue=0x42; break; } while(Key!=0xdf&&a<50) { delay(1); a++; } } } a=0; Key=0xef; if(Key!=0xef) { delay(1); if(Key!=0xef) { switch(Key) { case(0xe7):keyValue=0x43; break; case(0xeb):keyValue=0x44; break; case(0xed):keyValue=0x45; break; case(0xee):keyValue=0x46; break; } while(Key!=0xdf&&a<50) { delay(1); a++; } } } } /******************lcd1602********************/ void writecom(uchar com) { lcden=0; lcdrs=0; lcdrw=0; lcd1602=com; delay(1); lcden=1; delay(5); lcden=0; } void writedate(uchar dat) { lcden=0; lcdrs=1; lcdrw=0; lcd1602=dat; delay(1); lcden=1; delay(5); lcden=0; } void lcdinit() { writecom(0x38); writecom(0x0f); writecom(0x06); writecom(0x01); writecom(0x80); } /**************AT24C02***************/ void delayus() { uchar a; for(a=2;a>0;a--); } void start() { sda=1; scl=1; delayus(); sda=0; delayus(); scl=0; delayus(); } void stop() { sda=0; delayus(); scl=1; delayus(); sda=1; delayus(); } void respons() { uchar i=0; scl=1; delayus(); while((sda==1)&&(i<255)) i++; scl=0; delayus(); } void writebyte(uchar date) { uchar i,temp; temp=date; for(i=0;i<8;i++) { temp=temp<<1; scl=0; delayus(); sda=CY; delayus(); scl=1; delayus(); } scl=0; delayus(); sda=1; delayus(); } uchar readbyte() { uchar i,temp; scl=0; delayus(); sda=1; delayus(); for(i=0;i<8;i++) { scl=1; temp=(temp)<<1|sda; scl=0; delayus(); } delayus(); return temp; } void write_add(uchar addr,uchar date) { start(); writebyte(0xa0); respons(); writebyte(addr); respons(); writebyte(date); respons(); stop(); } uchar read_add(uchar addr) { uchar date; start(); writebyte(0xa0); respons(); writebyte(addr); respons(); start(); writebyte(0xa1); respons(); date=readbyte(); stop(); return date; } /*************************************/ void SetPassword() { if(keyValue==0x43)//chongxinshuru { uchar i=0,j; key_state=0; lcdinit(); for(j=0;j<15;j++) { writedate(xianshi5[j]); } for(i=0;i<7;i++) { table=keyValue; } } delay(100); if(keyValue==0x42||(keyValue==0x41&&num!=7))//1shiqueren 2shiquxiao { uchar k,h; lcdinit(); for(k=0;k<8;k++) { write_add(2*k,0); } delay(10); for(h=0;h<9;h++) { writedate(xianshi6[h]); } delay(10000); } } void input() { if(keyValue==0x44)//shurumima { uchar i; key_state=0; lcdinit(); for(i=0;i<16;i++) { writedate(xianshi0); } if(key_state==1) { key_state=0; lcdinit(); for(num=0;num<8;num++) { writedate(xianshi2[num]); mima[num]=keyValue; } delay(1000); } } } void compare() { if(keyValue==0x45)//querenmima { uchar i,c,j,k,h; lcdinit(); for(num=0;num<8;num++) { if(mima[num]==table[num]) { c++; } } if(7==c) { c=0; for(j=0;j<16;j++) { writedate(xianshi1[j]); count=0; } lcd1=0; delay(500); lcd1=1; for(h=0;h<2;h++) //蜂鸣器响两声提示开锁成功 { bump=0; delay(200); bump=1; delay(200); bump=0; delay(200); bump=1; delay(200); } } else { for(k=0;k<6;k++) { writedate(xianshi3[k]); } delay(1000); count++; lcdinit(); for(i=0;i<16;i++) { writedate(xianshi0); } delay(1000); } } if(count==3) { uint h,j; count=0; lcdinit(); for(h=0;h<16;h++) { writedate(xianshi7[h]); } delay(1000); for(j=0;j<10;j++) //蜂鸣器响两声提示开锁成功 { bump=0; delay(100); bump=1; delay(100); bump=0; delay(100); bump=1; delay(100); } } } void display() { if(keyValue==0x43) { for(num=0;num<7;num++) { writedate(table[num]); } } if(keyValue==0x44) { for(num=0;num<7;num++) { writedate(mima[num]); } } } void main() { uchar i; lcdinit(); for(i=0;i<7;i++) { writedate(xianshi8); } while(1) { key(); switch(keyValue) { case 0x43:SetPassword(); break; case 0x44:input(); break; case 0x45:compare(); break; } display(); } }
|
|
相关推荐
9个回答
|
|
直接贴代码比较好
|
|
|
|
|
|
|
|
|
|
|
|
#include
#include #define uint unsigned int #define uchar unsigned char #define Key P1 ***it lcd1=P3^0; //lcd灯 ***it bump=P2^2;//fengmingqi蜂 ***it relay=P2^3;//jidianqi ***it lcden=P2^7;//LCD1602液晶控制端 ***it lcdrs=P2^6; // ***it lcdrw=P2^5; ***it sda=P2^0; //AT24C02数据端口 ***it scl=P2^1; //时钟端口 uchar key_state=0; #define lcd1602 P0 uchar keyValue; uchar num=0,count; uchar code xianshi0[]="input password:"; uchar code xianshi1[]="Please come in!"; uchar code xianshi2[]="*"; uchar code xianshi3[]="error!"; uchar code xianshi4[]="new password"; uchar code xianshi5[]="Change Password"; uchar code xianshi6[]="Re-enter"; uchar code xianshi7[]="you are a thief"; uchar code xianshi8[]="welcome"; uchar mima[]={0}; uchar table[]={0}; void delay(uchar x); void key(); void writecom(uchar com); void writedate(uchar dat); void lcdinit(); void writebyte(uchar date); uchar readbyte(); void write_add(uchar addr,uchar date); uchar read_add(uchar addr); void SetPassword(); void input(); void compare(); void display(); /************delay()***************/ void delay(uchar x) { uchar i,j; for(i=x;i>0;i--) for(j=199;j>0;j--); } void key() { uint a=0; Key=0x7f; if(Key!=0x7f) { delay(1); if(Key!=0x7f) { key_state=1; switch(Key) { case(0x77):keyValue=0x31; break; case(0x7b):keyValue=0x32; break; case(0x7d):keyValue=0x33; break; case(0x7e):keyValue=0x34; break; } while(Key!=0x7f&&a<50) { delay(1); a++; } } } a=0; Key=0xbf; if(Key!=0xbf) { delay(1); if(Key!=0xbf) { key_state=1; switch(Key) { case(0xb7):keyValue=0x35; break; case(0xbb):keyValue=0x36; break; case(0xbd):keyValue=0x37; break; case(0xbe):keyValue=0x38; break; } while(Key!=0xbf&&a<50) { delay(1); a++; } } } a=0; Key=0xdf; if(Key!=0xdf) { delay(1); if(Key!=0xdf) { key_state=1; switch(Key) { case(0xd7):keyValue=0x39; break; case(0xdb):keyValue=0x30; break; case(0xdd):keyValue=0x41; break; case(0xde):keyValue=0x42; break; } while(Key!=0xdf&&a<50) { delay(1); a++; } } } a=0; Key=0xef; if(Key!=0xef) { delay(1); if(Key!=0xef) { switch(Key) { case(0xe7):keyValue=0x43; break; case(0xeb):keyValue=0x44; break; case(0xed):keyValue=0x45; break; case(0xee):keyValue=0x46; break; } while(Key!=0xdf&&a<50) { delay(1); a++; } } } } /******************lcd1602********************/ void writecom(uchar com) { lcden=0; lcdrs=0; lcdrw=0; lcd1602=com; delay(1); lcden=1; delay(5); lcden=0; } void writedate(uchar dat) { lcden=0; lcdrs=1; lcdrw=0; lcd1602=dat; delay(1); lcden=1; delay(5); lcden=0; } void lcdinit() { writecom(0x38); writecom(0x0f); writecom(0x06); writecom(0x01); writecom(0x80); } /**************AT24C02***************/ void delayus() { uchar a; for(a=2;a>0;a--); } void start() { sda=1; scl=1; delayus(); sda=0; delayus(); scl=0; delayus(); } void stop() { sda=0; delayus(); scl=1; delayus(); sda=1; delayus(); } void respons() { uchar i=0; scl=1; delayus(); while((sda==1)&&(i<255)) i++; scl=0; delayus(); } void writebyte(uchar date) { uchar i,temp; temp=date; for(i=0;i<8;i++) { temp=temp<<1; scl=0; delayus(); sda=CY; delayus(); scl=1; delayus(); } scl=0; delayus(); sda=1; delayus(); } uchar readbyte() { uchar i,temp; scl=0; delayus(); sda=1; delayus(); for(i=0;i<8;i++) { scl=1; temp=(temp)<<1|sda; scl=0; delayus(); } delayus(); return temp; } void write_add(uchar addr,uchar date) { start(); writebyte(0xa0); respons(); writebyte(addr); respons(); writebyte(date); respons(); stop(); } uchar read_add(uchar addr) { uchar date; start(); writebyte(0xa0); respons(); writebyte(addr); respons(); start(); writebyte(0xa1); respons(); date=readbyte(); stop(); return date; } /*************************************/ void SetPassword() { if(keyValue==0x43)//chongxinshuru { uchar i=0,j; key_state=0; lcdinit(); for(j=0;j<15;j++) { writedate(xianshi5[j]); } for(i=0;i<7;i++) { table[i]=keyValue; } } delay(100); if(keyValue==0x42||(keyValue==0x41&&num!=7))//1shiqueren 2shiquxiao { uchar k,h; lcdinit(); for(k=0;k<8;k++) { write_add(2*k,0); } delay(10); for(h=0;h<9;h++) { writedate(xianshi6[h]); } delay(10000); } } void input() { if(keyValue==0x44)//shurumima { uchar i; key_state=0; lcdinit(); for(i=0;i<16;i++) { writedate(xianshi0[i]); } if(key_state==1) { key_state=0; lcdinit(); for(num=0;num<8;num++) { writedate(xianshi2[num]); mima[num]=keyValue; } delay(1000); } } } void compare() { if(keyValue==0x45)//querenmima { uchar i,c,j,k,h; lcdinit(); for(num=0;num<8;num++) { if(mima[num]==table[num]) { c++; } } if(7==c) { c=0; for(j=0;j<16;j++) { writedate(xianshi1[j]); count=0; } lcd1=0; delay(500); lcd1=1; for(h=0;h<2;h++) //蜂鸣器响两声提示开锁成功 { bump=0; delay(200); bump=1; delay(200); bump=0; delay(200); bump=1; delay(200); } } else { for(k=0;k<6;k++) { writedate(xianshi3[k]); } delay(1000); count++; lcdinit(); for(i=0;i<16;i++) { writedate(xianshi0[i]); } delay(1000); } } if(count==3) { uint h,j; count=0; lcdinit(); for(h=0;h<16;h++) { writedate(xianshi7[h]); } delay(1000); for(j=0;j<10;j++) //蜂鸣器响两声提示开锁成功 { bump=0; delay(100); bump=1; delay(100); bump=0; delay(100); bump=1; delay(100); } } } void display() { if(keyValue==0x43) { for(num=0;num<7;num++) { writedate(table[num]); } } if(keyValue==0x44) { for(num=0;num<7;num++) { writedate(mima[num]); } } } void main() { uchar i; lcdinit(); for(i=0;i<7;i++) { writedate(xianshi8[i]); } while(1) { key(); switch(keyValue) { case 0x43:SetPassword(); break; case 0x44:input(); break; case 0x45:compare(); break; } display(); } } |
|
|
|
#include
#include #define uint unsigned int #define uchar unsigned char #define Key P1 ***it lcd1=P3^0; //lcd灯 ***it bump=P2^2;//fengmingqi蜂 ***it relay=P2^3;//jidianqi ***it lcden=P2^7;//LCD1602液晶控制端 ***it lcdrs=P2^6; // ***it lcdrw=P2^5; ***it sda=P2^0; //AT24C02数据端口 ***it scl=P2^1; //时钟端口 uchar key_state=0; #define lcd1602 P0 uchar keyValue; uchar num=0,count; uchar code xianshi0[]="input password:"; uchar code xianshi1[]="Please come in!"; uchar code xianshi2[]="*"; uchar code xianshi3[]="error!"; uchar code xianshi4[]="new password"; uchar code xianshi5[]="Change Password"; uchar code xianshi6[]="Re-enter"; uchar code xianshi7[]="you are a thief"; uchar code xianshi8[]="welcome"; uchar mima[]={0}; uchar table[]={0}; void delay(uchar x); void key(); void writecom(uchar com); void writedate(uchar dat); void lcdinit(); void writebyte(uchar date); uchar readbyte(); void write_add(uchar addr,uchar date); uchar read_add(uchar addr); void SetPassword(); void input(); void compare(); void display(); /************delay()***************/ void delay(uchar x) { uchar i,j; for(i=x;i>0;i--) for(j=199;j>0;j--); } void key() { uint a=0; Key=0x7f; if(Key!=0x7f) { delay(1); if(Key!=0x7f) { key_state=1; switch(Key) { case(0x77):keyValue=0x31; break; case(0x7b):keyValue=0x32; break; case(0x7d):keyValue=0x33; break; case(0x7e):keyValue=0x34; break; } while(Key!=0x7f&&a<50) { delay(1); a++; } } } a=0; Key=0xbf; if(Key!=0xbf) { delay(1); if(Key!=0xbf) { key_state=1; switch(Key) { case(0xb7):keyValue=0x35; break; case(0xbb):keyValue=0x36; break; case(0xbd):keyValue=0x37; break; case(0xbe):keyValue=0x38; break; } while(Key!=0xbf&&a<50) { delay(1); a++; } } } a=0; Key=0xdf; if(Key!=0xdf) { delay(1); if(Key!=0xdf) { key_state=1; switch(Key) { case(0xd7):keyValue=0x39; break; case(0xdb):keyValue=0x30; break; case(0xdd):keyValue=0x41; break; case(0xde):keyValue=0x42; break; } while(Key!=0xdf&&a<50) { delay(1); a++; } } } a=0; Key=0xef; if(Key!=0xef) { delay(1); if(Key!=0xef) { switch(Key) { case(0xe7):keyValue=0x43; break; case(0xeb):keyValue=0x44; break; case(0xed):keyValue=0x45; break; case(0xee):keyValue=0x46; break; } while(Key!=0xdf&&a<50) { delay(1); a++; } } } } /******************lcd1602********************/ void writecom(uchar com) { lcden=0; lcdrs=0; lcdrw=0; lcd1602=com; delay(1); lcden=1; delay(5); lcden=0; } void writedate(uchar dat) { lcden=0; lcdrs=1; lcdrw=0; lcd1602=dat; delay(1); lcden=1; delay(5); lcden=0; } void lcdinit() { writecom(0x38); writecom(0x0f); writecom(0x06); writecom(0x01); writecom(0x80); } /**************AT24C02***************/ void delayus() { uchar a; for(a=2;a>0;a--); } void start() { sda=1; scl=1; delayus(); sda=0; delayus(); scl=0; delayus(); } void stop() { sda=0; delayus(); scl=1; delayus(); sda=1; delayus(); } void respons() { uchar i=0; scl=1; delayus(); while((sda==1)&&(i<255)) i++; scl=0; delayus(); } void writebyte(uchar date) { uchar i,temp; temp=date; for(i=0;i<8;i++) { temp=temp<<1; scl=0; delayus(); sda=CY; delayus(); scl=1; delayus(); } scl=0; delayus(); sda=1; delayus(); } uchar readbyte() { uchar i,temp; scl=0; delayus(); sda=1; delayus(); for(i=0;i<8;i++) { scl=1; temp=(temp)<<1|sda; scl=0; delayus(); } delayus(); return temp; } void write_add(uchar addr,uchar date) { start(); writebyte(0xa0); respons(); writebyte(addr); respons(); writebyte(date); respons(); stop(); } uchar read_add(uchar addr) { uchar date; start(); writebyte(0xa0); respons(); writebyte(addr); respons(); start(); writebyte(0xa1); respons(); date=readbyte(); stop(); return date; } /*************************************/ void SetPassword() { if(keyValue==0x43)//chongxinshuru { uchar i=0,j; key_state=0; lcdinit(); for(j=0;j<15;j++) { writedate(xianshi5[j]); } for(i=0;i<7;i++) { table[i]=keyValue; } } delay(100); if(keyValue==0x42||(keyValue==0x41&&num!=7))//1shiqueren 2shiquxiao { uchar k,h; lcdinit(); for(k=0;k<8;k++) { write_add(2*k,0); } delay(10); for(h=0;h<9;h++) { writedate(xianshi6[h]); } delay(10000); } } void input() { if(keyValue==0x44)//shurumima { uchar i; key_state=0; lcdinit(); for(i=0;i<16;i++) { writedate(xianshi0[i]); } if(key_state==1) { key_state=0; lcdinit(); for(num=0;num<8;num++) { writedate(xianshi2[num]); mima[num]=keyValue; } delay(1000); } } } void compare() { if(keyValue==0x45)//querenmima { uchar i,c,j,k,h; lcdinit(); for(num=0;num<8;num++) { if(mima[num]==table[num]) { c++; } } if(7==c) { c=0; for(j=0;j<16;j++) { writedate(xianshi1[j]); count=0; } lcd1=0; delay(500); lcd1=1; for(h=0;h<2;h++) //蜂鸣器响两声提示开锁成功 { bump=0; delay(200); bump=1; delay(200); bump=0; delay(200); bump=1; delay(200); } } else { for(k=0;k<6;k++) { writedate(xianshi3[k]); } delay(1000); count++; lcdinit(); for(i=0;i<16;i++) { writedate(xianshi0[i]); } delay(1000); } } if(count==3) { uint h,j; count=0; lcdinit(); for(h=0;h<16;h++) { writedate(xianshi7[h]); } delay(1000); for(j=0;j<10;j++) //蜂鸣器响两声提示开锁成功 { bump=0; delay(100); bump=1; delay(100); bump=0; delay(100); bump=1; delay(100); } } } void display() { if(keyValue==0x43) { for(num=0;num<7;num++) { writedate(table[num]); } } if(keyValue==0x44) { for(num=0;num<7;num++) { writedate(mima[num]); } } } void main() { uchar i; lcdinit(); for(i=0;i<7;i++) { writedate(xianshi8[i]); } while(1) { key(); switch(keyValue) { case 0x43:SetPassword(); break; case 0x44:input(); break; case 0x45:compare(); break; } display(); } } |
|
|
|
为什么刷新下就没了
|
|
|
|
首先给按键定义键值,然后读取按键情况,再显示到1602上面 |
|
|
|
void main() { uchar i=0,j; lcdinit(); while(1) { for(i=0;i<7;i++) { table=key(); //扫描键盘 delay(100); } for(j=0;j<7;j++) { writedate(table[j]);//液晶屏显示 delay(100); } } } 主函数是这样的,但是还是出不来结果,能请问下到底应该怎么修改 |
|
|
|
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
211 浏览 0 评论
242 浏览 0 评论
串口发送中断中对发送I/O口定义为GPIO输出,出现发送数据不完整,缺少2个字节,求解答
1912 浏览 3 评论
841 浏览 0 评论
【xG24 Matter开发套件试用体验】10-Bluetooth示例1
998 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
10878 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-8-28 00:25 , Processed in 0.987933 second(s), Total 93, Slave 73 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191