0% found this document useful (0 votes)
0 views1 page

Microcontroller Cp Merged-10

The document outlines a program for a basic calculator that accepts two numbers and an operator from a keypad. It performs the specified arithmetic operation and displays the result on an LCD, handling division by zero errors. The program also includes a feature to clear the display upon pressing 'C'.

Uploaded by

shashankcm0313
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views1 page

Microcontroller Cp Merged-10

The document outlines a program for a basic calculator that accepts two numbers and an operator from a keypad. It performs the specified arithmetic operation and displays the result on an LCD, handling division by zero errors. The program also includes a feature to clear the display upon pressing 'C'.

Uploaded by

shashankcm0313
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

key_press = keypad_scan();

if(key_press >= '0' && key_press <= '9') {


lcd_data(key_press);
num1 = num1 * 10 + (key_press - '0');
delay_ms(300);
} else if(key_press == '+' || key_press == '-' || key_press == 'x' || key_press == '/') {
lcd_data(key_press);
op = key_press;
delay_ms(300);
break;
}
}

// Input second number


num2 = 0;
while(1) {
key_press = keypad_scan();
if(key_press >= '0' && key_press <= '9') {
lcd_data(key_press);
num2 = num2 * 10 + (key_press - '0');
delay_ms(300);
} else if(key_press == '=') {
lcd_data('=');
delay_ms(300);
break;
}
}

// Perform calculation
switch(op) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case 'x': result = num1 * num2; break;
case '/':
if(num2 != 0)
result = num1 / num2;
else
result = 0; // handle div by zero
break;
default: result = 0;
}

// Display result
lcd_cmd(0xC0); // second line
if(result == 0 && op == '/' && num2 == 0) {
lcd_print("Error");
} else {
char str[6]; // support up to 5 digits + null
int_to_str(result, str);
lcd_print(str);
}

// Wait for 'C' to clear


while(1) {
key_press = keypad_scan();
if(key_press == 'C') {
lcd_cmd(0x01); // clear LCD
delay_ms(300);
break;
}
}
}
}

Dept of ECE, BIET, Davanagere 8|Page

You might also like