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)
|
||||
*
|
||||
* 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): 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 (=)
|
||||
* 0-9, A-F: Digit input (valid based on current base)
|
||||
*
|
||||
* OPERATOR MODE (Hold Key 0 + another key):
|
||||
* Key 0 + B: Addition (+)
|
||||
* Key 0 + C: Clear (C)
|
||||
* Key 0 + D: Subtraction (-)
|
||||
* Key 0 + E: Multiplication (*)
|
||||
* Key 0 + F: Equals (=)
|
||||
*
|
||||
* 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_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 input_num = 0;
|
||||
unsigned int stored_num = 0;
|
||||
unsigned int operation = OP_NONE;
|
||||
unsigned int result = 0;
|
||||
unsigned char lcd_flag = 0;
|
||||
|
||||
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];
|
||||
int i = 0;
|
||||
|
||||
if(num == 0){
|
||||
lcd_data('0');
|
||||
return;
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
|
||||
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;
|
||||
if(num == 0){
|
||||
lcd_data('0');
|
||||
return;
|
||||
}
|
||||
|
||||
while(num > 0 && i < 16){
|
||||
buffer[i++] = '0' + (num % 10);
|
||||
num = num / 10;
|
||||
}
|
||||
}
|
||||
|
||||
while(i > 0){
|
||||
|
|
@ -180,6 +201,16 @@ void display_input(void){
|
|||
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 col, row;
|
||||
unsigned int row_bits;
|
||||
|
|
@ -204,6 +235,22 @@ unsigned int scan_keypad(void){
|
|||
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){
|
||||
// Return 1 if button is pressed (active low), 0 if not pressed
|
||||
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_OCT && key >= 8) return 0;
|
||||
if(current_base == MODE_DEC && key >= 10) return 0;
|
||||
// In HEX mode, all keys 0-15 are valid digits!
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
@ -234,6 +282,7 @@ int main(void){
|
|||
unsigned int stable = 0;
|
||||
unsigned int button_state, last_button_state = 0;
|
||||
unsigned int button_stable = 0;
|
||||
unsigned int shift_active = 0;
|
||||
|
||||
// Configure pins
|
||||
LPC_PINCON->PINSEL0 = 0;
|
||||
|
|
@ -248,7 +297,6 @@ int main(void){
|
|||
|
||||
// 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);
|
||||
|
|
@ -262,6 +310,10 @@ int main(void){
|
|||
display_input();
|
||||
|
||||
for(;;){
|
||||
// Check if shift key (Key 0) is being held
|
||||
shift_active = is_key0_pressed();
|
||||
|
||||
// Scan for other keys
|
||||
key = scan_keypad();
|
||||
button_state = scan_mode_button();
|
||||
|
||||
|
|
@ -291,73 +343,81 @@ int main(void){
|
|||
if(stable == 3 && key != 0xFF){
|
||||
// Key pressed and stable
|
||||
|
||||
// Handle digit input (0-9, A-F)
|
||||
if(is_valid_digit(key)){
|
||||
input_num = input_num * current_base + key;
|
||||
if(input_num > 9999) input_num = input_num % 10000;
|
||||
display_input();
|
||||
}
|
||||
// Addition (Key B / 11)
|
||||
else if(key == 11){
|
||||
stored_num = input_num;
|
||||
operation = OP_ADD;
|
||||
input_num = 0;
|
||||
display_input();
|
||||
}
|
||||
// Clear (Key C / 12)
|
||||
else if(key == 12){
|
||||
input_num = 0;
|
||||
stored_num = 0;
|
||||
operation = OP_NONE;
|
||||
result = 0;
|
||||
display_input();
|
||||
}
|
||||
// Subtraction (Key D / 13)
|
||||
else if(key == 13){
|
||||
stored_num = input_num;
|
||||
operation = OP_SUB;
|
||||
input_num = 0;
|
||||
display_input();
|
||||
}
|
||||
// Multiplication (Key E / 14)
|
||||
else if(key == 14){
|
||||
stored_num = input_num;
|
||||
operation = OP_MUL;
|
||||
input_num = 0;
|
||||
display_input();
|
||||
}
|
||||
// Equals (Key F / 15)
|
||||
else if(key == 15){
|
||||
if(operation == OP_ADD)
|
||||
result = stored_num + input_num;
|
||||
else if(operation == OP_SUB)
|
||||
result = stored_num - input_num;
|
||||
else if(operation == OP_MUL)
|
||||
result = stored_num * input_num;
|
||||
else
|
||||
result = input_num;
|
||||
// If Key 0 is also pressed, we're in operator mode
|
||||
if(shift_active && key != 0){
|
||||
// OPERATOR MODE (Key 0 + another key)
|
||||
|
||||
lcd_cmd(0xC0);
|
||||
lcd_print_str("Res: ");
|
||||
lcd_print_num(result, current_base);
|
||||
lcd_print_str(" ");
|
||||
if(key == 11){ // Key 0 + B = Addition
|
||||
stored_num = input_num;
|
||||
operation = OP_ADD;
|
||||
input_num = 0;
|
||||
display_operator_feedback("+");
|
||||
}
|
||||
else if(key == 12){ // Key 0 + C = Clear
|
||||
input_num = 0;
|
||||
stored_num = 0;
|
||||
operation = OP_NONE;
|
||||
result = 0;
|
||||
display_operator_feedback("CLR");
|
||||
}
|
||||
else if(key == 13){ // Key 0 + D = Subtraction
|
||||
stored_num = input_num;
|
||||
operation = OP_SUB;
|
||||
input_num = 0;
|
||||
display_operator_feedback("-");
|
||||
}
|
||||
else if(key == 14){ // Key 0 + E = Multiplication
|
||||
stored_num = input_num;
|
||||
operation = OP_MUL;
|
||||
input_num = 0;
|
||||
display_operator_feedback("*");
|
||||
}
|
||||
else if(key == 15){ // Key 0 + F = Equals
|
||||
if(operation == OP_ADD)
|
||||
result = stored_num + input_num;
|
||||
else if(operation == OP_SUB)
|
||||
result = stored_num - input_num;
|
||||
else if(operation == OP_MUL)
|
||||
result = stored_num * input_num;
|
||||
else
|
||||
result = input_num;
|
||||
|
||||
input_num = result;
|
||||
operation = OP_NONE;
|
||||
lcd_cmd(0xC0);
|
||||
lcd_print_str("Res: ");
|
||||
lcd_print_num(result, current_base);
|
||||
lcd_print_str(" ");
|
||||
|
||||
input_num = result;
|
||||
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)
|
||||
if(input_num < 16){
|
||||
LPC_GPIO0->FIOCLR = (0xFF << SEG_SHIFT);
|
||||
LPC_GPIO0->FIOSET = (seven_seg[input_num] << SEG_SHIFT);
|
||||
} else {
|
||||
unsigned int display_digit = input_num % 16;
|
||||
LPC_GPIO0->FIOCLR = (0xFF << SEG_SHIFT);
|
||||
LPC_GPIO0->FIOSET = (seven_seg[display_digit] << SEG_SHIFT);
|
||||
// MODIFIED: Use decimal point to indicate negative numbers
|
||||
int display_value = (input_num < 0) ? -input_num : input_num;
|
||||
unsigned int display_digit = display_value % 16;
|
||||
unsigned int seg_pattern = seven_seg[display_digit];
|
||||
|
||||
// Turn on decimal point if number is negative
|
||||
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;
|
||||
|
||||
delay(3000);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue