Combination lock course design

Publisher:火星叔叔Latest update time:2016-09-14 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Require:

1. Design a password lock based on single chip microcomputer control

2. Basic functional requirements:

a.12 buttons for password input and setting, of which s/c is divided into short press (clear) and long press (set)

b. Two LED lights reflect whether the password is entered correctly (green light on) or incorrectly (red light on)

c. The original password is: 000000, which can be modified at any time (the new password will not be lost in case of power failure)

 

Here is the specific process I did:

The following is a screenshot of Protues (you can set it up in Proteus as follows):

Combination lock course design - gududesiling - suixin
If you can't see it, right click and save as... download it... and it will be fine...
When proteus is running, you must first enter a 6-digit password, then click the OK button. If it is correct, the green light will be on, otherwise, the red light will be on (it is clearly stated in the instructions above)

 

The code is posted and can be run directly on uvision4. . .

//The following program is a password lock control program, which can set the password, determine whether the password is correct, and keep the password from being lost when the power is off

 

#include

//Type redefinition

#define uchar unsigned char

#define uint unsigned int

 

 

// Function declaration

void start(); //Start signal

uchar read_add(uchar);

void key_scan();

void write_add(uchar address,uchar date);

void delay1ms(uint z);

void delay();

void stop(); //Stop

void respons(); //response

void init();

void write_byte(uchar date);

uchar read_byte();

 

//Global variable declaration

uchar key=0;

uchar password[6];

uchar KeyCnt;

uchar flag=0;

uchar password_err;

uchar ok_right;

 

//The clock line and data line of the ii2c chip

sbit sda=P2^3;

sbit scl=P2^2;

 

//Main function

void main(){

    uchar temp,i;

    uchar password_err=0;

    uchar password_old[6]={0};

    KeyCnt=0;

    flag=0;

    ok_right=0;  

    password_err=0;

 

    init(); //ii2c chip initialization

    for(i=0;i<6;i++){

       temp=read_add(i+1); //Call the data in the ii2c chip (set to 6 bits)

      

       if(temp<0 || temp >9 ){ //If the read data is incorrect, it means that the password lock is used for the first time, so it is initialized to 0

           for(i=0;i<6;i++)

              password_old[i]=0;

           break;

       }

       password_old[i]=read_add(i+1); 

       delay1ms(10);

    } 

    key=255; //The default key value is set to 255 for debugging purposes

     

    while(1){

       key_scan(); //Scan key scan function

       if( key <= 9 ){

           password[KeyCnt]=key;

           P3=KeyCnt; //Output debugging information

       }

 

       if(key == 11){ //Corresponds to the ok key to determine whether the password is correct

           for(i=0;i<6;i++)

              if(password_old[i] != password[i]){

                  password_err=1;                       

                  break;

              }

           if(password_err==1){

               P2 = (P2&0xfc)|0x02;

               password_err=0;    

           }

           else {

               P2 = (P2 & 0xfc) | 0x01;

           }

       }

 

       if(key==10){ //Corresponds to the s/c key, enter the reset password subroutine

           KeyCnt=0;

           ok_right=0;

           while(1){

              key_scan();

              //;//key=0; key=16;

              if( key <= 9 ){

                  password[KeyCnt]=key;

                  P3=KeyCnt;

              }

 

              if(flag==2){ //Long press

                  P2 = P2&0xfc; //If reset, both lights will be on

                  if(ok_right==7&&key==11){

                     for(i=0;i<6;i++)

                     { //Write the reset password into the storage chip

                         write_add(i+1,password[i]);

                         delay1ms(10);

                     }

                     key=255;

                     KeyCnt=0;

                     ok_right=0;

                     break;

                  }

              }

              if(flag == 1){ //short press

                  for(i=0;i<6;i++){

                     write_add(i+1,0);

                     if((i+1)/2) P2 =(P2&0xfc)|0x02;

                     else P2 = (P2&0xfc)|0x01; 

                     delay1ms(300);

                  }

                  KeyCnt=0;

                  ok_right=0;

                  key=255;  

                  break;

              }

           }

      

       }

    }  

   

}

 

/***************************The following is the ii2c chip operation function set**************************/

void delay()

{ ;; }

 

void delay1ms(uint z)

{

    uint x,y;

    for(x=z;x>0;x--)

       for(y=110;y>0;y--);

}

 

 

void start() //Start signal

{  

    sda=1;

    delay();

    scl=1;

    delay();

    sda=0;

    delay();

}

void stop() //Stop

{

    sda=0;

    delay();

    scl=1;

    delay();

    sda=1;

    delay();

}

void respons() //response

{

    uchar i;

    scl=1;

    delay();

    while((sda==1)&&(i<250))i++;

    scl=0;

    delay();

}

void init()

{

    sda=1;

    delay();

    scl=1;

    delay();

}

 

void write_byte(uchar date)

{

    uchar i,temp;

    temp=date;

    for(i=0;i<8;i++)

    {

       temp=temp<<1;

       scl=0;

        delay();

       sda=CY;

       delay();

       scl=1;

       delay();

    }

    scl=0;

    delay();

    sda=1;

    delay();

}

uchar read_byte()

{

    uchar i,k;

    scl=0;

    delay();

    sda=1;

    delay();

    for(i=0;i<8;i++)

    {

       scl=1;

       delay();  

       k=(k<<1)|sda;

       scl=0;

       delay();  

    }

    return k;

}

 

//Write data to the ii2c chip

void write_add(uchar address,uchar date)

{

    start();

    write_byte(0xa0);

    respond();

    write_byte(address);

    respond();

    write_byte(date);

    respond();

    stop();

}

 

//Read data from the ii2c chip

uchar read_add(uchar address)

{

    uchar date;

    start();

    write_byte(0xa0);

    respond();

    write_byte(address);

    respond();

    start();

    write_byte(0xa1);

    respond();

    date=read_byte();

    stop();

    return date;

}

 

 

/***************************The following is the key scanning function**************************/

void key_scan()

{  

       uchar m0,m1;

       uchar temp;   

      

       P1=0xf0; //This setting is to make the lower four bits pull the upper four bits down, and automatically pull the upper four bits up when released 

       temp=P1;      

       if(temp!=0xf0)

       {             

              delay1ms(10); //delay, remove jitter

              if(temp!=0xf0)

              {    

                     m0=temp; //Get the column number of the key (corresponding to the upper 4 bits)            

                     P1=0x0f;

                     temp=P1;

                     if(temp!=0x0f)

                     {    

                          m1=temp; //Get the row number of the key (corresponding to the lower 4 bits)     

                         temp=m0|m1; //Combined into the final key

                     }    

                  KeyCnt++;

                  if(KeyCnt==7)

                     KeyCnt=0;

                  ok_right++;

                  if(ok_right==8)

                     ok_right=0;

                     switch(temp)

                     {

 

                          case 0xee:key=0;break; //Code table corresponding to the key

                            case 0xde:key=1;break;

                            case 0xbe:key=2;break;

                            case 0x7e:key=3;break;                                

                            case 0xed:key=4;break;

                            case 0xdd:key=5;break;

                            case 0xbd:key=6;break;

                            case 0x7d:key=7;break;                                 

                            case 0xeb:key=8;break;

                            case 0xdb:key=9;break;

                            case 0xbb:

                         key=10;

                         P1=0x0f;                       

                         delay1ms(700);

                         temp=P1;

                         if(temp == 0x0f) //short press

                            flag=1;

                         else

                            flag=2; //Long press                         

                         break;

                            case 0x7b:key=11;break;                          

                         default:key=16;break; 

                     }

                    do{

                             temp=P1; //Eliminate the jitter when pressing

                            temp&=0X0f;

                     }while(temp!=0x0f);

              }

       }

}

 

 

The above content can meet the requirements. . . .

Reference address:Combination lock course design

Previous article:Accurate calculation of microcontroller delay
Next article:KEILC51 compilation problem ERROR L104

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号