Calculadora com microcontrolador 8051(89c51,89c52), LCD 16×2 e teclado 4×4

Calculator with 8051 microcontroller (89c51,89c52), 16×2 LCD and 4×4 keyboard

In this tutorial I will build a simple two-digit calculator with 8051(89c51,89c52) microcontroller. The calculator takes two single digits and an operator as input and produces output. Input is taken via the 4x4 numeric keypad and output is displayed on a 16x2 character LCD. The DIY calculator can perform four operations of addition, subtraction, negation and division.
Calculator with Keil IDE, 8051 microcontroller and 16x2 LCD

Calculator with Keil IDE, 8051 microcontroller and 16×2 LCD

Project Hardware Requirement

  • An 89c52 or 89c51 microcontroller
  • Breadboard or PCB (Circuit Manufacturing)
  • 16×2 LCD (text display)
  • 4×4 keyboard (getting input)
  • Potentiometer (to set LCD contrast)
  • Crystal (11.0592 MHz)

Calculator with circuit diagram of 8051 microcontroller

Port 1 of the 8051 microcontroller is interfaced with a 4×4 numeric keypad. The bottom four bits of the 8051-1 gate are sweeping rows of the 4x4 keyboard and the top four bits are sweeping coulombs of the 4x4 keyboard. Port 1 is used as the input port. The 16×2 LCD is interfaced with the 8051 microcontroller in 8-bit mode. The 16×2 LCD is connected to port 2 of the 89c51 microcontroller. The 20 MHz oscillator is used to provide clock source to the 89c52 microcontroller. The rest of the connections are power supply for the microcontroller and reset button settings.
Calculator with microcontroller 8051(89c51,89c52)

Calculator with microcontroller 8051(89c51,89c52)

Calculator project code 8051

Read the explanation carefully because it covers all the important points of the code and each statement in the code is explained briefly. we assume that you are already familiar with the syntax of the c/c++ language. Otherwise, first take some c/c++ programming tutorials and familiarize yourself with the syntax of the c/c++ programming language.

Starting from the beginning, the first line of code. Don't forget to include reg51.h header file for all your projects that contain an 8051(89c51,89c52) microcontroller. This header file is very important. The Keil compiler first checks this header file in your code. If it is present, the compiler compiles your code, absence of this header file will lead to an error. Below are some function declarations.

Functions in Calculator Code 8051

void lcdcmd (unsigned character)

  • Sending COMMANDS to LCD. (If you don't know about commands, just click on the commands word)

void lcddata (unsigned character)

  • Sending DATA to LCD. (If you don't know about Data, just click on data word)

void MSDelay (unsigned int)

  • Generating delay.

void disp_num(float num)

  • Displaying number on LCD after calculation.

int get_num(char ch)

  • Transforming character into number

void lcdinit

  • Initializing the LCD chipset driver. (click the link to see what Initializing LCD means)

char scan_key(void)

  • Scanning a number on the keyboard.

Code 8051 calculator main function

I put the body of the main function in a continuous while loop, so I could run the calculator forever. The lcdinit function is initializing the lcd chipset driver with some necessary lcd commands (I didn't understand, just click the link and you will get a good tutorial on how to initialize the lcd). Than in the while loop I am printing the string on the lcd ENTER 1 NO =. Enter the desired number on the keypad. key=scan_key statement is the scan key you press on the keyboard. scan_key function returns a character that you pressed on the keyboard. k2=get_num(key) is converting the key returned from scan_key function into a digit.

Scan_key is reading key as character. To perform the arithmetic operation on the key we have to convert it into a number. get_num(key) is doing this work for us. So the lcddata function is printing the character on the lcd and the lcdcmd (0x01) command clears all the contents of the lcd.

The next string is printed on the lcd saying operator = . You have to enter the operator and all previous steps are repeated. The next string appears saying Enter 2 no= Just type the no. Now you see a switch statement that is making a decision if the operator entered is + it will go to the function disp_num(K1+k2) adding the two numbers you just entered. For the – operator it will go to negation and for other operators it will go to them. Sending commands and data to LCD is a very important function, its brief details are available in LCD data and command function. Than the delay function to delay the process is very important. One can understand this by running embedded code on hardware equipment. scan_key The function is explained in the following link 4×4 Keyboard Programming.

The disp_num function is displaying our calculated number on 16×2 LCD.

In functions, some character arrays are declared. Strings in character matrices are displayed on 16×2 LCD during the operation of the 8051 microcontroller calculator. In fact, they are communicating with the user, requesting information and results. I am also using internal memory registers from the 8051(89c51,89c52) or sfr microcontroller.

sfr's are direct memory locations for ports, registers and timers of 8051 microcontrollers. Sfr ldata=0xA0 is accessing port 2 of the microcontroller. 0xA0 is Sfr for Port-2. Now ldata is a variable that points to port 2 of the microcontroller. Then some pins are declared. The LCD is controlled by port 3# pins 5,6,7. The LCD data pins are connected to port 2 of the microcontroller. Port 1 is checking lines and coulombs from the 4x4 keyboard.

Now, how is it working?

First, I'm converting my float num to integer by numb=(int)num. So I'm checking if num is less than 0 or if it's negative. If the number is negative, I multiply it by -1 and make it positive. Then send – sign on the LCD because the calculated value is in the format –. Than if the value is greater than 10 I am finding ten digits by dividing numb/10 now if the tendigit is not equal to 0 then display it on LCD. lcd data (tenth digit + 0x30) you guys are thinking what this means. In fact, when I divide numb/10, the value returned for TEnthdigit is in ASCII format because TenthDigit is a character variable, so to turn an ASCII value into a character just add 0x30 or 48 to it because those are the values ​​where the digits start. . Than for the unit digit numb-TenthDigit*10, it will give us the unit digit in ASCII format to add the value 0x30 to it. If you don't add 0x30 (hexadecimal) or 48 (decimal) to it, the LCD will show some special characters. The initial characters of the ASCII table. The decimal value is calculated and also printed on the LCD using the same method. Just copy the code that made the hardware circuit write the code to the 8051(89c51,89c52) microcontroller and verify its creation.

Character and sfr arrays used in calculator functions

Some more projects on how to make double-digit calculators with microcontrollers are below. You can find free source codes and circuit diagrams of the projects in the tutorials. Click the buttons below to visit the tutorials.

pic

Two-digit calculator with Arduino Uno

Download project files, code (C++, Hex) compiled in keil U-vision 4. Give us your feedback on the project. If you have any questions, write them in the comments section below
Watch the project video here……
Calculator code (899c52) Microcontroller

Conteúdo Relacionado

A network of sensors is embedded in every vehicle,...
The motor controller is one of the most important...
ESP32-CAM is a compact camera module that combines the...
A evolução dos padrões USB foi fundamental para moldar...
A SCHURTER anuncia um aprimoramento para sua conhecida série...
A Sealevel Systems anuncia o lançamento da Interface Serial...
A STMicroelectronics introduziu Diodos retificadores Schottky de trincheira de...
Determinar uma localização precisa é necessário em várias indústrias...
O novo VIPerGaN50 da STMicroelectronics simplifica a construção de...
A Samsung Electronics, fornecedora de tecnologia de memória avançada,...
O mercado embarcado tem uma necessidade de soluções de...
You have probably come across the term ' drag...
You probably have a support insulator if you've noticed...
You've probably seen stand an insulator sit on power...
You've probably seen shackle insulators enthroned on electricity poles,...
You have probably experienced situations where controlling a circuit...
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.