51 single chip microcomputer - button operation

Publisher:runaway2000Latest update time:2022-12-14 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

On the 51 microcontroller development board, operate the button settings, including pressing the button, displaying the corresponding position, and button password lock


1. Press the button to display the corresponding position

main.c

#include

#include "Delay.h"

#include "LCD1602.h"

#include "MatrixKey.h"

unsigned char KeyNum;


void main()

{

LCD_Init();

LCD_ShowString(1,1,"helloworld");

while(1)

{

KeyNum=MatrixKey();

if(KeyNum)

{

LCD_ShowNum(2,1,KeyNum,2);

}

}

}

Delay.c

void Delay(unsigned int xms)

{

unsigned char i, j;

while(xms--)

{

i = 2;

j = 239;

do

{

while (--j);

} while (--i);

}

}


Delay.h

#ifndef __DELAY_H__

#define __DELAY_H__


void Delay(unsigned int xms);


#endif

LCD1640.c

#include


//Pin configuration:

sbit LCD_RS=P2^6;

sbit LCD_RW=P2^5;

sbit LCD_EN=P2^7;

#define LCD_DataPort P0


//Function definition:

/**

  * @brief LCD1602 delay function, 12MHz call can delay 1ms

  * @param None

  * @retval None

  */

void LCD_Delay()

{

unsigned char i, j;


i = 2;

j = 239;

do

{

while (--j);

} while (--i);

}


/**

  * @brief LCD1602 write command

  * @param Command The command to be written

  * @retval None

  */

void LCD_WriteCommand(unsigned char Command)

{

LCD_RS=0;

LCD_RW=0;

LCD_DataPort=Command;

LCD_EN=1;

LCD_Delay();

LCD_EN=0;

LCD_Delay();

}


/**

  * @brief LCD1602 write data

  * @param Data The data to be written

  * @retval None

  */

void LCD_WriteData(unsigned char Data)

{

LCD_RS=1;

LCD_RW=0;

LCD_DataPort=Data;

LCD_EN=1;

LCD_Delay();

LCD_EN=0;

LCD_Delay();

}


/**

  * @brief LCD1602 sets the cursor position

  * @param Line line position, range: 1~2

  * @param Column Column position, range: 1~16

  * @retval None

  */

void LCD_SetCursor(unsigned char Line,unsigned char Column)

{

if(Line==1)

{

LCD_WriteCommand(0x80|(Column-1));

}

else if(Line==2)

{

LCD_WriteCommand(0x80|(Column-1+0x40));

}

}


/**

  * @brief LCD1602 initialization function

  * @param None

  * @retval None

  */

void LCD_Init()

{

LCD_WriteCommand(0x38);//Eight-bit data interface, two lines of display, 5*7 dot matrix

LCD_WriteCommand(0x0c);//Display on, cursor off, flashing off

LCD_WriteCommand(0x06);//After data reading and writing operations, the cursor automatically increases by one and the screen does not move.

LCD_WriteCommand(0x01);//Reset the cursor and clear the screen

}


/**

  * @brief Display a character at the specified position on LCD1602

  * @param Line line position, range: 1~2

  * @param Column Column position, range: 1~16

  * @param Char The character to be displayed

  * @retval None

  */

void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)

{

LCD_SetCursor(Line,Column);

LCD_WriteData(Char);

}


/**

  * @brief Start displaying the given string at the specified position on LCD1602

  * @param Line starting line position, range: 1~2

  * @param Column Starting column position, range: 1~16

  * @param String The string to be displayed

  * @retval None

  */

void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)

{

unsigned char i;

LCD_SetCursor(Line,Column);

for(i=0;String[i]!='';i++)

{

LCD_WriteData(String[i]);

}

}


/**

  * @brief return value = X raised to the Y power

  */

int LCD_Pow(int X,int Y)

{

unsigned char i;

int Result=1;

for(i=0;i {

Result*=X;

}

return Result;

}


/**

  * @brief Start displaying the given number at the specified position on LCD1602

  * @param Line starting line position, range: 1~2

  * @param Column Starting column position, range: 1~16

  * @param Number The number to be displayed, range: 0~65535

  * @param Length The length of the number to be displayed, range: 1~5

  * @retval None

  */

void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)

{

unsigned char i;

LCD_SetCursor(Line,Column);

for(i=Length;i>0;i--)

{

LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');

}

}


/**

  * @brief Display the given number in signed decimal starting from the specified position on LCD1602

  * @param Line starting line position, range: 1~2

  * @param Column Starting column position, range: 1~16

  * @param Number The number to be displayed, range: -32768~32767

  * @param Length The length of the number to be displayed, range: 1~5

  * @retval None

  */

void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)

{

unsigned char i;

unsigned int Number1;

LCD_SetCursor(Line,Column);

if(Number>=0)

{

LCD_WriteData('+');

Number1=Number;

}

else

{

LCD_WriteData('-');

Number1=-Number;

}

for(i=Length;i>0;i--)

{

LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0');

}

}


/**

  * @brief Display the given number in hexadecimal starting from the specified position on LCD1602

  * @param Line starting line position, range: 1~2

  * @param Column Starting column position, range: 1~16

  * @param Number The number to be displayed, range: 0~0xFFFF

  * @param Length The length of the number to be displayed, range: 1~4

  * @retval None

  */

void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)

{

unsigned char i,SingleNumber;

LCD_SetCursor(Line,Column);

for(i=Length;i>0;i--)

{

SingleNumber=Number/LCD_Pow(16,i-1)%16;

if(SingleNumber<10)

{

LCD_WriteData(SingleNumber+'0');

}

else

{

LCD_WriteData(SingleNumber-10+'A');

}

}

}


/**

  * @brief Display the given number in binary starting from the specified position on LCD1602

  * @param Line starting line position, range: 1~2

  * @param Column Starting column position, range: 1~16

  * @param Number Number to be displayed, range: 0~1111 1111 1111 1111

  * @param Length The length of the number to be displayed, range: 1~16

  * @retval None

  */

void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)

{

unsigned char i;

LCD_SetCursor(Line,Column);

for(i=Length;i>0;i--)

{

LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');

}

}

LCD1640.h

#ifndef __LCD1602_H__

#define __LCD1602_H__


//User calls function:

void LCD_Init();

void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char);

void LCD_ShowString(unsigned char Line,unsigned char Column,char *String);

void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);

void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length);

void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);

void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);


#endif

MatrixKey.c

#include 

#include "Delay.h"

unsigned char MatrixKey()

{

unsigned char KeyNumber = 0;

P1=0xFF;

P1_3=0;

if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=1;}

if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=5;}

if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=9;}

if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=13;}

P1=0xFF;

P1_2=0;

if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=2;}

if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=6;}

if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=10;}

if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=14;}

P1=0xFF;

P1_1=0;

if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=3;}

if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=7;}

if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=11;}

if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=15;}

P1=0xFF;

P1_0=0;

if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=4;}

if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=8;}

if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=12;}

if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=16;}

return KeyNumber;

}

MatrixKey.h

#ifndef __MATRIXKEY_H__

#define __MATRIXKEY_H__


unsigned char MatrixKey();

#endif

2.Key code lock

Except for the main function, the functions in the password lock are different, and other library functions are the same. This is the benefit of modular programming. The user only needs to edit the logic part, and can call functions for programming other rule classes.


main.c

#include

#include "Delay.h"

#include "LCD1602.h"

#include "MatrixKey.h"

unsigned char KeyNum;

unsigned int Password;

unsigned int Count;

void main()

{

LCD_Init();

LCD_ShowString(1,1,"password:");

while(1)

{

KeyNum=MatrixKey();

if(KeyNum)

{

if(KeyNum<=10)

{

if(Count<4)

{

Password*=10;       //ÃÜÂë×óÒÆһλ

Password+=KeyNum%10;//»ñȡһλÃÜÂë

Count++;

}

LCD_ShowNum(2,1,Password,4);

}

if(KeyNum==11)

{

if(Password==2345)

{

LCD_ShowString(1,15,"OK");

Password=0;

Count=0;

LCD_ShowNum(2,1,Password,4);

}

else

{

LCD_ShowString(1,14,"ERR");

Password=0;

Count=0;

LCD_ShowNum(2,1,Password,4);

}

}

if(KeyNum==12)

{

Password=0;

Count=0;

LCD_ShowNum(2,1,Password,4);

}

}

}

}

Reference address:51 single chip microcomputer - button operation

Previous article:7 common 51 microcontroller clock circuit diagrams
Next article:51 single chip microcomputer - digital tube display

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号