The project is carried out by Hsu Ruoyang and I right after we enter the college together from the same high school. He was the leader of the workshop (you can find us in DIY-CNC-Laser-Engraver), and also a great hacker friend of mine.
The task
Design and build a simple calculator. The calculator should allow input of numbers and operators via a keypad and display the result on a seven‑segment display or an LCD.
Requirements
Basic functions
- Display: Light up the seven‑segment display or LCD and show four different digits simultaneously.
- Keypad Scanning: Scan the keypad and display the pressed key’s label.
- Basic Integer Operations: Implement addition, subtraction, multiplication, and division for integers in the range of –999 to 999.
- Error Handling: When the operation goes out of range or encounters an error, display an error prompt and allow the user to exit the error state.
Optional features
- Non‑volatile Storage: Store one number so that it is retained even after power is turned off.
- Decimal Operations: Implement basic arithmetic operations for decimal numbers
- Higher Precision: Support at least 6 significant digits or higher.
- Equation Solving: Solve the equation a·X + e^X = b with a precision of 0.01;
- The given equation has only one solution.
- Constraints: a > 0, b > 0, 0 < X < 20.
- Values a and b are input from the keypad.
- The solving time must not exceed 1 second.
- Other Extensions: For example, build a custom minimal system, support more digits, or add more operations.
Additional Notes
- A commercially available development board is allowed.
- Basic operations refer to addition, subtraction, multiplication, and division.
- The calculator must support continuous input: when a new operator is pressed, it should automatically compute the previous result and use it as the first operand for the next operation. Operator precedence is not required.
Solution
1. System Proposal
The system mainly consists of a microcontroller module, display module, power supply module, input module, and a few other peripheral components. The following sections describe and justify the selection of each module.
1.1 Microcontroller Module – Analysis and Selection
Option 1: STC89C52 as the main controller.
Its operating speed is relatively low, leading to slower system response.
Option 2: ATMEGA328P-PU as the main controller.
This microcontroller runs faster with stronger computing performance, and our team already has development experience on this platform. After analysis, ATMEGA328P-PU is more suitable for this project. It has a large global developer base, abundant documentation, and extensive third‑party support. It can be easily built into a minimal system and applied in various fields.
We use Atmel Studio 7 as the integrated development environment for coding.
Chosen: Option 2.
1.2 Display Module – Analysis and Selection
Option 1: 1602 LCD.
Limited display content, many pins, large module size, not suitable for miniaturization.
Option 2: 12864 LCD.
Displays more content and supports richer functions but still uses many pins, large size, inconvenient.
Option 3: OLED display.
When using I²C interface, it only occupies two pins, delivers excellent display quality, high dot density, compact size, and low cost. With the Universal 8bit Graphics Library, driving an OLED is straightforward, allowing more effort to be focused on UI development.
Chosen: Option 3.
1.3 Power Supply Module – Analysis and Selection
All modules support a wide input voltage (3.3 – 5 V).
Option 1: Powered via USB.
Stable voltage but requires an external power source, conflicting with portability.
Option 2: Powered by dry batteries.
Replaceable and convenient but environmentally unfriendly, heavy, and bulky, reducing portability.
Option 3: Powered by lithium battery.
High energy density, long life, lightweight and compact, improving aesthetics and portability. Pairing with a common lithium charging/protection module enhances safety and usability.
Chosen: Option 3.
1.4 Input Module and Other Components
A 4×4 matrix keypad is used, supplemented by one toggle switch, a multifunction key, and a multilevel clear key. Other components include a buzzer and LEDs.
The product uses a two‑board stack design, connected at the corners with double‑pass copper pillars. The lower board carries the charging and protection circuits and the lithium battery, connected to the upper main board via pin headers.
2. Theoretical Analysis and Calculation
2.1 Equation-Solving Algorithm
Given the strong computing capability of our chosen MCU, we can directly use the bisection method to solve the equation a·X + e^X = b with a precision of 0.01 (a > 0, b > 0) well within one second.
3. Circuit and Software Design
3.1 System Composition
3.1.1 System Overview
Controller: ATMEGA328P-PU
Display: 0.96‑inch OLED
Keypad: Matrix keypad with two independent keys
Buzzer: Active type
Communication: MCU communicates with OLED via I²C (A4/A5 pins)
3.2 Circuit Schematics
3.2.1 MCU and Peripherals:
ATMEGA328P-PU minimal system with 16 MHz crystal and two 22 pF ceramic capacitors.
4×4 keypad occupying 8 pins, plus two independent keys.
OLED via I²C interface.
3.2.2 Lithium Battery Charging Circuit:
TP4056 typical application circuit with Micro‑USB input. Red LED for charging, green LED for charge completion.
3.2.3 PCB Design:
Optimized layout using onboard jumpers, minimal fly wires. Both PCB layout and wiring diagrams are prepared for clarity and future fabrication.
3.3 Program Design
3.3.1 Features and Design Ideas
Visualization:
-
Drives 128×64 monochrome OLED, supports ASCII, Chinese characters, bitmaps, and dynamic graphics.
-
User‑friendly interface with modern design.
-
Supports animations and dense numeric display.
-
Provides a guided startup interface.
Peripheral Control:
-
Active buzzer and status LEDs for key feedback, breathing effects, and click flash.
-
PWM control for LED brightness.
-
Key debouncing.
-
Backspace key with dual function: short press to delete last digit, long press to clear entire screen with audible feedback.
-
Buzzer can be toggled off.
-
Configurable precision of displayed results.
-
All settings are stored in non‑volatile memory.
Computation & Input:
-
Clear screen layout: previous result and current input separated, dedicated operator display.
-
Prevents numbers from starting with multiple zeros.
-
Supports decimal arithmetic and input validation (single decimal point).
-
Limits input length to prevent exceeding precision.
-
Repeated operator input overwrites previous operator.
-
Starting a new calculation after result is shown is seamless.
-
Advanced operations: negation, absolute value, square, cube, square root, cube root, e^x, ln(x).
-
Error handling with prompt and recovery without MCU reset.
-
Keypad scanning shows key labels.
-
Supports 7 significant digits of precision.
-
Non‑volatile storage of one 3‑digit number (0–999).
-
Solves aX + e^X = b with 0.00001 precision (a>0,b>0).
-
Menu navigation supports step‑by‑step return.
3.3.2 Program Flowchart
(See attached diagrams in the original document)