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):
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. . . .
Previous article:Accurate calculation of microcontroller delay
Next article:KEILC51 compilation problem ERROR L104
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- General Concept of Frequency Response
- (FPGA Experiment 5): Verilog HDL language digital tube clock (hours, minutes, seconds)
- How to choose the right AFE for BMS
- Design and production of LED electronic dot matrix screen control card (provide some ideas for question I)
- HC05 and HC06 Bluetooth configuration experience summary
- Some understanding of C language memory
- VGA display image
- Two watchdogs of STM32
- [NXP Rapid IoT Review] + Mobile Synchronizer 4
- The difference between bit rate, baud rate and data transmission rate