From fd3ec40397b6934cbfc5417c00b5c1c9400ddf0d Mon Sep 17 00:00:00 2001 From: sherlock Date: Thu, 16 Oct 2025 12:59:30 +0530 Subject: [PATCH] Updated data for ES --- ES/Project/code.c | 63 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/ES/Project/code.c b/ES/Project/code.c index 9a4a9dc..bba0fcf 100644 --- a/ES/Project/code.c +++ b/ES/Project/code.c @@ -19,15 +19,19 @@ * RS: P0.27 * EN: P0.28 * + * MODE BUTTON: + * P2.12 (Input, internal pull-up) + * * KEY FUNCTIONS: * 0-9: Digit input (valid in all bases based on base) * A-F: Digit input (valid only in appropriate base) - * Key A (10): Mode/Base selection (cycles: BIN->OCT->DEC->HEX) + * Key A (10): Digit input (value 10 in HEX mode) * Key B (11): Addition (+) * Key C (12): Clear (C) * Key D (13): Subtraction (-) * Key E (14): Multiplication (*) * Key F (15): Equals (=) + * P2.12 Button: Mode selection (cycles: DEC->BIN->OCT->HEX) */ // 7-Segment patterns @@ -52,6 +56,9 @@ const unsigned char seven_seg[16] = { #define LCD_RS (1<<27) #define LCD_EN (1<<28) +// Mode button defines +#define MODE_BUTTON (1<<12) + // Calculator states #define MODE_BIN 2 #define MODE_OCT 8 @@ -197,6 +204,11 @@ unsigned int scan_keypad(void){ return 0xFF; } +unsigned int scan_mode_button(void){ + // Return 1 if button is pressed (active low), 0 if not pressed + return ((LPC_GPIO2->FIOPIN & MODE_BUTTON) == 0) ? 1 : 0; +} + unsigned int is_valid_digit(unsigned int key){ if(key >= 16) return 0; if(current_base == MODE_BIN && key >= 2) return 0; @@ -205,20 +217,39 @@ unsigned int is_valid_digit(unsigned int key){ return 1; } +void change_mode(void){ + 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(); +} + int main(void){ unsigned int key, last_key = 0xFF; unsigned int stable = 0; + unsigned int button_state, last_button_state = 0; + unsigned int button_stable = 0; // Configure pins LPC_PINCON->PINSEL0 = 0; LPC_PINCON->PINSEL1 = 0; LPC_PINCON->PINSEL3 = 0; + LPC_PINCON->PINSEL4 = 0; // Configure P2.12 // Keypad: Columns output, Rows input LPC_GPIO0->FIODIR |= COL_MASK; LPC_GPIO0->FIODIR &= ~ROW_MASK; LPC_GPIO0->FIOSET = COL_MASK; + // Mode button: Input with internal pull-up + LPC_GPIO2->FIODIR &= ~MODE_BUTTON; // Set as input + // Note: Internal pull-up is enabled by default on LPC17xx + // 7-Segment LPC_GPIO0->FIODIR |= (0xFF << SEG_SHIFT); LPC_GPIO1->FIODIR |= DIGIT_EN; @@ -232,8 +263,9 @@ int main(void){ for(;;){ key = scan_keypad(); + button_state = scan_mode_button(); - // Debounce + // Debounce keypad if(key == last_key){ if(stable < 5) stable++; } else { @@ -241,6 +273,21 @@ int main(void){ stable = 0; } + // Debounce mode button + if(button_state == last_button_state){ + if(button_stable < 5) button_stable++; + } else { + last_button_state = button_state; + button_stable = 0; + } + + // Handle mode button press + if(button_stable == 3 && button_state == 1){ + change_mode(); + button_stable = 5; // Prevent multiple triggers + } + + // Handle keypad input if(stable == 3 && key != 0xFF){ // Key pressed and stable @@ -250,18 +297,6 @@ int main(void){ if(input_num > 9999) input_num = input_num % 10000; display_input(); } - // Mode selection (Key A / 10) - else if(key == 10){ - if(current_base == MODE_BIN) - current_base = MODE_OCT; - else if(current_base == MODE_OCT) - current_base = MODE_DEC; - else if(current_base == MODE_DEC) - current_base = MODE_HEX; - else - current_base = MODE_BIN; - display_mode(); - } // Addition (Key B / 11) else if(key == 11){ stored_num = input_num;