ESD Project
This commit is contained in:
parent
200a988cfb
commit
cf66e155a9
3 changed files with 1200 additions and 135 deletions
|
|
@ -23,14 +23,15 @@
|
||||||
* P2.12 (Input, internal pull-up)
|
* P2.12 (Input, internal pull-up)
|
||||||
*
|
*
|
||||||
* KEY FUNCTIONS:
|
* KEY FUNCTIONS:
|
||||||
* 0-9: Digit input (valid in all bases based on base)
|
* 0-9, A-F: Digit input (valid based on current base)
|
||||||
* A-F: Digit input (valid only in appropriate base)
|
*
|
||||||
* Key A (10): Digit input (value 10 in HEX mode)
|
* OPERATOR MODE (Hold Key 0 + another key):
|
||||||
* Key B (11): Addition (+)
|
* Key 0 + B: Addition (+)
|
||||||
* Key C (12): Clear (C)
|
* Key 0 + C: Clear (C)
|
||||||
* Key D (13): Subtraction (-)
|
* Key 0 + D: Subtraction (-)
|
||||||
* Key E (14): Multiplication (*)
|
* Key 0 + E: Multiplication (*)
|
||||||
* Key F (15): Equals (=)
|
* Key 0 + F: Equals (=)
|
||||||
|
*
|
||||||
* P2.12 Button: Mode selection (cycles: DEC->BIN->OCT->HEX)
|
* P2.12 Button: Mode selection (cycles: DEC->BIN->OCT->HEX)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
@ -70,12 +71,12 @@ const unsigned char seven_seg[16] = {
|
||||||
#define OP_SUB 2
|
#define OP_SUB 2
|
||||||
#define OP_MUL 3
|
#define OP_MUL 3
|
||||||
|
|
||||||
// Global variables
|
// Global variables - CHANGED to signed int
|
||||||
|
int input_num = 0;
|
||||||
|
int stored_num = 0;
|
||||||
|
int result = 0;
|
||||||
unsigned int current_base = MODE_DEC;
|
unsigned int current_base = MODE_DEC;
|
||||||
unsigned int input_num = 0;
|
|
||||||
unsigned int stored_num = 0;
|
|
||||||
unsigned int operation = OP_NONE;
|
unsigned int operation = OP_NONE;
|
||||||
unsigned int result = 0;
|
|
||||||
unsigned char lcd_flag = 0;
|
unsigned char lcd_flag = 0;
|
||||||
|
|
||||||
void delay(volatile unsigned int d){
|
void delay(volatile unsigned int d){
|
||||||
|
|
@ -137,22 +138,42 @@ void lcd_print_str(const char* str){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void lcd_print_num(unsigned int num, unsigned int base){
|
// MODIFIED to handle negative numbers
|
||||||
|
void lcd_print_num(int num, unsigned int base){
|
||||||
char buffer[17];
|
char buffer[17];
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
|
// For non-decimal bases, show as unsigned (two's complement representation)
|
||||||
|
if(base != MODE_DEC){
|
||||||
|
unsigned int unum = (unsigned int)num;
|
||||||
|
if(unum == 0){
|
||||||
|
lcd_data('0');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
while(unum > 0 && i < 16){
|
||||||
|
unsigned int digit = unum % base;
|
||||||
|
if(digit < 10)
|
||||||
|
buffer[i++] = '0' + digit;
|
||||||
|
else
|
||||||
|
buffer[i++] = 'A' + (digit - 10);
|
||||||
|
unum = unum / base;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Decimal mode: handle negative with minus sign
|
||||||
|
if(num < 0){
|
||||||
|
lcd_data('-');
|
||||||
|
num = -num;
|
||||||
|
}
|
||||||
|
|
||||||
if(num == 0){
|
if(num == 0){
|
||||||
lcd_data('0');
|
lcd_data('0');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
while(num > 0 && i < 16){
|
while(num > 0 && i < 16){
|
||||||
unsigned int digit = num % base;
|
buffer[i++] = '0' + (num % 10);
|
||||||
if(digit < 10)
|
num = num / 10;
|
||||||
buffer[i++] = '0' + digit;
|
}
|
||||||
else
|
|
||||||
buffer[i++] = 'A' + (digit - 10);
|
|
||||||
num = num / base;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while(i > 0){
|
while(i > 0){
|
||||||
|
|
@ -180,6 +201,16 @@ void display_input(void){
|
||||||
lcd_print_str(" ");
|
lcd_print_str(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void display_operator_feedback(const char* op_symbol){
|
||||||
|
// Brief feedback on line 2
|
||||||
|
lcd_cmd(0xC0);
|
||||||
|
lcd_print_str("Op: ");
|
||||||
|
lcd_print_str(op_symbol);
|
||||||
|
lcd_print_str(" ");
|
||||||
|
delay(100000); // Brief delay to show feedback
|
||||||
|
display_input(); // Return to showing input
|
||||||
|
}
|
||||||
|
|
||||||
unsigned int scan_keypad(void){
|
unsigned int scan_keypad(void){
|
||||||
unsigned int col, row;
|
unsigned int col, row;
|
||||||
unsigned int row_bits;
|
unsigned int row_bits;
|
||||||
|
|
@ -204,6 +235,22 @@ unsigned int scan_keypad(void){
|
||||||
return 0xFF;
|
return 0xFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if Key 0 is currently pressed (shift key)
|
||||||
|
unsigned int is_key0_pressed(void){
|
||||||
|
unsigned int col = 0; // Key 0 is at column 0, row 0
|
||||||
|
unsigned int row_bits;
|
||||||
|
|
||||||
|
LPC_GPIO0->FIOSET = COL_MASK;
|
||||||
|
delay(50);
|
||||||
|
LPC_GPIO0->FIOCLR = (1 << (COL_BASE + col));
|
||||||
|
delay(200);
|
||||||
|
row_bits = (LPC_GPIO0->FIOPIN & ROW_MASK) >> ROW_BASE;
|
||||||
|
LPC_GPIO0->FIOSET = COL_MASK;
|
||||||
|
|
||||||
|
// Check if row 0 is pressed (Key 0)
|
||||||
|
return ((row_bits & 0x01) == 0) ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned int scan_mode_button(void){
|
unsigned int scan_mode_button(void){
|
||||||
// Return 1 if button is pressed (active low), 0 if not pressed
|
// Return 1 if button is pressed (active low), 0 if not pressed
|
||||||
return ((LPC_GPIO2->FIOPIN & MODE_BUTTON) == 0) ? 1 : 0;
|
return ((LPC_GPIO2->FIOPIN & MODE_BUTTON) == 0) ? 1 : 0;
|
||||||
|
|
@ -214,6 +261,7 @@ unsigned int is_valid_digit(unsigned int key){
|
||||||
if(current_base == MODE_BIN && key >= 2) return 0;
|
if(current_base == MODE_BIN && key >= 2) return 0;
|
||||||
if(current_base == MODE_OCT && key >= 8) return 0;
|
if(current_base == MODE_OCT && key >= 8) return 0;
|
||||||
if(current_base == MODE_DEC && key >= 10) return 0;
|
if(current_base == MODE_DEC && key >= 10) return 0;
|
||||||
|
// In HEX mode, all keys 0-15 are valid digits!
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -234,6 +282,7 @@ int main(void){
|
||||||
unsigned int stable = 0;
|
unsigned int stable = 0;
|
||||||
unsigned int button_state, last_button_state = 0;
|
unsigned int button_state, last_button_state = 0;
|
||||||
unsigned int button_stable = 0;
|
unsigned int button_stable = 0;
|
||||||
|
unsigned int shift_active = 0;
|
||||||
|
|
||||||
// Configure pins
|
// Configure pins
|
||||||
LPC_PINCON->PINSEL0 = 0;
|
LPC_PINCON->PINSEL0 = 0;
|
||||||
|
|
@ -248,7 +297,6 @@ int main(void){
|
||||||
|
|
||||||
// Mode button: Input with internal pull-up
|
// Mode button: Input with internal pull-up
|
||||||
LPC_GPIO2->FIODIR &= ~MODE_BUTTON; // Set as input
|
LPC_GPIO2->FIODIR &= ~MODE_BUTTON; // Set as input
|
||||||
// Note: Internal pull-up is enabled by default on LPC17xx
|
|
||||||
|
|
||||||
// 7-Segment
|
// 7-Segment
|
||||||
LPC_GPIO0->FIODIR |= (0xFF << SEG_SHIFT);
|
LPC_GPIO0->FIODIR |= (0xFF << SEG_SHIFT);
|
||||||
|
|
@ -262,6 +310,10 @@ int main(void){
|
||||||
display_input();
|
display_input();
|
||||||
|
|
||||||
for(;;){
|
for(;;){
|
||||||
|
// Check if shift key (Key 0) is being held
|
||||||
|
shift_active = is_key0_pressed();
|
||||||
|
|
||||||
|
// Scan for other keys
|
||||||
key = scan_keypad();
|
key = scan_keypad();
|
||||||
button_state = scan_mode_button();
|
button_state = scan_mode_button();
|
||||||
|
|
||||||
|
|
@ -291,43 +343,36 @@ int main(void){
|
||||||
if(stable == 3 && key != 0xFF){
|
if(stable == 3 && key != 0xFF){
|
||||||
// Key pressed and stable
|
// Key pressed and stable
|
||||||
|
|
||||||
// Handle digit input (0-9, A-F)
|
// If Key 0 is also pressed, we're in operator mode
|
||||||
if(is_valid_digit(key)){
|
if(shift_active && key != 0){
|
||||||
input_num = input_num * current_base + key;
|
// OPERATOR MODE (Key 0 + another key)
|
||||||
if(input_num > 9999) input_num = input_num % 10000;
|
|
||||||
display_input();
|
if(key == 11){ // Key 0 + B = Addition
|
||||||
}
|
|
||||||
// Addition (Key B / 11)
|
|
||||||
else if(key == 11){
|
|
||||||
stored_num = input_num;
|
stored_num = input_num;
|
||||||
operation = OP_ADD;
|
operation = OP_ADD;
|
||||||
input_num = 0;
|
input_num = 0;
|
||||||
display_input();
|
display_operator_feedback("+");
|
||||||
}
|
}
|
||||||
// Clear (Key C / 12)
|
else if(key == 12){ // Key 0 + C = Clear
|
||||||
else if(key == 12){
|
|
||||||
input_num = 0;
|
input_num = 0;
|
||||||
stored_num = 0;
|
stored_num = 0;
|
||||||
operation = OP_NONE;
|
operation = OP_NONE;
|
||||||
result = 0;
|
result = 0;
|
||||||
display_input();
|
display_operator_feedback("CLR");
|
||||||
}
|
}
|
||||||
// Subtraction (Key D / 13)
|
else if(key == 13){ // Key 0 + D = Subtraction
|
||||||
else if(key == 13){
|
|
||||||
stored_num = input_num;
|
stored_num = input_num;
|
||||||
operation = OP_SUB;
|
operation = OP_SUB;
|
||||||
input_num = 0;
|
input_num = 0;
|
||||||
display_input();
|
display_operator_feedback("-");
|
||||||
}
|
}
|
||||||
// Multiplication (Key E / 14)
|
else if(key == 14){ // Key 0 + E = Multiplication
|
||||||
else if(key == 14){
|
|
||||||
stored_num = input_num;
|
stored_num = input_num;
|
||||||
operation = OP_MUL;
|
operation = OP_MUL;
|
||||||
input_num = 0;
|
input_num = 0;
|
||||||
display_input();
|
display_operator_feedback("*");
|
||||||
}
|
}
|
||||||
// Equals (Key F / 15)
|
else if(key == 15){ // Key 0 + F = Equals
|
||||||
else if(key == 15){
|
|
||||||
if(operation == OP_ADD)
|
if(operation == OP_ADD)
|
||||||
result = stored_num + input_num;
|
result = stored_num + input_num;
|
||||||
else if(operation == OP_SUB)
|
else if(operation == OP_SUB)
|
||||||
|
|
@ -345,19 +390,34 @@ int main(void){
|
||||||
input_num = result;
|
input_num = result;
|
||||||
operation = OP_NONE;
|
operation = OP_NONE;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// NORMAL MODE - Digit input
|
||||||
|
if(is_valid_digit(key)){
|
||||||
|
input_num = input_num * current_base + key;
|
||||||
|
// Handle overflow with wrap-around for signed int
|
||||||
|
if(input_num > 32767) input_num = input_num % 32768;
|
||||||
|
if(input_num < -32768) input_num = -32768;
|
||||||
|
display_input();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
stable = 5;
|
stable = 5; // Prevent repeated triggers
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display result on 7-segment (last digit only)
|
// Display result on 7-segment (last digit only)
|
||||||
if(input_num < 16){
|
// MODIFIED: Use decimal point to indicate negative numbers
|
||||||
LPC_GPIO0->FIOCLR = (0xFF << SEG_SHIFT);
|
int display_value = (input_num < 0) ? -input_num : input_num;
|
||||||
LPC_GPIO0->FIOSET = (seven_seg[input_num] << SEG_SHIFT);
|
unsigned int display_digit = display_value % 16;
|
||||||
} else {
|
unsigned int seg_pattern = seven_seg[display_digit];
|
||||||
unsigned int display_digit = input_num % 16;
|
|
||||||
LPC_GPIO0->FIOCLR = (0xFF << SEG_SHIFT);
|
// Turn on decimal point if number is negative
|
||||||
LPC_GPIO0->FIOSET = (seven_seg[display_digit] << SEG_SHIFT);
|
if(input_num < 0){
|
||||||
|
seg_pattern |= 0x80; // Bit 7 is the decimal point
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LPC_GPIO0->FIOCLR = (0xFF << SEG_SHIFT);
|
||||||
|
LPC_GPIO0->FIOSET = (seg_pattern << SEG_SHIFT);
|
||||||
LPC_GPIO1->FIOSET = DIGIT_EN;
|
LPC_GPIO1->FIOSET = DIGIT_EN;
|
||||||
|
|
||||||
delay(3000);
|
delay(3000);
|
||||||
|
|
|
||||||
773
ES/Project/longreport.md
Normal file
773
ES/Project/longreport.md
Normal file
|
|
@ -0,0 +1,773 @@
|
||||||
|
# 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
|
||||||
|
|
||||||
|
1. [System Overview](#1-system-overview)
|
||||||
|
2. [Hardware Architecture](#2-hardware-architecture)
|
||||||
|
3. [Pin Configuration](#3-pin-configuration)
|
||||||
|
4. [Software Architecture](#4-software-architecture)
|
||||||
|
5. [Functional Specification](#5-functional-specification)
|
||||||
|
6. [Code Documentation](#6-code-documentation)
|
||||||
|
7. [Operating Procedures](#7-operating-procedures)
|
||||||
|
8. [Technical Reference](#8-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:**
|
||||||
|
|
||||||
|
```c
|
||||||
|
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:
|
||||||
|
|
||||||
|
1. **Application Layer:** User interface and calculator logic
|
||||||
|
2. **Hardware Abstraction:** LCD, keypad, and display control functions
|
||||||
|
3. **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
|
||||||
|
|
||||||
|
```c
|
||||||
|
// 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
|
||||||
|
|
||||||
|
```c
|
||||||
|
// 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
|
||||||
|
|
||||||
|
```c
|
||||||
|
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:**
|
||||||
|
|
||||||
|
```c
|
||||||
|
// 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).
|
||||||
|
|
||||||
|
```c
|
||||||
|
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:**
|
||||||
|
|
||||||
|
```c
|
||||||
|
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
|
||||||
|
|
||||||
|
```c
|
||||||
|
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
|
||||||
|
|
||||||
|
```c
|
||||||
|
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)
|
||||||
|
|
||||||
|
```c
|
||||||
|
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
|
||||||
|
|
||||||
|
```c
|
||||||
|
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)
|
||||||
|
|
||||||
|
```c
|
||||||
|
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
|
||||||
|
|
||||||
|
```c
|
||||||
|
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
|
||||||
|
|
||||||
|
```c
|
||||||
|
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:**
|
||||||
|
1. System initializes, LCD displays: "Mode: DEC" / "Inp: 0"
|
||||||
|
2. Select base using P2.12 button (DEC → BIN → OCT → HEX → DEC)
|
||||||
|
3. 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:**
|
||||||
|
1. Enter first number
|
||||||
|
2. Hold Key 0, press operator key (B/D/E)
|
||||||
|
3. LCD shows: "Op: +/-/*"
|
||||||
|
4. Enter second number
|
||||||
|
5. Hold Key 0, press F (equals)
|
||||||
|
6. 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
|
||||||
|
```c
|
||||||
|
if(input_num > 65535)
|
||||||
|
input_num = input_num % 65536;
|
||||||
|
```
|
||||||
|
|
||||||
|
**Underflow:** Unsigned wrap-around
|
||||||
|
```c
|
||||||
|
// 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 |
|
||||||
|
|
@ -1,68 +1,300 @@
|
||||||
# Calculator Program for Multi-Base Arithmetic
|
# LPC1768 Multi-Base Calculator - Code Explainer & Hardware Mapping Report
|
||||||
|
|
||||||
## How to Use the Calculator
|
## Overview
|
||||||
|
This code implements a **multi-base calculator** for the LPC1768 microcontroller with support for Binary, Octal, Decimal, and Hexadecimal number systems. It features a 4x4 matrix keypad for input, a 16x2 LCD for display, and a single 7-segment display for quick output visualization.
|
||||||
|
|
||||||
### Initial Setup
|
---
|
||||||
- On power-up, the calculator starts in **DECIMAL mode**
|
|
||||||
- LCD shows: `Mode: DEC` on line 1, `Inp: 0` on line 2
|
|
||||||
- 7-segment displays the last digit of your input
|
|
||||||
|
|
||||||
### Basic Operations
|
## Core Functionality
|
||||||
|
|
||||||
1. **Entering Numbers**
|
### 1. **Number Base Modes**
|
||||||
- Press keys 0-9 for decimal digits
|
The calculator supports four number bases:
|
||||||
- In HEX mode, you can also use A-F (keys 10-15)
|
- **Binary (BIN)** - Base 2 (digits 0-1)
|
||||||
- In OCT mode, only 0-7 are valid
|
- **Octal (OCT)** - Base 8 (digits 0-7)
|
||||||
- In BIN mode, only 0-1 are valid
|
- **Decimal (DEC)** - Base 10 (digits 0-9) - *Default mode*
|
||||||
- Invalid digits for the current mode are ignored
|
- **Hexadecimal (HEX)** - Base 16 (digits 0-9, A-F)
|
||||||
|
|
||||||
2. **Changing Base Mode**
|
Mode switching is done via the **P2.12 button** in the cycle: DEC → BIN → OCT → HEX → DEC...
|
||||||
- Press **Key A (10)** to cycle through bases
|
|
||||||
- Order: BIN → OCT → DEC → HEX → BIN...
|
|
||||||
- Current mode displays on LCD top line
|
|
||||||
|
|
||||||
3. **Performing Calculations**
|
### 2. **Keypad Operation**
|
||||||
- Enter first number
|
|
||||||
- Press operation key:
|
|
||||||
- **Key B (11)**: Addition
|
|
||||||
- **Key D (13)**: Subtraction
|
|
||||||
- **Key E (14)**: Multiplication
|
|
||||||
- Enter second number
|
|
||||||
- Press **Key F (15)** for equals/result
|
|
||||||
|
|
||||||
4. **Clearing**
|
#### Standard Digit Input (Keys 0-F)
|
||||||
- Press **Key C (12)** to clear all (input, stored number, operation)
|
- Keys 0-9 and A-F enter digits
|
||||||
|
- Only valid digits for the current base are accepted
|
||||||
|
- Numbers are built up by multiplying the current value by the base and adding the new digit
|
||||||
|
|
||||||
### Example Usage
|
#### Operator Mode (Hold Key 0 + Another Key)
|
||||||
|
The code uses **Key 0 as a "shift" key** for operator access:
|
||||||
|
|
||||||
**Example 1: Add 5 + 3 in Decimal**
|
| Combination | Function | Operation |
|
||||||
```
|
|-------------|----------|-----------|
|
||||||
Press: 5 → B → 3 → F
|
| Key 0 + B | Addition | Stores current number, sets + operation |
|
||||||
Display: Res: 8
|
| Key 0 + C | Clear | Resets all values |
|
||||||
|
| Key 0 + D | Subtraction | Stores current number, sets - operation |
|
||||||
|
| Key 0 + E | Multiplication | Stores current number, sets × operation |
|
||||||
|
| Key 0 + F | Equals | Calculates result based on stored operation |
|
||||||
|
|
||||||
|
### 3. **Signed Integer Handling**
|
||||||
|
The code uses **signed 32-bit integers** (`int` type) with special display logic:
|
||||||
|
- **Decimal mode**: Negative numbers shown with "-" sign
|
||||||
|
- **Other bases**: Numbers displayed as unsigned (two's complement representation)
|
||||||
|
- **7-segment display**: Decimal point illuminates to indicate negative numbers
|
||||||
|
|
||||||
|
### 4. **Display Outputs**
|
||||||
|
|
||||||
|
#### LCD Display (16x2)
|
||||||
|
- **Line 1**: Current mode ("Mode: BIN/OCT/DEC/HEX")
|
||||||
|
- **Line 2**: Input number ("Inp: [number]") or Result ("Res: [number]")
|
||||||
|
- Brief operator feedback shown when operations are selected
|
||||||
|
|
||||||
|
#### 7-Segment Display
|
||||||
|
- Shows the **last digit** (rightmost) of the current input in base-16
|
||||||
|
- **Decimal point lights up** when the number is negative
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Hardware Mapping to FRC Connectors
|
||||||
|
|
||||||
|
### **CNA Connector (P0.4 to P0.11)**
|
||||||
|
**Usage: 7-Segment Display**
|
||||||
|
|
||||||
|
| Pin CNA | LPC1768 Pin | Function | Code Reference |
|
||||||
|
|---------|-------------|----------|----------------|
|
||||||
|
| 1 | P0.4 | Segment a | `SEG_SHIFT = 4` |
|
||||||
|
| 2 | P0.5 | Segment b | Bits 4-11 control segments |
|
||||||
|
| 3 | P0.6 | Segment c | `seven_seg[]` array patterns |
|
||||||
|
| 4 | P0.7 | Segment d | |
|
||||||
|
| 5 | P0.8 | Segment e | |
|
||||||
|
| 6 | P0.9 | Segment f | |
|
||||||
|
| 7 | P0.10 | Segment g | |
|
||||||
|
| 8 | P0.11 | Decimal point | Bit 7 of pattern (0x80) |
|
||||||
|
| 10 | GND | Ground | |
|
||||||
|
|
||||||
|
**Code Configuration:**
|
||||||
|
```c
|
||||||
|
#define SEG_SHIFT 4
|
||||||
|
LPC_GPIO0->FIODIR |= (0xFF << SEG_SHIFT); // Configure as output
|
||||||
|
LPC_GPIO0->FIOSET = (seg_pattern << SEG_SHIFT); // Set segments
|
||||||
```
|
```
|
||||||
|
|
||||||
**Example 2: Multiply 1010 × 11 in Binary**
|
---
|
||||||
```
|
|
||||||
Press: A (change to BIN mode)
|
### **CNB Connector (P1.23-P1.26, P2.10-P2.13)**
|
||||||
Press: 1 → 0 → 1 → 0 → E → 1 → 1 → F
|
**Usage: 7-Segment Digit Enable & Mode Button**
|
||||||
Display: Res: 11110 (30 in decimal)
|
|
||||||
|
| Pin CNB | LPC1768 Pin | Function | Code Reference |
|
||||||
|
|---------|-------------|----------|----------------|
|
||||||
|
| 1 | P1.23 | 7-Seg Digit Enable | `DIGIT_EN (1<<23)` |
|
||||||
|
| 7 | P2.12 | Mode Button (Input) | `MODE_BUTTON (1<<12)` |
|
||||||
|
| 2-6, 8 | P1.24-26, P2.10-11, P2.13 | *Unused* | |
|
||||||
|
| 10 | GND | Ground | |
|
||||||
|
|
||||||
|
**Code Configuration:**
|
||||||
|
```c
|
||||||
|
#define DIGIT_EN (1<<23)
|
||||||
|
LPC_GPIO1->FIODIR |= DIGIT_EN; // P1.23 as output
|
||||||
|
LPC_GPIO1->FIOSET = DIGIT_EN; // Enable digit
|
||||||
|
|
||||||
|
#define MODE_BUTTON (1<<12)
|
||||||
|
LPC_GPIO2->FIODIR &= ~MODE_BUTTON; // P2.12 as input (pull-up)
|
||||||
```
|
```
|
||||||
|
|
||||||
**Example 3: Subtract F - A in Hexadecimal**
|
**Mode Button Behavior:**
|
||||||
|
- Active LOW (reads 0 when pressed)
|
||||||
|
- Debounced with 5-sample stability check
|
||||||
|
- Cycles through number bases
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### **CNC Connector (P0.15-P0.22, P2.13)**
|
||||||
|
**Usage: 4×4 Matrix Keypad**
|
||||||
|
|
||||||
|
| Pin CNC | LPC1768 Pin | Function | Keypad Role |
|
||||||
|
|---------|-------------|----------|-------------|
|
||||||
|
| 1 | P0.15 | Column 0 | Output (scan low) |
|
||||||
|
| 2 | P0.16 | Column 1 | Output (scan low) |
|
||||||
|
| 3 | P0.17 | Column 2 | Output (scan low) |
|
||||||
|
| 4 | P0.18 | Column 3 | Output (scan low) |
|
||||||
|
| 5 | P0.19 | Row 0 | Input (pull-high) |
|
||||||
|
| 6 | P0.20 | Row 1 | Input (pull-high) |
|
||||||
|
| 7 | P0.21 | Row 2 | Input (pull-high) |
|
||||||
|
| 8 | P0.22 | Row 3 | Input (pull-high) |
|
||||||
|
| 10 | GND | Ground | |
|
||||||
|
|
||||||
|
**Keypad Matrix Layout:**
|
||||||
```
|
```
|
||||||
Press: A (cycle to HEX mode if not already)
|
Col0 Col1 Col2 Col3
|
||||||
Press: F → D → A (10) → F
|
(15) (16) (17) (18)
|
||||||
Display: Res: 5
|
Row0 (19) 0 1 2 3
|
||||||
|
Row1 (20) 4 5 6 7
|
||||||
|
Row2 (21) 8 9 A B
|
||||||
|
Row3 (22) C D E F
|
||||||
```
|
```
|
||||||
|
|
||||||
### Tips
|
**Scanning Algorithm:**
|
||||||
- The 7-segment always shows the last hex digit of your current input
|
1. Set all columns HIGH
|
||||||
- Results stay in the current base mode
|
2. Pull one column LOW
|
||||||
- If result exceeds display capacity, only visible portion shows
|
3. Read row pins
|
||||||
- After equals, the result becomes the new input for next operation
|
4. If a row is LOW, a key at that (column, row) is pressed
|
||||||
|
5. Key number = `col × 4 + row`
|
||||||
|
|
||||||
### Mapping
|
**Code Configuration:**
|
||||||
CND -> LCD
|
```c
|
||||||
CNC -> KeyPad Matrix
|
#define COL_BASE 15
|
||||||
CNB -> SW2
|
#define ROW_BASE 19
|
||||||
CNA -> 7 Segment Display
|
#define COL_MASK (0x0F << COL_BASE) // P0.15-18
|
||||||
|
#define ROW_MASK (0x0F << ROW_BASE) // P0.19-22
|
||||||
|
|
||||||
|
LPC_GPIO0->FIODIR |= COL_MASK; // Columns as output
|
||||||
|
LPC_GPIO0->FIODIR &= ~ROW_MASK; // Rows as input
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### **CND Connector (P0.23-P0.28, P2.0-P2.1)**
|
||||||
|
**Usage: 16×2 LCD Display (4-bit mode)**
|
||||||
|
|
||||||
|
| Pin CND | LPC1768 Pin | Function | LCD Pin |
|
||||||
|
|---------|-------------|----------|---------|
|
||||||
|
| 1 | P0.23 | Data bit 4 (D4) | LCD D4 |
|
||||||
|
| 2 | P0.24 | Data bit 5 (D5) | LCD D5 |
|
||||||
|
| 3 | P0.25 | Data bit 6 (D6) | LCD D6 |
|
||||||
|
| 4 | P0.26 | Data bit 7 (D7) | LCD D7 |
|
||||||
|
| 5 | P0.27 | Register Select (RS) | LCD RS |
|
||||||
|
| 6 | P0.28 | Enable (EN) | LCD EN |
|
||||||
|
| 7-8 | P2.0-1 | *Unused* | |
|
||||||
|
| 10 | GND | Ground | |
|
||||||
|
|
||||||
|
**LCD Communication (4-bit Mode):**
|
||||||
|
- Each byte sent as two 4-bit nibbles (high nibble first)
|
||||||
|
- **RS = 0**: Command mode
|
||||||
|
- **RS = 1**: Data mode
|
||||||
|
- **EN pulse**: Latches data into LCD
|
||||||
|
|
||||||
|
**Code Configuration:**
|
||||||
|
```c
|
||||||
|
#define LCD_DATA_SHIFT 23
|
||||||
|
#define LCD_DATA_MASK (0x0F << LCD_DATA_SHIFT) // P0.23-26
|
||||||
|
#define LCD_RS (1<<27) // P0.27
|
||||||
|
#define LCD_EN (1<<28) // P0.28
|
||||||
|
|
||||||
|
LPC_GPIO0->FIODIR |= LCD_DATA_MASK | LCD_RS | LCD_EN; // All outputs
|
||||||
|
```
|
||||||
|
|
||||||
|
**LCD Initialization Sequence:**
|
||||||
|
1. Wait 5ms after power-on
|
||||||
|
2. Send 0x03 three times (8-bit mode reset)
|
||||||
|
3. Send 0x02 (switch to 4-bit mode)
|
||||||
|
4. Configure: 4-bit, 2-line, 5×8 font (0x28)
|
||||||
|
5. Display ON, cursor OFF (0x0C)
|
||||||
|
6. Clear display (0x01)
|
||||||
|
7. Entry mode: increment, no shift (0x06)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Key Code Functions
|
||||||
|
|
||||||
|
### Keypad Scanning
|
||||||
|
```c
|
||||||
|
unsigned int scan_keypad(void)
|
||||||
|
```
|
||||||
|
- Scans 4×4 matrix by pulling columns low sequentially
|
||||||
|
- Returns key number (0-15) or 0xFF if no key pressed
|
||||||
|
- Includes debouncing delay
|
||||||
|
|
||||||
|
### Shift Key Detection
|
||||||
|
```c
|
||||||
|
unsigned int is_key0_pressed(void)
|
||||||
|
```
|
||||||
|
- Specifically checks if Key 0 is being held down
|
||||||
|
- Enables "operator mode" when Key 0 + another key pressed
|
||||||
|
|
||||||
|
### Digit Validation
|
||||||
|
```c
|
||||||
|
unsigned int is_valid_digit(unsigned int key)
|
||||||
|
```
|
||||||
|
- Ensures entered digit is valid for current base
|
||||||
|
- BIN: 0-1, OCT: 0-7, DEC: 0-9, HEX: 0-F
|
||||||
|
|
||||||
|
### LCD Number Display
|
||||||
|
```c
|
||||||
|
void lcd_print_num(int num, unsigned int base)
|
||||||
|
```
|
||||||
|
- Handles negative numbers in decimal mode (shows "-")
|
||||||
|
- For other bases, displays unsigned representation
|
||||||
|
- Converts number to string in specified base
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Operation Flow Example
|
||||||
|
|
||||||
|
**Example: Calculate 5 + 3 in Decimal Mode**
|
||||||
|
|
||||||
|
1. **Power on** → Mode: DEC, Input: 0
|
||||||
|
2. **Press Key 5** → Input: 5 (displayed on LCD and 7-seg)
|
||||||
|
3. **Hold Key 0 + Press B** → "Op: +" feedback, stored = 5, input = 0
|
||||||
|
4. **Press Key 3** → Input: 3
|
||||||
|
5. **Hold Key 0 + Press F** → "Res: 8" displayed, input = 8
|
||||||
|
6. Result can be used for next calculation
|
||||||
|
|
||||||
|
**Example: Switch to Binary Mode**
|
||||||
|
|
||||||
|
1. **Press P2.12 button** → Mode cycles DEC → BIN
|
||||||
|
2. LCD shows "Mode: BIN"
|
||||||
|
3. Only keys 0 and 1 are now valid
|
||||||
|
4. Display shows binary representation
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Debouncing Strategy
|
||||||
|
|
||||||
|
Both keypad and mode button use **count-based debouncing**:
|
||||||
|
|
||||||
|
```c
|
||||||
|
if(key == last_key){
|
||||||
|
if(stable < 5) stable++;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- Requires **3 consecutive stable reads** before acting (`stable == 3`)
|
||||||
|
- After action, sets `stable = 5` to prevent repeat triggers
|
||||||
|
- Prevents false triggers from mechanical switch bounce
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Safety & Overflow Handling
|
||||||
|
|
||||||
|
```c
|
||||||
|
if(input_num > 32767) input_num = input_num % 32768;
|
||||||
|
if(input_num < -32768) input_num = -32768;
|
||||||
|
```
|
||||||
|
|
||||||
|
- Wraps overflow for signed 32-bit integers
|
||||||
|
- Prevents display issues with very large numbers
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Pin Configuration Summary Table
|
||||||
|
|
||||||
|
| GPIO Port | Pins Used | Direction | Function | FRC Connector |
|
||||||
|
|-----------|-----------|-----------|----------|---------------|
|
||||||
|
| P0.4-11 | 8 bits | Output | 7-seg segments | **CNA 1-8** |
|
||||||
|
| P0.15-18 | 4 bits | Output | Keypad columns | **CNC 1-4** |
|
||||||
|
| P0.19-22 | 4 bits | Input | Keypad rows | **CNC 5-8** |
|
||||||
|
| P0.23-26 | 4 bits | Output | LCD data D4-D7 | **CND 1-4** |
|
||||||
|
| P0.27 | 1 bit | Output | LCD RS | **CND 5** |
|
||||||
|
| P0.28 | 1 bit | Output | LCD EN | **CND 6** |
|
||||||
|
| P1.23 | 1 bit | Output | 7-seg digit enable | **CNB 1** |
|
||||||
|
| P2.12 | 1 bit | Input | Mode button | **CNB 7** |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Conclusion
|
||||||
|
|
||||||
|
This calculator demonstrates effective use of the LPC1768's GPIO capabilities across multiple peripherals:
|
||||||
|
- Efficient matrix keypad scanning minimizes pin usage
|
||||||
|
- 4-bit LCD mode reduces wiring complexity
|
||||||
|
- Single 7-segment provides instant visual feedback
|
||||||
|
- Clever "shift key" mechanism provides operator access without dedicated buttons
|
||||||
|
|
||||||
|
The hardware mapping efficiently utilizes all four FRC connectors to create a fully functional multi-base calculator with a clean user interface.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue