# 1, Hardware introduction

This design uses two peripheral modules, one uses the 4 * 4 matrix key, and the other uses the LCD1602 display screen for display.

## 1. Introduction to LCD1602 LCD

### (1) Composition of LCD1602

1602 LCD is also called 1602 character LCD. It can display 2 lines of character information, and each line can display 16 characters. It is a dot matrix LCD module specially used to display letters, numbers and symbols. It is composed of several 5x7 or 5x10 dot matrix character bits. Each dot matrix character bit can be used to display a character. There is an interval between each dot and between each line, which plays the role of character spacing and line spacing. Because of this, it can not display pictures well. Its physical figure is shown below

### (2) Function introduction of each pin

In the figure above, we can see that there are 16 pin holes, the numbering sequence from left to right is 1-16, and their functions are defined as follows:

### (3)DDRAM

LCD1602 contains 80 bytes of DDRAM, which is used to register display characters. The correspondence between the address and the screen is shown in the following table:

## 2. Matrix key introduction

### (1) Advantages of matrix keys:

When the independent keyboard is connected with the single chip microcomputer, each key needs an I/O port of the single chip microcomputer. If a single chip microcomputer system needs more keys, if independent keys are used, too many I/O port resources will be occupied. I/O port resources in single chip microcomputer system are often valuable. When multiple keys are used, matrix keyboard is introduced in order to reduce I/O port pins.

### (2) Principle:

The development board arranges 16 keys into 4 rows and 4 columns. The first row connects one end of each key together to form a row line, and the first column connects the other end of each key together to form a column line. In this way, there are 4 rows and 4 columns, a total of 8 lines. We connect these 8 lines to the 8 I/O ports of the single chip microcomputer, and 16 keys can be detected by scanning the keyboard through the program. In this way, we can also achieve 9 keys in 3 rows and 3 columns, 25 keys in 5 rows and 5 columns, 36 keys in 6 rows and 6 columns or even more.

### (3) Test method:

Both ends of the matrix keyboard are connected with the I/O port of the single chip microcomputer, so it is necessary to program to send the low level through the I/O port of the single chip microcomputer during detection. There are many detection methods, the most commonly used are row column scanning and line reversal.

When detecting by row column scanning method, first send one column as low level, and the other columns are all high level (at this time, we determine the number of columns), and then immediately detect whether each row has low level in turn. If a low level of a behavior is detected (at this time, we determine the number of rows), we can confirm which row and column of the currently pressed key is, In the same way, send each column a low level in turn, and then detect whether each row becomes a low level in turn. In this way, all keys can be detected. When a key is pressed, which key is pressed can be determined. Of course, we can also set the row line to low level and scan whether the column has low level. So as to achieve the detection of the whole keyboard.

Line reversal method is to make all line lines at low level, detect whether all column lines have low level, and if so, record the column line value; Then turn it over to make all column lines low level, detect the values of all row lines, and the values of row lines will also change due to the pressing of the key, and record the values of row lines. Thus, all keys can be detected. The matrix keyboard also needs the key shaking elimination link. In this chapter, the row column scanning method is used to detect which key is pressed.

### (4) Physical drawing of matrix key

## 3. Schematic diagram of development board

The modules corresponding to the matrix key and LCD1602 are:

In this experiment, the values or symbols represented by each position of the matrix keyboard:

| 1 | | 2 | | 3 | | 4 |

| 5 | | 6 | | 7 | | 8 |

| 9 | | 0 | | + | | - |

| * | | / | | = |

# 2, Algorithm design

This design adopts the idea of traversal, which stores the input data and symbols in two different arrays respectively. In order to realize the priority calculation, that is, calculate the multiplication and division method first, and then calculate the addition and subtraction method. Therefore, this experiment adopts the idea of twice traversal: the first traversal finds out the multiplication and division symbols and their corresponding two numbers, stores them in two different arrays, and gives priority to their calculation, After the calculation is completed, the second traversal is carried out to calculate the addition and subtraction method. There are two different arrays of data and operators, and they are added and subtracted in order from beginning to end. The last element of the last array is the output result.

Note: This calculator can calculate integer addition, subtraction, multiplication and division,

And conforms to the priority of addition, subtraction, multiplication and division, but cannot calculate floating-point type.

# 3, Programming implementation

## 1. Main function part:

#include <REGX51.H> #include "LCD1602.h" #include "MatrixKey.h" /*********************************************** 51 Single chip microcomputer operation -- a simple calculator based on AT89C51 Author: long Sicheng, 2002 class of intelligent medical engineering | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | | 0 | | + | | - | | * | | / | | = | LCD Screen display: ****************************************** * INPUT:Value entered OP: operator entered* * OUTPUT: Calculation results* ****************************************** Note: This calculator can calculate integer addition, subtraction, multiplication and division, And conforms to the priority of addition, subtraction, multiplication and division, but cannot calculate floating-point type. ***********************************************/ int shuju[5]; //Array for storing data char yunsuan[5]; //An array of operators int keynum; //Return to matrix keyboard int dat; int j=0; int i=0; int jj; int ii; int j3=0; int i3=0; int yshuju[5]; //Store data corresponding to * and / or char youxian[5]; //An array that stores the * and / operators void result() //Result display function { LCD_ShowString(1,16," "); if(shuju[jj]<=9){LCD_ShowNum(2,8,shuju[jj],1);} if(shuju[jj]>9&&shuju[jj]<=99){LCD_ShowNum(2,8,shuju[jj],2);} if(shuju[jj]>99&&shuju[jj]<=999){LCD_ShowNum(2,8,shuju[jj],3);} } void showdata() //Displays the entered data { if(dat<=9){LCD_ShowNum(1,7,dat,1);} if(dat>9&&dat<=99){LCD_ShowNum(1,7,dat,2);} if(dat>99&&dat<=999){LCD_ShowNum(1,7,dat,3);} } void main() { LCD_Init(); LCD_ShowString(1,1,"INPUT:"); LCD_ShowString(1,13,"OP:"); LCD_ShowString(2,1,"OUTPUT:"); while(1) { keynum=MatrixKey(); //Returns the key value of the matrix keyboard if(keynum) { if(keynum<=10) //Input data processing, if more than two bits, move left { dat*=10; dat+=keynum%10; showdata(); } if(keynum>10) { shuju[j]=dat; j++; dat=0; LCD_ShowString(1,7," "); if(keynum==11){yunsuan[i]='+';LCD_ShowString(1,16,"+");i++;} if(keynum==12){yunsuan[i]='-';LCD_ShowString(1,16,"-");i++;} if(keynum==13){yunsuan[i]='*';LCD_ShowString(1,16,"*");i++;} if(keynum==14){yunsuan[i]='/';LCD_ShowString(1,16,"/");i++;} if(keynum==15) { for(ii=0,jj=1;jj<j;jj++) //First traversal, find the array elements corresponding to * and / and give priority to calculation { if(yunsuan[ii]=='*'|yunsuan[ii]=='/') { youxian[i3]=yunsuan[ii]; yshuju[j3]=shuju[jj]; if(youxian[i3]=='*') { yshuju[j3]=shuju[jj]*shuju[jj-1]; shuju[jj]=yshuju[j3]; shuju[jj-1]=0; } if(youxian[i3]=='/') { yshuju[j3]=shuju[jj-1]/shuju[jj]; shuju[jj]=yshuju[j3]; shuju[jj-1]=0; } yunsuan[ii]='+'; } ii++; } for(ii=0,jj=1;jj<j;jj++) //Second traversal, calculation + - Method { if(yunsuan[ii]=='+') { shuju[jj]=shuju[jj]+shuju[jj-1]; result(); } if(yunsuan[ii]=='-') { shuju[jj]=shuju[jj-1]-shuju[jj]; result(); } if(yunsuan[ii]=='*') { shuju[jj]=shuju[jj]*shuju[jj-1]; result(); } if(yunsuan[ii]=='/') { shuju[jj]=shuju[jj-1]/shuju[jj]; result(); } ii++; } } } } } }

## 2. Matrix key scan function

#include <REGX52.H> #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; }