tak_iiec@yahoo.com
Newbie level 6
Dear All I have made a simple calculator using pic16F877A in Mikroc. Now I want to improve it. I want to do operations on string instead of single single digit. Like 220*450, 556+778 etc.
earlier its is just for single digit like 4+8, 9-4 etc.
pls help me in this regard and Also I want to make functions of arithmetic operations. Do help me plz.
My Complete code is give below.
// Simple Calculator by Tanvir Ahmad
char txt[2];
int kp;
// Keypad module connections
char keypadPort at PORTD;
// LCD connections definitions
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// MAIN
void main() {
int Calc, Op1, Op2, txt1;
Keypad_Init(); // Initialize Keypad
Lcd_Init(); // Initialize Lcd
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1, 3, "Welcome to"); // Write message text on Lcd
Lcd_Out(2, 8, "UES"); // Write message text on Lcd
delay_ms(2000) ;
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1, 5, "Simple"); // Write message text on Lcd
Lcd_Out(2, 3, "Calculator"); // Write message text on Lcd
delay_ms(3000) ;
Lcd_Cmd(_LCD_CLEAR);
start:
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1, 1, "Key1:"); // Write message text on Lcd
Lcd_Out(2, 1, "Key2:"); // Write message text on Lcd
Lcd_Out(1,12, "k3:");
while(1){
kp = 0; // Reset key code variable
do
kp = Keypad_Key_Click(); // Store key code in kp variable
while (!kp);
switch (kp) {
case 9: kp = 49; break; // 1
case 10: kp = 50; break; // 2
case 11: kp = 51; break; // 3
case 5: kp = 52; break; // 4
case 6: kp = 53; break; // 5
case 7: kp = 54; break; // 6
case 1: kp = 55; break; // 7
case 2: kp = 56; break; // 8
case 3: kp = 57; break; // 9
case 14: kp = 48; break; // 0
}
Lcd_Chr(1, 8, kp);
delay_ms(200);
{
op1 = 0; // Reset key code variable
do
op1 = Keypad_Key_Click(); // Store key code in kp variable
while (!op1);
switch (op1) {
case 9: op1 = 49; break; // 1
case 10: op1 = 50; break; // 2
case 11: op1 = 51; break; // 3
// case 4: op1 = 47; break; // /
case 5: op1 = 52; break; // 4
case 6: op1 = 53; break; // 5
case 7: op1 = 54; break; // 6
case 1: op1 = 55; break; // 7
case 2: op1 = 56; break; // 8
case 3: op1 = 57; break; // 9
case 14: op1 = 48; break; // 0
}
Lcd_Chr(2, 8, op1);
}
{
op2=0;
do
op2 = Keypad_Key_Click(); // Store key code in kp variable
while (!op2);
switch (op2) {
case 16: Op2= 43; break; // +
case 12: Op2= 45; break; // -
case 8: Op2= 42; break; // *
case 4: Op2= 47; break; // /
//case 15: Op2= 61; break; // =
}
Lcd_Chr(2, 12, op2);
//Addition
if (op2==43) {
Op1 = Op1-48;
kp = kp-48;
calc = kp+op1;
intToStr(calc, txt); // Transform Calc value to string
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,2, " Result is :");
Lcd_Out(2, 10, txt); // Display Calc value on Lcd
}
// Subtraction
if (op2==45) {
Op1 = Op1-48;
kp = kp-48;
calc = kp-op1;
if(op1>kp){
Calc=Op1-kp ;
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(2,9,"-");
WordToStr(calc, txt); // Transform Calc value to string
// Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,2, " Result is :");
Lcd_Out(2, 10, txt);
delay_ms(1200);
Lcd_Cmd(_LCD_CLEAR);
goto start;
}
WordToStr(calc, txt); // Transform Calc value to string
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,2, " Result is :");
Lcd_Out(2, 10, txt); // Display Calc value on Lcd
}
// Multiplication
if (op2==42) {
Op1 = Op1-48;
kp = kp-48;
calc = kp*op1;
WordToStr(calc, txt); // Transform Calc value to string
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,2, " Result is :");
Lcd_Out(2, 10, txt); // Display Calc value on Lcd
}
// Division
if (op2==47) {
Op1 = Op1-48;
kp = kp-48;
calc = kp/op1;
WordToStr(calc, txt); // Transform Calc value to string
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,2, " Result is :");
Lcd_Out(2, 10, txt); // Display Calc value on Lcd
}
delay_ms(1000);
goto start;
}
}
}
earlier its is just for single digit like 4+8, 9-4 etc.
pls help me in this regard and Also I want to make functions of arithmetic operations. Do help me plz.
My Complete code is give below.
// Simple Calculator by Tanvir Ahmad
char txt[2];
int kp;
// Keypad module connections
char keypadPort at PORTD;
// LCD connections definitions
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// MAIN
void main() {
int Calc, Op1, Op2, txt1;
Keypad_Init(); // Initialize Keypad
Lcd_Init(); // Initialize Lcd
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1, 3, "Welcome to"); // Write message text on Lcd
Lcd_Out(2, 8, "UES"); // Write message text on Lcd
delay_ms(2000) ;
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1, 5, "Simple"); // Write message text on Lcd
Lcd_Out(2, 3, "Calculator"); // Write message text on Lcd
delay_ms(3000) ;
Lcd_Cmd(_LCD_CLEAR);
start:
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1, 1, "Key1:"); // Write message text on Lcd
Lcd_Out(2, 1, "Key2:"); // Write message text on Lcd
Lcd_Out(1,12, "k3:");
while(1){
kp = 0; // Reset key code variable
do
kp = Keypad_Key_Click(); // Store key code in kp variable
while (!kp);
switch (kp) {
case 9: kp = 49; break; // 1
case 10: kp = 50; break; // 2
case 11: kp = 51; break; // 3
case 5: kp = 52; break; // 4
case 6: kp = 53; break; // 5
case 7: kp = 54; break; // 6
case 1: kp = 55; break; // 7
case 2: kp = 56; break; // 8
case 3: kp = 57; break; // 9
case 14: kp = 48; break; // 0
}
Lcd_Chr(1, 8, kp);
delay_ms(200);
{
op1 = 0; // Reset key code variable
do
op1 = Keypad_Key_Click(); // Store key code in kp variable
while (!op1);
switch (op1) {
case 9: op1 = 49; break; // 1
case 10: op1 = 50; break; // 2
case 11: op1 = 51; break; // 3
// case 4: op1 = 47; break; // /
case 5: op1 = 52; break; // 4
case 6: op1 = 53; break; // 5
case 7: op1 = 54; break; // 6
case 1: op1 = 55; break; // 7
case 2: op1 = 56; break; // 8
case 3: op1 = 57; break; // 9
case 14: op1 = 48; break; // 0
}
Lcd_Chr(2, 8, op1);
}
{
op2=0;
do
op2 = Keypad_Key_Click(); // Store key code in kp variable
while (!op2);
switch (op2) {
case 16: Op2= 43; break; // +
case 12: Op2= 45; break; // -
case 8: Op2= 42; break; // *
case 4: Op2= 47; break; // /
//case 15: Op2= 61; break; // =
}
Lcd_Chr(2, 12, op2);
//Addition
if (op2==43) {
Op1 = Op1-48;
kp = kp-48;
calc = kp+op1;
intToStr(calc, txt); // Transform Calc value to string
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,2, " Result is :");
Lcd_Out(2, 10, txt); // Display Calc value on Lcd
}
// Subtraction
if (op2==45) {
Op1 = Op1-48;
kp = kp-48;
calc = kp-op1;
if(op1>kp){
Calc=Op1-kp ;
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(2,9,"-");
WordToStr(calc, txt); // Transform Calc value to string
// Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,2, " Result is :");
Lcd_Out(2, 10, txt);
delay_ms(1200);
Lcd_Cmd(_LCD_CLEAR);
goto start;
}
WordToStr(calc, txt); // Transform Calc value to string
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,2, " Result is :");
Lcd_Out(2, 10, txt); // Display Calc value on Lcd
}
// Multiplication
if (op2==42) {
Op1 = Op1-48;
kp = kp-48;
calc = kp*op1;
WordToStr(calc, txt); // Transform Calc value to string
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,2, " Result is :");
Lcd_Out(2, 10, txt); // Display Calc value on Lcd
}
// Division
if (op2==47) {
Op1 = Op1-48;
kp = kp-48;
calc = kp/op1;
WordToStr(calc, txt); // Transform Calc value to string
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,2, " Result is :");
Lcd_Out(2, 10, txt); // Display Calc value on Lcd
}
delay_ms(1000);
goto start;
}
}
}