#define DELAY 10000
volatile char *outf, *ink, *outa, *outc, *outl, *outb;
unsigned int digit[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f}; // Patterns for 0-9
int digits[4] = {-1, -1, -1, -1}; // Array to store up to 4 digits, initialized to -1 (no digit)
int currentDigitIndex = 0; // Index to keep track of which display to update next
void setup() {
volatile char *dirf, *dirk, *dira, *dirc, *dirl, *dirb;
dirf = (char *)0x030; // Example address
dirk = (char *)0x107; // Example address
dira = (char *)0x021; // Example address
dirc = (char *)0x027; // Example address
dirl = (char *)0x10A; // Example address
dirb = (char *)0x024; // Example address
// Set port directions
*dirf = 0x0F; // Set as output for keypad rows
*dirk = 0x00; // Set as input for keypad columns
*dira = 0xFF; // Set as output for 7-segment display 1
*dirc = 0xFF; // Set as output for 7-segment display 2
*dirl = 0xFF; // Set as output for 7-segment display 3
*dirb = 0xFF; // Set as output for 7-segment display 4
// Define port data registers
outf = (char *)0x031; // Example address
ink = (char *)0x106; // Example address
outa = (char *)0x022; // Example address
outc = (char *)0x028; // Example address
outl = (char *)0x10B; // Example address
outb = (char *)0x025; // Example address
}
void loop() {
int number = scan_keypad();
if (number != -1 && currentDigitIndex < 4) { // Check if a valid key is pressed and we have space for more digits
digits[currentDigitIndex] = number; // Store the pressed digit
currentDigitIndex++; // Move to the next segment
}
displayDigits(); // Display the stored digits
delay1();
}
void displayDigits() {
// Display the stored digits on the corresponding seven-segment displays
*outa = 0x00;
*outc = 0x00;
*outl = 0x00;
*outb = 0x00;
if (digits[0] != -1) *outb = digit[digits[0]]; // Display on the first 7-segment display
delay1();
delay1();
if (digits[1] != -1) *outl = digit[digits[1]]; // Display on the second 7-segment display
delay1();
delay1();
if (digits[2] != -1) *outc = digit[digits[2]]; // Display on the third 7-segment display
delay1();
delay1();
if (digits[3] != -1) *outa = digit[digits[3]]; // Display on the fourth 7-segment display
delay1();
delay1();
}
void delay1(void) {
volatile long j;
for (j = 0; j < DELAY; j++);
}
int scan_keypad() {
int i;
for (i = 0; i < 4; i++) {
*outf = 1 << i; // Activate one row at a time
delay1();
if (*ink != 0) {
int key = decode_key(*ink, i); // Decode the key pressed
return key;
}
}
return -1; // No key pressed
}
int decode_key(char input, int row) {
if (row == 0) {
if (input == 0x01) return 1;
else if (input == 0x02) return 2;
else if (input == 0x04) return 3;
} else if (row == 1) {
if (input == 0x01) return 4;
else if (input == 0x02) return 5;
else if (input == 0x04) return 6;
} else if (row == 2) {
if (input == 0x01) return 7;
else if (input == 0x02) return 8;
else if (input == 0x04) return 9;
} else if (row == 3) {
if (input == 0x02) return 0;
}
return -1; // Invalid key
}