Microprocessor Laboratory

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Summer21

MICROPROCESSOR LABORATORY
PROJECT REPORT

PROJECT TITLE: Simple Calculator in LCD capable of


performing simple calculations
OBJECTIVE

The objective of this project is to build a


ATmega32 microcontroller based simple
calculator which is capable of doing simple
operations such as addition, subtraction,
multiplication and division.

APPARATUS

COMPONENTS

● Microcontroller - ATMEGA32
● Keypad - KEYPAD SMALLCALC
● 16x2 LCD Display- LM016L

SOFTWARE

● CodeVisionAVR
● Proteus 8 professional

SPECIFICATIONS
● Input to the calculator is taken through the “small calc” keypad
● After each data is entered, it is displayed in the 16x2 LM016L LCD display
● Shows the result after “=” is pressed
● Only after 2 operands and 1 operator is entered, the calculator will show the
result. That is if “=” is pressed after entering only one operand and or after
entering the one operand and one operator, it will not show any result.
● Capable of doing logical operation such as 24/0
● Can perform simple mathematical operations which are addition,
subtraction, multiplication and division.
● Result is displayed upto 5 decimal places.
● It can continue the calculation after the result is shown, that is it can perform
operations with the result also.
● If the key “CLEAR” pressed, it will reset the calculator and the user can
enter another expression

METHODOLOGY
● We connected the keypad to PORT B and the LCD display to PORT A of
the ATmega32 microcontroller

We know each key of the keypad is


connected to a pull up resistor and hence
they are active low, that is when a key is
pressed, the column and row will be 0.

The row are connected to upper 4 pins of


PORT B and the columns are connected
to lower 4 pins of the same port

● We then confirmed the key pressed by sending ‘0’ to each column of the
keypad and correspondingly checking the row value for each row and if we
get (0,0) for column and row, we know which key has been pressed
● The Vss pin of the LCD is connected to ground and Vdd and Vee connected
to +5V power supply. RS, RW, E connected to the first 3 pins of PORT A
and the last 4 data pins are connected to the last 4 pins of PORT A.
● Then the data is stored as the input and displayed in the LCD
● Similarly, the operator and the 2nd operand is entered and stored and only
after the “=” is pressed, the result is shown.
● The C program calculates the result
● The stored result is then printed on the LCD screen
CIRCUIT DIAGRAM

CODE
#include <mega32.h>
#include <delay.h>
#include <alcd.h>
#include <string.h>
#include <stdlib.h>
#define dp 5
int i, j, row, len = 0;
char display_output[80];
float first_num = 0, second_num = 0, result_flag = 0;
int operation = 0;

char number_data[][10] = {
"7",
"4",
"1",
"CLEAR",
"8",
"5",
"2",
"0",
"9",
"6",
"3",
"=",
"/",
"x",
"-",
"+"
};

int numbers[11] = {
7,
4,
1,
0,
8,
5,
2,
0,
9,
6,
3
};

void display(int n){

switch(n) {
case 0:
case 1:
case 2:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
if(!operation) {
if(!len) {
first_num = numbers[n];
}
else {
if(!result_flag) {
first_num = (first_num * 10) + numbers[n];
}
else {
strcpy(display_output, "");
len = 0;
operation = 0;
first_num = numbers[n];
second_num = 0;
result_flag = 0;
}
}
}
else{
if(!len) {
second_num = numbers[n];
}
else {
second_num = (second_num * 10) + numbers[n];
}
}
len++;
strcat(display_output, number_data[n]);
break;
case 11:
if(len) {
if(operation) {
switch(operation) {
case 15:
first_num = first_num + second_num;
break;
case 14:
first_num = first_num - second_num;
break;
case 13:
first_num = first_num * second_num;
break;
case 12:
first_num = first_num / second_num;
break;
}
result_flag = 1;
}
operation = 0;
strcpy(display_output, "");
//itoa(first_num, display_output);
ftoa(first_num, dp, display_output);
}
break;
case 3:
strcpy(display_output, "");
len = 0;
operation = 0;
first_num = 0;
second_num = 0;
break;
case 12:
case 13:
case 14:
case 15:
if(len) {
strcat(display_output, number_data[n]);
operation = n;
len = 0;
}
break;
}

lcd_clear();
lcd_gotoxy(0, 0);
lcd_puts(display_output);
}

void data_input(void) {
PORTB = 0b11101111;
for (i = 0; i < 4; i++) {
row = 0b11111110; /*the for loop is for column checking */
for (j = 0; j < 4; j++) {
if (PINB == (row & PORTB)) {
/*the nested for loop is for row checking*/

display(4 * i + j);
}
row = ~(~row << 1);
}
PORTB = ~(~PORTB << 1);
}
delay_ms(200);
}

void main(void) {

DDRB = 0xF0;
lcd_init(16);
lcd_printf("WELCOME!");
delay_ms(1000);
lcd_clear();
lcd_gotoxy(0, 0);
lcd_printf("PLEASE ENTER THE EXPRESSION");

while (1) {
data_input();
}
}
EXPLANATION
1. In the void main function, we declared the output pins and printed the
welcome and enter expression message.
2. In the while loop, it will call the data function which will check the key
pressed row by row and column by column using the nested for loop and
then go to the display function.
3. Inside the display function, it will execute cases 0 to 10 (except 3) for if a
number is entered. If case 4 is executed, it will reset everything and start to
read data again.
4. After an operator is entered, case 12 to 15 will be executed and then go on to
5. store the 2nd number.
6. When 2nd number is entered, again case 0 to 10 (except 3) and then after
‘=’ is pressed, case 11 is executed and the result is calculated which is then
displayed on the LCD display
PROTEUS SIMULATION RESULTS
Logical Operation

Result

Mathematical Operations
Addition

Result
Subtraction

Multiplication

Division
ALTERNATIVE DESIGN
Using Atmel studio there is an inbuilt library (calc.h) where we can directly
program a calculator without even mapping the keypads and commanding the
mathematical operation. But we want to finish our project having the basic
knowledge of the code and how we use build projects in proteus. That’s why we
haven’t used this alternative way of building the calculator.

Alternative Code:

#include “calc.h”

int main(void)

cal_init();

dis_string(0,0,"When Come ;)");

dis_string(1,4,"Dreams True ;)");

_delay_ms(50);

for (int start = 0;start < 25; ++start)


{

delay_ms(5);

dis_shift('L');

while(1)

calculate();

Alternative Circuit: Instead of LCD alternatively we can also use multiple 7


segment displays.
But using a 7 segment display makes the circuit connection as well as the code
more complex because we then also have to define the control ports separately and
with a 7 segment display we can’t show as many digits as with a LCD display.

APPLICATION

As it is a calculator, it has its use in every sector of life like from studying to
business purposes and so on. Besides calculation we can use it as a counter or even
as a password security system too and be used to perform calculations in other
complex embedded systems.

IMPACT
The calculator has had a profound impact on the world, making computations
quicker and more exact. In the classroom, calculators have given many students the
ability to learn about and put complex formulas and concepts into practice more
easily. It has helped us save a lot of calculation time.

DISCUSSION AND FUTURE DEVELOPMENT


Overall we have built a calculator where simple calculation with 2 bit operators
will be done and the logical expressions will also be executed. For this we have
connected a keypad as input and lcd as output with ATmega32. The data entry
method, logical and mathematical expressions are shown in code using
CodeVisionAVR. Now there is plenty of room for the future development of the
project. The features we want to add in future:

1)While doing operation with three numbers 2+3*5 BODMAS rule will be
followed.

2)We want to add other complex calculations like trigonometric functions,


differentiation, integration and so on

3) Lastly we want to add a voice sensor in our calculator where by giving voice
commands we can operate the operations without even typing the numbers.

You might also like