21 KiB
Multi-Base Calculator System
Technical Manual and Documentation
Document Version: 1.0 Publication Date: October 24, 2025 Target Platform: NXP LPC1768 ARM Cortex-M3 Microcontroller Document Status: Final Release
Table of Contents
- System Overview
- Hardware Architecture
- Pin Configuration
- Software Architecture
- Functional Specification
- Code Documentation
- Operating Procedures
- Technical Reference
1. System Overview
1.1 Executive Summary
This document describes a multi-base arithmetic calculator implemented on the LPC1768 ARM Cortex-M3 microcontroller. The system performs calculations in Binary (Base 2), Octal (Base 8), Decimal (Base 10), and Hexadecimal (Base 16) number systems.
1.2 System Features
- Four numerical base modes with dynamic switching
- 4×4 matrix keypad for input
- 16×2 LCD display for status and results
- Single-digit 7-segment display for quick reference
- Three arithmetic operations: Addition, Subtraction, Multiplication
- Shift-key operator access mechanism
- Hardware debouncing for stable input
1.3 System Architecture
┌──────────────────────────────────────────┐
│ LPC1768 Microcontroller │
│ (ARM Cortex-M3) │
├──────────┬──────────────┬────────────────┤
│ │ │ │
│ Input │ Processing │ Output │
│ Keypad │ Calculator │ LCD Display │
│ (4×4) │ Logic │ 7-Segment │
│ Button │ Base Conv. │ │
└──────────┴──────────────┴────────────────┘
1.4 Operating Modes
| Mode | Base | Valid Digits | Application |
|---|---|---|---|
| DEC | 10 | 0-9 | General arithmetic |
| BIN | 2 | 0-1 | Digital logic |
| OCT | 8 | 0-7 | Unix permissions |
| HEX | 16 | 0-9, A-F | Memory addressing |
2. Hardware Architecture
2.1 Component Specifications
Microcontroller (LPC1768):
- ARM Cortex-M3 core, 100 MHz
- 512 KB Flash, 32 KB SRAM
- 70 GPIO pins (5V tolerant)
- 3.3V operating voltage
Input Subsystem:
- 4×4 matrix keypad (16 keys)
- Mode selection button (P2.12)
- Software debouncing (9ms effective)
Output Subsystem:
- 16×2 LCD (HD44780-compatible, 4-bit mode)
- 7-segment LED display (single digit)
- Real-time display updates
2.2 System Block Diagram
Input Processing Calculator Core Output System
┌────────────┐ ┌──────────────┐ ┌──────────────┐
│ Keypad │──────────>│ State │───────>│ LCD Display │
│ Scanning │ │ Machine │ │ (16×2) │
│ │ │ │ │ │
│ Mode │──────────>│ Arithmetic │───────>│ 7-Segment │
│ Button │ │ Engine │ │ (Single) │
│ │ │ │ │ │
│ Debounce │ │ Base │ │ Feedback │
│ Logic │ │ Conversion │ │ Messages │
└────────────┘ └──────────────┘ └──────────────┘
3. Pin Configuration
3.1 Connector Overview
The LPC1768 board provides four 10-pin FRC connectors (CNA, CNB, CNC, CND) for peripheral interfacing.
3.2 Connector CNA - 7-Segment Display
Function: Seven-segment LED display driver (P0.4-P0.11)
| Pin | LPC1768 | Port.Bit | Function | Direction |
|---|---|---|---|---|
| 1 | 81 | P0.4 | Segment A | Output |
| 2 | 80 | P0.5 | Segment B | Output |
| 3 | 79 | P0.6 | Segment C | Output |
| 4 | 78 | P0.7 | Segment D | Output |
| 5 | 77 | P0.8 | Segment E | Output |
| 6 | 76 | P0.9 | Segment F | Output |
| 7 | 48 | P0.10 | Segment G | Output |
| 8 | 49 | P0.11 | Decimal Point | Output |
| 10 | - | GND | Ground | - |
7-Segment Encoding Array:
const unsigned char seven_seg[16] = {
0x3F, 0x06, 0x5B, 0x4F, // 0-3
0x66, 0x6D, 0x7D, 0x07, // 4-7
0x7F, 0x6F, 0x77, 0x7C, // 8-B
0x39, 0x5E, 0x79, 0x71 // C-F
};
3.3 Connector CNB - Display Enable and Mode Button
Function: 7-segment enable and mode selection (P1.23, P2.12)
| Pin | LPC1768 | Port.Bit | Function | Direction |
|---|---|---|---|---|
| 1 | 37 | P1.23 | 7-Seg Digit Enable | Output |
| 7 | 51 | P2.12 | Mode Button | Input (Pull-up) |
| 10 | - | GND | Ground | - |
3.4 Connector CNC - Keypad Matrix
Function: 4×4 keypad interface (P0.15-P0.22)
| Pin | LPC1768 | Port.Bit | Function | Direction |
|---|---|---|---|---|
| 1 | 62 | P0.15 | Column 0 | Output |
| 2 | 63 | P0.16 | Column 1 | Output |
| 3 | 61 | P0.17 | Column 2 | Output |
| 4 | 60 | P0.18 | Column 3 | Output |
| 5 | 59 | P0.19 | Row 0 | Input (Pull-up) |
| 6 | 58 | P0.20 | Row 1 | Input (Pull-up) |
| 7 | 57 | P0.21 | Row 2 | Input (Pull-up) |
| 8 | 56 | P0.22 | Row 3 | Input (Pull-up) |
| 10 | - | GND | Ground | - |
Keypad Layout:
Col0 Col1 Col2 Col3
│ │ │ │
Row0──┼──0───┼──1───┼──2───┼──3──
Row1──┼──4───┼──5───┼──6───┼──7──
Row2──┼──8───┼──9───┼──A───┼──B──
Row3──┼──C───┼──D───┼──E───┼──F──
Key Index Calculation: Key = (Column × 4) + Row
3.5 Connector CND - LCD Display
Function: HD44780 LCD interface in 4-bit mode (P0.23-P0.28)
| Pin | LPC1768 | Port.Bit | Function | Direction |
|---|---|---|---|---|
| 1 | 9 | P0.23 | LCD D4 | Output |
| 2 | 8 | P0.24 | LCD D5 | Output |
| 3 | 7 | P0.25 | LCD D6 | Output |
| 4 | 6 | P0.26 | LCD D7 | Output |
| 5 | 25 | P0.27 | LCD RS | Output |
| 6 | 24 | P0.28 | LCD EN | Output |
| 10 | - | GND | Ground | - |
LCD Control Signals:
- RS (Register Select): 0 = Command, 1 = Data
- EN (Enable): Falling edge latches data
- D4-D7: 4-bit data interface
3.6 Pin Mapping Summary
| Function | Pins | Connector | Direction | Count |
|---|---|---|---|---|
| 7-Segment Data | P0.4-P0.11 | CNA | Output | 8 |
| Keypad Columns | P0.15-P0.18 | CNC | Output | 4 |
| Keypad Rows | P0.19-P0.22 | CNC | Input | 4 |
| LCD Data | P0.23-P0.26 | CND | Output | 4 |
| LCD Control | P0.27-P0.28 | CND | Output | 2 |
| 7-Seg Enable | P1.23 | CNB | Output | 1 |
| Mode Button | P2.12 | CNB | Input | 1 |
| Total | - | - | - | 24 |
4. Software Architecture
4.1 System Design Overview
The software follows a polled input, state-based architecture with three primary layers:
- Application Layer: User interface and calculator logic
- Hardware Abstraction: LCD, keypad, and display control functions
- Hardware Access: Direct register manipulation
4.2 State Machine
┌──────────┐
│ INIT │
└─────┬────┘
│
▼
┌──────────┐ ┌─────────────┐
│ IDLE │────>│ DIGIT INPUT │
│ READY │ └──────┬──────┘
└─────┬────┘ │
│ ▼
│ ┌──────────────┐
│ │ ACCUMULATE │
│ │ NUMBER │
│ └──────┬───────┘
│ │
├──────────────────┤
│ │
▼ ▼
┌──────────┐ ┌──────────────┐
│ SHIFT │────> │ OPERATOR │
│ DETECT │ │ PENDING │
└──────────┘ └──────┬───────┘
│ │
▼ ▼
┌──────────┐ ┌──────────────┐
│ MODE │ │ CALCULATE │
│ CHANGE │ │ RESULT │
└──────────┘ └──────────────┘
4.3 Global State Variables
// Calculator state
unsigned int current_base; // Active base (2, 8, 10, 16)
unsigned int input_num; // Current input number
unsigned int stored_num; // First operand
unsigned int operation; // Pending operation (0-3)
unsigned int result; // Calculation result
// Input state
unsigned int key, last_key; // Key scan results
unsigned int stable; // Debounce counter
unsigned int shift_active; // Shift key flag
// Mode button state
unsigned int button_state, last_button_state;
unsigned int button_stable; // Button debounce counter
4.4 Constant Definitions
// Operational modes
#define MODE_BIN 2
#define MODE_OCT 8
#define MODE_DEC 10
#define MODE_HEX 16
// Operations
#define OP_NONE 0
#define OP_ADD 1
#define OP_SUB 2
#define OP_MUL 3
// Hardware pins
#define COL_BASE 15
#define ROW_BASE 19
#define COL_MASK (0x0F << COL_BASE)
#define ROW_MASK (0x0F << ROW_BASE)
#define SEG_SHIFT 4
#define DIGIT_EN (1<<23)
#define LCD_DATA_SHIFT 23
#define LCD_DATA_MASK (0x0F << LCD_DATA_SHIFT)
#define LCD_RS (1<<27)
#define LCD_EN (1<<28)
#define MODE_BUTTON (1<<12)
4.5 Main Program Flow
int main(void){
// Initialize hardware
configure_pins();
configure_gpio();
lcd_init();
// Initial display
display_mode();
display_input();
// Main loop
for(;;){
// Input acquisition
shift_active = is_key0_pressed();
key = scan_keypad();
button_state = scan_mode_button();
// Debouncing
debounce_keypad();
debounce_button();
// Mode change
if(stable_mode_press()){
cycle_mode();
}
// Key processing
if(stable_key_press()){
if(shift_active){
process_operator(key);
} else {
process_digit(key);
}
}
// Display update
update_7segment();
delay(3000); // 3ms scan interval
}
}
5. Functional Specification
5.1 Numerical Base Modes
| Mode | Base | Valid Keys | Input Example | Display |
|---|---|---|---|---|
| DEC | 10 | 0-9 | 123 | "123" |
| BIN | 2 | 0-1 | 1011 | "1011" |
| OCT | 8 | 0-7 | 177 | "177" |
| HEX | 16 | 0-9, A-F | 1A2F | "1A2F" |
Input Accumulation Algorithm:
// For any base:
input_num = input_num * current_base + key_value;
// Example (HEX): A, B → AB
// Step 1: 0 * 16 + 10 = 10
// Step 2: 10 * 16 + 11 = 171 (0xAB)
5.2 Operator Functions
Shift Key Paradigm: Key 0 acts as a modifier for operator access.
| Key Combo | Function | Operation | Display |
|---|---|---|---|
| 0 + B | Addition | stored + input | "Op: +" |
| 0 + C | Clear | Reset all | "Op: CLR" |
| 0 + D | Subtraction | stored - input | "Op: -" |
| 0 + E | Multiplication | stored × input | "Op: *" |
| 0 + F | Equals | Calculate result | "Res: XXX" |
Operation Sequence:
1. Enter first number
2. Hold Key 0 + Press operator (B/D/E)
3. Enter second number
4. Hold Key 0 + Press F (equals)
5. View result on LCD
5.3 Input Processing
Debouncing: Key must be stable for 3 consecutive scans (9ms total).
if(key == last_key){
if(stable < 5) stable++;
} else {
last_key = key;
stable = 0;
}
if(stable == 3 && key != 0xFF){
process_key(key);
stable = 5; // Lock-out
}
Digit Validation:
unsigned int is_valid_digit(unsigned int key){
if(key >= 16) return 0;
if(current_base == MODE_BIN && key >= 2) return 0;
if(current_base == MODE_OCT && key >= 8) return 0;
if(current_base == MODE_DEC && key >= 10) return 0;
return 1; // Valid in HEX
}
6. Code Documentation
6.1 Peripheral Control Functions
Keypad Scanning
unsigned int scan_keypad(void){
unsigned int col, row, row_bits;
for(col = 0; col < 4; col++){
LPC_GPIO0->FIOSET = COL_MASK; // All columns HIGH
delay(50);
LPC_GPIO0->FIOCLR = (1 << (COL_BASE + col)); // Pull column LOW
delay(200);
row_bits = (LPC_GPIO0->FIOPIN & ROW_MASK) >> ROW_BASE;
if(row_bits != 0x0F){ // Key detected
for(row = 0; row < 4; row++){
if((row_bits & (1 << row)) == 0){
LPC_GPIO0->FIOSET = COL_MASK;
return (col * 4) + row;
}
}
}
}
LPC_GPIO0->FIOSET = COL_MASK;
return 0xFF; // No key pressed
}
Shift Key Detection
unsigned int is_key0_pressed(void){
unsigned int row_bits;
// Check Key 0 (Column 0, Row 0)
LPC_GPIO0->FIOSET = COL_MASK;
delay(50);
LPC_GPIO0->FIOCLR = (1 << COL_BASE);
delay(200);
row_bits = (LPC_GPIO0->FIOPIN & ROW_MASK) >> ROW_BASE;
LPC_GPIO0->FIOSET = COL_MASK;
return ((row_bits & 0x01) == 0) ? 1 : 0;
}
6.2 LCD Control Functions
LCD Initialization (4-bit Mode)
void lcd_init(void){
lcd_delay(5000000); // Power-on wait
// 8-bit mode initialization
lcd_write_nibble(0x03, 0);
lcd_delay(500000);
lcd_write_nibble(0x03, 0);
lcd_delay(500000);
lcd_write_nibble(0x03, 0);
lcd_delay(500000);
// Switch to 4-bit mode
lcd_write_nibble(0x02, 0);
lcd_delay(500000);
lcd_cmd(0x28); // 4-bit, 2 lines, 5x8 font
lcd_cmd(0x0C); // Display ON, cursor OFF
lcd_cmd(0x01); // Clear display
lcd_delay(500000);
lcd_cmd(0x06); // Entry mode: increment
}
LCD Communication
void lcd_write_nibble(unsigned char nibble, unsigned char is_data){
unsigned long temp;
temp = (nibble & 0x0F) << LCD_DATA_SHIFT;
LPC_GPIO0->FIOPIN = (LPC_GPIO0->FIOPIN & ~LCD_DATA_MASK) | temp;
if(is_data)
LPC_GPIO0->FIOSET = LCD_RS; // Data mode
else
LPC_GPIO0->FIOCLR = LCD_RS; // Command mode
LPC_GPIO0->FIOSET = LCD_EN; // EN pulse
lcd_delay(100);
LPC_GPIO0->FIOCLR = LCD_EN;
lcd_delay(500000);
}
void lcd_cmd(unsigned char cmd){
lcd_write_nibble(cmd >> 4, 0); // High nibble
lcd_write_nibble(cmd & 0x0F, 0); // Low nibble
}
void lcd_data(unsigned char data){
lcd_write_nibble(data >> 4, 1); // High nibble
lcd_write_nibble(data & 0x0F, 1); // Low nibble
}
6.3 Display Functions
Number Display (Base Conversion)
void lcd_print_num(unsigned int num, unsigned int base){
char buffer[17];
int i = 0;
if(num == 0){
lcd_data('0');
return;
}
// Extract digits in reverse
while(num > 0 && i < 16){
unsigned int digit = num % base;
if(digit < 10)
buffer[i++] = '0' + digit;
else
buffer[i++] = 'A' + (digit - 10);
num = num / base;
}
// Print in correct order
while(i > 0){
lcd_data(buffer[--i]);
}
}
Mode Display
void display_mode(void){
lcd_cmd(0x80); // Line 1
lcd_print_str("Mode: ");
if(current_base == MODE_BIN)
lcd_print_str("BIN ");
else if(current_base == MODE_OCT)
lcd_print_str("OCT ");
else if(current_base == MODE_DEC)
lcd_print_str("DEC ");
else
lcd_print_str("HEX ");
}
6.4 GPIO Configuration
void configure_system(void){
// Pin function selection (GPIO mode)
LPC_PINCON->PINSEL0 = 0;
LPC_PINCON->PINSEL1 = 0;
LPC_PINCON->PINSEL3 = 0;
LPC_PINCON->PINSEL4 = 0;
// GPIO direction setup
LPC_GPIO0->FIODIR |= COL_MASK; // Columns: output
LPC_GPIO0->FIODIR &= ~ROW_MASK; // Rows: input
LPC_GPIO0->FIODIR |= (0xFF << SEG_SHIFT); // Segments: output
LPC_GPIO0->FIODIR |= LCD_DATA_MASK | LCD_RS | LCD_EN;
LPC_GPIO1->FIODIR |= DIGIT_EN;
LPC_GPIO2->FIODIR &= ~MODE_BUTTON;
// Initial states
LPC_GPIO0->FIOSET = COL_MASK; // Columns inactive
}
7. Operating Procedures
7.1 Basic Operation
Power-On Sequence:
- System initializes, LCD displays: "Mode: DEC" / "Inp: 0"
- Select base using P2.12 button (DEC → BIN → OCT → HEX → DEC)
- Begin entering numbers
Numeric Input:
- Press digit keys (0-9, A-F as valid for current base)
- LCD shows accumulated number: "Inp: XXX"
- 7-segment displays last hexadecimal digit
Arithmetic Operations:
- Enter first number
- Hold Key 0, press operator key (B/D/E)
- LCD shows: "Op: +/-/*"
- Enter second number
- Hold Key 0, press F (equals)
- LCD shows: "Res: XXX"
Clear Function:
- Hold Key 0, press C
- All values reset to zero
- Current mode preserved
7.2 Usage Examples
Example 1: Decimal Addition (25 + 17)
1. Mode: DEC
2. Press 2, 5 → "Inp: 25"
3. Hold 0 + B → "Op: +"
4. Press 1, 7 → "Inp: 17"
5. Hold 0 + F → "Res: 42"
Example 2: Hexadecimal Calculation (AB + 1F)
1. Press P2.12 until "Mode: HEX"
2. Press A, B → "Inp: AB"
3. Hold 0 + B → "Op: +"
4. Press 1, F → "Inp: 1F"
5. Hold 0 + F → "Res: CA"
Example 3: Binary Multiplication (101 × 11)
1. Select "Mode: BIN"
2. Press 1, 0, 1 → "Inp: 101"
3. Hold 0 + E → "Op: *"
4. Press 1, 1 → "Inp: 11"
5. Hold 0 + F → "Res: 1111"
7.3 Key Reference
Numeric Input:
- Keys 0-9: Decimal digits (base-dependent)
- Keys A-F: Hexadecimal digits (HEX mode only)
Operators (Hold Key 0 + Press):
- Key B: Addition (+)
- Key C: Clear (CLR)
- Key D: Subtraction (-)
- Key E: Multiplication (×)
- Key F: Equals (=)
Mode Control:
- P2.12 Button: Cycle through bases
7.4 System Limitations
| Parameter | Limit | Notes |
|---|---|---|
| Maximum Input | 65535 | 16-bit unsigned integer |
| Display Digits | Variable | Based on current base |
| Overflow Behavior | Wrap-around | Result % 65536 |
| Underflow Behavior | Wrap-around | Unsigned arithmetic |
| Operations | 3 | Add, Subtract, Multiply |
| Decimal Support | No | Integer only |
8. Technical Reference
8.1 LCD Command Set
| Command | Code | Function |
|---|---|---|
| Clear Display | 0x01 | Clear all, cursor home |
| Return Home | 0x02 | Cursor to position 0 |
| Entry Mode | 0x06 | Increment, no shift |
| Display Control | 0x0C | Display ON, cursor OFF |
| Function Set | 0x28 | 4-bit, 2 lines, 5×8 |
| Set Line 1 | 0x80 | Cursor to line 1 |
| Set Line 2 | 0xC0 | Cursor to line 2 |
8.2 Timing Specifications
| Parameter | Value | Unit |
|---|---|---|
| Keypad Scan Interval | 3 | ms |
| Debounce Time | 9 | ms |
| LCD Enable Pulse | 100 | μs |
| LCD Processing Delay | 500 | ms (software) |
| 7-Segment Update | Real-time | - |
8.3 Memory Usage
| Component | Size |
|---|---|
| Program Code | ~4 KB |
| Global Variables | 48 bytes |
| Stack Usage | ~512 bytes |
| LCD Buffer | None (direct write) |
8.4 Power Consumption
| Component | Typical | Maximum |
|---|---|---|
| LPC1768 MCU | 50 mA | 100 mA |
| LCD Display | 2 mA | 5 mA |
| LCD Backlight | 20 mA | 50 mA |
| 7-Segment | 10 mA | 80 mA |
| Total System | 82 mA | 235 mA |
8.5 Error Handling
Invalid Input: Silently ignored (no response)
Overflow: Modulo arithmetic applied
if(input_num > 65535)
input_num = input_num % 65536;
Underflow: Unsigned wrap-around
// Example: 5 - 10 = 65531 (not -5)
Appendices
A. Complete Code Listing
The complete source code is provided in the implementation section of this document.
B. Troubleshooting Guide
| Issue | Cause | Solution |
|---|---|---|
| No keypad response | GPIO configuration | Verify COL_MASK and ROW_MASK |
| LCD blank | Initialization failure | Check timing delays |
| Incorrect display | Base mismatch | Verify current_base value |
| Mode button stuck | Debounce too short | Increase button_stable threshold |
C. Revision History
| Version | Date | Changes |
|---|---|---|
| 1.0 | 2025-10-24 | Initial release |