This repository is a course project for China University of Petroleum (Beijing). It implements an expression calculator on AT89C51 (8051 MCU) + LCD1602 + a 4×5 keypad input (4×4 matrix + 4 single keys). The calculator supports decimals and parentheses.
This repo includes the Keil project, a ready-to-flash HEX, the board schematic, and common ISP/download tools/drivers.
- Input (4×5)
- P1: 4×4 matrix keypad (digits/operators/parentheses)
- P3.0~P3.3: 4 single keys (Clear/Backspace/Decimal/Evaluate)
- LCD1602 UI
- Line 1 shows the current expression (
IN:prefix, tail view when too long) - Line 2 shows a hint or the result (
=prefix)
- Line 1 shows the current expression (
- Expression evaluation
- Supports
+ - * /, parentheses(), operator precedence (*/>+-) - Supports unary plus/minus (e.g.
-3+2,-(1+2))
- Supports
- Fixed-point arithmetic (no float library)
SCALE = 10000(internal value = real value × 10000)- Display: up to 4 decimal digits, trailing zeros trimmed (no padding)
- Error messages
- Division by zero:
DIV0 - Invalid expression:
ERR
- Division by zero:
Type an expression and press =:
6.31*3.45→21.76951/16→0.06251/2→0.5(1+2)*3→9
Note: Up to 4 decimals are displayed and zeros are not padded (e.g. 1.2300 is shown as 1.23).
If your board wiring differs, update the macros and pin definitions in
code/main.c.
- Data bus:
P0(LCD_PORT) - Control pins:
RS = P2.6RW = P2.5EN = P2.7
- Matrix keypad:
P1(low nibble drives rows, high nibble reads columns) - Single keys:
P3.0~P3.3(active-low)
The layout is defined by key_map in main.c:
1 2 3 +
4 5 6 -
7 8 9 *
( 0 ) /
P3.0 (K1)→C: clearP3.1 (K2)→D: backspaceP3.2 (K3)→.: decimal pointP3.3 (K4)→=: evaluate
To reduce code size and runtime overhead on 8051, this project avoids float and uses fixed-point numbers:
- Internal integer
X = real_value × SCALE - With
SCALE = 10000, for example:6.31is stored as631003.45is stored as34500
Note: When SCALE is large, naive a*b or a*SCALE may overflow 32-bit long. This project implements safer * and / in apply_op_scaled() using decomposition/remainder-based methods.
- Open the Keil project:
code/code.uv2
- Confirm target chip settings (commonly AT89C51 / 12MHz).
- Build to generate HEX:
- Output is typically
code/code.hex
- Output is typically
The repo already contains:
code/code.hex
tools/ contains common flashing tools and USB-UART drivers (e.g. PZ-ISP-for64位.exe, CH341/PL2303 drivers).
Typical steps:
- Install the correct USB-UART driver (CH341 or PL2303 depending on your board).
- Open
PZ-ISP, select the COM port and baud rate. - Choose the HEX file (e.g.
code/code.hex). - Put the board into ISP/download mode and click Download.
Entering ISP/download mode depends on your board (jumpers/buttons/power sequence). Please follow your board documentation.
.
├─ README.md
├─ README.zh-CN.md
├─ LICENSE
├─ HC6800EM3 v20原理图(电路图).pdf
├─ 初始化安装与实验内容介绍.docx
├─ code/
│ ├─ main.c # 主程序(LCD、键盘扫描、表达式求值、定点运算)
│ ├─ code.uv2 # Keil uVision3 工程
│ ├─ code.hex # 编译生成的HEX(可直接烧录)
│ ├─ STARTUP.A51 # 启动文件
│ └─ ...(OBJ/LST/中间文件)
└─ tools/
├─ PZ-ISP-for64位.exe # 下载工具
├─ CH341SER.ZIP # CH341驱动
├─ PL-2303.zip # PL2303驱动
└─ Keil3_Full.zip # Keil安装包(仅备份/学习用途)
- Check LCD power, contrast potentiometer, and RW/RS/EN wiring.
- This project uses
P0for data andP2.5/2.6/2.7for control. Update macros if your wiring differs.
- Ensure the matrix keypad is wired as: low nibble = rows (output), high nibble = columns (input) on
P1. - Single keys are active-low on
P3.0~P3.3.
- This project uses fixed-point numbers with
SCALE=10000. - If you change
SCALE, re-check overflow risks in multiplication/division.
This project is licensed under the Apache-2.0 license. See LICENSE for details.
- Keil C51 / uVision toolchain
- LCD1602 & 8051 reference materials / course handouts