Project 005: Simple Integer Calculator

You will implement a simple integer calculator using C language in this project. The calculator will have the following operators: addition, subtraction, multiplication, and division.

SimpleIntegerCalculator

Required Components List

Component/DeviceDescriptionQuantity
CharacterLcdDisplay s 4 x 20 Character LCD Module × 1
MatrixKeypad 4x4 64 4 x 4 Matrix Keypad × 1
LED Green 64 10K ohm Potentiometer (Variable Resistor) × 1

Circuit

Pin configurations

DevicePort.Pin Signal TypeModuleDirectionDrive Mode

You will use the following typedef enum to define the operators:

typedef enum CAL_OPS{
    CAL_ADD,
    CAL_SUB,
    CAL_MUL,
    CAL_DIV,
    CAL_EQU,
    CAL_CLR
} CAL_OPS;

You will also use a struct to define the variables of the calculator, as follows:

typedef struct CALULATOR{
    int     currValue;
    int     result;
    CAL_OPS opCode;
} CALCULATOR;

You will declare a global variable of the calculator as follows


CALCULATOR  Cal;

You will implement the following functions to handle the calculator response:

  • void Cal_DigitIn(int din)
    This function will take an integer digit (din) ranging from 0 to 9 as input. It will add the din to the current value (Cal.currValue) and display it on the LCD screen.
  • void Cal_OperatorIn(CAL_OPS op)
    This function will take an operator code (op) as input. The behavior depends on the op value:
    • Equal operator (CAL_EQU):
      • Call the Cal_doCalculate() function to compute the result.
      • Reset the current value (Cal.currValue) to 0.
      • Set the operator code (Cal.opCode) to CAL_ADD.
    • Clear operator (CAL_CLR):
      • Clear both Cal.result and Cal.currValue to zero.
      • Set the operator code (Cal.opCode) to CAL_ADD.
    • Arithmetic operators (CAL_ADD, CAL_SUB, CAL_MUL, CAL_DIV):
      • Call the Cal_doCalculate() function to perform the calculation.
      • Set the operator code (Cal.opCode) to the new input operator code (op).
      • Reset the current value (Cal.currValue) to 0.
    • Finally, it will display the result (Cal.result) on the LCD screen.
    • For example, if the current value is 5, the current operator code is CAL_ADD, and the input operator code is CAL_SUB, the function will perform the following calculation: result = result + currValue = 0 + 5 = 5. Then, it will reset the current value to 0 and set the operator code to CAL_SUB. The LCD screen will display the result 5.
  • void Cal_doCalculate()
    This function performs the specified calculation using the current operator code (Cal.opCode) and the current value (Cal.currValue). It then updates the operation's result with the formula: result = result opCode currValue.

After setting up the peripherals in your embedded code, call the Cal_Clear() function to initialize the calculator. In the infinite loop, read the keypad every 20ms using the ReadKeypad() function.

  • If the ReadKeypad() function returns a digit key (0 to 9), call the Cal_DigitIn() function with the corresponding digit as the input parameter.
  • If the ReadKeypad() function returns the A key, call the Cal_OperatorIn() function with the CAL_ADD operator code as the input parameter.
  • If the ReadKeypad() function returns the B key, call the Cal_OperatorIn() function with the CAL_SUB operator code as the input parameter.
  • If the ReadKeypad() function returns the C key, call the Cal_OperatorIn() function with the CAL_MUL operator code as the input parameter.
  • If the ReadKeypad() function returns the D key, call the Cal_OperatorIn() function with the CAL_DIV operator code as the input parameter.
  • If the ReadKeypad() function returns the * key, call the Cal_OperatorIn() function with the CAL_CLR operator code as the input parameter.
  • If the ReadKeypad() function returns the # key, call the Cal_OperatorIn() function with the CAL_EQU operator code as the input parameter.

You can create a simple integer calculator that can perform basic arithmetic operations by implementing these functions. Good luck!

© 2024 Air Supply Information Center (Air Supply BBS)