Cydiaº 13971392243
关注数: 2 粉丝数: 29 发帖数: 325 关注贴吧数: 71
求助,实训内容求助攻 设计一个密码锁,输入4位数字与预设密码进行比较,若与预设密码相等则密码匹配成功,亮绿灯并在数码管显示“PASS”,若密码匹配不成功则显示“ERR.”,红灯闪烁,如果输入错误密码次数超过3次则锁死,显示“END”,按任何键均无任何反应,需要等到30s后重新输入。 这是要求,老师提供了部分资料,,我们是16单片机 #include <iom16v.h> #include <macros.h> #define uchar unsigned char #define uint unsigned int uchar table[17]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82, 0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xBF}; //0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,-的显示码 uchar disp_buf; //显示缓存 uchar temp; //暂存器 uchar key; //键顺序码 #define beep_0 (PORTD=PORTD&0x7f) //PD7上的蜂鸣器发声 #define beep_1 (PORTD=PORTD|0x80) //PD7上的蜂鸣器不发声 /********端口设置函数********/ void port_init(void) { PORTA = 0xFF; //输出高电平 DDRA = 0xFF; //设为输出 PORTC = 0xfe; //选通数码管第一位 DDRC = 0xFF; //输出 DDRD = 0xff; //设为输出 PORTD = 0xFF; //输出高电平 } /********函数功能:延时函数********/ void Delay_ms(uint xms) { int i,j; for(i=0;i<xms;i++) { for(j=0;j<1140;j++) ; } } /*********以下是蜂鸣器响一声函数********/ void beep() { beep_0; //蜂鸣器响 Delay_ms(100); beep_1; //关闭蜂鸣器 Delay_ms(100); } /********以下是矩阵按键扫描函数********/ void MatrixKey() { PORTB=0xef; //置第1行PB4为低电平,开始扫描第1行 DDRB=0xf0; //高4位设为输出,低4位设为输入 temp=PINB; //读PB口按键 temp=temp & 0x0f; //判断低4位是否有0,即判断列线(PB0~PB3)是否有0 if (temp!=0x0f) //若temp不等于0x0f,说明有键按下 { Delay_ms(10); //延时10ms去抖 temp=PINB; //再读取PB口按键 temp=temp & 0x0f; //再判断列线(PB0~PB3)是否有0 if (temp!=0x0f) //若temp不等于0x0f,说明确实有键按下 { temp=PINB; //读取PB口按键,开始判断键值 switch(temp) { case 0xee:key=0;break; case 0xed:key=1;break; case 0xeb:key=2;break; case 0xe7:key=3;break; } temp=PINB; //将读取的键值送temp beep(); //蜂鸣器响一声 disp_buf =table[key]; //查表求出键值对应的数码管显示码,送显示缓冲区disp_buf temp=temp & 0x0f; //取出列线值(PB0~PB3) while(temp!=0x0f) //若temp不等于0x0f,说明按键还没有释放,继续等待 { temp=PINB; //若按键释放,再读取PB口 temp=temp & 0x0f;// 判断列线(PB0~PB3)是否有0 } } } PORTB=0xdf; //置第2行PB5为低电平,开始扫描第2行 DDRB=0xf0; temp=PINB; temp=temp & 0x0f; if (temp!=0x0f) { Delay_ms(10); temp=PINB; temp=temp & 0x0f; if (temp!=0x0f) { temp=PINB; switch(temp) { case 0xde:key=4;break; case 0xdd:key=5;break; case 0xdb:key=6;break; case 0xd7:key=7;break; } temp=PINB; beep(); disp_buf =table[key]; temp=temp & 0x0f; while(temp!=0x0f) { temp=PINB; temp=temp & 0x0f; } } } PORTB=0xbf; //置第3行PB6为低电平,开始扫描第3行 DDRB=0xf0; temp=PINB; temp=temp & 0x0f; if (temp!=0x0f) { Delay_ms(10); temp=PINB; temp=temp & 0x0f; if (temp!=0x0f) { temp=PINB; switch(temp) { case 0xbe:key=8;break; case 0xbd:key=9;break; case 0xbb:key=10;break; case 0xb7:key=11;break; } temp=PINB; beep(); disp_buf=table[key]; temp=temp & 0x0f; while(temp!=0x0f) { temp=PINB; temp=temp & 0x0f; } } } PORTB=0x7f; //置第4行PB7为低电平,开始扫描第4行 DDRB=0xf0; temp=PINB; temp=temp & 0x0f; if (temp!=0x0f) { Delay_ms(10); temp=PINB; temp=temp & 0x0f; if (temp!=0x0f) { temp=PINB; switch(temp) { case 0x7e:key=12;break; case 0x7d:key=13;break; case 0x7b:key=14;break; case 0x77:key=15;break; } temp=PINB; beep(); disp_buf =table[key]; temp=temp & 0x0f; while(temp!=0x0f) { temp=PINB; temp=temp & 0x0f; } } } } /********以下是主函数********/ main() { port_init(); //端口初始化 disp_buf=0xBF; //开机显示"-"符号 while(1) { MatrixKey(); //调矩阵按键扫描函数 PORTA = disp_buf; //键值送P0口显示 Delay_ms(2); //延时2ms } }
1 下一页