I want to write c program for calculator but I think at this time I don't have capabilities to writes c program for calculator. so for deep understanding I started with addition only .. I want to write c program that can do addition like below example
Example 1+2 =3, 2+1=3, 12+1=13, 1+12=13, 21+2=23, 21+1=22..... and so on
consider I have 8051 LCD and four switches to perform addition
Switch 1 = "1"
switch 2 = "+"
Switch 3 = "2"
Switch 4 = "="
if switch 1 is pressed than display number '1" on LCD.
if switch 2 is pressed than display operator "+" on LCD.
if switch 3 is pressed than display operator "2" on LCD.
if switch 4 is pressed than display"result" on LCD
basic steps
1) get the first argument, maybe one or digits
2) get the operator '+'
3) get the second argument maybe one or digits
4) get another operator '=' then perform the operation
Look at this program. This program display "1" when I press switch 1 and display "2" when I press switch 2
Now how to write c program for addition ?
Note: I have replaced keypad with switch for easy understanding
Example 1+2 =3, 2+1=3, 12+1=13, 1+12=13, 21+2=23, 21+1=22..... and so on
consider I have 8051 LCD and four switches to perform addition
Switch 1 = "1"
switch 2 = "+"
Switch 3 = "2"
Switch 4 = "="
if switch 1 is pressed than display number '1" on LCD.
if switch 2 is pressed than display operator "+" on LCD.
if switch 3 is pressed than display operator "2" on LCD.
if switch 4 is pressed than display"result" on LCD
basic steps
1) get the first argument, maybe one or digits
2) get the operator '+'
3) get the second argument maybe one or digits
4) get another operator '=' then perform the operation
Look at this program. This program display "1" when I press switch 1 and display "2" when I press switch 2
C:
#include<reg51.h>
/* Data pins connected to port P1 of 8051 */
#define Data_Port_Pins (P1)
sbit Register_Select_Pin = P2^0; /* Register Pin of LCD connected to Pin 0 of Port P2 */
sbit Read_Write_Pin = P2^1; /* Read/Write Pin of LCD connected to Pin 1 of Port P2 */
sbit Enable_Pin = P2^2; /* EN pin connected to pin 2 of port P2 */
sbit Switch1 = P3^0; /* switch 1 connected to port pin P3.0 */
sbit Switch2 = P3^3; /* switch 2 connected to port pin P3.3 */
sbit Switch3 = P3^4;
sbit Switch4 = P3^5;
/* Function for creating delay in milliseconds */
void Delay(unsigned int wait)
{
unsigned i, j;
for(i = 0; i < wait; i++)
for(j = 0; j < 1200; j++);
}
/* Function to send command instruction to LCD */
void LCD_Command (unsigned char cmd)
{
Data_Port_Pins = cmd;
Register_Select_Pin =0;
Read_Write_Pin=0;
Enable_Pin =1;
Delay (2);
Enable_Pin =0;
}
/* Function to send display data to LCD */
void LCD_Data (unsigned char Data)
{
Data_Port_Pins = Data;
Register_Select_Pin=1;
Read_Write_Pin=0;
Enable_Pin =1;
Delay(2);
Enable_Pin =0;
}
/* Function to prepare the LCD and get it ready */
void LCD_Initialization()
{
LCD_Command(0x38);
Delay(20);
LCD_Command(0x0e);
Delay(20);
LCD_Command(0x06);
Delay(20);
LCD_Command(0x01);
Delay(20);
}
/* Function to send string to LCD */
void LCD_String (unsigned char *string)
{
int i=0;
while(string[i]!='\0')
{
LCD_Data(string[i]);
i++;
Delay(10);
}
return;
}
void main()
{
unsigned char string1[] ="1";
unsigned char string2[] ="2";
LCD_Initialization();
while(1)
{
if (Switch1 == 1)
{
LCD_Command(0x82);
LCD_String(string1);
Delay(500);
LCD_Command(0x01);
}
else if (Switch2 == 1)
{
LCD_Command(0x82);
LCD_String(string2);
Delay(500);
LCD_Command(0x01);
Delay(10);
}
else
{
LCD_Command(0x01);
}
}
}
Note: I have replaced keypad with switch for easy understanding