This commit is contained in:
sherlock 2025-10-29 12:36:04 +05:30
parent 77490396d4
commit c59338aeea

View file

@ -2,35 +2,33 @@
/* =================================================== /* ===================================================
* SMART CALCULATOR - LPC176x * SMART CALCULATOR - LPC176x
* Supports DEC, BIN, OCT, and HEX input modes * Supports BIN, BASE4, OCT, DEC, and HEX modes with
*, with 4x4 keypad, LCD, 7-segment, and mode button. * 4x4 keypad, LCD, and 7-segment display.
* --------------------------------------------------- * ---------------------------------------------------
* Decimal mode: Operators B-F active directly * DEC/BIN/BASE4/OCT : Operators BF work directly
* Hexadecimal mode: Operators B-F active with MODE button * HEX : Operators BF require MODE pressed
* =================================================== * ===================================================
*/ */
// 7-Segment patterns for 0-F // 7-Segment patterns for 0F
const unsigned char seven_seg[16] = { const unsigned char seven_seg[16] = {
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,
0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71
}; };
/* ---------------- Pin Mapping ---------------- /* ===== Pin Mapping =====
* Keypad (4x4): * Keypad (4x4):
* Columns: P0.15P0.18 (Output) * Columns: P0.15P0.18 (Outputs)
* Rows: P0.19P0.22 (Input, pull-up) * Rows: P0.19P0.22 (Inputs, pull-ups)
* *
* 7-Segment: * 7-Segment:
* Segments: P0.4P0.11 * Segments: P0.4P0.11
* Digit Enable: P1.23 * Digit Enable: P1.23
* *
* LCD: * LCD:
* Data: P0.23P0.26 * Data: P0.23P0.26, RS: P0.27, EN: P0.28
* RS: P0.27, EN: P0.28
* *
* Mode Button: P2.12 * Mode Button: P2.12 (active low)
* ------------------------------------------------
*/ */
/* ===== MACROS ===== */ /* ===== MACROS ===== */
@ -50,6 +48,7 @@ const unsigned char seven_seg[16] = {
#define MODE_BUTTON (1 << 12) #define MODE_BUTTON (1 << 12)
#define MODE_BIN 2 #define MODE_BIN 2
#define MODE_BASE4 4
#define MODE_OCT 8 #define MODE_OCT 8
#define MODE_DEC 10 #define MODE_DEC 10
#define MODE_HEX 16 #define MODE_HEX 16
@ -61,6 +60,7 @@ const unsigned char seven_seg[16] = {
/* ===== Global Variables (Predeclared) ===== */ /* ===== Global Variables (Predeclared) ===== */
int input_num; int input_num;
unsigned int seg_pattern;
int stored_num; int stored_num;
int result; int result;
unsigned int current_base; unsigned int current_base;
@ -70,7 +70,6 @@ unsigned char result_displayed;
unsigned int key, last_key, stable; unsigned int key, last_key, stable;
unsigned int button_state, last_button_state, button_stable; unsigned int button_state, last_button_state, button_stable;
unsigned int shift_active;
/* ===== Function Declarations ===== */ /* ===== Function Declarations ===== */
void delay(volatile unsigned int d); void delay(volatile unsigned int d);
@ -91,7 +90,7 @@ void change_mode(void);
void operate(unsigned int op); void operate(unsigned int op);
void perform_operation(void); void perform_operation(void);
/* ===== Utility & LCD Handling ===== */ /* ===== Delays ===== */
void delay(volatile unsigned int d) { void delay(volatile unsigned int d) {
while (d--) while (d--)
__NOP(); __NOP();
@ -103,6 +102,7 @@ void lcd_delay(unsigned long r) {
; ;
} }
/* ===== LCD ===== */
void lcd_write_nibble(unsigned char nibble, unsigned char is_data) { void lcd_write_nibble(unsigned char nibble, unsigned char is_data) {
unsigned long temp; unsigned long temp;
temp = (nibble & 0x0F) << LCD_DATA_SHIFT; temp = (nibble & 0x0F) << LCD_DATA_SHIFT;
@ -188,11 +188,14 @@ void lcd_print_num(int num, unsigned int base) {
lcd_data(buffer[--i]); lcd_data(buffer[--i]);
} }
/* ===== Display Handling ===== */
void display_mode(void) { void display_mode(void) {
lcd_cmd(0x80); lcd_cmd(0x80);
lcd_print_str("Mode: "); lcd_print_str("Mode: ");
if (current_base == MODE_BIN) if (current_base == MODE_BIN)
lcd_print_str("BIN "); lcd_print_str("BIN ");
else if (current_base == MODE_BASE4)
lcd_print_str("BASE4 ");
else if (current_base == MODE_OCT) else if (current_base == MODE_OCT)
lcd_print_str("OCT "); lcd_print_str("OCT ");
else if (current_base == MODE_DEC) else if (current_base == MODE_DEC)
@ -217,7 +220,7 @@ void display_operator_feedback(const char *op_symbol) {
display_input(); display_input();
} }
/* ====== Keypad & Button Handling ===== */ /* ===== Input Handling ===== */
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;
@ -246,11 +249,28 @@ unsigned int scan_mode_button(void) {
return ((LPC_GPIO2->FIOPIN & MODE_BUTTON) == 0) ? 1 : 0; return ((LPC_GPIO2->FIOPIN & MODE_BUTTON) == 0) ? 1 : 0;
} }
/* ===== Mode Switching ===== */
void change_mode(void) {
if (current_base == MODE_DEC)
current_base = MODE_BIN;
else if (current_base == MODE_BIN)
current_base = MODE_BASE4;
else if (current_base == MODE_BASE4)
current_base = MODE_OCT;
else if (current_base == MODE_OCT)
current_base = MODE_HEX;
else
current_base = MODE_DEC;
display_mode();
}
unsigned int is_valid_digit(unsigned int key) { unsigned int is_valid_digit(unsigned int key) {
if (key >= 16) if (key >= 16)
return 0; return 0;
if (current_base == MODE_BIN && key >= 2) if (current_base == MODE_BIN && key >= 2)
return 0; return 0;
if (current_base == MODE_BASE4 && key >= 4)
return 0;
if (current_base == MODE_OCT && key >= 8) if (current_base == MODE_OCT && key >= 8)
return 0; return 0;
if (current_base == MODE_DEC && key >= 10) if (current_base == MODE_DEC && key >= 10)
@ -258,20 +278,7 @@ unsigned int is_valid_digit(unsigned int key) {
return 1; // HEX accepts all 0-F return 1; // HEX accepts all 0-F
} }
void change_mode(void) { /* ===== Operations ===== */
if (current_base == MODE_DEC)
current_base = MODE_BIN;
else if (current_base == MODE_BIN)
current_base = MODE_OCT;
else if (current_base == MODE_OCT)
current_base = MODE_HEX;
else
current_base = MODE_DEC;
display_mode();
}
/* ===== Operator Handling ===== */
void operate(unsigned int op) { void operate(unsigned int op) {
stored_num = input_num; stored_num = input_num;
operation = op; operation = op;
@ -301,7 +308,7 @@ void perform_operation(void) {
/* ===== MAIN ===== */ /* ===== MAIN ===== */
int main(void) { int main(void) {
// Init globals // Initialize globals
input_num = stored_num = result = 0; input_num = stored_num = result = 0;
current_base = MODE_DEC; current_base = MODE_DEC;
operation = OP_NONE; operation = OP_NONE;
@ -311,20 +318,25 @@ int main(void) {
stable = button_stable = 0; stable = button_stable = 0;
button_state = last_button_state = 0; button_state = last_button_state = 0;
// Configure Pins
LPC_PINCON->PINSEL0 = 0; LPC_PINCON->PINSEL0 = 0;
LPC_PINCON->PINSEL1 = 0; LPC_PINCON->PINSEL1 = 0;
LPC_PINCON->PINSEL3 = 0; LPC_PINCON->PINSEL3 = 0;
LPC_PINCON->PINSEL4 = 0; LPC_PINCON->PINSEL4 = 0;
// Keypad setup
LPC_GPIO0->FIODIR |= COL_MASK; LPC_GPIO0->FIODIR |= COL_MASK;
LPC_GPIO0->FIODIR &= ~ROW_MASK; LPC_GPIO0->FIODIR &= ~ROW_MASK;
LPC_GPIO0->FIOSET = COL_MASK; LPC_GPIO0->FIOSET = COL_MASK;
// Mode button setup
LPC_GPIO2->FIODIR &= ~MODE_BUTTON; LPC_GPIO2->FIODIR &= ~MODE_BUTTON;
// 7-Segment setup
LPC_GPIO0->FIODIR |= (0xFF << SEG_SHIFT); LPC_GPIO0->FIODIR |= (0xFF << SEG_SHIFT);
LPC_GPIO1->FIODIR |= DIGIT_EN; LPC_GPIO1->FIODIR |= DIGIT_EN;
// LCD setup
LPC_GPIO0->FIODIR |= LCD_DATA_MASK | LCD_RS | LCD_EN; LPC_GPIO0->FIODIR |= LCD_DATA_MASK | LCD_RS | LCD_EN;
lcd_init(); lcd_init();
@ -335,6 +347,7 @@ int main(void) {
key = scan_keypad(); key = scan_keypad();
button_state = scan_mode_button(); button_state = scan_mode_button();
// Debounce keypad
if (key == last_key) { if (key == last_key) {
if (stable < 5) if (stable < 5)
stable++; stable++;
@ -343,6 +356,7 @@ int main(void) {
stable = 0; stable = 0;
} }
// Debounce mode button
if (button_state == last_button_state) { if (button_state == last_button_state) {
if (button_stable < 5) if (button_stable < 5)
button_stable++; button_stable++;
@ -351,18 +365,24 @@ int main(void) {
button_stable = 0; button_stable = 0;
} }
// Cycle through modes
if (button_stable == 3 && button_state == 1) { if (button_stable == 3 && button_state == 1) {
change_mode(); change_mode();
button_stable = 5; button_stable = 5;
} }
// Handle keypad press
if (stable == 3 && key != 0xFF) { if (stable == 3 && key != 0xFF) {
/* ========================= unsigned char allow_operator = 0;
* DECIMAL MODE: Operator works directly
* HEX MODE: Operator works with MODE_BUTTON held // Operators allowed directly for all modes except HEX (must hold mode)
* ========================= */ if (current_base == MODE_HEX && button_state == 1)
if ((current_base == MODE_DEC) || allow_operator = 1;
(current_base == MODE_HEX && button_state == 1)) { else if (current_base != MODE_HEX)
allow_operator = 1;
// Operator Keys: BF (indices 1115)
if (allow_operator) {
if (key == 11) { // B = + if (key == 11) { // B = +
operate(OP_ADD); operate(OP_ADD);
display_operator_feedback("+"); display_operator_feedback("+");
@ -391,21 +411,18 @@ int main(void) {
display_input(); display_input();
} }
} }
stable = 5; stable = 5;
} }
// ===== STATE DISPLAY on 7-Segment ===== // ===== STATE DISPLAY on 7-Segment =====
unsigned int seg_pattern;
if (operation == OP_NONE && result_displayed == 0) if (operation == OP_NONE && result_displayed == 0)
seg_pattern = 0x06; // '1' seg_pattern = 0x06; // '1' - Input mode
else if (operation != OP_NONE) else if (operation != OP_NONE)
seg_pattern = 0x5B | 0x80; // '2' + DP seg_pattern = 0x5B | 0x80; // '2' + DP - Waiting for 2nd operand
else if (result_displayed == 1) else if (result_displayed == 1)
seg_pattern = 0x4F | 0x80; // '3' + DP seg_pattern = 0x4F | 0x80; // '3' + DP - Result shown
else else
seg_pattern = 0x3F; // '0' seg_pattern = 0x3F; // '0' - Reset
LPC_GPIO0->FIOCLR = (0xFF << SEG_SHIFT); LPC_GPIO0->FIOCLR = (0xFF << SEG_SHIFT);
LPC_GPIO0->FIOSET = (seg_pattern << SEG_SHIFT); LPC_GPIO0->FIOSET = (seg_pattern << SEG_SHIFT);