仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
仿真图
单片机源码:
- #include <p18F452.h> //调用头文件
- #include<string.h>
- #define RB0 PORTBbits.RB0
- #define RB1 PORTBbits.RB1
- #define RB2 PORTBbits.RB2
- #define RB3 PORTBbits.RB3
- #define RB4 PORTBbits.RB4
- #define RB5 PORTBbits.RB5
- #define RB6 PORTBbits.RB6
- #define RB7 PORTBbits.RB7
- /*定义密码*/
- char password1[9]={'1','2','3','4','5','6','B','A','\0'
- };
- unsigned int VALUE[]=
- { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,16};
- const unsigned char LED[17]=
- {
- 0x80, //.
- 0x3f, //0
- 0x06, //1
- 0x5b, //2
- 0x4f, //3
- 0x66, //4
- 0x6d, //5
- 0x7d, //6
- 0x07, //7
- 0x7f, //8
- 0x6f, //9
- 0x77, //A
- 0x7c, //b
- 0x39, //C
- 0x5e, //d
- 0x79, //E
- 0x71, //F
- };
- int i,j,k,wei,t,m;
- char x[9]={'0','0','0','0','0','0','0','0','\0'};
- void key(void); //按键扫描
- void choose(void); //按键确定
- void delay(void); //延时
- char turn(char z); //将asca值转化为字符类型值
- void reset(void); //重置
- //---------------------------------------
- //名称: 主函数
- //-----------------------------------------
- void main(void)
- {
-
- TRISB=0B11110000; //RC高四位输入,低四位输出
- TRISD=0B00000000; //RD设置为输出
- TRISC=0B00000000;
- PORTD=0B00000000; //
- PORTC=0B00000000;
- j=0;
- wei=0;
-
- while(1)
- {
-
- if(wei==1){
- PORTC=0B00000000;
-
- }
- if(wei==2){
- PORTC=0B00000001;
- }
- if(wei==3){
- PORTC=0B00000010;
- }
- if(wei==4){
- PORTC=0B00000011;
- }
- if(wei==5){
- PORTC=0B00000100;
- }
- if(wei==6){
- PORTC=0B00000101;
- }
- if(wei==7){
- PORTC=0B00000110;
- }
- if(wei==8){
- PORTC=0B00000111;
- }
- if(k==16){ //按F 重置
- reset();
- }
- key();
- PORTD=LED[k];
- x[wei-1]=turn(k-1);
- //PORTCbits.RC7=1;
-
- if(strcmp(x,password1)==0 && wei==8){
- PORTCbits.RC7=1;
- }
-
-
- }
-
- }
- void choose()
- {
- if(RB4==0){
- delay();
- if(RB4==0){
- j=VALUE[i];
- wei++;
- while(!RB4);
- delay();
- while(!RB4);
- }
- }
- else if(RB5==0){
- delay();
- if(RB5==0){
- j=VALUE[i+1];
- wei++;
- while(!RB5);
- delay();
- while(!RB5);
- }
- }
- else if(RB6==0){
- delay();
- if(RB6==0){
- j=VALUE[i+2];
- wei++;
- while(!RB6);
- delay();
- while(!RB6);
- }
- }
- else if(RB7==0){
- delay();
- if(RB7==0){
- j=VALUE[i+3];
- wei++;
- while(!RB7);
- delay();
- while(!RB7);
- }
- }
- k=j;
- Nop();
- }
- void key()
- {
- RB0=0,RB1=1,RB2=1,RB3=1;
- i=0;
- choose();
- RB0=1,RB1=0,RB2=1,RB3=1;
- i=4;
- choose();
- RB0=1,RB1=1,RB2=0,RB3=1;
- i=8;
- choose();
- RB0=1,RB1=1,RB2=1,RB3=0;
- i=12;
- choose();
-
- Nop();
- }
- void delay(){
- for(t=2;t>0;t--)
- for(m=100;m>0;m--);
- }
- char turn(char z)
- {
- if(z>=0 && z<=0x09) return z+'1'-1;
- if(z>=0X0A && z<=0x0F) return z+'A'-10;
- }
- void reset(void)
- {
- PORTD=0B00000000; //
- PORTC=0B00000000;
- j=0;
- wei=0;
- x[0]='0';
- x[1]='0';
- x[2]='0';
- x[3]='0';
- x[4]='0';
- x[5]='0';
- x[6]='0';
- x[7]='0';
- x[8]='\0';
- i=0;
- k=0;
- }
复制代码 全部资料51hei下载地址:
password.rar
(90.39 KB, 下载次数: 59)
|