Skip to main content

Calculator



  • Aim: To make a calculator using ATmega16 microcontroller, LCD (16x2) and Keypad (4x4).

Note: LCD used in 4 bit mode

  • Code:
#define F_CPU 8000000UL
#include<avr/io.h>
#include<util/delay.h>
#include<math.h>
void initialize();
void lcd_cmd(char value);
void lcd_data(char value);
void lcd_string(char *str);
void lcd_num(long int number);
void keypad();
void operate();
void check();
void special();
long int z=0,i=0,j=0,ar[10],arr[10],far[10],farr[10],op[10],inc=0;
void initialize()
{
lcd_cmd(0x01); //clear LCD
lcd_cmd(0x02);    //home position
lcd_cmd(0x06);    //L-R
//lcd_cmd(0x04);
lcd_cmd(0x28); //4 bit mode
lcd_cmd(0x0C);    //character generation
}
void lcd_cmd(char value)
{
int un,ln; // pc7-pc4 = un ; pc3-pc4 =ln
un=value&0xF0; // & is anding function
PORTC=0x04|un;
_delay_ms(2);
PORTC=0x00|un;
_delay_ms(2);
ln=(value<<4)&0xF0; //<<=left shif by 4 bits
_delay_ms(2);
PORTC=0x04|ln;
_delay_ms(2);
PORTC=0x00|ln;
}
void lcd_data(char value)
{
int un,ln; // pc7-pc4 = un ; pc3-pc4 =ln
un=value&0xF0; // & is anding function
PORTC=0x05|un;
_delay_ms(2);
PORTC=0x01|un;
_delay_ms(2);
ln=(value<<4)&0xF0; //<<=left shif by 4 bits
_delay_ms(2);
PORTC=0x05|ln;
_delay_ms(2);
PORTC=0x01|ln;
}
void lcd_string(char *str)
{
int i=0;
while(str[i]!='\0')
{
lcd_data(str[i]);
i++;
}
}
void lcd_num(long int number)
{
lcd_cmd(0x04);
long int d=0;
while(number!=0)
{
d=number%10;
lcd_data(d+48);
number=number/10;
}
}
int power(int x){
int i=0;
long int t=1;
for(i=0;i<x;i++){
t=t*10;
}
return t;
}
void operate(){
if(i>j){
i=i-j-1;
}
else if(i<j){
i=j-i-1;
}
long int u=i,v=j,d=0,k=0,e=0,l=0,q=0,w=0,f=0,g=0;
int m=0,n=0;
k=u;
l=v;
while(u>=0){
q=q+power(u)*ar[u];
  u--;
}
while(q!=0){
d=q%10;
e=e+power(k-1)*d;
q=q/10;
k--;
_delay_ms(80);
}
far[m]=e;
                              // final number1
while(v>=0){
w=w+power(v)*arr[v];
v--;
}
while(w!=0){
f=w%10;
g=g+power(l-1)*f;
w=w/10;
l--;
_delay_ms(80);
}
farr[n]=g;
m++;
n++;
check();
}
void special(){
lcd_data(0x00);
lcd_data(0x00);
lcd_data(0x00);
lcd_data(0x0A);
lcd_data(0x15);
lcd_data(0x15);
lcd_data(0x0A);
lcd_data(0x00);
}
void check(){
long int sum=0,sub=0,mul=1;
float div=1.00;
if(z==1){
div=far[0]/farr[0];
if(div==0){
lcd_cmd(0xCF);
lcd_string("0");
}
else if(farr[0]==0){
while(1){
lcd_cmd(0x40);
special();
lcd_cmd(0xCE);
lcd_data(0);
}
}
else{
lcd_cmd(0xCF);
lcd_num(div);
}
}
else if(z==2){
mul=far[0]*farr[0];
if(mul==0){
lcd_cmd(0xCF);
lcd_string("0");
}
else{
lcd_cmd(0xCF);
lcd_num(mul);
}
}
else if(z==3){
sub=far[0]-farr[0];
if(sub==0){
lcd_cmd(0xCF);
lcd_string("0");
}
else if(sub<0){
lcd_cmd(0x8F);
lcd_string("-");
lcd_cmd(0xCF);
lcd_num(farr[0]-far[0]);
}
else {
lcd_cmd(0xCF);
lcd_num(sub);
}
}
else if(z==4){
sum=far[0]+farr[0];
if(sum==0){
lcd_cmd(0xCF);
lcd_string("0");
}
else{
lcd_cmd(0xCF);
lcd_num(sum);
}}
else {
lcd_cmd(0xC3);
lcd_string("NA");
}
}
void keypad(){
PORTD=0x0F;
PORTB=0x0E;
if(PIND==0x0E)
{
lcd_cmd(0x80+i);
lcd_string("7");
_delay_ms(250);
ar[i]=7;
if(z!=0){
arr[j]=7;
j++;
}
i++;
}
else if(PIND==0x0D){
lcd_cmd(0x80+i);
lcd_string("4");
_delay_ms(250);
ar[i]=4;
if(z!=0){
arr[j]=4;
j++;
}
i++;
}
else if(PIND==0x0B){
lcd_cmd(0x80+i);
lcd_string("1");
_delay_ms(250);
ar[i]=1;
if(z!=0){
arr[j]=1;
j++;
}
i++;
}
else if(PIND==0x07 ){
lcd_cmd(0x01);
_delay_ms(100);
i=0;
z=0;
j=0;
inc=0;
}
PORTD=0x0F;
PORTB=0x0D;
if(PIND==0x0E ){
lcd_cmd(0x80+i);
lcd_string("8");
_delay_ms(250);
ar[i]=8;
if(z!=0){
arr[j]=8;
j++;
}
i++;
}
else if(PIND==0x0D){
lcd_cmd(0x80+i);
lcd_string("5");
_delay_ms(250);
ar[i]=5;
if(z!=0){
arr[j]=5;
j++;
}
i++;
}
else if(PIND==0x0B){
lcd_cmd(0x80+i);
lcd_string("2");
_delay_ms(250);
ar[i]=2;
if(z!=0){
arr[j]=2;
j++;
}
i++;
}
else if(PIND==0x07){
lcd_cmd(0x80+i);
lcd_string("0");
_delay_ms(250);
ar[i]=0;
if(z!=0){
arr[j]=0;
j++;
}
i++;
}
PORTD=0x0F;
PORTB=0x0B;
if(PIND==0x0E ){
lcd_cmd(0x80+i);
lcd_string("9");
_delay_ms(250);
ar[i]=9;
if(z!=0){
arr[j]=9;
j++;
}
i++;
}
else if(PIND==0x0D ){
lcd_cmd(0x80+i);
lcd_string("6");
_delay_ms(200);
ar[i]=6;
if(z!=0){
arr[j]=6;
j++;
}
i++;
}
else if(PIND==0x0B){
lcd_cmd(0x80+i);
lcd_string("3");
_delay_ms(250);
ar[i]=3;
if(z!=0){
arr[j]=3;
j++;
}
i++;
}
else if(PIND==0x07){
lcd_cmd(0xC1);
lcd_string("=");
operate();
}
PORTD=0x0F;
PORTB=0x07;
if(PIND==0x0E){
lcd_cmd(0x80+i);
lcd_string("/");
_delay_ms(250);
z=1;
op[inc]=1;
inc++;
i++;

}
else if(PIND==0x0D ){
lcd_cmd(0x80+i);
lcd_string("x");
_delay_ms(250);
z=2;
op[inc]=2;
inc++;
i++;

}
else if(PIND==0x0B){
lcd_cmd(0x80+i);
lcd_string("-");
_delay_ms(250);
z=3;
op[inc]=3;
inc++;
i++;

}
else if(PIND==0x07){
lcd_cmd(0x80+i);
lcd_string("+");
_delay_ms(250);
z=4;
op[inc]=4;
inc++;
i++;

}
}
void main()
{

DDRC=0xFF;
initialize();
DDRD=0x00;
DDRB=0xFF;
while(1){
keypad();
}}


  • Hardware information: 
PortD= Row of keypad (PD0-A, PD1-B, PD2-C, PD3-D);
portB=Column of keypad (PB0-1, PB1-2, PB2-3, PB3-4);
portC= LCD Display
{
 pc0=RS
pc1=R/W
pc2=E

pc4-pc7 = Data pins (D4-D7)
 

  • Proteus Simulation:
 
 
Simulation of Calculator
 
 
  •  Constraints: 
1) Only displays integer
2) Small delay 
3) To correct clear the whole screen

  • Conclusion: 
Calculator has been created using C programming in Avr studio software and simulation of the same in proteus software. The simulation picture is shown above and the constraints have been noted.

Comments

Popular posts from this blog

Python Ping Pong Game

Game: Ping Pong Libraries used: 1) pygame  2) Math 3) Random Version: Python 3.7 Python Code: -------------------------------------------------------------------------------------------------------------------------- import pygame import math import random pygame.init() dis=pygame.display.set_mode((600,400)) gameover=False white=(255,255,255) black=(0,0,0) red=(255,0,0) blue=(0,0,255) col=(0,255,0) x1=575 y1=200 dx1=0 dy1=0 x2=25 y2=185 dx2=1 dy2=0 c=0 t=0 font_style = pygame.font.SysFont(None, 50) def message(msg,color):     mesg = font_style.render(msg, True, color)     dis.blit(mesg, [300, 200]) font_style1 = pygame.font.SysFont(None, 30)    def show(msg,color):     mesg = font_style1.render(msg, True, color)     dis.blit(mesg, [300, 10])    clock=pygame.time.Clock() while not gameover:     for event in pygame.event.get():       ...

The Projectile Motion

GRAVITY This is a projectile motion simulator. It takes velocity and angle as input and gives an animated projectile motion with time of flight and range Time of flight: seconds Max height: meters Select option: Velocity(m/s): Angle: degrees Action

Lighting of a marriage garden

Aim: To design a basic lighting system for a standard marriage garden. Software: Autodesk Revit 2019 Plan of the Garden: Entrance: Stage: Food Area: 3D View of Stage: