Skip to content

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.

License

Notifications You must be signed in to change notification settings

SinceraXY/Comprehensive_Hardware_Design_Practice

Repository files navigation

Hardware Design Practice: 8051 Fixed-Point Expression Calculator

中文说明(README.zh-CN.md)

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.


Features

  • 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)
  • Expression evaluation
    • Supports + - * /, parentheses ( ), operator precedence (*/ > +-)
    • Supports unary plus/minus (e.g. -3+2, -(1+2))
  • 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

Quick demo

Type an expression and press =:

  • 6.31*3.4521.7695
  • 1/160.0625
  • 1/20.5
  • (1+2)*39

Note: Up to 4 decimals are displayed and zeros are not padded (e.g. 1.2300 is shown as 1.23).


Hardware & pin mapping (as implemented in code)

If your board wiring differs, update the macros and pin definitions in code/main.c.

LCD1602 (8-bit parallel)

  • Data bus: P0 (LCD_PORT)
  • Control pins:
  • RS = P2.6
  • RW = P2.5
  • EN = P2.7

Keypad input

  • Matrix keypad: P1 (low nibble drives rows, high nibble reads columns)
  • Single keys: P3.0~P3.3 (active-low)

Key mapping

4×4 matrix keypad (P1)

The layout is defined by key_map in main.c:

1   2   3   +
4   5   6   -
7   8   9   *
(   0   )   /

4 single keys (P3.0~P3.3)

  • P3.0 (K1)C: clear
  • P3.1 (K2)D: backspace
  • P3.2 (K3).: decimal point
  • P3.3 (K4)=: evaluate

Fixed-point arithmetic

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.31 is stored as 63100
    • 3.45 is stored as 34500

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.


Build & flash

Option A: Keil uVision3 (recommended)

  1. Open the Keil project:
    • code/code.uv2
  2. Confirm target chip settings (commonly AT89C51 / 12MHz).
  3. Build to generate HEX:
    • Output is typically code/code.hex

Option B: use the prebuilt HEX

The repo already contains:

  • code/code.hex

Serial ISP flashing (PZ-ISP)

tools/ contains common flashing tools and USB-UART drivers (e.g. PZ-ISP-for64位.exe, CH341/PL2303 drivers).

Typical steps:

  1. Install the correct USB-UART driver (CH341 or PL2303 depending on your board).
  2. Open PZ-ISP, select the COM port and baud rate.
  3. Choose the HEX file (e.g. code/code.hex).
  4. 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.


Project structure

.
├─ 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安装包(仅备份/学习用途)

FAQ

1) LCD shows garbled characters / nothing is displayed

  • Check LCD power, contrast potentiometer, and RW/RS/EN wiring.
  • This project uses P0 for data and P2.5/2.6/2.7 for control. Update macros if your wiring differs.

2) Keys do not respond / wrong key mapping

  • 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.

3) Decimal results look wrong

  • This project uses fixed-point numbers with SCALE=10000.
  • If you change SCALE, re-check overflow risks in multiplication/division.

License

This project is licensed under the Apache-2.0 license. See LICENSE for details.


Acknowledgements

  • Keil C51 / uVision toolchain
  • LCD1602 & 8051 reference materials / course handouts

About

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.

Topics

Resources

License

Stars

Watchers

Forks