proj
This commit is contained in:
parent
085c1023a6
commit
6a914a6eaa
1 changed files with 93 additions and 86 deletions
|
|
@ -53,8 +53,8 @@
|
||||||
|
|
||||||
// 7-Segment patterns
|
// 7-Segment patterns
|
||||||
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
|
||||||
};
|
};
|
||||||
|
|
||||||
// Keypad defines
|
// Keypad defines
|
||||||
|
|
@ -65,16 +65,16 @@ const unsigned char seven_seg[16] = {
|
||||||
|
|
||||||
// 7-Segment defines
|
// 7-Segment defines
|
||||||
#define SEG_SHIFT 4
|
#define SEG_SHIFT 4
|
||||||
#define DIGIT_EN (1<<23)
|
#define DIGIT_EN (1 << 23)
|
||||||
|
|
||||||
// LCD defines
|
// LCD defines
|
||||||
#define LCD_DATA_SHIFT 23
|
#define LCD_DATA_SHIFT 23
|
||||||
#define LCD_DATA_MASK (0x0F << LCD_DATA_SHIFT)
|
#define LCD_DATA_MASK (0x0F << LCD_DATA_SHIFT)
|
||||||
#define LCD_RS (1<<27)
|
#define LCD_RS (1 << 27)
|
||||||
#define LCD_EN (1<<28)
|
#define LCD_EN (1 << 28)
|
||||||
|
|
||||||
// Mode button defines
|
// Mode button defines
|
||||||
#define MODE_BUTTON (1<<12)
|
#define MODE_BUTTON (1 << 12)
|
||||||
|
|
||||||
// Calculator states
|
// Calculator states
|
||||||
#define MODE_BIN 2
|
#define MODE_BIN 2
|
||||||
|
|
@ -94,17 +94,28 @@ int result = 0;
|
||||||
unsigned int current_base = MODE_DEC;
|
unsigned int current_base = MODE_DEC;
|
||||||
unsigned int operation = OP_NONE;
|
unsigned int operation = OP_NONE;
|
||||||
unsigned char result_displayed = 0;
|
unsigned char result_displayed = 0;
|
||||||
|
unsigned int key = 0xFF;
|
||||||
|
unsigned int last_key = 0xFF;
|
||||||
|
unsigned int stable = 0;
|
||||||
|
unsigned int mode_held = 0;
|
||||||
|
unsigned int mode_press_counter = 0;
|
||||||
|
unsigned int key_pressed_with_mode = 0;
|
||||||
|
unsigned int mode_current = 0;
|
||||||
|
int is_operator_key = 0;
|
||||||
|
int operator_mode = 0;
|
||||||
|
unsigned int seg_pattern = 0;
|
||||||
|
|
||||||
void delay(volatile unsigned int d){
|
void delay(volatile unsigned int d) {
|
||||||
while(d--) __NOP();
|
while(d--)
|
||||||
|
__NOP();
|
||||||
}
|
}
|
||||||
|
|
||||||
void lcd_delay(unsigned long r){
|
void lcd_delay(unsigned long r) {
|
||||||
unsigned long i;
|
unsigned long i;
|
||||||
for(i=0; i<r; i++);
|
for(i = 0; i < r; i++);
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
LPC_GPIO0->FIOPIN = (LPC_GPIO0->FIOPIN & ~LCD_DATA_MASK) | temp;
|
LPC_GPIO0->FIOPIN = (LPC_GPIO0->FIOPIN & ~LCD_DATA_MASK) | temp;
|
||||||
|
|
@ -120,17 +131,17 @@ void lcd_write_nibble(unsigned char nibble, unsigned char is_data){
|
||||||
lcd_delay(500000);
|
lcd_delay(500000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lcd_cmd(unsigned char cmd){
|
void lcd_cmd(unsigned char cmd) {
|
||||||
lcd_write_nibble(cmd >> 4, 0);
|
lcd_write_nibble(cmd >> 4, 0);
|
||||||
lcd_write_nibble(cmd, 0);
|
lcd_write_nibble(cmd, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lcd_data(unsigned char data){
|
void lcd_data(unsigned char data) {
|
||||||
lcd_write_nibble(data >> 4, 1);
|
lcd_write_nibble(data >> 4, 1);
|
||||||
lcd_write_nibble(data, 1);
|
lcd_write_nibble(data, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lcd_init(void){
|
void lcd_init(void) {
|
||||||
lcd_delay(5000000);
|
lcd_delay(5000000);
|
||||||
lcd_write_nibble(0x03, 0);
|
lcd_write_nibble(0x03, 0);
|
||||||
lcd_delay(500000);
|
lcd_delay(500000);
|
||||||
|
|
@ -148,23 +159,23 @@ void lcd_init(void){
|
||||||
lcd_cmd(0x06);
|
lcd_cmd(0x06);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lcd_print_str(const char* str){
|
void lcd_print_str(const char* str) {
|
||||||
while(*str){
|
while(*str) {
|
||||||
lcd_data(*str++);
|
lcd_data(*str++);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void lcd_print_num(int num, unsigned int base){
|
void lcd_print_num(int num, unsigned int base) {
|
||||||
char buffer[17];
|
char buffer[17];
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
if(base != MODE_DEC){
|
if(base != MODE_DEC) {
|
||||||
unsigned int unum = (unsigned int)num;
|
unsigned int unum = (unsigned int)num;
|
||||||
if(unum == 0){
|
if(unum == 0) {
|
||||||
lcd_data('0');
|
lcd_data('0');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
while(unum > 0 && i < 16){
|
while(unum > 0 && i < 16) {
|
||||||
unsigned int digit = unum % base;
|
unsigned int digit = unum % base;
|
||||||
if(digit < 10)
|
if(digit < 10)
|
||||||
buffer[i++] = '0' + digit;
|
buffer[i++] = '0' + digit;
|
||||||
|
|
@ -173,28 +184,28 @@ void lcd_print_num(int num, unsigned int base){
|
||||||
unum = unum / base;
|
unum = unum / base;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if(num < 0){
|
if(num < 0) {
|
||||||
lcd_data('-');
|
lcd_data('-');
|
||||||
num = -num;
|
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) {
|
||||||
buffer[i++] = '0' + (num % 10);
|
buffer[i++] = '0' + (num % 10);
|
||||||
num = num / 10;
|
num = num / 10;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while(i > 0){
|
while(i > 0) {
|
||||||
lcd_data(buffer[--i]);
|
lcd_data(buffer[--i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
|
@ -207,27 +218,28 @@ void display_mode(void){
|
||||||
lcd_print_str("HEX ");
|
lcd_print_str("HEX ");
|
||||||
}
|
}
|
||||||
|
|
||||||
void display_status(void){
|
void display_status(void) {
|
||||||
lcd_cmd(0xC0);
|
lcd_cmd(0xC0);
|
||||||
|
|
||||||
if(operation != OP_NONE){
|
if(operation != OP_NONE) {
|
||||||
// Show operation pending
|
// Show operation pending
|
||||||
lcd_print_str("Op:");
|
lcd_print_str("Op:");
|
||||||
if(operation == OP_ADD) lcd_data('+');
|
if(operation == OP_ADD)
|
||||||
else if(operation == OP_SUB) lcd_data('-');
|
lcd_data('+');
|
||||||
else if(operation == OP_MUL) lcd_data('*');
|
else if(operation == OP_SUB)
|
||||||
|
lcd_data('-');
|
||||||
|
else if(operation == OP_MUL)
|
||||||
|
lcd_data('*');
|
||||||
|
|
||||||
lcd_print_str(" Val:");
|
lcd_print_str(" Val:");
|
||||||
lcd_print_num(stored_num, current_base);
|
lcd_print_num(stored_num, current_base);
|
||||||
lcd_print_str(" ");
|
lcd_print_str(" ");
|
||||||
}
|
} else if(result_displayed) {
|
||||||
else if(result_displayed){
|
|
||||||
// Show result
|
// Show result
|
||||||
lcd_print_str("Res: ");
|
lcd_print_str("Res: ");
|
||||||
lcd_print_num(result, current_base);
|
lcd_print_num(result, current_base);
|
||||||
lcd_print_str(" ");
|
lcd_print_str(" ");
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// Show current input
|
// Show current input
|
||||||
lcd_print_str("Inp: ");
|
lcd_print_str("Inp: ");
|
||||||
lcd_print_num(input_num, current_base);
|
lcd_print_num(input_num, current_base);
|
||||||
|
|
@ -235,22 +247,22 @@ void display_status(void){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
for(col=0; col<4; col++){
|
for(col = 0; col < 4; col++) {
|
||||||
LPC_GPIO0->FIOSET = COL_MASK;
|
LPC_GPIO0->FIOSET = COL_MASK;
|
||||||
delay(50);
|
delay(50);
|
||||||
LPC_GPIO0->FIOCLR = (1 << (COL_BASE + col));
|
LPC_GPIO0->FIOCLR = (1 << (COL_BASE + col));
|
||||||
delay(200);
|
delay(200);
|
||||||
row_bits = (LPC_GPIO0->FIOPIN & ROW_MASK) >> ROW_BASE;
|
row_bits = (LPC_GPIO0->FIOPIN & ROW_MASK) >> ROW_BASE;
|
||||||
|
|
||||||
if(row_bits != 0x0F){
|
if(row_bits != 0x0F) {
|
||||||
for(row=0; row<4; row++){
|
for(row = 0; row < 4; row++) {
|
||||||
if((row_bits & (1<<row)) == 0){
|
if((row_bits & (1 << row)) == 0) {
|
||||||
LPC_GPIO0->FIOSET = COL_MASK;
|
LPC_GPIO0->FIOSET = COL_MASK;
|
||||||
return col*4 + row;
|
return col * 4 + row;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -259,19 +271,23 @@ unsigned int scan_keypad(void){
|
||||||
return 0xFF;
|
return 0xFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int is_mode_button_pressed(void){
|
unsigned int is_mode_button_pressed(void) {
|
||||||
return ((LPC_GPIO2->FIOPIN & MODE_BUTTON) == 0) ? 1 : 0;
|
return ((LPC_GPIO2->FIOPIN & MODE_BUTTON) == 0) ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int is_valid_digit(unsigned int key){
|
unsigned int is_valid_digit(unsigned int key) {
|
||||||
if(key >= 16) return 0;
|
if(key >= 16)
|
||||||
if(current_base == MODE_BIN && key >= 2) return 0;
|
return 0;
|
||||||
if(current_base == MODE_OCT && key >= 8) return 0;
|
if(current_base == MODE_BIN && key >= 2)
|
||||||
if(current_base == MODE_DEC && key >= 10) return 0;
|
return 0;
|
||||||
|
if(current_base == MODE_OCT && key >= 8)
|
||||||
|
return 0;
|
||||||
|
if(current_base == MODE_DEC && key >= 10)
|
||||||
|
return 0;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void change_mode(void){
|
void change_mode(void) {
|
||||||
if(current_base == MODE_DEC)
|
if(current_base == MODE_DEC)
|
||||||
current_base = MODE_BIN;
|
current_base = MODE_BIN;
|
||||||
else if(current_base == MODE_BIN)
|
else if(current_base == MODE_BIN)
|
||||||
|
|
@ -291,13 +307,7 @@ void change_mode(void){
|
||||||
display_status();
|
display_status();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void){
|
int main(void) {
|
||||||
unsigned int key, last_key = 0xFF;
|
|
||||||
unsigned int stable = 0;
|
|
||||||
unsigned int mode_held = 0;
|
|
||||||
unsigned int mode_press_counter = 0;
|
|
||||||
unsigned int key_pressed_with_mode = 0;
|
|
||||||
|
|
||||||
// Configure pins
|
// Configure pins
|
||||||
LPC_PINCON->PINSEL0 = 0;
|
LPC_PINCON->PINSEL0 = 0;
|
||||||
LPC_PINCON->PINSEL1 = 0;
|
LPC_PINCON->PINSEL1 = 0;
|
||||||
|
|
@ -323,28 +333,30 @@ int main(void){
|
||||||
display_mode();
|
display_mode();
|
||||||
display_status();
|
display_status();
|
||||||
|
|
||||||
for(;;){
|
for(;;) {
|
||||||
// Check if MODE button is currently pressed
|
// Check if MODE button is currently pressed
|
||||||
unsigned int mode_current = is_mode_button_pressed();
|
mode_current = is_mode_button_pressed();
|
||||||
|
|
||||||
// Scan keypad
|
// Scan keypad
|
||||||
key = scan_keypad();
|
key = scan_keypad();
|
||||||
|
|
||||||
// Debounce keypad
|
// Debounce keypad
|
||||||
if(key == last_key){
|
if(key == last_key) {
|
||||||
if(stable < 5) stable++;
|
if(stable < 5)
|
||||||
|
stable++;
|
||||||
} else {
|
} else {
|
||||||
last_key = key;
|
last_key = key;
|
||||||
stable = 0;
|
stable = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Track MODE button hold
|
// Track MODE button hold
|
||||||
if(mode_current){
|
if(mode_current) {
|
||||||
mode_press_counter++;
|
mode_press_counter++;
|
||||||
mode_held = 1;
|
mode_held = 1;
|
||||||
} else {
|
} else {
|
||||||
// MODE button released
|
// MODE button released
|
||||||
if(mode_held && !key_pressed_with_mode && mode_press_counter < 50){
|
if(mode_held && !key_pressed_with_mode &&
|
||||||
|
mode_press_counter < 50) {
|
||||||
// Short press without key combo = change mode
|
// Short press without key combo = change mode
|
||||||
change_mode();
|
change_mode();
|
||||||
}
|
}
|
||||||
|
|
@ -354,47 +366,44 @@ int main(void){
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle keypad input
|
// Handle keypad input
|
||||||
if(stable == 3 && key != 0xFF){
|
if(stable == 3 && key != 0xFF) {
|
||||||
// Key pressed and stable
|
// Key pressed and stable
|
||||||
|
|
||||||
// Check if this is an operator key (A-E)
|
// Check if this is an operator key (A-E)
|
||||||
int is_operator_key = (key >= 10 && key <= 14);
|
is_operator_key = (key >= 10 && key <= 14);
|
||||||
|
|
||||||
// In HEX mode, operators require MODE button
|
// In HEX mode, operators require MODE button
|
||||||
// In other modes, operator keys work directly
|
// In other modes, operator keys work directly
|
||||||
int operator_mode = 0;
|
operator_mode = 0;
|
||||||
if(current_base == MODE_HEX){
|
if(current_base == MODE_HEX) {
|
||||||
operator_mode = (mode_held && is_operator_key);
|
operator_mode = (mode_held && is_operator_key);
|
||||||
} else {
|
} else {
|
||||||
operator_mode = is_operator_key;
|
operator_mode = is_operator_key;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(operator_mode){
|
if(operator_mode) {
|
||||||
// OPERATOR INPUT
|
// OPERATOR INPUT
|
||||||
key_pressed_with_mode = 1;
|
key_pressed_with_mode = 1;
|
||||||
|
|
||||||
if(key == 10){ // A = Addition
|
if(key == 10) { // A = Addition
|
||||||
stored_num = input_num;
|
stored_num = input_num;
|
||||||
operation = OP_ADD;
|
operation = OP_ADD;
|
||||||
input_num = 0;
|
input_num = 0;
|
||||||
result_displayed = 0;
|
result_displayed = 0;
|
||||||
display_status();
|
display_status();
|
||||||
}
|
} else if(key == 11) { // B = Subtraction
|
||||||
else if(key == 11){ // B = Subtraction
|
|
||||||
stored_num = input_num;
|
stored_num = input_num;
|
||||||
operation = OP_SUB;
|
operation = OP_SUB;
|
||||||
input_num = 0;
|
input_num = 0;
|
||||||
result_displayed = 0;
|
result_displayed = 0;
|
||||||
display_status();
|
display_status();
|
||||||
}
|
} else if(key == 12) { // C = Multiplication
|
||||||
else if(key == 12){ // C = Multiplication
|
|
||||||
stored_num = input_num;
|
stored_num = input_num;
|
||||||
operation = OP_MUL;
|
operation = OP_MUL;
|
||||||
input_num = 0;
|
input_num = 0;
|
||||||
result_displayed = 0;
|
result_displayed = 0;
|
||||||
display_status();
|
display_status();
|
||||||
}
|
} else if(key == 13) { // D = Equals
|
||||||
else if(key == 13){ // D = Equals
|
|
||||||
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)
|
||||||
|
|
@ -408,8 +417,7 @@ int main(void){
|
||||||
operation = OP_NONE;
|
operation = OP_NONE;
|
||||||
result_displayed = 1;
|
result_displayed = 1;
|
||||||
display_status();
|
display_status();
|
||||||
}
|
} else if(key == 14) { // E = Clear
|
||||||
else if(key == 14){ // E = Clear
|
|
||||||
input_num = 0;
|
input_num = 0;
|
||||||
stored_num = 0;
|
stored_num = 0;
|
||||||
operation = OP_NONE;
|
operation = OP_NONE;
|
||||||
|
|
@ -417,14 +425,15 @@ int main(void){
|
||||||
result_displayed = 0;
|
result_displayed = 0;
|
||||||
display_status();
|
display_status();
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// DIGIT INPUT MODE
|
// DIGIT INPUT MODE
|
||||||
if(is_valid_digit(key)){
|
if(is_valid_digit(key)) {
|
||||||
result_displayed = 0;
|
result_displayed = 0;
|
||||||
input_num = input_num * current_base + key;
|
input_num = input_num * current_base + key;
|
||||||
if(input_num > 32767) input_num = input_num % 32768;
|
if(input_num > 32767)
|
||||||
if(input_num < -32768) input_num = -32768;
|
input_num = input_num % 32768;
|
||||||
|
if(input_num < -32768)
|
||||||
|
input_num = -32768;
|
||||||
display_status();
|
display_status();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -433,18 +442,14 @@ int main(void){
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== STATE MACHINE DISPLAY ON 7-SEGMENT =====
|
// ===== STATE MACHINE 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; // Display '1'
|
seg_pattern = 0x06; // Display '1'
|
||||||
}
|
} else if(operation != OP_NONE) {
|
||||||
else if(operation != OP_NONE) {
|
|
||||||
seg_pattern = 0x5B | 0x80; // Display '2' with DP
|
seg_pattern = 0x5B | 0x80; // Display '2' with DP
|
||||||
}
|
} else if(result_displayed == 1) {
|
||||||
else if(result_displayed == 1) {
|
|
||||||
seg_pattern = 0x4F | 0x80; // Display '3' with DP
|
seg_pattern = 0x4F | 0x80; // Display '3' with DP
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
seg_pattern = 0x3F; // Display '0'
|
seg_pattern = 0x3F; // Display '0'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -455,4 +460,6 @@ int main(void){
|
||||||
|
|
||||||
delay(3000);
|
delay(3000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue